From electro.nnn at gmail.com Wed Aug 21 22:30:40 2013 From: electro.nnn at gmail.com (electron) Date: Thu, 22 Aug 2013 02:30:40 +0000 (UTC) Subject: Encoding problem in python References: Message-ID: If you use Arabic frequently on your system, I suggest to change your windows system locale from "Region and Language" in control panel (Administrative tab) and set to Arabic. From cheirasacan at gmail.com Sat Aug 10 06:37:21 2013 From: cheirasacan at gmail.com (cheirasacan at gmail.com) Date: Sat, 10 Aug 2013 03:37:21 -0700 (PDT) Subject: Get filename using filefialog.askfilename In-Reply-To: References: Message-ID: <1f73a66e-4bdd-4aa3-aaa0-f6de827b806f@googlegroups.com> .... Sarcastic.... what the kcuf ?????? My english is SO bad? Do you know me? I can not understand this paranoia.... i only was giving thanks. From joshua at landau.ws Sat Aug 10 07:33:09 2013 From: joshua at landau.ws (Joshua Landau) Date: Sat, 10 Aug 2013 12:33:09 +0100 Subject: Get filename using filefialog.askfilename In-Reply-To: <1f73a66e-4bdd-4aa3-aaa0-f6de827b806f@googlegroups.com> References: <1f73a66e-4bdd-4aa3-aaa0-f6de827b806f@googlegroups.com> Message-ID: On 10 August 2013 11:37, wrote: > .... Sarcastic.... what the kcuf ?????? > > My english is SO bad? > > Do you know me? > > I can not understand this paranoia.... i only was giving thanks. Sarcasm and a lack thereof is very hard to see over the Internet. It's probably just a misunderstanding :). From toby at tobiah.org Mon Aug 5 13:54:06 2013 From: toby at tobiah.org (Tobiah) Date: Mon, 05 Aug 2013 10:54:06 -0700 Subject: Piping processes works with 'shell = True' but not otherwise. In-Reply-To: <4fd43d0e-9c72-41d0-8c8d-dbc0d0a50022@googlegroups.com> References: <062f557e-8e1a-4efb-9178-7d685b47a834@googlegroups.com> <08aa32b7-0fb7-4665-83fa-5cc7ec36f898@googlegroups.com> <4fd43d0e-9c72-41d0-8c8d-dbc0d0a50022@googlegroups.com> Message-ID: <2FRLt.360866$aW2.91845@fx13.iad> On 05/27/2013 04:33 AM, Luca Cerone wrote: >> >> >> 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.. > p1 = Popen(['nsa_snoop', 'terror_suspect', '--no-privacy', '--dispatch-squad'], ... From luca.cerone at gmail.com Mon Aug 5 11:33:55 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Mon, 5 Aug 2013 08:33:55 -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> <8c89fb10-63eb-446c-855b-f4c5406976ea@googlegroups.com> Message-ID: <9926f4b9-b5df-4ee1-b9d9-e359203ae97d@googlegroups.com> thanks and what about python 2.7? > > > In Python 3.3 and above: > > > > p = subprocess.Popen(..., stderr=subprocess.DEVNULL) P.s. sorry for the late reply, I discovered I don't receive notifications from google groups.. From nospam at nospam.com Tue Aug 6 06:07:34 2013 From: nospam at nospam.com (Gilles) Date: Tue, 06 Aug 2013 12:07:34 +0200 Subject: Source code to identify user through browser? References: Message-ID: On Wed, 05 Jun 2013 15:08:54 +0200, Gilles wrote: >I was wondering if some Python module were available to identify a >user through their browser, like it's done on the Panopticlick site: > >http://panopticlick.eff.org/ It appears that flash cookies is a better solution to keep most trolls away from a forum/blog: www.stackoverflow.com/questions/216542/ Still, I wish there a browser that could run in real incognito mode, ie. would only send the minimum amount of infos to the server. From sanjay.k.arora at gmail.com Mon Aug 5 09:13:57 2013 From: sanjay.k.arora at gmail.com (Sanjay Arora) Date: Mon, 5 Aug 2013 18:43:57 +0530 Subject: Simple Python script as SMTP server for outgoing e-mails? In-Reply-To: References: <368qu85msgfhuk2j2s13qj0bqn4rkcint9@4ax.com> <51ED3CEB.1070706@gmail.com> Message-ID: On Tue, Jul 23, 2013 at 2:49 PM, Chris Angelico wrote: > Ah, there's a solution to this one. You simply use your own > envelope-from address; SPF shouldn't be being checked for the From: > header. Forwarding and using the original sender's address in the SMTP > 'MAIL FROM' command is forging mail from them, so it is correct for > that to be thrown out. The mail is coming from your own account, so > you put your address in it, and you might even be able to put an > uber-strict SPF record like "v=spf1 ip4:1.2.3.4 -all" which is quick > to process and guarantees that nobody can pretend to forward mail on > your behalf. The checks are for the *current connection*, not anything > earlier. > > ChrisA > Bit Late, but do check out http://www.openspf.org/FAQ/Forwarding Forwarding does get broken, but a partial solution in whitelisting is there, though too manual & therefore cumbersome. Another option http://www.openspf.org/SRS is there to be worked in conjunction with spf. There is a best spf practices guide on the site. And all this email authentication issue given on openspf.org makes an interesting read. Ciao. Sanjay. -------------- next part -------------- An HTML attachment was scrubbed... URL: From nospam at nospam.com Thu Aug 1 10:15:22 2013 From: nospam at nospam.com (Gilles) Date: Thu, 01 Aug 2013 16:15:22 +0200 Subject: Simple Python script as SMTP server for outgoing e-mails? References: <2nutu8la1eogr08vf2jdnrkbka7o7q4iei@4ax.com> Message-ID: <19rkv8tu2uj7g1mpmbgdb2039tuv91sh6r@4ax.com> On Wed, 24 Jul 2013 10:38:52 -0400, Kevin Walzer wrote: >> Thanks. hMailServer was one of the apps I checked, and I was just >> making sure there weren't something simpler, considering my needs, >> ideally something like Mongoose MTA. >> >> Regardless, because of the SPAM anti-measures mentioned above, it >> seems like I was over-optimistic about running an MTA and sending >> e-mails from my home computer :-/ >> > >The reason I mentioned hMailServer is that I host my own mail server for >my business--I have a static IP address, and correctly configured >DNS--and so I'm able to send out large batches of e-mails to customers >from my network without being blocked by my ISP. I'm running a Mac >server so my mail system is the typical Unix setup (Postfix, etc.), but >hMailServer was the closest thing I've found for Windows. > >Configuring your own server isn't cheap in terms of time even if you use >FOSS components, and your ISP may charge more for a static IP, so I >completely understand if you don't want to go that route. Thanks for the infos. Indeed, it seems like hMailServer is one of the few good MTAs for Windows. I already have a static IP, so the issue is more that remote MTAs might not accept connections from MTAs running on users' PC instead of ISP's. From wayne at waynewerner.com Sat Aug 3 07:47:07 2013 From: wayne at waynewerner.com (Wayne Werner) Date: Sat, 3 Aug 2013 06:47:07 -0500 (CDT) Subject: Simple Python script as SMTP server for outgoing e-mails? In-Reply-To: <19rkv8tu2uj7g1mpmbgdb2039tuv91sh6r@4ax.com> References: <2nutu8la1eogr08vf2jdnrkbka7o7q4iei@4ax.com> <19rkv8tu2uj7g1mpmbgdb2039tuv91sh6r@4ax.com> Message-ID: On Thu, 1 Aug 2013, Gilles wrote: > On Wed, 24 Jul 2013 10:38:52 -0400, Kevin Walzer > wrote: >>> Thanks. hMailServer was one of the apps I checked, and I was just >>> making sure there weren't something simpler, considering my needs, >>> ideally something like Mongoose MTA. Have you checked Kenneth Rietz's inbox.py[1]? It's fairly simple to use/extend and might fit your modest needs. -W [1]:https://crate.io/packages/inbox/ From kw at codebykevin.com Sat Aug 3 21:41:16 2013 From: kw at codebykevin.com (Kevin Walzer) Date: Sat, 03 Aug 2013 21:41:16 -0400 Subject: Simple Python script as SMTP server for outgoing e-mails? In-Reply-To: <19rkv8tu2uj7g1mpmbgdb2039tuv91sh6r@4ax.com> References: <2nutu8la1eogr08vf2jdnrkbka7o7q4iei@4ax.com> <19rkv8tu2uj7g1mpmbgdb2039tuv91sh6r@4ax.com> Message-ID: On 8/1/13 10:15 AM, Gilles wrote: > I already have a static IP, so the issue is more that remote MTAs > might not accept connections from MTAs running on users' PC instead of > ISP's. For what it's worth, that hasn't been my experience. -- Kevin Walzer Code by Kevin/Mobile Code by Kevin http://www.codebykevin.com http://www.wtmobilesoftware.com From nospam at nospam.com Tue Aug 6 06:45:18 2013 From: nospam at nospam.com (Gilles) Date: Tue, 06 Aug 2013 12:45:18 +0200 Subject: Simple Python script as SMTP server for outgoing e-mails? References: <2nutu8la1eogr08vf2jdnrkbka7o7q4iei@4ax.com> <19rkv8tu2uj7g1mpmbgdb2039tuv91sh6r@4ax.com> Message-ID: On Sat, 03 Aug 2013 21:41:16 -0400, Kevin Walzer wrote: >For what it's worth, that hasn't been my experience. Thanks for the feedback. I'll experiment and see how it goes. From nospam at nospam.com Tue Aug 6 06:44:59 2013 From: nospam at nospam.com (Gilles) Date: Tue, 06 Aug 2013 12:44:59 +0200 Subject: Simple Python script as SMTP server for outgoing e-mails? References: <2nutu8la1eogr08vf2jdnrkbka7o7q4iei@4ax.com> <19rkv8tu2uj7g1mpmbgdb2039tuv91sh6r@4ax.com> Message-ID: <2sk109lr9cooblevbs3tb3412qkhfv95oi@4ax.com> On Sat, 3 Aug 2013 06:47:07 -0500 (CDT), Wayne Werner wrote: >Have you checked Kenneth Rietz's inbox.py[1]? It's fairly simple to >use/extend and might fit your modest needs. > > >-W > >[1]:https://crate.io/packages/inbox/ Thanks. I'll check it out. From cmpython at gmail.com Thu Aug 1 11:57:55 2013 From: cmpython at gmail.com (CM) Date: Thu, 1 Aug 2013 08:57:55 -0700 (PDT) Subject: how to package embedded python? In-Reply-To: References: <99b6fbcb-5456-4421-89d6-d0453b722e8c@googlegroups.com> <110caaff-720a-4cc6-a4d4-ad7a7fe612dd@googlegroups.com> <8505c5aa-0b16-4c32-bab3-17370b4f8f2e@googlegroups.com> Message-ID: On Wednesday, July 31, 2013 11:47:19 AM UTC-4, David M. Cotter wrote: > okay, well that might turn out to be useful, except i don't quite know how to use it, and there are no "from scratch" instructions. > > > > i managed to download "py2exe-0.6.9.zip" and unzip it, but how does one "install" this package? (yes, still a newb at that) What's your OS? For Windows there is an exe that installs it. Did you download that? Or you could, in the command line, do: cd c:\some_folder\the_py2exe_folder python setup.py install (The Python site has a huge page of details on that route. http://docs.python.org/2/install/) Or you could probably get away with just moving the py2exe folder to Python27\site-packages (or whatever Python you have) Or you could first install pip (recommended!) and then just: pip install py2exe (http://dubroy.com/blog/so-you-want-to-install-a-python-package) > then, once installed, how do i say "include the entire world" instead of just "mymodule" ? cuz the point of embedding python on my app is that the end-user can run any script at all, not just one module. I don't know. As I understand it, py2exe will pull whatever is needed in Python to run your module. If your module requires all of Python, I guess that will work. I think regardless of the module, even if it is a "Hello, World" program, py2exe has to include the Python interpreter. Unfortunately, and surprisingly, the py2exe site is still down. Odd. If it returns, that should help. There is also a py2exe users mailing list that you could find by Googling for it. In terms of From beliavsky at aol.com Thu Aug 1 09:11:06 2013 From: beliavsky at aol.com (beliavsky at aol.com) Date: Thu, 1 Aug 2013 06:11:06 -0700 (PDT) Subject: Best Python book(s) for a pre-teen? In-Reply-To: <20619edc.0302181127.6ad39e68@posting.google.com> References: <20619edc.0302181127.6ad39e68@posting.google.com> Message-ID: On Tuesday, February 18, 2003 2:27:58 PM UTC-5, Mike Silva wrote: > Hi all, > > My son is 11 and wants to try programming, partly because it's what I > do for a living. Even though I don't (yet?) use or even know Python, > through some unexplainable thought process I've decided it would be a > good language to start him off with. The Art of Problem Solving site, geared toward the best U.S. junior high and high school math students, offers a 10-week "Introduction to Programming" class http://www.artofproblemsolving.com/School/classlist.php that uses Python. There are weekly lectures in an online classroom and graded homework assignments. My 10yo boy is taking the class. But he tells me JavaScript is his favorite language :). From ishish at domhain.de Thu Aug 1 10:02:52 2013 From: ishish at domhain.de (ishish) Date: Thu, 01 Aug 2013 15:02:52 +0100 Subject: Best Python book(s) for a =?UTF-8?Q?pre-teen=3F?= In-Reply-To: References: <20619edc.0302181127.6ad39e68@posting.google.com> Message-ID: <77b1f0650e27854c0bdb92dd44a82b45@home.minuskel.de> Am 01.08.2013 14:11, schrieb beliavsky at aol.com: > On Tuesday, February 18, 2003 2:27:58 PM UTC-5, Mike Silva wrote: >> Hi all, >> >> My son is 11 and wants to try programming, partly because it's what >> I >> do for a living. Even though I don't (yet?) use or even know >> Python, >> through some unexplainable thought process I've decided it would be >> a >> good language to start him off with. > > The Art of Problem Solving site, geared toward the best U.S. junior > high and high school math students, offers a 10-week "Introduction to > Programming" class > http://www.artofproblemsolving.com/School/classlist.php that uses > Python. There are weekly lectures in an online classroom and graded > homework assignments. My 10yo boy is taking the class. But he tells > me > JavaScript is his favorite language :). I would have a look at this one: http://www.cosc.canterbury.ac.nz/csclub/book.pdf. If you like it you can still buy the print version. sas From gouzounakis at hotmail.com Thu Aug 1 20:07:41 2013 From: gouzounakis at hotmail.com (D. Xenakis) Date: Thu, 1 Aug 2013 17:07:41 -0700 (PDT) Subject: PyQt5 and virtualenv problem In-Reply-To: <39e192a0-779e-4be6-b08f-950ed1c200ac@googlegroups.com> References: <39e192a0-779e-4be6-b08f-950ed1c200ac@googlegroups.com> Message-ID: <0fe14c14-69ae-43cc-8a45-4764744012b3@googlegroups.com> Any advice?Plz? From petef4+usenet at gmail.com Fri Aug 2 06:11:22 2013 From: petef4+usenet at gmail.com (Pete Forman) Date: Fri, 02 Aug 2013 11:11:22 +0100 Subject: PyQt5 and virtualenv problem References: <39e192a0-779e-4be6-b08f-950ed1c200ac@googlegroups.com> Message-ID: <86a9l0mmgl.fsf@gmail.com> "D. Xenakis" writes: > I tried to install SIP and PyQt5 using the pip install command but it > didnt work on both cases (i was getting errors), so i finally > installed them using the windows installers provided in > riverbankcomputing website. > My problem though here is that whenever i try to create a new > virtualenv enviroment, those packages are not included and i cant > import them. How can i add PyQt5 to my new virt enviroment? What is > the logic behind this problem so i understand whats going on here? > > Thx in advance I can't comment on PyQt5 but I can say how to use PyQt4 with virtualenv on Windows. The Riverbank installers do not work in a virtualenv. However PySide wraps PyQt4 in a compatible installer. To use the installer it should be invoked with easy_install rather than pip install. Having installed it, pip uninstall works. https://pypi.python.org/pypi/PySide The Riverbank installer can install PyQt5 to your master copy of Python. You can then use the --system-site-packages flag when creating a virtualenv. The default behavior of virtualenv changed in 1.7 (2011-11-30) from including system packages to excluding them. -- Pete Forman From fabiosantosart at gmail.com Thu Aug 1 10:02:04 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Thu, 1 Aug 2013 15:02:04 +0100 Subject: sqlite3 version lacks instr In-Reply-To: References: <13d04b58a1024039b66ea54a9d5bb85b@exch.activenetwerx.com> Message-ID: I'm not sure but it seems like you could use operator.__contains__ . it might be faster. On 28 Jul 2013 20:18, "Peter Otten" <__peter__ at web.de> wrote: > Joseph L. Casale wrote: > > >> Has anyone encountered this and utilized other existing functions > >> within the shipped 3.6.21 sqlite version to accomplish this? > > > > Sorry guys, forgot about create_function... > > Too late, I already did the demo ;) > > >>> import sqlite3 > >>> db = sqlite3.connect(":memory:") > >>> cs = db.cursor() > >>> cs.execute('select instr("the quick brown fox", > "brown")').fetchone()[0] > Traceback (most recent call last): > File "", line 1, in > sqlite3.OperationalError: no such function: instr > >>> def instr(a, b): > ... return a.find(b) + 1 # add NULL-handling etc. > ... > >>> db.create_function("instr", 2, instr) > >>> cs.execute('select instr("the quick brown fox", > "brown")').fetchone()[0] > 11 > >>> cs.execute('select instr("the quick brown fox", "red")').fetchone()[0] > 0 > > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From rurpy at yahoo.com Thu Aug 1 11:20:34 2013 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Thu, 1 Aug 2013 08:20:34 -0700 (PDT) Subject: email 8bit encoding In-Reply-To: References: <36aa5535-a821-4d7e-8414-1e40820705e4@googlegroups.com> Message-ID: <1672c141-7d90-477a-a62c-8e0e97bda9be@googlegroups.com> On 07/29/2013 02:52 PM, Antoon Pardon wrote: > Op 29-07-13 01:41, rurpy at yahoo.com schreef: >> How, using Python-3.3's email module, do I "flatten" (I think >> that's the right term) a Message object to get utf-8 encoded >> body with the headers: >> Content-Type: text/plain; charset=UTF-8 >> Content-Transfer-Encoding: 8bit >> when the message payload was set to a python (unicode) string? >> > > I am just trying out some things for my self on python 3.2 so > be sure to test this out but you could try the following. > > msg.set_charset('utf-8') > msg['Content-Transfer-Encoding'] = '8bit' You can do that but the problem occurs when you call email.generator.flatten (or it is called on your behalf by somthing like smtplib.send_message) with such a message. flatten always assumes a 7bit encoding and uses the ascii codec to encode the message resulting in a UnicodeEncode exception when it hits an 8 bit character. So gymnastics like W. Trevor King implemented are necessary. From antoon.pardon at rece.vub.ac.be Fri Aug 2 07:19:11 2013 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Fri, 02 Aug 2013 13:19:11 +0200 Subject: email 8bit encoding In-Reply-To: <1672c141-7d90-477a-a62c-8e0e97bda9be@googlegroups.com> References: <36aa5535-a821-4d7e-8414-1e40820705e4@googlegroups.com> <1672c141-7d90-477a-a62c-8e0e97bda9be@googlegroups.com> Message-ID: <51FB95AF.5030602@rece.vub.ac.be> Op 01-08-13 17:20, rurpy at yahoo.com schreef: > On 07/29/2013 02:52 PM, Antoon Pardon wrote: >> Op 29-07-13 01:41, rurpy at yahoo.com schreef: >>> How, using Python-3.3's email module, do I "flatten" (I think >>> that's the right term) a Message object to get utf-8 encoded >>> body with the headers: >>> Content-Type: text/plain; charset=UTF-8 >>> Content-Transfer-Encoding: 8bit >>> when the message payload was set to a python (unicode) string? >>> >> >> I am just trying out some things for my self on python 3.2 so >> be sure to test this out but you could try the following. >> >> msg.set_charset('utf-8') >> msg['Content-Transfer-Encoding'] = '8bit' > > You can do that but the problem occurs when you call > email.generator.flatten (or it is called on your behalf by > somthing like smtplib.send_message) with such a message. > flatten always assumes a 7bit encoding and uses the ascii > codec to encode the message resulting in a UnicodeEncode > exception when it hits an 8 bit character. So gymnastics > like W. Trevor King implemented are necessary. Well this works for me. I had a little look in the code and it seems buggy to me, at least the 3.2 version is. There is a _encode but (1) The bytes generator version is defined to allways use us-ascii as encoding and (2) I couldn't find it actually being called on a mesg part. ------------------------------------------------------------ import smtplib from email.mime.text import MIMEText txt = '''\ Het adres is Fr?deric Bo?ven Fr?re Orbanstraat 17 ''' recipient = "..." sender = "..." msg = MIMEText(txt) msg.set_charset("utf-8") msg['Subject'] = 'Python email tets' msg['From'] = sender msg['To'] = recipient s = smtplib.SMTP('localhost') s.sendmail(sender, [recipient], msg.as_string().encode("utf-8")) s.quit() From antoon.pardon at rece.vub.ac.be Fri Aug 2 08:12:04 2013 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Fri, 02 Aug 2013 14:12:04 +0200 Subject: email 8bit encoding In-Reply-To: <1672c141-7d90-477a-a62c-8e0e97bda9be@googlegroups.com> References: <36aa5535-a821-4d7e-8414-1e40820705e4@googlegroups.com> <1672c141-7d90-477a-a62c-8e0e97bda9be@googlegroups.com> Message-ID: <51FBA214.4070104@rece.vub.ac.be> Op 01-08-13 17:20, rurpy at yahoo.com schreef: > On 07/29/2013 02:52 PM, Antoon Pardon wrote: >> Op 29-07-13 01:41, rurpy at yahoo.com schreef: >>> How, using Python-3.3's email module, do I "flatten" (I think >>> that's the right term) a Message object to get utf-8 encoded >>> body with the headers: >>> Content-Type: text/plain; charset=UTF-8 >>> Content-Transfer-Encoding: 8bit >>> when the message payload was set to a python (unicode) string? >>> >> >> I am just trying out some things for my self on python 3.2 so >> be sure to test this out but you could try the following. >> >> msg.set_charset('utf-8') >> msg['Content-Transfer-Encoding'] = '8bit' > > You can do that but the problem occurs when you call > email.generator.flatten (or it is called on your behalf by > somthing like smtplib.send_message) with such a message. > flatten always assumes a 7bit encoding and uses the ascii > codec to encode the message resulting in a UnicodeEncode > exception when it hits an 8 bit character. So gymnastics > like W. Trevor King implemented are necessary. This works too, but I don't know how usefull it is with multipart messages. import smtplib import os import io from email.mime.text import MIMEText from email.generator import BytesGenerator from email.generator import BytesGenerator class EncodeGenerator(BytesGenerator): def flatten(self, msg, unixfrom=False, linesep='\n'): self._encoding = str(msg.get_charset()) super().flatten(msg, unixfrom, linesep) def write(self, s): self._fp.write(s.encode(self._encoding)) def _encode(self, s): return s.encode(self._encoding) txt = '''\ Het adres is Fr?deric Bo?ven Fr?re Orbanstraat 17 ''' recipient = " ... " sender = " ... " msg = MIMEText(txt) msg.set_charset("utf-8") msg['Subject'] = 'Python email test: %d' % os.getpid() msg['From'] = sender msg['To'] = recipient print(msg['Subject']) # Send the message via our own SMTP server. s = smtplib.SMTP('localhost') with io.BytesIO() as bytesmsg: g = EncodeGenerator(bytesmsg) g.flatten(msg, linesep='\r\n') flat_msg = bytesmsg.getvalue() print(flat_msg) s.sendmail(sender, [recipient], flat_msg) s.quit() From steve+comp.lang.python at pearwood.info Thu Aug 1 02:20:23 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 01 Aug 2013 06:20:23 GMT Subject: Unexpected results comparing float to Fraction References: <51f68d9c$0$30000$c3e8da3$5496439d@news.astraweb.com> <-tidnZTes6qk72TMnZ2dnUVZ8kadnZ2d@giganews.com> Message-ID: <51f9fe27$0$30000$c3e8da3$5496439d@news.astraweb.com> On Wed, 31 Jul 2013 15:23:21 -0500, Tony the Tiger wrote: > On Mon, 29 Jul 2013 15:43:24 +0000, Steven D'Aprano wrote: > >> Am I the only one who is surprised by this? > > Most likely. > > Floats aren't precise enough to be equal to a (true) fraction. > float(1/3) is cut short somewhere by the computer, a (true) fraction of > one third is not, it goes on forever. I know this, and that's not what surprised me. What surprised me was that Fraction converts the float to a fraction, then compares. It surprises me because in other operations, Fractions down-cast to float. Adding a float to a Fraction converts the Fraction to the nearest float, then adds: py> 1/3 + Fraction(1, 3) 0.6666666666666666 but comparing a float to a Fraction does the conversion the other way, the float is up-cast to an exact Fraction, then compared. -- Steven From rosuav at gmail.com Thu Aug 1 02:32:56 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 1 Aug 2013 07:32:56 +0100 Subject: Unexpected results comparing float to Fraction In-Reply-To: <51f9fe27$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <51f68d9c$0$30000$c3e8da3$5496439d@news.astraweb.com> <-tidnZTes6qk72TMnZ2dnUVZ8kadnZ2d@giganews.com> <51f9fe27$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, Aug 1, 2013 at 7:20 AM, Steven D'Aprano wrote: > I know this, and that's not what surprised me. What surprised me was that > Fraction converts the float to a fraction, then compares. It surprises me > because in other operations, Fractions down-cast to float. > > Adding a float to a Fraction converts the Fraction to the nearest float, > then adds: > > py> 1/3 + Fraction(1, 3) > 0.6666666666666666 Hmm. This is the one that surprises me. That would be like the addition of a float and an int resulting in an int (at least in C; in Python, where floats have limited range and ints have arbitrary precision, the matter's not quite so clear-cut). Perhaps this needs to be changed? ChrisA From oscar.j.benjamin at gmail.com Thu Aug 1 05:44:12 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 1 Aug 2013 10:44:12 +0100 Subject: Unexpected results comparing float to Fraction In-Reply-To: References: <51f68d9c$0$30000$c3e8da3$5496439d@news.astraweb.com> <-tidnZTes6qk72TMnZ2dnUVZ8kadnZ2d@giganews.com> <51f9fe27$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 1 August 2013 07:32, Chris Angelico wrote: > On Thu, Aug 1, 2013 at 7:20 AM, Steven D'Aprano > wrote: >> I know this, and that's not what surprised me. What surprised me was that >> Fraction converts the float to a fraction, then compares. It surprises me >> because in other operations, Fractions down-cast to float. >> >> Adding a float to a Fraction converts the Fraction to the nearest float, >> then adds: >> >> py> 1/3 + Fraction(1, 3) >> 0.6666666666666666 > > Hmm. This is the one that surprises me. That would be like the > addition of a float and an int resulting in an int (at least in C; in > Python, where floats have limited range and ints have arbitrary > precision, the matter's not quite so clear-cut). Perhaps this needs to > be changed? The Python numeric tower is here: http://docs.python.org/3/library/numbers.html#module-numbers Essentially it says that Integral < Rational < Real < Complex and that numeric coercions in mixed type arithmetic should go from left to right which makes sense mathematically in terms of the subset/superset relationships between the numeric fields. When you recast this in terms of Python's builtin/stdlib types it becomes int < Fraction < {float, Decimal} < complex and taking account of boundedness and imprecision we find that the only subset/superset relationships that are actually valid are int < Fraction and float < complex In fact Fraction is a superset of both float and Decimal (ignoring inf/nan/-0 etc.). int is not a subset of float, Decimal or complex. float is a superset of none of the types. Decimal is a superset of float but the tower places them on the same level. The real dividing line between {int, Fraction} and {float, Decimal, complex} is about (in)exactness. The numeric tower ensures the property that inexactness is contagious which I think is a good thing. This is not explicitly documented anywhere. PEP 3141 makes a dangling reference to an Exact ABC as a superclass of Rational but this is unimplemented anywhere AFAICT: http://www.python.org/dev/peps/pep-3141/ The reason contagious inexactness is a good thing is the same as having contagious quite NaNs. It makes it possible to rule out inexact computations playing a role in the final computed result. In my previous post I asked what the use case is for mixing floats and Rationals in computation. I have always considered this to be something that I wanted to avoid and I'm glad that contagious inexactness helps me to avoid mixing floats into exact computations. Oscar From rosuav at gmail.com Thu Aug 1 05:48:40 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 1 Aug 2013 10:48:40 +0100 Subject: Unexpected results comparing float to Fraction In-Reply-To: References: <51f68d9c$0$30000$c3e8da3$5496439d@news.astraweb.com> <-tidnZTes6qk72TMnZ2dnUVZ8kadnZ2d@giganews.com> <51f9fe27$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, Aug 1, 2013 at 10:44 AM, Oscar Benjamin wrote: > The real dividing line between {int, Fraction} and {float, Decimal, > complex} is about (in)exactness. The numeric tower ensures the > property that inexactness is contagious which I think is a good thing. *nods slowly* That does make sense, albeit a little oddly. So when you're sorting out different integer sizes (C's short/int/long, Py2's int/long), you go to the "better" one, but when working with inexact types, you go to the "worse" one. But I can see the logic in it. ChrisA From joshua at landau.ws Thu Aug 1 13:21:32 2013 From: joshua at landau.ws (Joshua Landau) Date: Thu, 1 Aug 2013 18:21:32 +0100 Subject: PEP8 79 char max In-Reply-To: References: <51F6C5F5.5020201@Gmail.com> <51f6e1d8$0$30000$c3e8da3$5496439d@news.astraweb.com> <51F6ED13.5010508@Gmail.com> Message-ID: On 31 July 2013 17:32, Grant Edwards wrote: > On 2013-07-31, Tim Chase wrote: > > On 2013-07-31 07:16, Joshua Landau wrote: > >> On 30 July 2013 18:52, Grant Edwards wrote: > >>> I also find intializers for tables of data to be much more easily > >>> read and maintained if the columns can be aligned. > >> > >> Why do you have tables in your Python code? > > For example: if you're writing an assembler, you usually have a table > of mnemonics/opcodes/instruction-format/addressing-modes. Why are you writing an assembler? > > I've had occasion to write things like: > > > > for name, value, description in ( > > ("cost", 42, "How much it cost"), > > ("status", 3141, "Status code from ISO-3.14159"), > > ... > > ): > > do_something(name, value) > > print(description) > > > > I interpret Grant's statement as wanting the "table" to look like > > > > for name, value, description in ( > > ("cost", 42, "How much it cost"), > > ("status", 3141, "Status code from ISO-3.14159"), > > ... > > ): > > do_something(name, value) > > print(description) > > Exactly. When you have more than about 5 columns and 10 rows, having > things aligned makes it far, far, easier to maintain. Honestly I've never had to do something like this. If it got that large, though, I'd factor it out into it's own file and possibly take the advice from others on this list by making it a CSV. That said for someone like me the very tiny frequency I'd have to do such a thing would pale in comparison to the other costs and benefits of variable with fonts. > > which does give some modest readability benefits, but at a creation > > cost I personally am unwilling to pay. > > It only gets typed once, it gets read hundreds or thousands of times. > Optimize the common case. > PEP 8, under the things-not-to-do section, says: ? More than one space around an assignment (or other) operator to align it with another. Yes: x = 1 y = 2 long_variable = 3 No: x = 1 y = 2 long_variable = 3 I assume similar applies here. Obviously PEP 8 isn't a rule but it's a rough stab at general consensus. -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Thu Aug 1 15:03:04 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 01 Aug 2013 19:03:04 GMT Subject: PEP8 79 char max References: <51F6C5F5.5020201@Gmail.com> <51f6e1d8$0$30000$c3e8da3$5496439d@news.astraweb.com> <51F6ED13.5010508@Gmail.com> Message-ID: <51fab0e8$0$30000$c3e8da3$5496439d@news.astraweb.com> On Thu, 01 Aug 2013 18:21:32 +0100, Joshua Landau wrote: > On 31 July 2013 17:32, Grant Edwards wrote: > >> On 2013-07-31, Tim Chase wrote: >> > On 2013-07-31 07:16, Joshua Landau wrote: >> >> On 30 July 2013 18:52, Grant Edwards wrote: >> >>> I also find intializers for tables of data to be much more easily >> >>> read and maintained if the columns can be aligned. >> >> >> >> Why do you have tables in your Python code? >> >> For example: if you're writing an assembler, you usually have a table >> of mnemonics/opcodes/instruction-format/addressing-modes. > > > Why are you writing an assembler? Maybe he's (re-)creating PyPy :-) -- Steven From invalid at invalid.invalid Thu Aug 1 15:29:06 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Thu, 1 Aug 2013 19:29:06 +0000 (UTC) Subject: PEP8 79 char max References: <51F6C5F5.5020201@Gmail.com> <51f6e1d8$0$30000$c3e8da3$5496439d@news.astraweb.com> <51F6ED13.5010508@Gmail.com> Message-ID: On 2013-08-01, Joshua Landau wrote: > On 31 July 2013 17:32, Grant Edwards wrote: > >> On 2013-07-31, Tim Chase wrote: >> > On 2013-07-31 07:16, Joshua Landau wrote: >> >> On 30 July 2013 18:52, Grant Edwards wrote: >> >>> I also find intializers for tables of data to be much more easily >> >>> read and maintained if the columns can be aligned. >> >> >> >> Why do you have tables in your Python code? >> >> For example: if you're writing an assembler, you usually have a table >> of mnemonics/opcodes/instruction-format/addressing-modes. > > Why are you writing an assembler? I got tired of hand assembling (and disassembling) code for a custom microprocessor, so I wrote an assembler and a disassembler. -- Grant Edwards grant.b.edwards Yow! I smell like a wet at reducing clinic on Columbus gmail.com Day! From invalid at invalid.invalid Thu Aug 1 15:53:26 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Thu, 1 Aug 2013 19:53:26 +0000 (UTC) Subject: PEP8 79 char max References: <51F6C5F5.5020201@Gmail.com> <51f6e1d8$0$30000$c3e8da3$5496439d@news.astraweb.com> <51F6ED13.5010508@Gmail.com> Message-ID: On 2013-08-01, Grant Edwards wrote: > On 2013-08-01, Joshua Landau wrote: >> On 31 July 2013 17:32, Grant Edwards wrote: >> >>> On 2013-07-31, Tim Chase wrote: >>> > On 2013-07-31 07:16, Joshua Landau wrote: >>> >> On 30 July 2013 18:52, Grant Edwards wrote: >>> >>> I also find intializers for tables of data to be much more easily >>> >>> read and maintained if the columns can be aligned. >>> >> >>> >> Why do you have tables in your Python code? >>> >>> For example: if you're writing an assembler, you usually have a table >>> of mnemonics/opcodes/instruction-format/addressing-modes. >> >> Why are you writing an assembler? > > I got tired of hand assembling (and disassembling) code for a custom > microprocessor, so I wrote an assembler and a disassembler. FWIW, 250 lines of Python gets you a pretty decent 2-pass absolute assembler with full arithmetic expression support (including user-defined symbols). That 250 lines includes the "table" that defines the individual instructions. And yes, the columns in that table are aligned using spaces. ;) -- Grant Edwards grant.b.edwards Yow! I just had a NOSE at JOB!! gmail.com From invalid at invalid.invalid Fri Aug 2 09:58:16 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Fri, 2 Aug 2013 13:58:16 +0000 (UTC) Subject: PEP8 79 char max References: <51F6ED13.5010508@Gmail.com> Message-ID: On 2013-08-02, Dennis Lee Bieber wrote: > On Thu, 1 Aug 2013 19:29:06 +0000 (UTC), Grant Edwards > declaimed the following: > >> >>I got tired of hand assembling (and disassembling) code for a custom >>microprocessor, so I wrote an assembler and a disassembler. > > Let me know when you recreate XDS meta-symbol. It didn't even have the > Sigma native instruction set built in, one had to specify the instruction > set using a "system" directive. What it did have was a directive to define > instruction formats (my manuals are in storage so this is pseudo-code). > > MOV opt,2,3,3 b'01',af(1),af(2) > > Which translates as: mnemonic is MOV, format is 2-bits, 3-bits, 3-bits, > first field is binary 01, second field is argument field 1, third field is > argument field 2. That's pretty much how my assembler works. There is one variable bit-width operand field in the opcode byte, and some instructions have a single byte immediate operand following the opcode byte. Operands can eitehr be absolute values or PC-relative offsets (in the case of branch instructions). -- Grant Edwards grant.b.edwards Yow! I hope the at ``Eurythmics'' practice gmail.com birth control ... From wayne at waynewerner.com Sat Aug 3 07:23:21 2013 From: wayne at waynewerner.com (Wayne Werner) Date: Sat, 3 Aug 2013 06:23:21 -0500 (CDT) Subject: PEP8 79 char max In-Reply-To: References: <51F6C5F5.5020201@Gmail.com> <51f6e1d8$0$30000$c3e8da3$5496439d@news.astraweb.com> <51F6ED13.5010508@Gmail.com> Message-ID: On Wed, 31 Jul 2013, Joshua Landau wrote: > > To explain, I tend to take the "HTML" form of alignment by wrapping: > > open stuff stuff stuff close > > to > > open > ? ? stuff > ? ? stuff > ? ? stuff > close Depending on how much 'stuff' I have, I, for one, prefer a third: open stuff stuff stuff close Which then makes it 1) fairly easy to read, 2) fairly easy to extend. Of course it could just be that I'm used to that style - our brains are wired weird. -W From dwightdhutto at gmail.com Sun Aug 4 10:27:04 2013 From: dwightdhutto at gmail.com (David Hutto) Date: Sun, 4 Aug 2013 10:27:04 -0400 Subject: =?windows-1252?Q?Re=3A_Modeling_life_on_Earth_=96=2D_an_object=2Doriented_=28?= =?windows-1252?Q?Python=3F=29_challenge?= In-Reply-To: References: Message-ID: You basically have, currently, widgets,data,data manipulation through variables and widget utilization to call the functions,and data visualization. Next step would be algorithm, and pseudo code, plus a prototyped mockup of the GUI. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alister.ware at ntlworld.com Thu Aug 1 06:39:24 2013 From: alister.ware at ntlworld.com (Alister) Date: Thu, 01 Aug 2013 10:39:24 GMT Subject: Python script help References: <4566d0e7-2576-4d09-83f5-fca3b370710a@googlegroups.com> Message-ID: On Thu, 01 Aug 2013 10:57:01 +1000, alex23 wrote: > On 31/07/2013 6:15 PM, cool1574 at gmail.com wrote: >> Here are some scripts, how do I put them together to create the script >> I want? (to search a online document and download all the links in it) > > 1. Think about the requirements. > 2. Write some code. > 3. Test it. > 4. Repeat until requirements are met. > >> p.s: can I set a destination folder for the downloads? > > Yes. > > Show us you're actively trying to solve this yourself rather than just > asking us to write the code for you. alternatively i can provide a quotation to produce a product to your specification. (My rates are extremely high) -- Hand me a pair of leather pants and a CASIO keyboard -- I'm living for today! From piet at vanoostrum.org Fri Aug 23 22:37:14 2013 From: piet at vanoostrum.org (Piet van Oostrum) Date: Fri, 23 Aug 2013 22:37:14 -0400 Subject: Python script help References: <4566d0e7-2576-4d09-83f5-fca3b370710a@googlegroups.com> Message-ID: cool1574 at gmail.com writes: > Here are some scripts, how do I put them together to create the script > I want? (to search a online document and download all the links in it) > p.s: can I set a destination folder for the downloads? You can use os.chdir to go to the desired folder. > > urllib.urlopen("http://....") > > possible_urls = re.findall(r'\S+:\S+', text) > > import urllib2 > response = urllib2.urlopen('http://www.example.com/') > html = response.read() If you insist on not using wget, here is a simple script with BeautifulSoup (v4): ######################################################################## from bs4 import BeautifulSoup from urllib2 import urlopen from urlparse import urljoin import os import re os.chdir('OUT') def generate_filename(url): url = re.sub('^[a-zA-Z0-9+.-]+:/*', '', url) return url.replace('/', '_') URL = "http://www.example.com/" soup = BeautifulSoup(urlopen(URL).read()) links = soup.select('a[href]') for link in links: url = urljoin(URL, link['href']) print url html = urlopen(url).read() fn = generate_filename(url) with open(fn, 'wb') as outfile: outfile.write(html) ######################################################################## You should add a more intelligent filename generator, filter out mail: urls and possibly others and add exception handling for HTTP errors. -- Piet van Oostrum WWW: http://pietvanoostrum.com/ PGP key: [8DAE142BE17999C4] From cool1574 at gmail.com Thu Aug 1 12:02:00 2013 From: cool1574 at gmail.com (cool1574 at gmail.com) Date: Thu, 1 Aug 2013 09:02:00 -0700 (PDT) Subject: Python script help In-Reply-To: <4566d0e7-2576-4d09-83f5-fca3b370710a@googlegroups.com> References: <4566d0e7-2576-4d09-83f5-fca3b370710a@googlegroups.com> Message-ID: I know I should be testing out the script myself but I did, I tried and since I am new in python and I work for a security firm that ask me to scan hundreds of documents a day for unsafe links (by opening them) I thought writing a script will be much easier. I do not know how to combine those three scripts together (the ones I wrote in my previous replay) that is why I cam to here for help. please help me build a working script that will do the job. Thanks in advance. you can contact me at cool1574 at gmail.com From ulrich.eckhardt at dominolaser.com Fri Aug 2 04:44:58 2013 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Fri, 02 Aug 2013 10:44:58 +0200 Subject: Python script help In-Reply-To: References: <4566d0e7-2576-4d09-83f5-fca3b370710a@googlegroups.com> Message-ID: Am 01.08.2013 18:02, schrieb cool1574 at gmail.com: > I know I should be testing out the script myself but I did, I tried > and since I am new in python and I work for a security firm that ask > me to scan hundreds of documents a day for unsafe links (by opening > them) I thought writing a script will be much easier. I do not know > how to combine those three scripts together (the ones I wrote in my > previous replay) that is why I cam to here for help. please help me > build a working script that will do the job. This first option is to hire a programmer, which should give you the quickest results. If the most important thing is getting the job done, then this should be your #1 approach. Now, if you really want to do it yourself, you will have to do some learning yourself. Start with http://docs.python.org, which includes tutorials, references and a bunch of other links, in particular go through the tutorials. Make sure you pick the documentation corresponding to your Python version though, versions 2 and 3 are subtly different! Then, read http://www.catb.org/esr/faqs/smart-questions.html. This is a a bit metatopical but still important, and while this doesn't make you a programmer in an afternoon, it will help you understand various reactions you received here. hope that gets you started Uli From cool1574 at gmail.com Fri Aug 2 05:46:08 2013 From: cool1574 at gmail.com (cool1574 at gmail.com) Date: Fri, 2 Aug 2013 02:46:08 -0700 (PDT) Subject: Python script help In-Reply-To: <4566d0e7-2576-4d09-83f5-fca3b370710a@googlegroups.com> References: <4566d0e7-2576-4d09-83f5-fca3b370710a@googlegroups.com> Message-ID: <1faf05ad-2cd4-497e-a605-db4650c04103@googlegroups.com> I do know some Python programming, I just dont know enough to put together the various scripts I need...I would really really appreciate if some one can help me with that... From rosuav at gmail.com Fri Aug 2 06:01:54 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 2 Aug 2013 11:01:54 +0100 Subject: Python script help In-Reply-To: <1faf05ad-2cd4-497e-a605-db4650c04103@googlegroups.com> References: <4566d0e7-2576-4d09-83f5-fca3b370710a@googlegroups.com> <1faf05ad-2cd4-497e-a605-db4650c04103@googlegroups.com> Message-ID: On Fri, Aug 2, 2013 at 10:46 AM, wrote: > I do know some Python programming, I just dont know enough to put together the various scripts I need...I would really really appreciate if some one can help me with that... Be aware that you might be paying money for that. If you know "some" carpentry but not enough to put together a bookcase, and you ask a professional carpenter to make you a bookcase, you'll have to pay him. The same is true in programming, except that there are more people willing to work for nothing, hence the vague "might be" rather than the inevitable "shall" or the mighty "must" [1]. To get people to work for you for free, you have to make them (us) want to, which in the geeky arts generally means making it an interesting problem. Achieving this is described well in esr's essay on asking smart questions [2], which Ulrich also just pointed you to. We do this sort of thing for fun, for love, so if you make your problem appeal to us, there's a high chance that someone will provide you with code. [1] http://www.youtube.com/watch?v=GVVTYII422k [2] http://www.catb.org/esr/faqs/smart-questions.html ChrisA From cool1574 at gmail.com Sun Aug 4 11:57:28 2013 From: cool1574 at gmail.com (cool1574 at gmail.com) Date: Sun, 4 Aug 2013 08:57:28 -0700 (PDT) Subject: Python script help In-Reply-To: References: <4566d0e7-2576-4d09-83f5-fca3b370710a@googlegroups.com> <1faf05ad-2cd4-497e-a605-db4650c04103@googlegroups.com> Message-ID: I understand I did not ask the question correctly, but is there any chance you can help me put together this code? I know that you all do this for fun and enjoy it and that is why I asked you guys instead of asking some one who will charge me for a very simple line of code. I would appreciate it, Thank you. From rosuav at gmail.com Sun Aug 4 12:20:34 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 4 Aug 2013 17:20:34 +0100 Subject: Python script help In-Reply-To: References: <4566d0e7-2576-4d09-83f5-fca3b370710a@googlegroups.com> <1faf05ad-2cd4-497e-a605-db4650c04103@googlegroups.com> Message-ID: On Sun, Aug 4, 2013 at 4:57 PM, wrote: > I understand I did not ask the question correctly, but is there any chance you can help me put together this code? I know that you all do this for fun and enjoy it and that is why I asked you guys instead of asking some one who will charge me for a very simple line of code. > I would appreciate it, Thank you. There are a million and one projects out there that I could do for fun. Why should I do yours rather than one of theirs? The key is to make your problem look more fun, or more useful, than the others. At the moment, it looks fairly un-fun (just recreating wget with less features), and not particularly useful (you could just use wget). So at the moment, I don't feel inclined to put in several hours of unpaid work for you. I'll give you a few examples of things I *have* put hours of unpaid work into, over the past few weeks: * The Savoynet Performing Group production of The Yeomen of the Guard. It's fun because the music's great and I'm working with awesome people. (Also because the director has come up with an interpretation of the finale that works better than any I've yet seen.) The lead soprano is very close to going insane, the tragic comic sends a shiver up my spine with the way he says "Elsie", and we have chocolate at rehearsal (which I provide at my own expense). Fun and useful. * The professional company performing Pirates of Penzance and Iolanthe needs help moving costumes in and out. Again, useful, and working with the best people. When the organizers of an international festival say you're invaluable, that's pretty high praise. * The Gilbert & Sullivan Society back home needs someone to manage its domain, web hosting, internal Mailman list, etc, etc, etc. Most of it is fairly mundane and unexciting, but it's useful. * Gypsum is my designated successor to my somewhat popular MUD client RosMud, achieving many of the things that I can't do with RosMud. As a gamer, I like my game clients. Very fun and very useful. * Related to the above, digging through the uncharted waters of mixed metaphors and the Pike programming language, discovering language bugs that probably nobody had ever run into before; and then submitting patches and, again, seeing the approval and appreciation from people I respect highly. * Reading Alice in Wonderland to my eleven-year-old sister who'd never heard it before. (Also to the rest of the family, who frequently 'just happened' to hang around as I was reading.) * Telling people about the Alice: Otherlands Kickstarter campaign [1], which I'd really like to see succeed (if it reaches $250,000 within the next few hours, the original voices of Alice and the Cheshire Cat will be brought in!). These are all projects that tie in with one of my interests or hobbies (Gilbert and Sullivan, MUDding, and Alice in Wonderland). That gives them a huge head-start in the "fun" and "interesting" categories. You're trying to get me to donate my time and effort to your project; to do that, you have to make your project look as interesting as one of those. Okay, maybe not quite; each of the above has had MANY dev hours donated to it, and you're just looking for maybe 1-2 hours. But still, that's worth maybe a hundred dollars, so think of your request as soliciting a donation of that amount. How are you going to pitch that? By the way, I am right now donating time towards a meta-cause: your ability to handle yourself on an internet mailing list. I consider that cause to be *extremely* useful, because it empowers the world and you in ways that will make life easier for everyone, most notably people on this list who I respect quite highly. So I'm happy to donate ten or fifteen minutes to explaining exactly what it takes to get something done, because - unless I've completely misread you - you, and the whole world, will benefit that many times over. [1] http://www.kickstarter.com/projects/spicyhorse/alice-otherlands ChrisA From torriem at gmail.com Sun Aug 4 18:58:26 2013 From: torriem at gmail.com (Michael Torrie) Date: Sun, 04 Aug 2013 16:58:26 -0600 Subject: Python script help In-Reply-To: <1faf05ad-2cd4-497e-a605-db4650c04103@googlegroups.com> References: <4566d0e7-2576-4d09-83f5-fca3b370710a@googlegroups.com> <1faf05ad-2cd4-497e-a605-db4650c04103@googlegroups.com> Message-ID: <51FEDC92.6080206@gmail.com> On 08/02/2013 03:46 AM, cool1574 at gmail.com wrote: > I do know some Python programming, I just dont know enough to put > together the various scripts I need...I would really really > appreciate if some one can help me with that... Seems like your first task, then, is to become proficient at python so that you can read the scripts you find and understand how they work so that you can then take that as inspiration for your own project. We're happy to answer questions about python programming in general. Good luck. Python is a really fun language and if you read the docs and tutorials, and start actually messing around with code (there are lots of examples of using urllib2 out there, and also parsing libraries), you'll make good progress. From jake.angulo at gmail.com Sun Aug 4 20:30:57 2013 From: jake.angulo at gmail.com (Jake Angulo) Date: Mon, 5 Aug 2013 10:30:57 +1000 Subject: Python script help In-Reply-To: <51FEDC92.6080206@gmail.com> References: <4566d0e7-2576-4d09-83f5-fca3b370710a@googlegroups.com> <1faf05ad-2cd4-497e-a605-db4650c04103@googlegroups.com> <51FEDC92.6080206@gmail.com> Message-ID: On Mon, Aug 5, 2013 at 8:58 AM, Michael Torrie wrote: > On 08/02/2013 03:46 AM, cool1574 at gmail.com wrote: > > I do know some Python programming, I just dont know enough to put > > together the various scripts I need...I would really really > > appreciate if some one can help me with that... > Hi Cool, Unfortunately you really gotta know enough Python to put things together, so if you have time - learn a little more python, and then you can choose any of these 2 tools to do the job: http://doc.scrapy.org/en/latest/intro/tutorial.html http://www.gregreda.com/2013/03/03/web-scraping-101-with-python/ In fact i agree you dont even need python. Even Bash / shell script with wget can do this. However if you dont have the time or dont want to exert the req'd effort, unfortunately this list is not for giving free code. I suggest you hire somebody at odesk.com or elance.com - you'd be amazed how low people there charge for python "web scraping". Good luck! -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Thu Aug 1 02:23:20 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 01 Aug 2013 06:23:20 GMT Subject: Script that converts between indentation and curly braces in Python code References: Message-ID: <51f9fed7$0$30000$c3e8da3$5496439d@news.astraweb.com> On Wed, 31 Jul 2013 10:45:36 +0700, Musical Notation wrote: > Is there any script that converts indentation in Python code to curly > braces? The indentation is sometime lost when I copy my code to an > application or a website. Complain to the website or application that it is throwing away significant data. If you had a website that turned this paragraph into this: "Complaintothewebsiteorapplicationthatitisthrowingawaysignificantdata.Ifyouhadawebsitethatturnedthisparagraphintothis" you would rightly complain about the bug, or stop using the website. Throwing away indentation is no different. -- Steven From steve+comp.lang.python at pearwood.info Thu Aug 1 01:55:55 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 01 Aug 2013 05:55:55 GMT Subject: Lambda function Turing completeness References: Message-ID: <51f9f86b$0$30000$c3e8da3$5496439d@news.astraweb.com> On Wed, 31 Jul 2013 13:53:26 +0700, Musical Notation wrote: > Is it possible to write a Turing-complete lambda function (which does > not depend on named functions) in Python? lambda s: eval(s) -- Steven From ian.g.kelly at gmail.com Thu Aug 1 13:05:07 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 1 Aug 2013 11:05:07 -0600 Subject: Lambda function Turing completeness In-Reply-To: <51f9f86b$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <51f9f86b$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Wed, Jul 31, 2013 at 11:55 PM, Steven D'Aprano wrote: > On Wed, 31 Jul 2013 13:53:26 +0700, Musical Notation wrote: > >> Is it possible to write a Turing-complete lambda function (which does >> not depend on named functions) in Python? > > > lambda s: eval(s) eval is a named function. From piet at vanoostrum.org Sat Aug 24 19:45:28 2013 From: piet at vanoostrum.org (Piet van Oostrum) Date: Sat, 24 Aug 2013 19:45:28 -0400 Subject: Lambda function Turing completeness References: Message-ID: Musical Notation writes: > Is it possible to write a Turing-complete lambda function (which does > not depend on named functions) in Python? The wording of this question is questionable. Turing completeness is not an attribute of a function, but of a system (for example a programming language or a machine). It means that for every Turing machine you can write a program in that language or program the machine in such a way that it emulates that Turing machine. So you could ask if the subset of the Python programs consisting only of a lambda expression is Turing complete. Or alternatively if for every Turing machine you can write a lambda expression that emulates that Turing machine. It has been proven that the ? calculus is equivalent to Turing machines, so if the lambda calculus can be emulated with Python's lambda expressions the answer is yes. In the lambda calculus you can define lambda expressions and apply functions to parameters. The parameters may be functions (in fact in the pure ? calculus there is nothing else), so functions must be first class citizens. Fortunately in Python this is the case. So we suspect it can be done. An important result in the ? calculus is that every expression can be expressed in three functions S, K and I with only function application. So we are going to try to do these in Python and see if it works. The definitions in the ? calculus are: S = ? x y z. (x z) (y z) K = ? x y. x I = ? x. x The dot is used where Python uses :, function application is written as juxtaposition: f x and ? x y is an abbreviation of ? x. ? y So we are going to translate these in python. We have to explicitely write the lambda's (each one with a single parameter) and add parentheses around the function arguments if not already there. >>> S = lambda x: lambda y: lambda z: x(z) (y(z)) >>> K = lambda x: lambda y: x >>> I = lambda x: x Now there is a theorem that SKK == I (I is the identity), so we are going to test that: >>> S(K)(K)('test') 'test' a few more tests: >>> for x in range(100): ... if S(K)(K)(x) != I(x): ... print('Not equal for x = %s' % x) ... All seem to be equal. Of course we still have used names for the functions, but this is not essential. We can just replace each of S, K, and I with their definition: >>> print((lambda x: lambda y: lambda z: x(z) (y(z))) # S ... (lambda x: lambda y: x) # (K) ... (lambda x: lambda y: x)('test')) # (K) ('test') test >>> for x in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ': ... if ((lambda x: lambda y: lambda z: x(z) (y(z))) ... (lambda x: lambda y: x) ... (lambda x: lambda y: x)(x)) != (lambda x: x)(x): ... print('Not equal for x = %s' % x) ... Success! Now the pure ? lambda calculus has to express inter=gers, booleans etc. also as lambda expressions and this makes it really unwieldy. However, you can add some standard functions or expressions for these and that doesn't diminish the expressiveness of the calculus. So I suppose that you want to allow the use of all standard Python functions and expressions. Modern Pythons have conditional expressions, so this helps. We don't have to emulate booleans and conditions with weird lambda expressions. In Python's lambda expressions you can not use statements, only expressions, so without conditional expressiosn Python's booleans wouldn't be very useful. The remaining problem is how to use loops or recursion. I'll do that in a separate posting. -- Piet van Oostrum WWW: http://pietvanoostrum.com/ PGP key: [8DAE142BE17999C4] From piet at vanoostrum.org Sat Aug 24 20:30:32 2013 From: piet at vanoostrum.org (Piet van Oostrum) Date: Sat, 24 Aug 2013 20:30:32 -0400 Subject: Lambda function Turing completeness References: Message-ID: This is the second part of my posting on the Turing completeness of Python's lambda expressions. This time I am going to define a recursive function as a lambda expression (I use lambda when I am talking about Python's lambda expressions, and ? for the theory ? ? calculus.) Now of course it is easy to define a recursive function if you can use its function name inside the body. But the question of the OP was if you can do it without named functions. The pure ? calculus only works with unnamed ? expressions. Therefore we need a special operator to define recursive functions. This is the so called Y combinator, or Y operator[1]. The defining characteristic of Y is: Y(f) = f(Y(f)) for all functions f. There are several possible definitions of this operator, but some of them work only for programming languages with lazy evaluation or call by name. For Python's call by valye the following one will work: Y = ?f.(?x.f (?v.((x x) v))) (?x.f (?v.((x x) v))) Translated in Python: >>> Y = lambda f: (lambda x: f (lambda v: ((x (x)) (v)))) \ ... (lambda x: f (lambda v: ((x (x)) (v)))) We are going to define a lambda expression for the factorial function. We need a helper function for this. The idea is to have the final recursive function as a parameter of the helper function. See [1]. def fact_helper(f, n): if n == 0: return 1 else: return n * f(n-1) No we have to rewrite this to get a proper lambda expression. We split the two parameters and give each of them a lambda, and we replace the if statement with a conditional expression. >>> fact_helper = lambda f: lambda n: (1 if n == 0 else n * f(n-1)) Now we apply the Y combinator to fact_helper to get the recursive fact function and check it: >>> fact = Y (fact_helper) >>> fact(5) 120 Of course to get pure we have to get rid of the names of the functions. So we replace each of Y, fact and fact_helper with their definition: >>> (lambda f: (lambda x: f (lambda v: ((x (x)) (v)))) \ ... (lambda x: f (lambda v: ((x (x)) (v))))) \ ... (lambda f: lambda n: (1 if n == 0 else n * f(n-1))) (5) 120 Lo and behold! We have the right answer. Now writing a universal Turing machine as a single Python lambda expression is left as an exercise for the reader. BTW. If you use Python 3 you can have print inside a lambda expression, so this makes all this even nicer. -------------- [1] http://en.wikipedia.org/wiki/Fixed-point_combinator#Y_combinator -- Piet van Oostrum WWW: http://pietvanoostrum.com/ PGP key: [8DAE142BE17999C4] From frank at chagford.com Fri Aug 2 02:23:01 2013 From: frank at chagford.com (Frank Millman) Date: Fri, 2 Aug 2013 08:23:01 +0200 Subject: Problem with psycopg2, bytea, and memoryview References: Message-ID: "Terry Reedy" wrote in message news:ktbj9i$4au$1 at ger.gmane.org... > On 7/31/2013 9:07 AM, Antoine Pitrou wrote: >> >> I would suggest asking the psycopg2 project why they made this choice, >> and >> if they would reconsider. Returning a memoryview doesn't make much sense >> IMHO. > > I agree. > "memoryview objects allow Python code to access the internal data of an > object that supports the buffer protocol without copying." > Example: the binary image data of an image object. > They are not intended to be a standalone objects when there is an obvious > alternative (in this case, bytes). For the record, I forwarded this to the psycopg2 list, and got the following reply from Daniele Varrazzo - Hi Frank, thank you for forwarding the thread. Until a not very long time ago, this was exactly the case: upon reading bytea we were handed over some data to pass to a libpq function to decode. The resulting decoded string was to be released by PQfreemem, and the memoryview was the right object to reconcile the lifetime of the python object with this deallocation requirement. Later things have changed: because of the change in bytea format in PostgreSQL 9.0 (the new format was the default and wasn't handled by libpq < 9.0, which created widespread problems) we wrote our own parser. As a consequence the memoryview is not really needed anymore, but we preferred to avoid breaking programs already using the current interface. So yes: bytes (and str on Py2) is definitely a better object to get in Python. It is already on the list of what I want in a version that would break compatibility in order to gain feature or improve the adapter in other ways (e.g. psycopg3). Frank From dieter at handshake.de Thu Aug 1 02:05:37 2013 From: dieter at handshake.de (dieter) Date: Thu, 01 Aug 2013 08:05:37 +0200 Subject: Problem with psycopg2, bytea, and memoryview References: Message-ID: <8761vqc5e6.fsf@handshake.de> "Frank Millman" writes: > ... > At present, I loop over a range of columns, comparing 'before' and 'after' > values, without worrying about their types. Strings are returned as str, > integers are returned as int, etc. Now I will have to check the type of each > column before deciding whether to cast to 'bytes'. Of course, you could implement your own "equality" function ("my_equal") and replace "a == b" by "my_equal(a, b)". In "my_equal", you could encapsulate whatever logic is necessary for a reliable equality check. From frank at chagford.com Thu Aug 1 04:03:03 2013 From: frank at chagford.com (Frank Millman) Date: Thu, 1 Aug 2013 10:03:03 +0200 Subject: Problem with psycopg2, bytea, and memoryview References: Message-ID: "Neil Cerutti" wrote in message news:b5sk3cFkiq8U1 at mid.individual.net... > On 2013-07-31, Frank Millman wrote: >> >> >> Can anyone explain *why* the results do not compare equal? If I >> understood the problem, I might be able to find a workaround. > > A memoryview will compare equal to another object that supports > the buffer protocol when the format and shape are also equal. The > database must be returning chunks of binary data in a different > shape or format than you are writing it. > > Perhaps psycopg2 is returning a chunk of ints when you have > written a chunk of bytes. Check the .format and .shape members of > the return value to see. > >>>> x = memoryview(b"12345") >>>> x.format > 'B' >>>> x.shape > (5,) >>>> x == b"12345" > True > > My guess is you're getting format "I" from psycopg2. Hopefully > there's a way to coerce your desired "B" format interpretation of > the raw data using psycopg2's API. > Thanks very much for the explanation, Neil. I tried what you suggested, and the object returned by psycopg2 has a format of 'c' and a shape of (5,). I don't know what it means, but luckily I have found a workaround. I enquired on the psycopg2 list, and someone explained how I can create an extension that forces it to return 'bytes' instead of a 'memoryview'. I tested it and it works. Problem solved :-) For the record, I passed on the suggestion from Antoine and Terry that they change their program to return 'bytes'. It will be interesting to see if anyone responds. Thanks again to all for your help. Frank From davea at davea.name Thu Aug 1 08:59:41 2013 From: davea at davea.name (Dave Angel) Date: Thu, 1 Aug 2013 12:59:41 +0000 (UTC) Subject: script to Login a website References: <0bf56e63-a6d3-4c7a-a4e9-642351081311@googlegroups.com> Message-ID: wachkama at gmail.com wrote: > On Wednesday, July 31, 2013 12:21:59 PM UTC-4, John Gordon wrote: >> >> How is the data in 'users.txt' and 'password.txt' organized? Given the >> >> filenames, I would expect that 'users.txt' contains one username on each >> >> line, and 'password.txt' contains one password on each line, with the >> >> first username belonging with the first password, the second username >> >> belonging with the second password, and so on. >> > the user.txt file has one user name on each line and so does the password.txt. with this in mind each user will attempt to log in with all the passwords on password.txt file. when it gets to the end of the line it will go to the next user in users.txt and do the same i.e attempt to log in with all the passwords in the file. > So to your second question I will use all the users one after the other attempting to log in. > I am able to get each user to use "x" number of passwords in the password.txt that part works fine. The login section is where I am stuck ? My guess is you've got a nested loop, with the outer loop going through the user file, and the inner loop going through the password file. You're attempting to do readline() or equivalent from the password file in the inner loop, and once you've gone through the outer loop once, there is nothing left in the password file. userfile = open("user.txt") passwordfile = open("password.txt") for user in userfile: for password in passwordfile: process this user/pw combination If that's your logic, simply move the open line for password.txt into the outer loop, so it gets reopened each time. You could also do a seek() to get to the beginning of the file each time, or you could preread all the passwords into a list, or ... Please note that the buggy googlegroups has totally messed up your quoting. Either restrict the quoting to a couple of lines, or remove all those extra blank lines. -- DaveA From wachkama at gmail.com Thu Aug 1 09:48:22 2013 From: wachkama at gmail.com (wachkama at gmail.com) Date: Thu, 1 Aug 2013 06:48:22 -0700 (PDT) Subject: script to Login a website In-Reply-To: References: Message-ID: <6ad4bcae-40ba-4fd1-9b88-f0072cc17764@googlegroups.com> On Wednesday, July 31, 2013 11:33:25 AM UTC-4, wach... at gmail.com wrote: > I have created a script to log in a website. It gets its username and password from two files, then log's in with this credentials. My code is not showing me what username it is using to login from the file. And I am not sure if it is even opening up the url and prompting for login. I am stuck can someone help me ? > > > > > > > > > > > > import urllib, urllib2 > > > > user = open ('users.txt' , 'r') > > password = open ('password.txt' , 'r') > > > > for users in user: > > password.seek(0) > > for pass_list in password: > > login_data = users + '\n' + pass_list > > print login_data > > > > base_url = 'http://mysite.com' > > #login action we want to post data to > > response = urllib2.Request(base_url) > > login_action = '/auth/login' > > login_action = base_url + login_action > > response = urllib2.urlopen(login_action, login_data) > > response.read() > > print response.headers > > print response.getcode() This is how my out put looks like when I run the script User1 at mymail.com Password1 User1 at mymail.com Password2 User1 at mymail.com Password3 User1 at mymail.com Password4 User2 at mymail.com Password1 User2 at mymail.com Password2 User2 at mymail.com Password3 User2 at mymail.com Password4 User3 at mymail.com Password1 User3 at mymail.com Password2 User3 at mymail.com Password3 User3 at mymail.com Password4 User4 at mymail.com Password1 User4 at mymail.com Password2 User4 at mymail.com Password3 User4 at mymail.com Password4 Date: Thu, 01 Aug 2013 13:45:07 GMT Server: Apache X-Powered-By: PHP/5.3.3 Set-Cookie: PHPSESSID=3523t5l1vhqisaabfss8ra5tv5; path=/ Cache-Control: no-cache Connection: close Transfer-Encoding: chunked Content-Type: text/html; charset=UTF-8 200 From davea at davea.name Thu Aug 1 10:18:40 2013 From: davea at davea.name (Dave Angel) Date: Thu, 1 Aug 2013 14:18:40 +0000 (UTC) Subject: script to Login a website References: <6ad4bcae-40ba-4fd1-9b88-f0072cc17764@googlegroups.com> Message-ID: wachkama at gmail.com wrote: > On Wednesday, July 31, 2013 11:33:25 AM UTC-4, wach... at gmail.com wrote: >> I have created a script to log in a website. It gets its username and password from two files, then log's in with this credentials. My code is not showing me what username it is using to login from the file. And I am not sure if it is even opening up the url and prompting for login. I am stuck can someone help me ? >> >> >> >> >> >> >> >> >> >> >> >> import urllib, urllib2 >> >> >> >> user = open ('users.txt' , 'r') >> >> password = open ('password.txt' , 'r') >> >> >> >> for users in user: >> >> password.seek(0) >> >> for pass_list in password: >> >> login_data = users + '\n' + pass_list >> >> print login_data Please ignore myearlier post, as I somehow missed your post where you showed the code. Anyway, your problem is that you print the login_data, but you don't act on it. You should make the call to the login function right after the print statement, rather than after the loops are done. At that point, all you have is the last user name and password. All the following code should be moved to a function, and called from the same level as the print. >> >> >> >> base_url = 'http://mysite.com' >> >> #login action we want to post data to >> >> response = urllib2.Request(base_url) >> >> login_action = '/auth/login' >> >> login_action = base_url + login_action >> >> response = urllib2.urlopen(login_action, login_data) >> >> response.read() >> >> print response.headers >> >> print response.getcode() > = -- DaveA From wachkama at gmail.com Thu Aug 1 15:31:50 2013 From: wachkama at gmail.com (wachkama at gmail.com) Date: Thu, 1 Aug 2013 12:31:50 -0700 (PDT) Subject: script to Login a website In-Reply-To: References: Message-ID: On Wednesday, July 31, 2013 11:33:25 AM UTC-4, wach... at gmail.com wrote: > I have created a script to log in a website. It gets its username and password from two files, then log's in with this credentials. My code is not showing me what username it is using to login from the file. And I am not sure if it is even opening up the url and prompting for login. I am stuck can someone help me ? > > > > > > > > > > > > import urllib, urllib2 > > > > user = open ('users.txt' , 'r') > > password = open ('password.txt' , 'r') > > > > for users in user: > > password.seek(0) > > for pass_list in password: > > login_data = users + '\n' + pass_list > > print login_data > > > > base_url = 'http://mysite.com' > > #login action we want to post data to > > response = urllib2.Request(base_url) > > login_action = '/auth/login' > > login_action = base_url + login_action > > response = urllib2.urlopen(login_action, login_data) > > response.read() > > print response.headers > > print response.getcode() That defiantly Got me my results. Thank you. I put the code in login code into the loop and that has given me my expected results. From ben+python at benfinney.id.au Fri Aug 2 02:35:53 2013 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 02 Aug 2013 16:35:53 +1000 Subject: Editing tabular data References: Message-ID: <7wk3k4y4za.fsf@benfinney.id.au> Skip Montanaro writes: > I really love Emacs, however... [?] > > This is clearly a case where choosing the proper tool is important. I > agree that using a spreadsheet to edit a 3x5 CSV file is likely > overkill (might just as well use Notepad or TextEdit), but tabular > data are tabular data, no matter how they might be delimited, and if > there are many of those little data critters, there are better tools > than a text editor (or Python IDE) for maintaining them. It seems an obvious thing for powerful text editors like Emacs and Vim to have a third-party mode for editing CSV data with a tabular interface. Indeed, such modes exist; one that I found immediately for Emacs is . Has anyone got a good Emacs mode for editing CSV data as a table and saving it back to CSV data? -- \ ?Isn't it enough to see that a garden is beautiful without | `\ having to believe that there are fairies at the bottom of it | _o__) too?? ?Douglas Adams | Ben Finney From ben+python at benfinney.id.au Fri Aug 2 03:14:36 2013 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 02 Aug 2013 17:14:36 +1000 Subject: LibreOffice (was: Editing tabular data) References: Message-ID: <7wfvusy36r.fsf_-_@benfinney.id.au> Chris Angelico writes: > [?] rather than OO/LibreOffice. (I'll not distinguish those two. Far > as I'm concerned, they're one product with two names.) That's simply false. LibreOffice has, since the 2010 fork of the code base and especially since the exodus of developers to The Document Foundation [0], gained a great number of improvements [1] and is now the clear inheritor of active development. Oracle's behaviour (as holder of the OpenOffice trademark) shows they have no interest in a free-software OpenOffice. Even Oracle Linux no longer has OpenOffice, instead shipping LibreOffice [2]. Claiming they're the same product is ignoring the transfer of development away from the OpenOffice.org code dump, and to LibreOffice as the actively-developed product. [0] https://en.wikipedia.org/wiki/LibreOffice#History_after_the_establishment_of_The_Document_Foundation [1] https://en.wikipedia.org/wiki/LibreOffice#Release_history [2] https://oss.oracle.com/ol6/docs/RELEASE-NOTES-U3-en.html -- \ ?True greatness is measured by how much freedom you give to | `\ others, not by how much you can coerce others to do what you | _o__) want.? ?Larry Wall | Ben Finney From rosuav at gmail.com Fri Aug 2 05:37:21 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 2 Aug 2013 10:37:21 +0100 Subject: LibreOffice (was: Editing tabular data) In-Reply-To: <7wfvusy36r.fsf_-_@benfinney.id.au> References: <7wfvusy36r.fsf_-_@benfinney.id.au> Message-ID: On Fri, Aug 2, 2013 at 8:14 AM, Ben Finney wrote: > Chris Angelico writes: > >> [?] rather than OO/LibreOffice. (I'll not distinguish those two. Far >> as I'm concerned, they're one product with two names.) > > That's simply false. ... > > Claiming they're the same product is ignoring the transfer of > development away from the OpenOffice.org code dump, and to LibreOffice > as the actively-developed product. To be sure, they're different; but they're part of one family tree. It's like referring to "Debian/Ubuntu" when you're discussing something where it makes absolutely zero difference which one you're talking about. The difference between using LibreOffice and using OpenOffice is nothing compared to the difference between working with either of the above and putting a literal in your code. ChrisA From invalid at invalid.invalid Fri Aug 2 10:00:11 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Fri, 2 Aug 2013 14:00:11 +0000 (UTC) Subject: LibreOffice (was: Editing tabular data) References: <7wfvusy36r.fsf_-_@benfinney.id.au> Message-ID: On 2013-08-02, Chris Angelico wrote: > On Fri, Aug 2, 2013 at 8:14 AM, Ben Finney wrote: >> Chris Angelico writes: >> >>> [?] rather than OO/LibreOffice. (I'll not distinguish those two. Far >>> as I'm concerned, they're one product with two names.) >> >> That's simply false. ... >> >> Claiming they're the same product is ignoring the transfer of >> development away from the OpenOffice.org code dump, and to LibreOffice >> as the actively-developed product. > > To be sure, they're different; but they're part of one family tree. > It's like referring to "Debian/Ubuntu" when you're discussing > something where it makes absolutely zero difference which one you're > talking about. The difference between using LibreOffice and using > OpenOffice is nothing compared to the difference between working with > either of the above and putting a literal in your code. In the context in which I mentioned LibreOffice, I don't even consider there to be a significant difference between Libre/OO and Excel. -- Grant Edwards grant.b.edwards Yow! Those people look at exactly like Donnie and gmail.com Marie Osmond!! From tjreedy at udel.edu Fri Aug 2 21:26:36 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 02 Aug 2013 21:26:36 -0400 Subject: LibreOffice In-Reply-To: <7wfvusy36r.fsf_-_@benfinney.id.au> References: <7wfvusy36r.fsf_-_@benfinney.id.au> Message-ID: On 8/2/2013 3:14 AM, Ben Finney wrote: > Chris Angelico writes: > >> [?] rather than OO/LibreOffice. (I'll not distinguish those two. Far >> as I'm concerned, they're one product with two names.) > > That's simply false. LibreOffice has, since the 2010 fork of the code > base and especially since the exodus of developers to The Document > Foundation [0], gained a great number of improvements [1] and is now the > clear inheritor of active development. Of relevance to this list, Libre Office upgraded the included Python interpreter to 3.3. I have no idea whether OO is still using 2.3 or also updated. -- Terry Jan Reedy From drobinow at gmail.com Sat Aug 3 11:08:08 2013 From: drobinow at gmail.com (David Robinow) Date: Sat, 3 Aug 2013 11:08:08 -0400 Subject: LibreOffice In-Reply-To: References: <7wfvusy36r.fsf_-_@benfinney.id.au> Message-ID: On Fri, Aug 2, 2013 at 9:26 PM, Terry Reedy wrote: > ... > Of relevance to this list, Libre Office upgraded the included Python > interpreter to 3.3. I have no idea whether OO is still using 2.3 or also > updated. They're up to 2.7 now. From neilc at norwich.edu Thu Aug 1 10:04:53 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 1 Aug 2013 14:04:53 GMT Subject: Editing tabular data [was: PEP8 79 char max] References: Message-ID: On 2013-08-01, Chris Angelico wrote: > On Wed, Jul 31, 2013 at 8:02 PM, Grant Edwards wrote: >> On 2013-07-31, Skip Montanaro wrote: >>>> I don't understand. That just moves them to a different >>>> file -- doesn't it? You've still got to deal with editing a >>>> large table of data (for example when I want to add >>>> instructions to your assembler). >>> >>> My guess is it would be more foolproof to edit that stuff >>> with a spreadsheet. >> >> Many years ago, I worked with somebody who used a spreadsheet >> like that. I tried it and found it to be way too cumbersome. >> The overhead involved of putting tables in to slew of >> different files and starting up LibreOffice to edit/view them >> is huge compared to just editing them with emacs in a file >> along with the source code. Maybe my computer is too >> old/slow. Maybe it's just due to how bad I am at >> Excel/LibreOffice... > > I'm glad someone else feels that way! > > At work, we have a number of CSV files (at my boss's > insistence; I would much rather they be either embedded in the > source, or in some clearer and simpler format) which I like to > manipulate in SciTE, rather than OO/LibreOffice. (I'll not > distinguish those two. Far as I'm concerned, they're one > product with two names.) My boss can't understand why I do > this. I can't understand why he objects to having to edit code > files to alter internal data. I have pointed him to [1] but to > no avail. > > The one thing I would do, though, is align with tabs rather > than spaces. That gives you an 8:1 (if you keep your tabs at > eight, which I do) improvement in maintainability, because > edits that don't cross a boundary don't require fiddling with > the layout. > > [1] http://thedailywtf.com/Articles/Soft_Coding.aspx Thanks for that link. Good food for thought. Here's an excerpt from one of my more questionable tables: Attribute, Description, Fund, Amount AFSO,Air Force Special Ops Command,, CSEN,English Proficiency Met,, CSMT,Math Proficiency Met,, GBFP,MBA Full Program,, GBMP,MBA Prereq Met,, GCEC,Continuing Education Civilian Tuition Rate,, GCEM,Continuing Education Military Tuition Rate,, GCFP,MCA Prereq Needed,, GCMP,MCE Prereq Met,, GCRT,Certificate Student,, GE25,25% to XCompany,, GE40,40% to XCompany,, GEMP,Employee,Fac,100% GI03,CISSP Scholarship,CISSP,1500 GIHR,Grad In-House Recruiting,, GRMS,Graduate Military Scholarship,Milit,1200 It lists all the student atributes, a description, what fund that attribute requires, if any, and what amount. A tiny amount of DSL is involved, with Faculty Scholarship paying 100% of tuition instead of a fixed number. Another _ (not shown above), which means the fund takes an arbitrary amount determined by a person we have to literally query to discover. I think I can see the potential problems. Two special codes for amount is managable, but the more special cases I end up creating the more of a mess I get. Plus, I haven't really documented the file. Most of the information is irrelevant, though I do like receiving an exception when Admissions tries to sneak in a new attribute without telling me. If I instead had a function that handled only the interesting attributes it might be pretty small. I'll have to think on this. -- Neil Cerutti From wxjmfauth at gmail.com Fri Aug 2 05:15:29 2013 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Fri, 2 Aug 2013 02:15:29 -0700 (PDT) Subject: Editing tabular data [was: PEP8 79 char max] In-Reply-To: References: Message-ID: Le jeudi 1 ao?t 2013 02:50:13 UTC+2, Chris Angelico a ?crit?: > ... > rather than OO/LibreOffice. (I'll not distinguish those two. Far as > > I'm concerned, they're one product with two names.) ... Very interesting aspect in LibreOffice. As the "center of gravity of the development" has moved from "US" to "Europe", the product becomes clearly less (practicaly no more traces) ascii oriented. jmf From rosuav at gmail.com Thu Aug 1 02:25:55 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 1 Aug 2013 07:25:55 +0100 Subject: Oddity with 'yield' as expression - parentheses demanded Message-ID: Was playing around with yield inside a lambda and ran into a distinct oddity. Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)] on win32 >>> foo=lambda x: yield(x) SyntaxError: invalid syntax >>> def foo(x): return yield(x) SyntaxError: invalid syntax >>> def foo(x): x=yield(x) return x >>> foo=lambda x: (yield x) If yield is an expression, why does it need extra parentheses around it? [1] suggest that "(yield x)" is an expression that can elide the parens only when it "is the sole expression on the right hand side of an assignment statement", and presumably there's a similar rule allowing the non-expression form "yield x" to omit the parens. Why is this so? Why is it not simply an expression on its own? [1] http://docs.python.org/3.3/reference/expressions.html#grammar-token-yield_expression ChrisA From ian.g.kelly at gmail.com Thu Aug 1 13:35:54 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 1 Aug 2013 11:35:54 -0600 Subject: Oddity with 'yield' as expression - parentheses demanded In-Reply-To: References: Message-ID: On Thu, Aug 1, 2013 at 12:25 AM, Chris Angelico wrote: > Was playing around with yield inside a lambda and ran into a distinct oddity. > > Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 > 32 bit (Intel)] on win32 >>>> foo=lambda x: yield(x) > SyntaxError: invalid syntax >>>> def foo(x): > return yield(x) > SyntaxError: invalid syntax >>>> def foo(x): > x=yield(x) > return x >>>> foo=lambda x: (yield x) > > If yield is an expression, why does it need extra parentheses around > it? [1] suggest that "(yield x)" is an expression that can elide the > parens only when it "is the sole expression on the right hand side of > an assignment statement", and presumably there's a similar rule > allowing the non-expression form "yield x" to omit the parens. Why is > this so? Why is it not simply an expression on its own? yield was a statement before it became an expression, and the syntax "yield x, y, z" was (and still is) perfectly legal, with all three expressions (technically a single tuple expression) being governed by the yield. That is to say, "yield x, y, z" and "yield (x, y, z)" are semantically equivalent. When it became an expression, in order to preserve this equivalence, that meant that the yield expression needed to bind even less tightly than the comma. In terms of the grammar, yield needed to take an expression_list, not just an expression. There are only three places in the grammar where expression_lists are used without enclosing them in brackets: expression statements (in this case analogous to the yield statement), the return statement (not normally used to return a value in a generator), and the assignment statements. So for consistency and clarity the rules for parenthesizing yield statements are basically adopted from the existing rules for parenthesizing expression_lists. From rosuav at gmail.com Thu Aug 1 13:58:33 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 1 Aug 2013 18:58:33 +0100 Subject: Oddity with 'yield' as expression - parentheses demanded In-Reply-To: References: Message-ID: On Thu, Aug 1, 2013 at 6:35 PM, Ian Kelly wrote: > yield was a statement before it became an expression, and the syntax > "yield x, y, z" was (and still is) perfectly legal, with all three > expressions (technically a single tuple expression) being governed by > the yield. That is to say, "yield x, y, z" and "yield (x, y, z)" are > semantically equivalent. When it became an expression, in order to > preserve this equivalence, that meant that the yield expression needed > to bind even less tightly than the comma. In terms of the grammar, > yield needed to take an expression_list, not just an expression. > > There are only three places in the grammar where expression_lists are > used without enclosing them in brackets: expression statements (in > this case analogous to the yield statement), the return statement (not > normally used to return a value in a generator), and the assignment > statements. So for consistency and clarity the rules for > parenthesizing yield statements are basically adopted from the > existing rules for parenthesizing expression_lists. Ahh, right. That makes good sense. If this were being created anew now, would yield be made to bind more tightly than the comma? That would mean that yield x, y, z # Yield a tuple would need to be written as: yield (x, y, z) which wouldn't, in my opinion, be a bad thing. (It'd probably catch a few people out, but a linter could notice that a tuple is being created and ignored.) So this is a bit of a wart, but it's for backward compatibility. Right? ChrisA From tjreedy at udel.edu Thu Aug 1 15:25:28 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 01 Aug 2013 15:25:28 -0400 Subject: Oddity with 'yield' as expression - parentheses demanded In-Reply-To: References: Message-ID: On 8/1/2013 1:58 PM, Chris Angelico wrote: > On Thu, Aug 1, 2013 at 6:35 PM, Ian Kelly wrote: >> yield was a statement before it became an expression, and the syntax >> "yield x, y, z" was (and still is) perfectly legal, with all three >> expressions (technically a single tuple expression) being governed by >> the yield. That is to say, "yield x, y, z" and "yield (x, y, z)" are >> semantically equivalent. When it became an expression, in order to >> preserve this equivalence, that meant that the yield expression needed >> to bind even less tightly than the comma. In terms of the grammar, >> yield needed to take an expression_list, not just an expression. >> >> There are only three places in the grammar where expression_lists are >> used without enclosing them in brackets: expression statements (in >> this case analogous to the yield statement), the return statement (not >> normally used to return a value in a generator), and the assignment >> statements. So for consistency and clarity the rules for >> parenthesizing yield statements are basically adopted from the >> existing rules for parenthesizing expression_lists. > > Ahh, right. That makes good sense. > > If this were being created anew now, would yield be made to bind more > tightly than the comma? As a statement (which is the primary use of yield), yield is like return, except that the execution frame is not discarded. So I think it should bind like return, which is very loosely. -- Terry Jan Reedy From jaiprakashsingh213 at gmail.com Thu Aug 1 03:01:44 2013 From: jaiprakashsingh213 at gmail.com (Jaiky) Date: Thu, 1 Aug 2013 00:01:44 -0700 (PDT) Subject: How to install googleapp engine on ubuntu 12.04 already downloads google_appengine_1.8.2.zip Message-ID: <10728740-e121-4605-890f-c41c1ff5db62@googlegroups.com> How to install googleapp engine on ubuntu 12.04 already did: downloaded google_appengine_1.8.2.zip unziped it now?????/ please help From jaiprakashsingh213 at gmail.com Thu Aug 1 16:36:34 2013 From: jaiprakashsingh213 at gmail.com (Jai) Date: Thu, 1 Aug 2013 13:36:34 -0700 (PDT) Subject: How to install googleapp engine on ubuntu 12.04 already downloads google_appengine_1.8.2.zip In-Reply-To: <10728740-e121-4605-890f-c41c1ff5db62@googlegroups.com> References: <10728740-e121-4605-890f-c41c1ff5db62@googlegroups.com> Message-ID: got answer downloaded google_appengine_1.8.2.zip unziped it command path-to-/dev_appserver.py path-to/application-diectory that it From alessia.visconti at gmail.com Thu Aug 1 05:49:23 2013 From: alessia.visconti at gmail.com (alesssia) Date: Thu, 1 Aug 2013 02:49:23 -0700 (PDT) Subject: Python and PyQt (Windows) - AttributeError: 'module' object has not attribute run Message-ID: <1414fafe-bc3c-4795-87db-3e8ece51f2c5@googlegroups.com> I developed a python3-PyQt4 application (I?m a newbie!) on a 32-bit Linux platform, and I?m experiencing some problems when testing it on Windows platforms (Windows 7 and 8, both 64-bit). I have a module called Pmc.py that contains two methods: run and main, but only the former is used in the PyQt main class. I import the module using the following statement: import Pmc and I call the function run with the following instruction: self.results = Pmc.run(self.p, self.m) obtaining this error: AttributeError: 'module' object has not attribute run After some tests, I discovered that renaming Pmc.py in pmc.py solves my issue. No problems are present when using Linux both 32 and 64 bit. I?m using python 3.3 and PyQt 4. What am I missing? Thanks in advance, Alessia From davea at davea.name Thu Aug 1 08:39:10 2013 From: davea at davea.name (Dave Angel) Date: Thu, 1 Aug 2013 12:39:10 +0000 (UTC) Subject: Python and PyQt (Windows) - AttributeError: 'module' object has not attribute run References: <1414fafe-bc3c-4795-87db-3e8ece51f2c5@googlegroups.com> Message-ID: alesssia wrote: > I developed a python3-PyQt4 application (I?m a newbie!) on a 32-bit Linux platform, and I?m experiencing some problems when testing it on Windows platforms (Windows 7 and 8, both 64-bit). > > I have a module called Pmc.py that contains two methods: run and main, but only the former is used in the PyQt main class. I import the module using the following statement: > > import Pmc > > and I call the function run with the following instruction: > > self.results = Pmc.run(self.p, self.m) > > obtaining this error: > > AttributeError: 'module' object has not attribute run When quoting something from your tests, please don't paraphrase or shorten. The actual error traceback starts with a line "Traceback (..." and is several lines long, and the line you did quote is missing some quotes and uses the word 'not' when i.t was originally 'no'. In this case, it doesn't matter, but frequently it will. > > After some tests, I discovered that renaming Pmc.py in pmc.py I don't know what that means. Strictly speaking I think that means you edited the content of pmc.py and renamed its use of the string to something else. But I think you're trying to say you renamed Pmc.py to pmc.py. That seems backwards to me. The reverse is more likely to fix the problem. > solves my issue. No problems are present when using Linux both 32 and > 64 bit. > I?m using python 3.3 and PyQt 4. > > What am I missing? Linux is sensitive to case, while Windows is not. Windows does, however preserve the case when you look a the directory, either manually with DIR, or via some Python function. My guess is that somehow when the zip file was extracted, the case of this file was not preserved, and it came out pmc.py. My suggestion is to simply use all lower-case for your filenames, and change the references within your source to import pmc self.results = pmc.run Pep-8 recommends that you use an uppercase leading letter to indicate a class name. So a class of the "same" name as the file would be obj = pmc.Pmc(args) Another possibility is that you have both a pmc.py AND a Pmc.py. That's not legal in Windows, at least if they're in the same directory. And it's not advisable in Windows, if they're even in the same project. -- DaveA From alessia.visconti at gmail.com Thu Aug 1 15:15:58 2013 From: alessia.visconti at gmail.com (alesssia) Date: Thu, 1 Aug 2013 12:15:58 -0700 (PDT) Subject: Python and PyQt (Windows) - AttributeError: 'module' object has not attribute run In-Reply-To: References: <1414fafe-bc3c-4795-87db-3e8ece51f2c5@googlegroups.com> Message-ID: >> After some tests, I discovered that renaming Pmc.py in pmc.py > I don't know what that means. I simply renamed Pmc.py to pmc.py, using a lower case instead of an upper case. Of course, I also changed lines referencing to 'Pmc' (upper case) to 'pmc' (lover case) in the source code. This allows my program to work. > My guess is that somehow when the zip file was extracted, the case of > this file was not preserved, and it came out pmc.py. The zip was not extracted because there was no zip. I copied the code from my computer to a USB pen drive and ran the code from there. > My suggestion is to simply use all lower-case for your filenames What surprised me is that Windows was unable of holding upper case, so I thought there was something else. Yet, it seems that this behaviour does not surprise anyone else. Then, I'll simply changes all my source file names, despite I felt it silly... Thanks, Alessia From davea at davea.name Thu Aug 1 19:43:22 2013 From: davea at davea.name (Dave Angel) Date: Thu, 1 Aug 2013 23:43:22 +0000 (UTC) Subject: Python and PyQt (Windows) - AttributeError: 'module' object has not attribute run References: <1414fafe-bc3c-4795-87db-3e8ece51f2c5@googlegroups.com> Message-ID: alesssia wrote: >> My guess is that somehow when the zip file was extracted, the case of >> this file was not preserved, and it came out pmc.py. > > The zip was not extracted because there was no zip. I copied the code from my computer to a USB pen drive and ran the code from there. That's probably where you lost the case distinction. The FAT file system doesn't really support lowercase, except with 'extended attributes', and probably your Linux system didn't create those in a way that Windows was happy with. If I had to get files between the two, I'd either use Linux tools to do the transfer (eg. scp or ftp), or I'd zip the files and just transfer the zip. > > >> My suggestion is to simply use all lower-case for your filenames > > What surprised me is that Windows was unable of holding upper case, so I thought there was something else. Yet, it seems that this behaviour does not surprise anyone else. Then, I'll simply changes all my source file names, despite I felt it silly... > It can hold uppercase; in fact, it's lower case that's "new". But because it's "case - preserving" rather than "case -sensitive", sometimes it's a mess. -- DaveA From jcasale at activenetwerx.com Thu Aug 1 11:05:08 2013 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Thu, 1 Aug 2013 15:05:08 +0000 Subject: Best practice for connections and cursors Message-ID: I posted this to the sqlite list but I suspect there are more C oriented users on that list than Python, hopefully someone here can shed some light on this one. I have created a python module that I import within several other modules that simply opens a connection to an sqlite file and defines several methods which each open a cursor before they either select or insert data. As the module opens a connection, wherever I import it I call a commit against the connection after invoking any methods that insert or change data. Seems I've made a proper mess, one of the modules causes a 5 second delay at import (big indicator there) and one of the modules calls a method that yields data while calling other methods as it iterates. Each of these methods opens its own cursor. One of which during some processing calls another method which opens a cursor and creates a temp table and this corrupts the top level cursor and causes its yield to exit early. If I open a debugger just as the top level method begins to yield, I can pull all the expected records. It seems to be one of the nested methods that leverages the singleton connection to the sqlite db, once it opens its own cursor and creates a temp table, things go south. Comment out its cursor and code and things work. A bit vague I know, but does anyone see the obvious mistake? I assumed the module setting up a singleton connection was a perfectly viable way to accomplish this? Thanks! jlc From neilc at norwich.edu Thu Aug 1 11:20:32 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 1 Aug 2013 15:20:32 GMT Subject: Best practice for connections and cursors References: Message-ID: On 2013-08-01, Joseph L. Casale wrote: > A bit vague I know, but does anyone see the obvious mistake? I > assumed the module setting up a singleton connection was a > perfectly viable way to accomplish this? My one db application started out creating a new connection to the db (sqlite3) for every select and insert. When I converted it to maintaining it's own single connection and creating new cursors instead it was a huge speed increase. So I too am interested in the answers you'll hopefully be getting. I have no plans for utilizing concurrent access to the db, but maybe I'll want to someday. -- Neil Cerutti From ian.g.kelly at gmail.com Thu Aug 1 21:22:51 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 1 Aug 2013 19:22:51 -0600 Subject: Best practice for connections and cursors In-Reply-To: References: Message-ID: On Thu, Aug 1, 2013 at 6:49 PM, Dennis Lee Bieber wrote: > On Thu, 1 Aug 2013 15:05:08 +0000, "Joseph L. Casale" > declaimed the following: > >> >>As the module opens a connection, wherever I import it I call a commit against >>the connection after invoking any methods that insert or change data. >> > > You probably should NOT be opening connections /during/ the import > operation... For one thing, if it is a module level connection, multiple > imports are replacing earlier connections with their fresh one. That's not true. Multiple imports of the same module will only execute the module-level code the first time. I agree though that simply importing the module should not trigger a connection to be opened. That should be an explicit step. Speaking to the OP: personally, I don't like the approach of putting data access methods at the module level to begin with. I'd rather use a class. Just because it makes sense to have a singleton connection now doesn't mean it will always make sense as your application grows. In fact, the conflict you describe where one cursor is interfering with another cursor suggests that you may already be at the point of needing multiple connections. The operation that is creating a temp table and messing things up should ideally be pulling an unused connection from a pool, so as to avoid potentially contaminating a connection that may already be in use elsewhere in the code. From jcasale at activenetwerx.com Fri Aug 2 09:10:50 2013 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Fri, 2 Aug 2013 13:10:50 +0000 Subject: Best practice for connections and cursors In-Reply-To: References: <719ac7ca4c274c4c884b809e5abd887c@exch.activenetwerx.com> Message-ID: >Speaking to the OP: personally, I don't like the approach of putting data access methods at the module level to >begin with. I'd rather use a class. Just because it makes sense to have a singleton connection now doesn't mean it >will always make sense as your application grows. >In fact, the conflict you describe where one cursor is interfering with another cursor suggests that you may >already be at the point of needing multiple connections. The operation that is creating a temp table and messing >things up should ideally be pulling an unused connection from a pool, so as to avoid potentially contaminating a >connection that may already be in use elsewhere in the code. Appreciate the opinion, it would clean it up to go this route so I will. It turns out the long delay was only a result of running the code through PyCharms debugger. Thanks for the suggestion, jlc From hambergcfa at gmail.com Thu Aug 1 13:31:33 2013 From: hambergcfa at gmail.com (hambergcfa at gmail.com) Date: Thu, 1 Aug 2013 10:31:33 -0700 (PDT) Subject: How to use Python to open Unread message(s) in specific Outlook folder, clicking a hyperlink within the email message to a secure login window, entering password to download .csv file, run Excel macro Message-ID: <1d7bfd13-52d0-4d2b-8f7f-e117cdec2a5b@googlegroups.com> Entire Project I am very new to Python and realize my subject line is more than a mouthful and the best way to tackle this project may be to break it down into smaller projects; but I wanted to let you know what my entire project is in the event there is/are shortcuts or more reliable ways to go about what I'm trying to accomplish. Details Every morning I receive three emails (three different subject lines) in the same Sub-Folder ("POINT") under Folder ("Reports") in my Inbox in Outlook. Each email has two secure hyperlinks, one provides me with a site to register and the other provides a window for me to enter my password to download a .csv file. Is there code that will open the "Unread" emails in the "POINT" folder, click the appropriate hyperlink, enter my password (the same for all emails), and download the .csv file and then run an Excel macro? I've already created the Excel macro, one for each of the different files I'm downloading. I have no idea where to begin with Outlook and have reservations as to whether or not this is even possible. From rosuav at gmail.com Thu Aug 1 13:47:37 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 1 Aug 2013 18:47:37 +0100 Subject: How to use Python to open Unread message(s) in specific Outlook folder, clicking a hyperlink within the email message to a secure login window, entering password to download .csv file, run Excel macro In-Reply-To: <1d7bfd13-52d0-4d2b-8f7f-e117cdec2a5b@googlegroups.com> References: <1d7bfd13-52d0-4d2b-8f7f-e117cdec2a5b@googlegroups.com> Message-ID: On Thu, Aug 1, 2013 at 6:31 PM, wrote: > Details > Every morning I receive three emails (three different subject lines) in the same Sub-Folder ("POINT") under Folder ("Reports") in my Inbox in Outlook. Each email has two secure hyperlinks, one provides me with a site to register and the other provides a window for me to enter my password to download a .csv file. > > Is there code that will open the "Unread" emails in the "POINT" folder, click the appropriate hyperlink, enter my password (the same for all emails), and download the .csv file and then run an Excel macro? I've already created the Excel macro, one for each of the different files I'm downloading. Okay, taking a few steps back here. 1) You receive an email 2) That email has two URLs in it ("secure hyperlinks" means they begin https:// ?) 3) You choose one of them as being "appropriate" - is it always the second? 4) You download the document at that URL, which requires a password 5) You then run some sort of alteration on the resulting CSV file. Please correct me on anything I've misunderstood. Python can certainly do all of these steps, with the possible exception of fetching the email. Dividing the problem up into separate steps will make the solving of it easier. ChrisA From hambergcfa at gmail.com Thu Aug 1 14:08:37 2013 From: hambergcfa at gmail.com (hambergcfa at gmail.com) Date: Thu, 1 Aug 2013 11:08:37 -0700 (PDT) Subject: How to use Python to open Unread message(s) in specific Outlook folder, clicking a hyperlink within the email message to a secure login window, entering password to download .csv file, run Excel macro In-Reply-To: References: <1d7bfd13-52d0-4d2b-8f7f-e117cdec2a5b@googlegroups.com> Message-ID: On Thursday, August 1, 2013 1:47:37 PM UTC-4, Chris Angelico wrote: > On Thu, Aug 1, 2013 at 6:31 PM, wrote: > > > Details > > > Every morning I receive three emails (three different subject lines) in the same Sub-Folder ("POINT") under Folder ("Reports") in my Inbox in Outlook. Each email has two secure hyperlinks, one provides me with a site to register and the other provides a window for me to enter my password to download a .csv file. > > > > > > Is there code that will open the "Unread" emails in the "POINT" folder, click the appropriate hyperlink, enter my password (the same for all emails), and download the .csv file and then run an Excel macro? I've already created the Excel macro, one for each of the different files I'm downloading. > > > > Okay, taking a few steps back here. > > > > 1) You receive an email > > 2) That email has two URLs in it ("secure hyperlinks" means they begin > > https:// ?) > > 3) You choose one of them as being "appropriate" - is it always the second? > > 4) You download the document at that URL, which requires a password > > 5) You then run some sort of alteration on the resulting CSV file. > > > > Please correct me on anything I've misunderstood. > > > > Python can certainly do all of these steps, with the possible > > exception of fetching the email. Dividing the problem up into separate > > steps will make the solving of it easier. > > > > ChrisA Thanks a lot ChrisA! You are correct with everything except that the login hyperlink is always the first link and the second one is the link to register. From rosuav at gmail.com Thu Aug 1 14:32:55 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 1 Aug 2013 19:32:55 +0100 Subject: How to use Python to open Unread message(s) in specific Outlook folder, clicking a hyperlink within the email message to a secure login window, entering password to download .csv file, run Excel macro In-Reply-To: References: <1d7bfd13-52d0-4d2b-8f7f-e117cdec2a5b@googlegroups.com> Message-ID: On Thu, Aug 1, 2013 at 7:08 PM, wrote: >> 1) You receive an email >> >> 2) That email has two URLs in it ("secure hyperlinks" means they begin >> >> https:// ?) >> >> 3) You choose one of them as being "appropriate" - is it always the second? >> >> 4) You download the document at that URL, which requires a password >> >> 5) You then run some sort of alteration on the resulting CSV file. >> >> >> >> Please correct me on anything I've misunderstood. >> >> >> >> Python can certainly do all of these steps, with the possible >> >> exception of fetching the email. Dividing the problem up into separate >> >> steps will make the solving of it easier. >> >> >> >> ChrisA > > Thanks a lot ChrisA! You are correct with everything except that the login hyperlink is always the first link and the second one is the link to register. Okay! (Digression: You seem to be using Google Groups. Please read http://wiki.python.org/moin/GoogleGroupsPython before posting further, to avoid antagonizing the list's best responders.) The first step is to figure out how to retrieve the email. You may want to tweak your setup to make this easier. The next thing I'd do would be to port the macro to Python. Everything in between is fairly easy. ChrisA From hambergcfa at gmail.com Fri Aug 2 14:18:00 2013 From: hambergcfa at gmail.com (hambergcfa at gmail.com) Date: Fri, 2 Aug 2013 11:18:00 -0700 (PDT) Subject: How to use Python to open Unread message(s) in specific Outlook folder, clicking a hyperlink within the email message to a secure login window, entering password to download .csv file, run Excel macro In-Reply-To: References: <1d7bfd13-52d0-4d2b-8f7f-e117cdec2a5b@googlegroups.com> Message-ID: <54f5caea-6526-4f8f-808f-3da46824dc1d@googlegroups.com> On Thursday, August 1, 2013 2:32:55 PM UTC-4, Chris Angelico wrote: On Thu, Aug 1, 2013 at 7:08 PM, wrote: 1) You receive an email 2) That email has two URLs in it ("secure hyperlinks" means they begin https:// ?) 3) You choose one of them as being "appropriate" - is it always the second? 4) You download the document at that URL, which requires a password 5) You then run some sort of alteration on the resulting CSV file. Please correct me on anything I've misunderstood. Python can certainly do all of these steps, with the possible exception of fetching the email. Dividing the problem up into separate steps will make the solving of it easier. ChrisA Thanks a lot ChrisA! You are correct with everything except that the login hyperlink is always the first link and the second one is the link to register. Okay! Digression: You seem to be using Google Groups. Please read http://wiki.python.org/moin/GoogleGroupsPython before posting further, to avoid antagonizing the list's best responders.) The first step is to figure out how to retrieve the email. You may want to tweak your setup to make this easier. The next thing I'd do would be to port the macro to Python. Everything in between is fairly easy. ChrisA Thanks for the heads up ChrisA! How should I tweak my setup to make it easier to retrieve my email? I hope I'm doing this reply correctly. From joel.goldstick at gmail.com Fri Aug 2 14:55:40 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Fri, 2 Aug 2013 14:55:40 -0400 Subject: How to use Python to open Unread message(s) in specific Outlook folder, clicking a hyperlink within the email message to a secure login window, entering password to download .csv file, run Excel macro In-Reply-To: <54f5caea-6526-4f8f-808f-3da46824dc1d@googlegroups.com> References: <1d7bfd13-52d0-4d2b-8f7f-e117cdec2a5b@googlegroups.com> <54f5caea-6526-4f8f-808f-3da46824dc1d@googlegroups.com> Message-ID: On Fri, Aug 2, 2013 at 2:18 PM, wrote: > On Thursday, August 1, 2013 2:32:55 PM UTC-4, Chris Angelico wrote: > On Thu, Aug 1, 2013 at 7:08 PM, wrote: > > 1) You receive an email > > 2) That email has two URLs in it ("secure hyperlinks" means they begin > > https:// ?) > > 3) You choose one of them as being "appropriate" - is it always the second? > > 4) You download the document at that URL, which requires a password > > 5) You then run some sort of alteration on the resulting CSV file. > > Please correct me on anything I've misunderstood. > > Python can certainly do all of these steps, with the possible > > exception of fetching the email. Dividing the problem up into separate > > steps will make the solving of it easier. > > ChrisA > > Thanks a lot ChrisA! You are correct with everything except that the login hyperlink is always the first link and the second one is the link to register. > > Okay! > > Digression: You seem to be using Google Groups. Please read > > http://wiki.python.org/moin/GoogleGroupsPython before posting further, > > to avoid antagonizing the list's best responders.) > > The first step is to figure out how to retrieve the email. You may > > want to tweak your setup to make this easier. The next thing I'd do > > would be to port the macro to Python. Everything in between is fairly > > easy. > > ChrisA > > Thanks for the heads up ChrisA! How should I tweak my setup to make it easier to retrieve my email? I hope I'm doing this reply correctly. > -- > http://mail.python.org/mailman/listinfo/python-list Reading outlook email, I found this: http://timgolden.me.uk/python/win32_how_do_i/read-my-outlook-inbox.html There is lots to find via google with 'reading outlook email with python' -- Joel Goldstick http://joelgoldstick.com From skip at pobox.com Fri Aug 2 15:25:30 2013 From: skip at pobox.com (Skip Montanaro) Date: Fri, 2 Aug 2013 14:25:30 -0500 Subject: How to use Python to open Unread message(s) in specific Outlook folder, clicking a hyperlink within the email message to a secure login window, entering password to download .csv file, run Excel macro In-Reply-To: References: <1d7bfd13-52d0-4d2b-8f7f-e117cdec2a5b@googlegroups.com> <54f5caea-6526-4f8f-808f-3da46824dc1d@googlegroups.com> Message-ID: > Reading outlook email, I found this: > > http://timgolden.me.uk/python/win32_how_do_i/read-my-outlook-inbox.html > > There is lots to find via google with 'reading outlook email with python' You might also want to look at the source for the SpamBayes Outlook plugin: https://pypi.python.org/pypi/spambayes Skip From rosuav at gmail.com Fri Aug 2 18:30:27 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 2 Aug 2013 23:30:27 +0100 Subject: How to use Python to open Unread message(s) in specific Outlook folder, clicking a hyperlink within the email message to a secure login window, entering password to download .csv file, run Excel macro In-Reply-To: <54f5caea-6526-4f8f-808f-3da46824dc1d@googlegroups.com> References: <1d7bfd13-52d0-4d2b-8f7f-e117cdec2a5b@googlegroups.com> <54f5caea-6526-4f8f-808f-3da46824dc1d@googlegroups.com> Message-ID: On Fri, Aug 2, 2013 at 7:18 PM, wrote: > How should I tweak my setup to make it easier to retrieve my email? I hope I'm doing this reply correctly. The best way is to use email, not the Google Groups interface. Start here: http://mail.python.org/mailman/listinfo/python-list You can use gmail, and it'll thread the conversations for you. (So will many other clients, but you clearly already have gmail.) The usual convention is to use angle brackets with quoted text, and also to wrap your sent text to a reasonable number of characters (about 80 is common; most clients will do this for you - gmail will as long as you send Plain Text rather than formatted, which is a good idea anyway). ChrisA From chris.pagnutti at gmail.com Thu Aug 1 13:37:19 2013 From: chris.pagnutti at gmail.com (Pacopag) Date: Thu, 1 Aug 2013 10:37:19 -0700 (PDT) Subject: How do I deal with packet data Message-ID: <48c837f0-3626-48cd-8d9c-255e2c4ed797@googlegroups.com> Hi. I have the hex stream of a packet that a program sent over the network. Now I want to view the data in the packet. I'm pretty sure the data was just a string (or at least contains a string), but when I decode it I just get gibberish. For example, the packet is sent something like this import socket s = socket.socket() s.connect(hostname,port) data = "HeresAStringToSend" s.send(data) # I'm not worried about receiving yet. # I just want to know the anatomy of a sent packet. Then I use a packet sniffer to look at the packet that was sent; this is just a string of hex. Then I isolate the data part of the packet. Let's say the data part of the hex string is in a variable called hexdata. If I do, print hexdata.decode("hex") all I get is gibberish. Looking at the individual bytes in the hex data, they map to strange or invalid ascii codes (e.g. less than 32 or greater than 127). I'm new to all this socket/packet stuff, so I don't really know what the s.send(data) method does to the data before sending it. Any help or insight would be great. Thanks. From davea at davea.name Thu Aug 1 15:42:02 2013 From: davea at davea.name (Dave Angel) Date: Thu, 1 Aug 2013 19:42:02 +0000 (UTC) Subject: How do I deal with packet data References: <48c837f0-3626-48cd-8d9c-255e2c4ed797@googlegroups.com> Message-ID: Pacopag wrote: > Hi. > > I have the hex stream of a packet that a program sent over the network. Now I want to view the data in the packet. I'm pretty sure the data was just a string (or at least contains a string), but when I decode it I just get gibberish. > > For example, the packet is sent something like this > > import socket > > s = socket.socket() > s.connect(hostname,port) > data = "HeresAStringToSend" > s.send(data) > # I'm not worried about receiving yet. > # I just want to know the anatomy of a sent packet. > > > Then I use a packet sniffer to look at the packet that was sent; this is just a string of hex. What packet sniffer was that? Why not use wireshark, and eliminate the middleman? If you're using some other tool, how have you decided you even have the right packet(s)? > Then I isolate the data part of the packet. Let's say the data part > of the hex string is in a variable called hexdata. So you've analyzed the header of the packet, and identified where the data part is? Have you seen where the host IP address is, and the port number? Do they fit the pattern? > > If I do, > > print hexdata.decode("hex") > > all I get is gibberish. Looking at the individual bytes in the hex data, they map to strange or invalid ascii codes (e.g. less than 32 or greater than 127). > > I'm new to all this socket/packet stuff, so I don't really know what the s.send(data) method does to the data before sending it. > > Any help or insight would be great. Thanks. What OS are you using? There are differences in Windows, for example, but someone else would have to help you there. If it were my problem, I'd be using Wireshark, which can not only display the data for each packet, but show how multiple packets relate to each other. -- DaveA From cmpython at gmail.com Thu Aug 1 13:57:07 2013 From: cmpython at gmail.com (CM) Date: Thu, 1 Aug 2013 10:57:07 -0700 (PDT) Subject: Does Python 'enable' poke and hope programming? Message-ID: <6b1769f8-222b-4953-99c8-a1d73cec3d60@googlegroups.com> (My subject line is meant to be tongue and cheek inflammatory) I've been thinking about why programming for me often feels like ice skating uphill. I think part of the problem, maybe the biggest part, is what now strikes me as a Very Bad Habit, which is "poke and hope" (trial and error) programming (of several names this page provided, I kind of like that one): http://en.wikipedia.org/wiki/Programming_by_permutation It seems that if I can make a change to the code and then immediately test it by running the Python interpreter and finding out, in a few seconds, if it worked, I am going to be *much* more likely to use this trial-and-error approach than if I had to use a compiled language, since compiling takes so long. E.g. "Oh, that doesn't work? Maybe if I add this...no. OK, what about if I increment that? No...OK, wait, maybe this...AH! That worked." (obviously it is not quite that uninformed all the time). Instead, with a compiled language, because of the pain of having to wait for the newest version to compile, one would be encouraged to get the mechanism of how something works *clear* and robustly represented in one's mind (or on scrap paper/notes document) prior to testing through compiling and running. Basically this amounts to: with an interpreted language (so of course this is not really just about Python--I just think in terms of Python), it's easier to be mentally lazy. But, ironically, being lazy winds up creating *way* more work ultimately, since one winds up programming in this terribly inefficient way, and progress proceeds at an, at times, evolutionary (slow!) pace. And of course I am not really blaming it on Python or any interpreted language; I am blaming it fully on my own lame habits and attitude. I'm sick of this in my own work, and want to avoid this trap as much as I can from now on. Thoughts? From neilc at norwich.edu Thu Aug 1 14:08:23 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 1 Aug 2013 18:08:23 GMT Subject: Does Python 'enable' poke and hope programming? References: <6b1769f8-222b-4953-99c8-a1d73cec3d60@googlegroups.com> Message-ID: On 2013-08-01, CM wrote: > http://en.wikipedia.org/wiki/Programming_by_permutation > > It seems that if I can make a change to the code and then > immediately test it by running the Python interpreter and > finding out, in a few seconds, if it worked, I am going to be > *much* more likely to use this trial-and-error approach than if > I had to use a compiled language, since compiling takes so > long. E.g. "Oh, that doesn't work? Maybe if I add this...no. > OK, what about if I increment that? No...OK, wait, maybe > this...AH! That worked." (obviously it is not quite that > uninformed all the time). > > Instead, with a compiled language, because of the pain of > having to wait for the newest version to compile, one would be > encouraged to get the mechanism of how something works *clear* > and robustly represented in one's mind (or on scrap paper/notes > document) prior to testing through compiling and running. With a big project running all tests for a Python program could be just as time consuming. If I were using a compiler for my programs compiling would still be virtually instantaneous. > I'm sick of this in my own work, and want to avoid this trap as > much as I can from now on. When I "know", for example. that it's an off-by-one error, I'm pretty guilty of just making the change and re-running the test. I think it's cool! If my test sucks, then I wasted my time, of course. As I said, I disagree that the speed of using an interpreter is the main issue. Changing certain things, even big things, in a Python program is often much easier than changing something in , say, a C program, due to Duck-Typing and dynamic typing. So experimentation is easier thanks to more maleable code. -- Neil Cerutti From rosuav at gmail.com Thu Aug 1 14:30:07 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 1 Aug 2013 19:30:07 +0100 Subject: Does Python 'enable' poke and hope programming? In-Reply-To: <6b1769f8-222b-4953-99c8-a1d73cec3d60@googlegroups.com> References: <6b1769f8-222b-4953-99c8-a1d73cec3d60@googlegroups.com> Message-ID: On Thu, Aug 1, 2013 at 6:57 PM, CM wrote: > It seems that if I can make a change to the code and then immediately test it by running the Python interpreter and finding out, in a few seconds, if it worked, I am going to be *much* more likely to use this trial-and-error approach than if I had to use a compiled language, since compiling takes so long. E.g. "Oh, that doesn't work? Maybe if I add this...no. OK, what about if I increment that? No...OK, wait, maybe this...AH! That worked." (obviously it is not quite that uninformed all the time). I would say that, often, that's a fine way to do things. With two very similar languages I work with (Python and Pike), there's a slight difference in the interpretation of a range subscript: Pike v7.8 release 700 running Hilfe v3.5 (Incremental Pike Frontend) > "Hello, world!"[..5]; (1) Result: "Hello," > "Hello, world!"[5..]; (2) Result: ", world!" Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)] on win32 >>> "Hello, world!"[:5] 'Hello' >>> "Hello, world!"[5:] ', world!' Python counts the boundaries between characters, Pike counts the characters themselves. So using the same number on different sides of the colon yields the exact same string in Python, but in Pike, both strings contain the indexed character. Both ways make sense, and the easiest way to make sure I haven't mucked something up - in either language - is to try it. ChrisA From davea at davea.name Thu Aug 1 15:58:28 2013 From: davea at davea.name (Dave Angel) Date: Thu, 1 Aug 2013 19:58:28 +0000 (UTC) Subject: Does Python 'enable' poke and hope programming? References: <6b1769f8-222b-4953-99c8-a1d73cec3d60@googlegroups.com> Message-ID: CM wrote: > what now strikes me as a Very Bad Habit, which is "poke and hope" > (trial and error) programming (of several names this page provided, I kind of like that one): > I recall when a "compile" took up to two days, before we got the punched paper tape to begin testing. If we wanted to "poke", it was done in hex. Once we got a local assembler/linker up and working, and (incremental) turnarounds were under 5 minutes, we thought we were in heaven. But the discipline of having to think it through was very good for us, in the long run. -- DaveA From demon.exh at gmail.com Thu Aug 1 20:52:43 2013 From: demon.exh at gmail.com (Sam Whitehead) Date: Thu, 1 Aug 2013 17:52:43 -0700 (PDT) Subject: Does Python 'enable' poke and hope programming? In-Reply-To: <6b1769f8-222b-4953-99c8-a1d73cec3d60@googlegroups.com> References: <6b1769f8-222b-4953-99c8-a1d73cec3d60@googlegroups.com> Message-ID: <6d251af6-7cb7-4c38-8791-64b243164fcb@googlegroups.com> I find myself doing this a lot with libraries whose documentation encourages an understanding based on intuition, rather than one based on formal concepts. When doing more 'pure' stuff with mostly the standard library, not so much. Most imperative languages let their users get kind of loose with data design, but I don't feel like Python is especially bad in this regard. Mostly I feel like it depends on how well the programmer understands the software they're working with. And that sense, Python tries to help by encouraging readability. In a sense, you could see almost all computer programming as "poke and hope", though. The variation lies in the size of the poke and the uncertainty of the hope. From vito.detullio at gmail.com Thu Aug 1 23:34:21 2013 From: vito.detullio at gmail.com (Vito De Tullio) Date: Fri, 02 Aug 2013 05:34:21 +0200 Subject: Does Python 'enable' poke and hope programming? References: <6b1769f8-222b-4953-99c8-a1d73cec3d60@googlegroups.com> Message-ID: CM wrote: > Basically this amounts to: with an interpreted language (so of course > this is not really just about Python--I just think in terms of Python), > it's easier to be mentally lazy. But, ironically, being lazy winds up > creating *way* more work ultimately, since one winds up programming in > this terribly inefficient way, and progress proceeds at an, at times, > evolutionary (slow!) pace. you have found repl are cool :D -- By ZeD From wayne at waynewerner.com Sat Aug 3 08:49:39 2013 From: wayne at waynewerner.com (Wayne Werner) Date: Sat, 3 Aug 2013 07:49:39 -0500 (CDT) Subject: Does Python 'enable' poke and hope programming? In-Reply-To: <6b1769f8-222b-4953-99c8-a1d73cec3d60@googlegroups.com> References: <6b1769f8-222b-4953-99c8-a1d73cec3d60@googlegroups.com> Message-ID: On Thu, 1 Aug 2013, CM wrote: > (My subject line is meant to be tongue and cheek inflammatory) > > I've been thinking about why programming for me often feels like ice skating uphill. I think part of the problem, maybe the biggest part, is what now strikes me as a Very Bad Habit, which is "poke and hope" (trial and error) programming (of several names this page provided, I kind of like that one): > > http://en.wikipedia.org/wiki/Programming_by_permutation > > It seems that if I can make a change to the code and then immediately test it by running the Python interpreter and finding out, in a few seconds, if it worked, I am going to be *much* more likely to use this trial-and-error approach than if I had to use a compiled language, since compiling takes so long. E.g. "Oh, that doesn't work? Maybe if I add this...no. OK, what about if I increment that? No...OK, wait, maybe this...AH! That worked." (obviously it is not quite that uninformed all the time). > > Instead, with a compiled language, because of the pain of having to wait for the newest version to compile, one would be encouraged to get the mechanism of how something works *clear* and robustly represented in one's mind (or on scrap paper/notes document) prior to testing through compiling and running. > > Basically this amounts to: with an interpreted language (so of course this is not really just about Python--I just think in terms of Python), it's easier to be mentally lazy. But, ironically, being lazy winds up creating *way* more work ultimately, since one winds up programming in this terribly inefficient way, and progress proceeds at an, at times, evolutionary (slow!) pace. > > And of course I am not really blaming it on Python or any interpreted language; I am blaming it fully on my own lame habits and attitude. > > I'm sick of this in my own work, and want to avoid this trap as much as I can from now on. > > Thoughts? I see that many others have had thoughts already - but rather than take the time to read their responses and either find out that they said the same thing (oops, sorry!) or become influenced by their arguments, I feel like I should respond to this with a clean slate. I don't think that Python enables the "poke and hope" style programming (I like the name!) any more than a compiled language does - if you're doing it right. Example: My brother had a kid in his C++ class that would go about randomly flipping >, <, <=, >= signs until he got the behavior that he wanted. There was no mental effort of thinking about the problem or applying the scientific method - i.e. form a hypothesis, test the hypothesis, check results. My experience is that people who go throughout their programming careers without this attitude will do it whether it requires several seconds (or minutes) of compile time or not. Whether or not it's a conscious choice I don't know - at least in your case you seem to desire to make a conscious choice in the direction of "wait a minute, this is a stupid way to program". Though "poke and hope" is headed in the right direction, I think it's a bit naive and misses the very essential nature of the better (best?) method - formulation of a /real/ hypothesis. For instance "I think my program will work" is a hypothesis of exactly the same quality of, "When I turn on my water faucet, it will rain." Of course the smaller the application, the more valid the original hypothesis. For instance, the "poke and hope" programmer might write this program: x = 3 if x > 3: print "x is less than 3" else: print "x is greater than 3" And then of course make the weak hypothesis "if I change my > to < then maybe my program will work" - or "if I change my x to 5 then maybe my program will work". The problem is that these are really just random guesses - flips of the coin that eventually might produce a correct result - but only because of the monkeys[1]. What you really want is to actually understand cause and effect at a more fundamental level (in programming) than what you may currently enjoy. And this is in fact something that, while makes it easier for the "poke and hope", also provides a much more enjoyable laboratory experience for the initiated. And when you combine that with the REPL (interactive interpreter) you get an embarassingly powerful laboratory in which to experiment to your heart's delight. For instance, say you want to really understand the previous example. You could do something like so: >>> x = 3 >>> x > 3 False >>> x < 3 False >>> x >= 3 True >>> x <= 3 True >>> x == 3 True And *this* type of "poke and hope" *is* actually valuable. This is where understanding is formed, and high-quality developers are forged. At your fingertips you begin to see "A hah! so *that's what this does!" You are free to explore and play and understand the rules of the system. And you can build on the previous experiments: >>> if x > 3: ... print("Hello?") #single space indentation for the interpeter ... >>> if True: ... print('True?') ... True? >>> x = 5 >>> if x > 3: ... print('x > 3') ... x > 3 >>> x = 2 >>> if x < 3: ... print('x < 3') x < 3 With this understanding you can then say something like, "If I write a program that checks to see `if x < 3` then print 'x < 3', and `if x > 3` print 'x > 3' and `if x == 3` then print out 'x is 3', that should do those things correctly" - which you then test by writing the (again naive) program: x = 5 if x > 3: print('x > 3') if x < 3: print('x < 3') if x == 3: print('x is 3!') This is still not the ideal program - obviously you can use elif/else - but the style of the program is beside the point. We really care about the thought process that went into (ultimately) developing the correct solution. Experimentation is OK - that's how we learn. But we need to direct our experiments and /use what we learned/ when formulating our hypothesis. when this happens we evolve beyond the "poke and hope" into the inquisitive, intelligent programmers that we all should be. Not merely fumbling around in the dark, flailing for the correct answers, but finding a problem and apply our understanding of the way the world (maybe just the digital world of 1's and 0's) works to create the solution. The solution that works[2]. At least that's my 2? -W [1]: You know, the ones with typewriters. [2]: Turns out there are several values for 'works'. If the computer is the only one that sees it, then 'produces the correct solution' is the most important. But if you have people coming 'round to change/update/extend/enhance, the obviously a system that's easier to maintain is a worthwhile trade-off. After all, people are the more valuble resource - computers just help us make better use of it. From cmpython at gmail.com Sat Aug 3 13:30:28 2013 From: cmpython at gmail.com (CM) Date: Sat, 3 Aug 2013 10:30:28 -0700 (PDT) Subject: Does Python 'enable' poke and hope programming? In-Reply-To: <6b1769f8-222b-4953-99c8-a1d73cec3d60@googlegroups.com> References: <6b1769f8-222b-4953-99c8-a1d73cec3d60@googlegroups.com> Message-ID: Wayne, thanks for your thoughts. I am all for the scientific method--in understanding the natural world, which doesn't come with a manual. But Python is an artificial system designed by mere people (as well as Guido), and, as such, does have a manual. Ideally, there should be very little need for experimentation and "hypotheses". That said, yes, when learning the language a little experimentation along the way is fine, and error messages Python throws back to you or unexpected results can and should be a form of real time instruction. But what I meant is that if one is writing a program, there is a way to **know**--without experimentation--what a particular set of code is going to do. This is often cognitively demanding work, but it is possible. And my point is that it is, in the end, far more efficient to be disciplined and do that work rather than try to take shortcuts by simply trying a few things until one of them works. In sum: experimentation is for when you don't know what you're doing and there is no manual; but, after the initial learning time, you *should* know what you're doing and you should have the manual handy, and therefore the time for experimentation is largely over. From rosuav at gmail.com Sat Aug 3 13:39:46 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 3 Aug 2013 18:39:46 +0100 Subject: Does Python 'enable' poke and hope programming? In-Reply-To: References: <6b1769f8-222b-4953-99c8-a1d73cec3d60@googlegroups.com> Message-ID: On Sat, Aug 3, 2013 at 6:30 PM, CM wrote: > In sum: experimentation is for when you don't know what you're doing and there is no manual; but, after the initial learning time, you *should* know what you're doing and you should have the manual handy, and therefore the time for experimentation is largely over. Yet with fast turnaround interactive languages, the interpreter IS part of the manual. Keeping IDLE (I prefer it to command-line Python on Windows, as the latter lacks GNU readline ergo no tab completion etc) handy is at least as useful as keeping the manual up. ChrisA From ethan at stoneleaf.us Sat Aug 3 14:31:56 2013 From: ethan at stoneleaf.us (Ethan Furman) Date: Sat, 03 Aug 2013 11:31:56 -0700 Subject: Does Python 'enable' poke and hope programming? In-Reply-To: References: <6b1769f8-222b-4953-99c8-a1d73cec3d60@googlegroups.com> Message-ID: <51FD4C9C.9020901@stoneleaf.us> On 08/03/2013 10:30 AM, CM wrote: > > But what I meant is that if one is writing a program, there is a way > to **know**--without experimentation--what a particular set of code > is going to do. Even when you /know/, experimenting is still good for two other purposes: - check that what you know is so - check that the interpreter behaves consistently with the docs But yes, I otherwise agree with you. -- ~Ethan~ From thakur.gautam at gmail.com Thu Aug 1 14:20:59 2013 From: thakur.gautam at gmail.com (thakur.gautam at gmail.com) Date: Thu, 1 Aug 2013 11:20:59 -0700 (PDT) Subject: Building python 2.7.5 with VS2012 - Regression test failures Message-ID: I managed to build a 64bit version of Python 2.7.5 from sources with VS2012 on Windows 8 following the wiki on modifying the build files as advised for VS2010. I ran the python regression tests on the build and it cleared most of the tests and skipped a few. It hanged when trying to run the following tests, - test_tuple - test_list - test_userlist - test_asyncore - test_gdb The system would become unresponsive and I would need to force shutdown and restart. It failed the following tests, - test_socket [it failed only for two tests, test_connect & test_create_connection, the issue seems to be around this - AssertionError: 10061 != 107] - test_asynchat [all errors were with respect to Errno 10035 & WSAEWOULDBLOCK] - test_ssl [it skipped stating "DLL load failed: %1 is not a valid Win32 application"] For the following I got warnings, - test_site [warning - modified sys.path] - test_distutils [warning - modified os.environ] I think test_socket and test_asynchat are to do with error codes returned as per errnomodule.c. Fixing this should be enough. I would really appreciate any help/advise on how to deal with the above errors. From tjreedy at udel.edu Thu Aug 1 15:52:31 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 01 Aug 2013 15:52:31 -0400 Subject: PEP8 revised: max line lengths Message-ID: Newly revised this morning: http://www.python.org/dev/peps/pep-0008/#maximum-line-length summary: 72 for text block (comments, triple-quoted strings) 79 for normal code 99 for code that is really more readable with extra The diff with all the changes is here http://hg.python.org/peps/rev/fb24c80e9afb -- Terry Jan Reedy From tjreedy at udel.edu Thu Aug 1 16:19:41 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 01 Aug 2013 16:19:41 -0400 Subject: PEP8 revised: max line lengths In-Reply-To: References: Message-ID: On 8/1/2013 3:52 PM, Terry Reedy wrote: > Newly revised this morning: > http://www.python.org/dev/peps/pep-0008/#maximum-line-length > summary: > 72 for text block (comments, triple-quoted strings) > 79 for normal code > 99 for code that is really more readable with extra Or maybe not for stdlib. Guido think this is too 'loose'. > > The diff with all the changes is here > http://hg.python.org/peps/rev/fb24c80e9afb -- Terry Jan Reedy From python.list at tim.thechases.com Thu Aug 1 16:22:43 2013 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 1 Aug 2013 15:22:43 -0500 Subject: PEP8 revised: max line lengths In-Reply-To: References: Message-ID: <20130801152243.683fbc64@bigbox.christie.dr> On 2013-08-01 15:52, Terry Reedy wrote: > Newly revised this morning: > http://www.python.org/dev/peps/pep-0008/#maximum-line-length > > The diff with all the changes is here > http://hg.python.org/peps/rev/fb24c80e9afb Just a quick spelling fix of s/experimants/experiments/ at http://hg.python.org/peps/rev/fb24c80e9afb#l1.396 :-) -tkc From tjreedy at udel.edu Thu Aug 1 17:33:44 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 01 Aug 2013 17:33:44 -0400 Subject: PEP8 revised: max line lengths In-Reply-To: <20130801152243.683fbc64@bigbox.christie.dr> References: <20130801152243.683fbc64@bigbox.christie.dr> Message-ID: On 8/1/2013 4:22 PM, Tim Chase wrote: > On 2013-08-01 15:52, Terry Reedy wrote: >> Newly revised this morning: >> http://www.python.org/dev/peps/pep-0008/#maximum-line-length >> >> The diff with all the changes is here >> http://hg.python.org/peps/rev/fb24c80e9afb > > Just a quick spelling fix of > > s/experimants/experiments/ > > at http://hg.python.org/peps/rev/fb24c80e9afb#l1.396 > > :-) Already patched in repository but after the nightly rebuild of the web pages. I found 3 more and fixed them. -- Terry Jan Reedy From davea at davea.name Thu Aug 1 19:33:35 2013 From: davea at davea.name (Dave Angel) Date: Thu, 1 Aug 2013 23:33:35 +0000 (UTC) Subject: PEP8 revised: max line lengths References: <20130801152243.683fbc64@bigbox.christie.dr> Message-ID: Terry Reedy wrote: >>> >>> The diff with all the changes is here >>> http://hg.python.org/peps/rev/fb24c80e9afb >> Just out of curiosity, where is "coding cookie" defined? I found enough distant references to decide it was supposed to mean the coding line (line 2, typically in Unix). But I originally thought it meant the BOM. It must have a different name in the reference, but i can't seem to find it right now. If indeed it has a different name, then Pep8 should use that name. -- DaveA From tjreedy at udel.edu Fri Aug 2 00:50:10 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 02 Aug 2013 00:50:10 -0400 Subject: PEP8 revised: max line lengths In-Reply-To: References: <20130801152243.683fbc64@bigbox.christie.dr> Message-ID: On 8/1/2013 7:33 PM, Dave Angel wrote: > Terry Reedy wrote: > > >>>> >>>> The diff with all the changes is here >>>> http://hg.python.org/peps/rev/fb24c80e9afb >>> > > Just out of curiosity, where is "coding cookie" defined? I found enough > distant references to decide it was supposed to mean the coding line > (line 2, typically in Unix). But I originally thought it meant the BOM. > > It must have a different name in the reference, but i can't seem to find > it right now. If indeed it has a different name, then Pep8 should use > that name. http://bugs.python.org/issue18628 -- Terry Jan Reedy From davea at davea.name Fri Aug 2 01:19:12 2013 From: davea at davea.name (Dave Angel) Date: Fri, 2 Aug 2013 05:19:12 +0000 (UTC) Subject: PEP8 revised: max line lengths References: <20130801152243.683fbc64@bigbox.christie.dr> Message-ID: Terry Reedy wrote: > On 8/1/2013 7:33 PM, Dave Angel wrote: >> Terry Reedy wrote: >> >> >>>>> >>>>> The diff with all the changes is here >>>>> http://hg.python.org/peps/rev/fb24c80e9afb >>>> >> >> Just out of curiosity, where is "coding cookie" defined? I found enough >> distant references to decide it was supposed to mean the coding line >> (line 2, typically in Unix). But I originally thought it meant the BOM. >> >> It must have a different name in the reference, but i can't seem to find >> it right now. If indeed it has a different name, then Pep8 should use >> that name. > > http://bugs.python.org/issue18628 > Thanks for the mention in the issue report. -- Signature file not found From tjreedy at udel.edu Fri Aug 2 21:20:19 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 02 Aug 2013 21:20:19 -0400 Subject: PEP8 revised: max line lengths In-Reply-To: References: <20130801152243.683fbc64@bigbox.christie.dr> Message-ID: On 8/2/2013 1:19 AM, Dave Angel wrote: > Terry Reedy wrote: > >> On 8/1/2013 7:33 PM, Dave Angel wrote: >>> Terry Reedy wrote: >>> >>> >>>>>> >>>>>> The diff with all the changes is here >>>>>> http://hg.python.org/peps/rev/fb24c80e9afb >>>>> >>> >>> Just out of curiosity, where is "coding cookie" defined? I found enough >>> distant references to decide it was supposed to mean the coding line >>> (line 2, typically in Unix). But I originally thought it meant the BOM. >>> >>> It must have a different name in the reference, but i can't seem to find >>> it right now. If indeed it has a different name, then Pep8 should use >>> that name. >> >> http://bugs.python.org/issue18628 >> > > Thanks for the mention in the issue report. That way I share both credit and blame, as the case turns out to be ;-). Anyway, the PEP is revised, but the index still needs the entry revised to 'Encoding declaration'. -- Terry Jan Reedy From roy at panix.com Thu Aug 1 20:51:43 2013 From: roy at panix.com (Roy Smith) Date: Thu, 01 Aug 2013 20:51:43 -0400 Subject: PEP8 revised: max line lengths References: Message-ID: In article , Terry Reedy wrote: > Newly revised this morning: > http://www.python.org/dev/peps/pep-0008/#maximum-line-length > summary: > 72 for text block (comments, triple-quoted strings) > 79 for normal code > 99 for code that is really more readable with extra And the people did rejoice and did feast upon the lambs and toads and tree-sloths and fruit-bats and orangutans and breakfast cereals. From steve+comp.lang.python at pearwood.info Fri Aug 2 00:28:46 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Aug 2013 04:28:46 GMT Subject: PEP8 revised: max line lengths References: Message-ID: <51fb357e$0$30000$c3e8da3$5496439d@news.astraweb.com> On Thu, 01 Aug 2013 20:51:43 -0400, Roy Smith wrote: > In article , > Terry Reedy wrote: > >> Newly revised this morning: >> http://www.python.org/dev/peps/pep-0008/#maximum-line-length summary: >> 72 for text block (comments, triple-quoted strings) 79 for normal code >> 99 for code that is really more readable with extra > > And the people did rejoice and did feast upon the lambs and toads and > tree-sloths and fruit-bats and orangutans and breakfast cereals. Except on Python-Dev, where feedback on allowing longer lines is almost entirely negative. The consensus seems to be, if you have a line of code that is more than 79 characters, and less than 100 characters, and there is no clean way to break it over multiple lines, then it is acceptable to not break it. -- Steven From kwpolska at gmail.com Fri Aug 2 07:07:47 2013 From: kwpolska at gmail.com (=?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?=) Date: Fri, 2 Aug 2013 13:07:47 +0200 Subject: PEP8 revised: max line lengths In-Reply-To: References: Message-ID: On Fri, Aug 2, 2013 at 2:51 AM, Roy Smith wrote: > In article , > Terry Reedy wrote: > >> Newly revised this morning: >> http://www.python.org/dev/peps/pep-0008/#maximum-line-length >> summary: >> 72 for text block (comments, triple-quoted strings) >> 79 for normal code >> 99 for code that is really more readable with extra > > And the people did rejoice and did feast upon the lambs and toads and > tree-sloths and fruit-bats and orangutans and breakfast cereals. The 99-characters rule does not apply for the stdlib. And outside of the stdlib, PEP 8 is not mandated by Python (but it might be by the project leader or similar entities). If someone wanted long lines, then they could do so ALL THE TIME. So, what are you feasting for? Nothing? -- Chris ?Kwpolska? Warrick PGP: 5EAAEA16 stop html mail | always bottom-post | only UTF-8 makes sense From nicholas.cole at gmail.com Sat Aug 3 07:35:29 2013 From: nicholas.cole at gmail.com (Nicholas Cole) Date: Sat, 3 Aug 2013 12:35:29 +0100 Subject: PEP8 revised: max line lengths In-Reply-To: References: Message-ID: On Friday, 2 August 2013, Chris ?Kwpolska? Warrick wrote: [snip] > > So, what are you feasting for? Nothing? I have long since ceased to be amazed at the number of people who would like their personal and arbitrary preferences, and the rationalisations that go with them, to be validated and endorsed by others, in law if possible and in policy documents if not! -------------- next part -------------- An HTML attachment was scrubbed... URL: From wxjmfauth at gmail.com Sun Aug 4 05:52:24 2013 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Sun, 4 Aug 2013 02:52:24 -0700 (PDT) Subject: PEP8 revised: max line lengths In-Reply-To: References: Message-ID: Le samedi 3 ao?t 2013 13:35:29 UTC+2, Nicholas a ?crit?: > On Friday, 2 August 2013, Chris ?Kwpolska? Warrick wrote: > > > [snip] > > > > So, what are you feasting for? ?Nothing? > > > I have long since ceased to be amazed at the number of people who would like their personal and arbitrary preferences, and the rationalisations that go with them,?to be validated and endorsed by others, in law if possible and in policy documents if not! ---------- I have always found, computer scientists are funny scientists. - They do love to solve the problems, they created themselves. - They have the tendency to take the consequences as the causalities. - And they forget: i) p => q <==> not(q) => not(p) ii) To proof a law is correct, one has to proof it's correct in all cases. To proof a law is incorrect, it is enough to find one, and solelely one, case showing the law is incorrect. In my computing experience, the "Python world" is really shining on these points. jmf From roy at panix.com Sun Aug 4 10:07:47 2013 From: roy at panix.com (Roy Smith) Date: Sun, 04 Aug 2013 10:07:47 -0400 Subject: PEP8 revised: max line lengths References: Message-ID: In article , wxjmfauth at gmail.com wrote: > I have always found, computer scientists are funny scientists. I have always found that sciences which contain the word "science" in their name tend to not be very scientific. Biology, Chemistry, Physics, those are real sciences. Computer Science, Social Science, Political Science, not so much. From rosuav at gmail.com Sun Aug 4 10:18:29 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 4 Aug 2013 15:18:29 +0100 Subject: PEP8 revised: max line lengths In-Reply-To: References: Message-ID: On Sun, Aug 4, 2013 at 3:07 PM, Roy Smith wrote: > In article , > wxjmfauth at gmail.com wrote: > >> I have always found, computer scientists are funny scientists. > > I have always found that sciences which contain the word "science" in > their name tend to not be very scientific. > > Biology, Chemistry, Physics, those are real sciences. Computer Science, > Social Science, Political Science, not so much. Right. We use the scientific method only in our worst work, like poking at a black-box system to try to reverse compile it. Our work generally involves examining a problem and figuring out which set of tools will best solve it, which is more of an engineering thing. ("Build a bridge. Your chasm is X meters across, your bridge must support Y kg of vehicles traversing it, the terrain is unsuited to pylons, and you must not exceed Z meters of height above the road surface.") And programmers, like engineers, have to deal with the possibility (or certainty) of idiots using their products. This is not a pure science by any means. ChrisA From s.pasoev at gmail.com Sun Aug 4 10:20:28 2013 From: s.pasoev at gmail.com (Sergi Pasoev) Date: Sun, 04 Aug 2013 18:20:28 +0400 Subject: PEP8 revised: max line lengths References: Message-ID: <87fvup1qs3.fsf@sergi-gnu-tp.fjellene> Let's say computer science isn't science. From s.pasoev at gmail.com Sun Aug 4 10:27:24 2013 From: s.pasoev at gmail.com (Sergi Pasoev) Date: Sun, 04 Aug 2013 18:27:24 +0400 Subject: PEP8 revised: max line lengths References: Message-ID: <87bo5d1qgj.fsf@sergi-gnu-tp.fjellene> What about Earth Science? But in this case, it is indeed science, because in its name the word science is applied to the object of its study. But in the case of "computer science", the word science is applied to the word which describes the tool this "science" uses!! It's like it was Telescope Science in the name of astronomy. From roy at panix.com Sun Aug 4 14:18:09 2013 From: roy at panix.com (Roy Smith) Date: Sun, 04 Aug 2013 14:18:09 -0400 Subject: PEP8 revised: max line lengths References: Message-ID: In article , Chris Angelico wrote: > programmers, like engineers, have to deal with the > possibility (or certainty) of idiots using their products. As a programmer, I'm OK with the idea that idiots are using my programs. What bothers me more is when, as a user of a program, I have to deal with the fact that idiots wrote it. From rosuav at gmail.com Sun Aug 4 14:23:28 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 4 Aug 2013 19:23:28 +0100 Subject: PEP8 revised: max line lengths In-Reply-To: References: Message-ID: On Sun, Aug 4, 2013 at 7:18 PM, Roy Smith wrote: > In article , > Chris Angelico wrote: > >> programmers, like engineers, have to deal with the >> possibility (or certainty) of idiots using their products. > > As a programmer, I'm OK with the idea that idiots are using my programs. > What bothers me more is when, as a user of a program, I have to deal > with the fact that idiots wrote it. Heh. Yeah. Too true. ChrisA From wxjmfauth at gmail.com Fri Aug 2 11:08:32 2013 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Fri, 2 Aug 2013 08:08:32 -0700 (PDT) Subject: PEP8 revised: max line lengths In-Reply-To: References: Message-ID: <69cdc3df-475f-472c-a163-f3b85f806ff6@googlegroups.com> Le vendredi 2 ao?t 2013 13:07:47 UTC+2, Chris ?Kwpolska? Warrick a ?crit?: > On Fri, Aug 2, 2013 at 2:51 AM, Roy Smith wrote: > > > In article , > > > Terry Reedy wrote: > > > > > >> Newly revised this morning: > > >> http://www.python.org/dev/peps/pep-0008/#maximum-line-length > > >> summary: > > >> 72 for text block (comments, triple-quoted strings) > > >> 79 for normal code > > >> 99 for code that is really more readable with extra > > > > > > And the people did rejoice and did feast upon the lambs and toads and > > > tree-sloths and fruit-bats and orangutans and breakfast cereals. > > > > The 99-characters rule does not apply for the stdlib. And outside of > > the stdlib, PEP 8 is not mandated by Python (but it might be by the > > project leader or similar entities). If someone wanted long lines, > > then they could do so ALL THE TIME. > > > > So, what are you feasting for? Nothing? > > > > -- > > Chris ?Kwpolska? Warrick > > PGP: 5EAAEA16 > > stop html mail | always bottom-post | only UTF-8 makes sense And do not forget, a monospaced font is never monospaced (for understandable reasons). The solely valid solution, assuming there is some wish, is to define a maximal line width (preferably in SI units ;-) The day, the devs succeed to think non ascii, is not arrived. jmf PS independently of the CJK stuff. From skip at pobox.com Fri Aug 2 11:19:11 2013 From: skip at pobox.com (Skip Montanaro) Date: Fri, 2 Aug 2013 10:19:11 -0500 Subject: PEP8 revised: max line lengths In-Reply-To: <69cdc3df-475f-472c-a163-f3b85f806ff6@googlegroups.com> References: <69cdc3df-475f-472c-a163-f3b85f806ff6@googlegroups.com> Message-ID: > The solely valid solution, assuming there is some wish, > is to define a maximal line width (preferably in SI units ;-) So, 79 * 8 points == 0.222955555696 meters, right? :-) Skip From wxjmfauth at gmail.com Fri Aug 2 17:15:31 2013 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Fri, 2 Aug 2013 14:15:31 -0700 (PDT) Subject: PEP8 revised: max line lengths In-Reply-To: References: <69cdc3df-475f-472c-a163-f3b85f806ff6@googlegroups.com> Message-ID: <8b60088c-941a-46e5-9aab-629b082ef45b@googlegroups.com> Le vendredi 2 ao?t 2013 17:19:11 UTC+2, Skip Montanaro a ?crit?: > > The solely valid solution, assuming there is some wish, > > > is to define a maximal line width (preferably in SI units ;-) > > > > So, 79 * 8 points == 0.222955555696 meters, right? :-) > > > > Skip You can correct your mistake yourself. In your equation, the unit at the left is [1] * [point] = [point], at the right the unit is [meter], obviously [point] != [meter]. Problem #1 For a tool which is supposed to be Unicode compliant, a Unicode compliant font has never a constant pitch, so counting a maximal width in number of characters does not make sense. Problem #2 The only valid constraint which makes sense is a maximal size in a length unit. Two possibilities: ? if you consider your document is to be viewed as a screen document, pixel comes in mind. ? if you condider your document will be printed, retain a physical unit length (cm, inch). Problem #3 cm or inch? The only serious unit is an SI unit. (In scientific publications, only SI units are accepted) Hint: Put you code in a pdf. jmf From rosuav at gmail.com Fri Aug 2 17:34:13 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 2 Aug 2013 22:34:13 +0100 Subject: PEP8 revised: max line lengths In-Reply-To: <8b60088c-941a-46e5-9aab-629b082ef45b@googlegroups.com> References: <69cdc3df-475f-472c-a163-f3b85f806ff6@googlegroups.com> <8b60088c-941a-46e5-9aab-629b082ef45b@googlegroups.com> Message-ID: On Fri, Aug 2, 2013 at 10:15 PM, wrote: > Le vendredi 2 ao?t 2013 17:19:11 UTC+2, Skip Montanaro a ?crit : >> > The solely valid solution, assuming there is some wish, >> > is to define a maximal line width (preferably in SI units ;-) >> >> So, 79 * 8 points == 0.222955555696 meters, right? :-) >> >> Skip > > You can correct your mistake yourself. In your > equation, the unit at the left is [1] * [point] = [point], > at the right the unit is [meter], obviously > [point] != [meter]. http://en.wikipedia.org/wiki/Point_(typography) A point is one twelfth of a pica, which is one sixth of an inch, which is 0.0254 SI millimeters. Do the arithmetic. Skip is absolutely correct. > Problem #1 > For a tool which is supposed to be Unicode compliant, > a Unicode compliant font has never a constant pitch, > so counting a maximal width in number of characters > does not make sense. > > Problem #2 > The only valid constraint which makes sense is > a maximal size in a length unit. Two possibilities: > ? if you consider your document is to be viewed > as a screen document, pixel comes in mind. > ? if you condider your document will be printed, > retain a physical unit length (cm, inch). > > Problem #3 > cm or inch? The only serious unit is an SI unit. > (In scientific publications, only SI units are accepted) The cm is not a primary SI unit either. > Hint: Put you code in a pdf. Hint: Put your code in a bitmap for screens *and* a PDF for printing. That's the only way to guarantee that it'll work. Since this is not possible, there is one solution left: Do not view your code on screen, but only print it out. Then you can guarantee that your columns are as you expected. Python could be enhanced to take advantage of this; for instance, instead of requiring the hash character (which may be difficult to type on certain keyboards, including mobile phones), a letter C (U+0043) in column 1 can cause the line to be considered a Comment. ChrisA From joshua at landau.ws Fri Aug 2 22:21:02 2013 From: joshua at landau.ws (Joshua Landau) Date: Sat, 3 Aug 2013 03:21:02 +0100 Subject: PEP8 revised: max line lengths In-Reply-To: References: <69cdc3df-475f-472c-a163-f3b85f806ff6@googlegroups.com> <8b60088c-941a-46e5-9aab-629b082ef45b@googlegroups.com> Message-ID: On 2 August 2013 22:34, Chris Angelico wrote: > On Fri, Aug 2, 2013 at 10:15 PM, wrote: > > Problem #3 > > cm or inch? The only serious unit is an SI unit. > > (In scientific publications, only SI units are accepted) > > The cm is not a primary SI unit either. But it is an SI unit. (As a side note, humorously when watching University Challenge today one of the semi-finalist teams were unable to convert degrees fahrenheit to celsius. This just goes to show that non-SI units are e?v?i?l? hard.) > > Hint: Put you code in a pdf. > > Hint: Put your code in a bitmap for screens *and* a PDF for printing. > That's the only way to guarantee that it'll work. > > Since this is not possible, there is one solution left: Do not view > your code on screen, but only print it out. Then you can guarantee > that your columns are as you expected. Python could be enhanced to > take advantage of this; for instance, instead of requiring the hash > character (which may be difficult to type on certain keyboards, > including mobile phones), a letter C (U+0043) in column 1 can cause > the line to be considered a Comment. > This is jmf. It'd have to be a Euro. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Sat Aug 3 02:43:03 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 3 Aug 2013 07:43:03 +0100 Subject: PEP8 revised: max line lengths In-Reply-To: References: <69cdc3df-475f-472c-a163-f3b85f806ff6@googlegroups.com> <8b60088c-941a-46e5-9aab-629b082ef45b@googlegroups.com> Message-ID: On Sat, Aug 3, 2013 at 3:21 AM, Joshua Landau wrote: > On 2 August 2013 22:34, Chris Angelico wrote: >> >> On Fri, Aug 2, 2013 at 10:15 PM, wrote: >> >> > Problem #3 >> > cm or inch? The only serious unit is an SI unit. >> > (In scientific publications, only SI units are accepted) >> >> The cm is not a primary SI unit either. > > > But it is an SI unit. Sorry. Term I should have used is "base SI unit". Instead of using the cm, use the m. Or go three orders of magnitude at a time to the km or the mm. ChrisA From roy at panix.com Sat Aug 3 11:00:14 2013 From: roy at panix.com (Roy Smith) Date: Sat, 03 Aug 2013 11:00:14 -0400 Subject: PEP8 revised: max line lengths References: <69cdc3df-475f-472c-a163-f3b85f806ff6@googlegroups.com> <8b60088c-941a-46e5-9aab-629b082ef45b@googlegroups.com> Message-ID: In article , Chris Angelico wrote: > Sorry. Term I should have used is "base SI unit". Instead of using the > cm, use the m. Or go three orders of magnitude at a time to the km or > the mm. Or, go digital, and use Kim (kibimeters). From jcasale at activenetwerx.com Thu Aug 1 16:55:58 2013 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Thu, 1 Aug 2013 20:55:58 +0000 Subject: Logging help Message-ID: I have a couple handlers applied to a logger for a file and console destination. Default levels have been set for each, INFO+ to console and anything to file. How does one prevent logging.exception from going to a specific handler when it falls within the desired levels? Thanks, jlc From wayne at waynewerner.com Sat Aug 3 09:04:01 2013 From: wayne at waynewerner.com (Wayne Werner) Date: Sat, 3 Aug 2013 08:04:01 -0500 (CDT) Subject: Logging help In-Reply-To: References: Message-ID: On Thu, 1 Aug 2013, Joseph L. Casale wrote: > I have a couple handlers applied to a logger for a file and console destination. > Default levels have been set for each, INFO+ to console and anything to file. > > How does one prevent logging.exception from going to a specific handler when > it falls within the desired levels? > There is probably a better way, I'm not too familiar with the more advanced logging, but off the top of my head I'd suggest subclassing the Handler classes that you're interested in and overriding the Handle method (I think - you can check the source to be sure) and if the type of message is exception then just skip handling it. Alternatively, you could simply create a different logger, e.g. exc_logger = logging.getLogger('mything.exceptions') And use that to log exceptions. Oh hai - as I was reading the documentation, look what I found: http://docs.python.org/2/library/logging.html#filter Methinks that should do exactly what you want. HTH, W From jcasale at activenetwerx.com Sun Aug 4 12:06:47 2013 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Sun, 4 Aug 2013 16:06:47 +0000 Subject: Logging help In-Reply-To: References: <4de058e63b2c48468eb11a1a57210747@exch.activenetwerx.com> Message-ID: >Oh hai - as I was reading the documentation, look what I found: > >http://docs.python.org/2/library/logging.html#filter > >Methinks that should do exactly what you want. Hi Wayne, I was too hasty when I looked at filters as I didn't think they could do what I wanted. Turns out a logging object sent the filter method will an exc_info attribute in my case. Thanks! jlc From adamyoshi2184 at gmail.com Thu Aug 1 21:03:34 2013 From: adamyoshi2184 at gmail.com (Adam Sweiger) Date: Thu, 1 Aug 2013 18:03:34 -0700 (PDT) Subject: Having troubles with list methods Message-ID: The code: pastebin.com/MyrLB704 I'm working on this program in my Python book, and for some reason it doesn't seem to be working. I've read my code, it seems fine to me. Here's what the book says: The High Scores program uses list methods to create and maintain a list of the user's best scores for a computer game. The program uses a simple, menu-driven interface. The user has a few choices. He or she can add a new score, delete a score, sort the scores, or quit the program. When I run my code, it just keeps printing the same thing infinitely. Any help? From demon.exh at gmail.com Thu Aug 1 21:11:42 2013 From: demon.exh at gmail.com (Sam Whitehead) Date: Thu, 1 Aug 2013 18:11:42 -0700 (PDT) Subject: Having troubles with list methods In-Reply-To: References: Message-ID: <901bb531-d093-4fca-9b90-563e63cc5e60@googlegroups.com> Your indentation is such that the first print statement is the only thing inside the while loop. From adamyoshi2184 at gmail.com Thu Aug 1 21:16:25 2013 From: adamyoshi2184 at gmail.com (Adam Sweiger) Date: Thu, 1 Aug 2013 18:16:25 -0700 (PDT) Subject: Having troubles with list methods In-Reply-To: <901bb531-d093-4fca-9b90-563e63cc5e60@googlegroups.com> References: <901bb531-d093-4fca-9b90-563e63cc5e60@googlegroups.com> Message-ID: <3d348b80-0027-4c8b-993b-79d5c736a466@googlegroups.com> On Thursday, August 1, 2013 8:11:42 PM UTC-5, Sam Whitehead wrote: > Your indentation is such that the first print statement is the only thing inside the while loop. I corrected the indentation and it works perfectly now. Thanks. From matt.doolittle33 at gmail.com Fri Aug 2 06:54:32 2013 From: matt.doolittle33 at gmail.com (matt.doolittle33 at gmail.com) Date: Fri, 2 Aug 2013 03:54:32 -0700 (PDT) Subject: outputting time in microseconds or milliseconds Message-ID: Hey everybody, I am using 2.7 on Ubuntu 12.10. All I need to do is to print time with the microseconds. I have been looking at the docs and trying things for about half a day now with no success. Currently my code looks like this: # write date and time and microseocnds self.logfile.write('%s\t'%(str(strftime("%Y-%m-%d", )))) self.logfile.write('%s\t'%(str(strftime("%H:%M:%S", )))) self.logfile.write('%s\t'%(str(time()))) the output looks like this: 2013-08-02 06:01:43 00:00:00 2013-08-02 06:01:43 00:00:00 2013-08-02 06:01:43 00:00:00 2013-08-02 06:01:43 00:00:00 2013-08-02 06:01:43 00:00:00 2013-08-02 06:01:43 00:00:00 2013-08-02 06:01:43 00:00:00 2013-08-02 06:01:43 00:00:00 2013-08-02 06:01:43 00:00:00 2013-08-02 06:01:44 00:00:00 2013-08-02 06:01:44 00:00:00 as you can see in the 2nd column the observations occur with 'fractional second' frequency, up to 8 or 9 per second. You can also see my latest attempt to get the microseconds in the third column is not working. It would really be great if python had a way to just tack the microseconds (actually i think milliseconds would be better) on to the second (in the 2nd column) as a fraction. If this is not possible I need the extra write statement for just the microseconds (millisecond if possible). Any help will be much appreciated. Thanks! From davea at davea.name Fri Aug 2 08:08:33 2013 From: davea at davea.name (Dave Angel) Date: Fri, 2 Aug 2013 12:08:33 +0000 (UTC) Subject: outputting time in microseconds or milliseconds References: Message-ID: matt.doolittle33 at gmail.com wrote: > Hey everybody, > > I am using 2.7 on Ubuntu 12.10. and what version of Python are you using? I don't know if it matters, but it's useful to always supply both Python version and OS version. > All I need to do is to print time with the microseconds. I have been > looking at the docs and trying things for about half a day now with > no success. Currently my code looks like this: > > # write date and time and microseocnds > self.logfile.write('%s\t'%(str(strftime("%Y-%m-%d", )))) > self.logfile.write('%s\t'%(str(strftime("%H:%M:%S", )))) You've already got a problem: since this has two (implicit) calls to localtime(). If this happens near enough to midnight, then your date could be for one day, but the time for the following day. You need to make a single call to get the time, and then format it into one or more fields. if you didn't need sub-second information, it could have been done by just using a longer spec for strftime(). > self.logfile.write('%s\t'%(str(time()))) When I test str(time()) I get an output like this: 1375444314.484557 I can't imagine how you would get that third column from the line you quote above. > > the output looks like this: > > 2013-08-02 06:01:43 00:00:00 > 2013-08-02 06:01:43 00:00:00 > 2013-08-02 06:01:43 00:00:00 > 2013-08-02 06:01:43 00:00:00 > 2013-08-02 06:01:43 00:00:00 > 2013-08-02 06:01:43 00:00:00 > 2013-08-02 06:01:43 00:00:00 > 2013-08-02 06:01:43 00:00:00 > 2013-08-02 06:01:43 00:00:00 > 2013-08-02 06:01:44 00:00:00 > 2013-08-02 06:01:44 00:00:00 > > as you can see in the 2nd column the observations occur with 'fractional second' frequency, up to 8 or 9 per second. You can also see my latest attempt to get the microseconds in the third column is not working. It would really be great if python had a way to just tack the microseconds (actually i think milliseconds would be better) on to the second (in the 2nd column) as a fraction. If this is not possible I need the extra write statement for just the microseconds (millisecond if possible). > now = time() #make a single call to get the time in seconds (float) localnow = localtime(now) print(strftime("%Y-%m-%d\t%H:%M:%S\t", llocalnow)) print(now-int(now)) #obviously you should format this to get the desired layout -- DaveA From rosuav at gmail.com Fri Aug 2 08:50:42 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 2 Aug 2013 13:50:42 +0100 Subject: outputting time in microseconds or milliseconds In-Reply-To: References: Message-ID: On Fri, Aug 2, 2013 at 1:08 PM, Dave Angel wrote: > matt.doolittle33 at gmail.com wrote: > >> Hey everybody, >> >> I am using 2.7 on Ubuntu 12.10. > > and what version of Python are you using? I don't know if it matters, > but it's useful to always supply both Python version and OS version. > Looks to me like Python 2.7. ChrisA From ulrich.eckhardt at dominolaser.com Fri Aug 2 07:59:30 2013 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Fri, 02 Aug 2013 13:59:30 +0200 Subject: outputting time in microseconds or milliseconds In-Reply-To: References: Message-ID: <2ijsca-ag8.ln1@satorlaser.homedns.org> Am 02.08.2013 12:54, schrieb matt.doolittle33 at gmail.com: > I am using 2.7 on Ubuntu 12.10. All I need to do is to print time with the microseconds.[...] > > # write date and time and microseocnds > self.logfile.write('%s\t'%(str(strftime("%Y-%m-%d", )))) > self.logfile.write('%s\t'%(str(strftime("%H:%M:%S", )))) > self.logfile.write('%s\t'%(str(time()))) > > the output looks like this: > > 2013-08-02 06:01:43 00:00:00 > 2013-08-02 06:01:43 00:00:00 > 2013-08-02 06:01:43 00:00:00 > 2013-08-02 06:01:43 00:00:00 > 2013-08-02 06:01:43 00:00:00 The output here and the code are surely not the same, provided that "time()" is the same as Python's "time.time()". That function will give you the time as a floating-point number and with as much precision as is available. You then only need the rest when dividing by one to get the fractional seconds. BTW: 1. You have a race condition, you are retrieving the time thrice, which can and should yield different results for each call. Call it once, then format the results in multiple steps if you want. 2. Python's strftime() gives you a string already, calling str() on it is redundant. Also, formatting a string in order to then format it into another string is unnecessary overhead. Further, '%s' will implicitly invoke str() on the argument. Belt and suspenders anyone? :) Good luck! Uli From matt.doolittle33 at gmail.com Fri Aug 2 09:17:03 2013 From: matt.doolittle33 at gmail.com (matt.doolittle33 at gmail.com) Date: Fri, 2 Aug 2013 06:17:03 -0700 (PDT) Subject: outputting time in microseconds or milliseconds In-Reply-To: <2ijsca-ag8.ln1@satorlaser.homedns.org> References: <2ijsca-ag8.ln1@satorlaser.homedns.org> Message-ID: <8f4958d4-d346-4561-88ba-9a49a67b8792@googlegroups.com> so you are saying that self.logfile.write('%s\t'%(str(time()))) should be: self.logfile.write('%s\t'%(str(time.time()))) ??? Thanks From ulrich.eckhardt at dominolaser.com Mon Aug 5 02:15:11 2013 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Mon, 05 Aug 2013 08:15:11 +0200 Subject: outputting time in microseconds or milliseconds In-Reply-To: <8f4958d4-d346-4561-88ba-9a49a67b8792@googlegroups.com> References: <2ijsca-ag8.ln1@satorlaser.homedns.org> <8f4958d4-d346-4561-88ba-9a49a67b8792@googlegroups.com> Message-ID: Am 02.08.2013 15:17, schrieb matt.doolittle33 at gmail.com: > so you are saying that > > self.logfile.write('%s\t'%(str(time()))) > > should be: > > self.logfile.write('%s\t'%(str(time.time()))) No, I'm not saying that. What I wanted to make clear is that your code is impossible to understand as it stands, because nobody knows what "time" refers to. My guess was that you had "from time import time, strftime" somewhere in your code so that the "time" in your code refers to the "time" function from Python's "time" module. Uli From skip at pobox.com Fri Aug 2 08:37:45 2013 From: skip at pobox.com (Skip Montanaro) Date: Fri, 2 Aug 2013 07:37:45 -0500 Subject: outputting time in microseconds or milliseconds In-Reply-To: References: Message-ID: Perhaps use datetime? >>> now = datetime.datetime.now() >>> now.isoformat() '2013-08-02T07:37:08.430131' >>> now.strftime("%f") '430131' Skip From steve+comp.lang.python at pearwood.info Fri Aug 2 08:35:13 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Aug 2013 12:35:13 GMT Subject: outputting time in microseconds or milliseconds References: Message-ID: <51fba781$0$30000$c3e8da3$5496439d@news.astraweb.com> On Fri, 02 Aug 2013 03:54:32 -0700, matt.doolittle33 wrote: > Hey everybody, > > I am using 2.7 on Ubuntu 12.10. All I need to do is to print time with > the microseconds. I have been looking at the docs and trying things for > about half a day now with no success. Currently my code looks like > this: > > # write date and time and microseocnds > self.logfile.write('%s\t'%(str(strftime("%Y-%m-%d", )))) > self.logfile.write('%s\t'%(str(strftime("%H:%M:%S", )))) > self.logfile.write('%s\t'%(str(time()))) What's this time() function? Where does it come from, and what does it do? By the look of it, it merely returns the string "00:00:00". The time.time() function returns a number of seconds: py> "%s" % time.time() '1375445812.873546' so I'm not sure what function you are calling there. Also, you don't need to call str() before using the %s template, since that automatically converts any object to a string. Besides, time.strftime already returns a string. You can replace all of those above lines with a single format string: py> import time py> print(time.strftime("%Y-%m-%d\t%H:%M:%S")) 2013-08-02 22:31:14 If you google for "strftime millisecond" with the search engine of your choice: https://duckduckgo.com/?q=strftime%20millisecond you will find this bug report: http://bugs.python.org/issue1982 Unfortunately, the %S.%f codes do not appear to work for me on Linux using Python 2.6, 2.7 or 3.3. Perhaps you will have better luck. -- Steven From matt.doolittle33 at gmail.com Fri Aug 2 11:09:15 2013 From: matt.doolittle33 at gmail.com (matt.doolittle33 at gmail.com) Date: Fri, 2 Aug 2013 08:09:15 -0700 (PDT) Subject: outputting time in microseconds or milliseconds In-Reply-To: <51fba781$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <51fba781$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: <0a347771-a725-4be4-ac68-6afff7ecd2a5@googlegroups.com> On Friday, August 2, 2013 8:35:13 AM UTC-4, Steven D'Aprano wrote: > On Fri, 02 Aug 2013 03:54:32 -0700, matt.doolittle33 wrote: > > > > > Hey everybody, > > > > > > I am using 2.7 on Ubuntu 12.10. All I need to do is to print time with > > > the microseconds. I have been looking at the docs and trying things for > > > about half a day now with no success. Currently my code looks like > > > this: > > > > > > # write date and time and microseocnds > > > self.logfile.write('%s\t'%(str(strftime("%Y-%m-%d", )))) > > > self.logfile.write('%s\t'%(str(strftime("%H:%M:%S", )))) > > > self.logfile.write('%s\t'%(str(time()))) > > What's this time() function? Where does it come from, and what does it > > do? By the look of it, it merely returns the string "00:00:00". The > > time.time() function returns a number of seconds: > > py> "%s" % time.time() > > '1375445812.873546' > > right this is the number that i need in the third column. so should i try?: > > self.logfile.write('%s\t'%(str(time.time()))) From matt.doolittle33 at gmail.com Fri Aug 2 10:18:54 2013 From: matt.doolittle33 at gmail.com (matt.doolittle33 at gmail.com) Date: Fri, 2 Aug 2013 07:18:54 -0700 (PDT) Subject: outputting time in microseconds or milliseconds In-Reply-To: References: Message-ID: <3e3f0032-3b3b-4c50-99b1-8db3676fb993@googlegroups.com> On Friday, August 2, 2013 8:37:45 AM UTC-4, Skip Montanaro wrote: > Perhaps use datetime? > > > > >>> now = datetime.datetime.now() > > >>> now.isoformat() > > '2013-08-02T07:37:08.430131' > > >>> now.strftime("%f") > > '430131' > > > > Skip Thanks Skip, what i currently i have is: dt = datetime.now() and self.logfile.write('%s\t'%(str(strftime("%Y-%m-%d", )))) self.logfile.write('%s\t'%(str(strftime("%H:%M:%S", )))) self.logfile.write('%s\t'%(str(dt.microsecond))) what i get is this: 2013-08-02 09:52:20 312961 2013-08-02 09:52:20 313274 2013-08-02 09:52:20 313461 2013-08-02 09:52:20 313580 2013-08-02 09:52:20 498705 2013-08-02 09:52:20 508610 2013-08-02 09:52:20 508963 2013-08-02 09:52:20 509191 2013-08-02 09:52:20 509477 2013-08-02 09:52:20 509703 2013-08-02 09:52:20 509798 2013-08-02 09:52:20 509887 2013-08-02 09:52:20 509975 2013-08-02 09:52:20 511013 2013-08-02 09:52:20 511112 2013-08-02 09:52:20 678554 2013-08-02 09:52:20 687994 2013-08-02 09:52:20 688291 2013-08-02 09:52:20 688519 2013-08-02 09:52:20 688740 2013-08-02 09:52:20 688963 is the third column is only the microsecond? how could i get this to write with the rest of the time (the hh:mm:ss) ? From skip at pobox.com Fri Aug 2 11:15:53 2013 From: skip at pobox.com (Skip Montanaro) Date: Fri, 2 Aug 2013 10:15:53 -0500 Subject: outputting time in microseconds or milliseconds In-Reply-To: <3e3f0032-3b3b-4c50-99b1-8db3676fb993@googlegroups.com> References: <3e3f0032-3b3b-4c50-99b1-8db3676fb993@googlegroups.com> Message-ID: > is the third column is only the microsecond? Yes. > how could i get this to write with the rest of the time (the hh:mm:ss) ? It sounds like you really want the time formatted in a particular way, not just the numeric value of one or more fields. Look at the documentation for the strftime method of datetime objects for full details: http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior Here's a simple example: >>> import datetime >>> now = datetime.datetime.now() >>> now.strftime("%H:%M:%S.%f") '10:14:36.526110' Skip From matt.doolittle33 at gmail.com Sun Aug 4 07:30:08 2013 From: matt.doolittle33 at gmail.com (matt.doolittle33 at gmail.com) Date: Sun, 4 Aug 2013 04:30:08 -0700 (PDT) Subject: outputting time in microseconds or milliseconds In-Reply-To: References: Message-ID: <07f6b1c7-069d-458b-a9fe-ff30c09f2f2d@googlegroups.com> ok so now i import the module like this: from time import strftime, time i made the write statement like this: self.logfile.write('%s\t'%(str(strftime("%Y-%m-%d", )))) self.logfile.write('%s\t'%(str(strftime("%H:%M:%S", )))) self.logfile.write('%s\t'%(str(time()))) (oh and btw,i know the code is hacker ugly). and the output is this: 2013-08-03 23:59:34 1375588774.89 2013-08-03 23:59:35 1375588775.06 2013-08-03 23:59:35 1375588775.25 2013-08-03 23:59:35 1375588775.43 2013-08-03 23:59:35 1375588775.80 2013-08-03 23:59:35 1375588775.99 2013-08-03 23:59:35 1375588775.99 2013-08-03 23:59:35 1375588775.99 2013-08-03 23:59:35 1375588776.00 2013-08-03 23:59:36 1375588776.15 2013-08-03 23:59:36 1375588776.15 2013-08-03 23:59:36 1375588776.16 2013-08-03 23:59:36 1375588776.16 2013-08-03 23:59:36 1375588776.16 2013-08-03 23:59:36 1375588776.16 2013-08-03 23:59:36 1375588776.16 2013-08-03 23:59:36 1375588776.16 2013-08-03 23:59:36 1375588776.16 2013-08-03 23:59:36 1375588776.34 2013-08-03 23:59:36 1375588776.35 2013-08-03 23:59:36 1375588776.35 2013-08-03 23:59:36 1375588776.35 the first two columns are for eyes so if they are a microsecond apart it doesn't matter. the numbers in the third column are for calculating duration which is where i need the precision. Why is it only giving me the centisecond precision? the docs say i should get microsecond precision with the code i put together. Thanks! From alain at dpt-info.u-strasbg.fr Sun Aug 4 07:57:08 2013 From: alain at dpt-info.u-strasbg.fr (Alain Ketterlin) Date: Sun, 04 Aug 2013 13:57:08 +0200 Subject: outputting time in microseconds or milliseconds References: <07f6b1c7-069d-458b-a9fe-ff30c09f2f2d@googlegroups.com> Message-ID: <87eha91xez.fsf@dpt-info.u-strasbg.fr> matt.doolittle33 at gmail.com writes: > self.logfile.write('%s\t'%(str(time()))) [...] > 2013-08-03 23:59:34 1375588774.89 [...] > Why is it only giving me the centisecond precision? the docs say i > should get microsecond precision with the code i put together. Because of str()'s default format. Use "%f" % (time()) (and maybe vary the precision). -- Alain. P/S: using str() + string formatting is kind of overkill: either use string format directives like %f, or use str() with simple string concatenation -- the latter giving less control. From davea at davea.name Sun Aug 4 09:38:43 2013 From: davea at davea.name (Dave Angel) Date: Sun, 4 Aug 2013 13:38:43 +0000 (UTC) Subject: outputting time in microseconds or milliseconds References: <07f6b1c7-069d-458b-a9fe-ff30c09f2f2d@googlegroups.com> Message-ID: matt.doolittle33 at gmail.com wrote: > ok so now i import the module like this: > > from time import strftime, time > > i made the write statement like this: > > self.logfile.write('%s\t'%(str(strftime("%Y-%m-%d", )))) > self.logfile.write('%s\t'%(str(strftime("%H:%M:%S", )))) > self.logfile.write('%s\t'%(str(time()))) > > (oh and btw,i know the code is hacker ugly). > and the output is this: > > 2013-08-03 23:59:34 1375588774.89 > 2013-08-03 23:59:35 1375588775.06 > 2013-08-03 23:59:35 1375588775.25 > 2013-08-03 23:59:35 1375588775.43 > 2013-08-03 23:59:35 1375588775.80 > 2013-08-03 23:59:35 1375588775.99 > 2013-08-03 23:59:35 1375588775.99 > 2013-08-03 23:59:35 1375588775.99 > 2013-08-03 23:59:35 1375588776.00 > 2013-08-03 23:59:36 1375588776.15 > 2013-08-03 23:59:36 1375588776.15 > 2013-08-03 23:59:36 1375588776.16 > 2013-08-03 23:59:36 1375588776.16 > 2013-08-03 23:59:36 1375588776.16 > 2013-08-03 23:59:36 1375588776.16 > 2013-08-03 23:59:36 1375588776.16 > 2013-08-03 23:59:36 1375588776.16 > 2013-08-03 23:59:36 1375588776.16 > 2013-08-03 23:59:36 1375588776.34 > 2013-08-03 23:59:36 1375588776.35 > 2013-08-03 23:59:36 1375588776.35 > 2013-08-03 23:59:36 1375588776.35 > > the first two columns are for eyes so if they are a microsecond apart it doesn't matter. But if the time is close enough to midnight, they'll be a *day* apart, as I (and others) said before. It's also hard to imagine why you resist the good advice you've been getting about formatting. You can do both the date and time with one call to strftime(). > the numbers in the third column are for calculating duration which is > where i need the precision. > > Why is it only giving me the centisecond precision? the docs say i should get microsecond precision with the code i put together. > Have you done any experimenting to decide which logic was losing those extra digits? Sit in the debugger, and do a t = time.time() Then investigate what t looks like, by repr(t), str(t), and "something" %t (for various values of "something") .... from time import time, localtime, strftime now = time() #make a single call to get the time in seconds (float) localnow = localtime(now) out12 = strftime("%Y-%m-%d\t%H:%M:%S\t", localnow) out3 = "%.6f" % now self.logfile.write(out12 + out3) You still have the problem that out12 is in the local time zone, while out3 is in DCT. But as you say, these are for different audiences, and presumably you won't really be putting them in the same file. -- DaveA From roy at panix.com Sun Aug 4 10:33:48 2013 From: roy at panix.com (Roy Smith) Date: Sun, 04 Aug 2013 10:33:48 -0400 Subject: outputting time in microseconds or milliseconds References: <07f6b1c7-069d-458b-a9fe-ff30c09f2f2d@googlegroups.com> Message-ID: In article <07f6b1c7-069d-458b-a9fe-ff30c09f2f2d at googlegroups.com>, matt.doolittle33 at gmail.com wrote: > self.logfile.write('%s\t'%(str(time()))) > [...] > 1375588774.89 > [...] > Why is it only giving me the centisecond precision? the docs say i should > get microsecond precision When citing documentation, it's a good idea to provide a link to the docs page, and/or a direct quote of what you read. I'm looking at http://docs.python.org/2/library/time.html#time.time, which says, "not all systems provide time with a better precision than 1 second". So, I don't know where you got the impression that you're guaranteed microsecond precision. Earlier in the thread, you did mention that you're on Ubuntu, and there you do indeed get pretty good precision. I'm not 100% sure if it's good to the microsecond (it appears to be), but it's certainly better than centisecond. Anyway, your problem appears to be that str(float) gives you two digits after the decimal (at least for values in the range we're talking about here), but repr() will give you more: >>> t = time.time() >>> str(t) '1375626035.26' >>> repr(t) '1375626035.260934' I don't know anywhere that those behaviors are guaranteed, however. If you want to make sure you print a float with 6 digits after the decimal, you should use %f, not %s: >>> '%.6f' % t '1375626035.260934' Of course, if the underlying system call that time.time() invokes returns less precision than microseconds, some of those 6 digits may always be zero. Or worse, garbage. Taking a step back, you're probably better off using datetimes. You'll get all this conversion nonsense for free: >>> print datetime.datetime.utcnow() 2013-08-04 14:33:09.255096 When I write an operating system, I'm going to have it keep time in units of YiTp (Yobi Plank times). From matt.doolittle33 at gmail.com Wed Aug 7 22:51:47 2013 From: matt.doolittle33 at gmail.com (matt.doolittle33 at gmail.com) Date: Wed, 7 Aug 2013 19:51:47 -0700 (PDT) Subject: outputting time in microseconds or milliseconds In-Reply-To: References: <07f6b1c7-069d-458b-a9fe-ff30c09f2f2d@googlegroups.com> Message-ID: <32a94a22-107c-4575-8808-d9355d635e83@googlegroups.com> > > Taking a step back, you're probably better off using datetimes. You'll > > get all this conversion nonsense for free: > i did: from time import strftime, time from datetime import datetime now = datetime.now() self.logfile.write('%s\t'%(strftime("%Y-%m-%d",))) self.logfile.write('%s\t'%(now.strftime("%H:%M:%S.%f",))) this gives me: 2013-08-04 14:01:27.906126 2013-08-04 14:01:28.052273 2013-08-04 14:01:28.058967 2013-08-04 14:01:28.243959 2013-08-04 14:01:28.251107 2013-08-04 14:01:28.251268 2013-08-04 14:01:28.251373 2013-08-04 14:01:28.251475 2013-08-04 14:01:28.424568 2013-08-04 14:01:28.612548 2013-08-04 14:01:28.616569 2013-08-04 14:01:28.616727 2013-08-04 14:01:28.792487 2013-08-04 14:01:28.796226 thats what i need. Thanks! From skip at pobox.com Thu Aug 8 08:03:31 2013 From: skip at pobox.com (Skip Montanaro) Date: Thu, 8 Aug 2013 07:03:31 -0500 Subject: outputting time in microseconds or milliseconds In-Reply-To: <32a94a22-107c-4575-8808-d9355d635e83@googlegroups.com> References: <07f6b1c7-069d-458b-a9fe-ff30c09f2f2d@googlegroups.com> <32a94a22-107c-4575-8808-d9355d635e83@googlegroups.com> Message-ID: > i did: > > from time import strftime, time > from datetime import datetime > > now = datetime.now() > > self.logfile.write('%s\t'%(strftime("%Y-%m-%d",))) > self.logfile.write('%s\t'%(now.strftime("%H:%M:%S.%f",))) Note that you don't need the time module here. Datetime objects have what you need all by themselves: from datetime import datetime now = datetime.now() self.logfile.write('%s\t%s\n' % (now.strftime("%Y-%m-%d"), now.strftime("%H:%M:%S.%f"))) The time module was historically the way Python did time, but it wasn't at all object-oriented and provided no direct support for date arithmetic. When Tim Peters wrote datetime, the world became a better place. Cozy up to the datetime documentation and put the time module aside. Skip Skip From roy at panix.com Thu Aug 8 09:30:29 2013 From: roy at panix.com (Roy Smith) Date: Thu, 08 Aug 2013 09:30:29 -0400 Subject: outputting time in microseconds or milliseconds References: <07f6b1c7-069d-458b-a9fe-ff30c09f2f2d@googlegroups.com> <32a94a22-107c-4575-8808-d9355d635e83@googlegroups.com> Message-ID: In article , Skip Montanaro wrote: > When Tim Peters wrote datetime, the world became a better > place. Lots of languages and databases have things called datetimes. It's easy to be lulled into thinking that they're all the same, but they're not. They all have slightly different ranges and precisions. Just something to be aware of. From arnaud.fontaine at nexedi.com Fri Aug 2 06:52:28 2013 From: arnaud.fontaine at nexedi.com (Arnaud Fontaine) Date: Fri, 02 Aug 2013 19:52:28 +0900 Subject: Dealing with ImportLock deadlock in Import Hooks Message-ID: <87ob9g1i1f.fsf@duckcorp.org> Hello, I'm currently working on implementing Import Hooks (PEP302) with Python 2.7 to be able to import modules whose code is in ZODB. However, I have stumbled upon a widely known issue about import deadlock[0][1] (note that this issue is not directly related to ZODB, but a more general question about dealing with ImportLock deadlock for Import Hooks), basically: Thread 1 is trying to import a module 'foo.bar' (where 'foo' is a package containing dynamic modules) handled by Import Hooks I implemented, so ImportLock is acquired before even running the hooks (Python/import.c:PyImport_ImportModuleLevel()). Then, these import hooks try to load objects from ZODB and a request is sent and handled by another thread (Thread 2) which itself tries to import another module. Of course, this causes a deadlock because the first thread still holds ImportLock. I have thought about the following solutions: * Backport the patch applied in python 3.3 from issue 9260[0]. This would be the best option because it would mean that even when trying to import any module from package 'foo', other modules and packages can be imported, which would solve my issue. However, I'm not sure it could be released into python 2.7? * Within the hooks, protect the Import Hooks with a separate lock for the loader method. This would prevent any other thread to import any modules from 'foo' package but still allows to call the finder method (ignoring module fullname not starting with 'foo') along with other finder methods, so that other ZODB modules can be imported. Then, in the loader method, until the module is actually inserted into sys.modules and then other load_module() PEP302 responsabilities being taken care of (such as exec the code), release the ImportLock so that Thread 2 can process requests and send objects back to Thread 1. However, even after trying to understand import.c, I'm not sure at all that this is enough and that releasing ImportLock would not have nasty side-effects, any thoughts about that? * Fix the code in ZODB to not avoid import but to me this seems like a dirty hack because it could happen again and I would prefer to fix this issue once and for all. Any thoughts or suggestion welcome, thanks! Regards, -- Arnaud Fontaine [0] http://bugs.python.org/issue9260 [1] http://mail.python.org/pipermail/python-dev/2003-February/033436.html From js at globe.de Fri Aug 2 08:00:09 2013 From: js at globe.de (Schneider) Date: Fri, 02 Aug 2013 14:00:09 +0200 Subject: Python performance Message-ID: <51FB9F49.9040209@globe.de> Hi list, I have to write a small SMTP-Relay script (+ some statistic infos) and I'm wondering, if this can be done in python (in terms of performance, of course not in terms of possibility ;) ). It has to handle around 2000 mails per hour for at least 8hours a day (which does not mean, that it is allowed not to respond the rest of the day. Can this be done? or should I better use some other programming language? My second choice would be erlang. bg, Johannes -- GLOBE Development GmbH K?nigsberger Strasse 260 48157 M?nsterGLOBE Development GmbH K?nigsber ger Strasse 260 48157 M?nster 0251/5205 390 From python.list at tim.thechases.com Fri Aug 2 08:59:26 2013 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 2 Aug 2013 07:59:26 -0500 Subject: Python performance In-Reply-To: <51FB9F49.9040209@globe.de> References: <51FB9F49.9040209@globe.de> Message-ID: <20130802075926.454bd391@bigbox.christie.dr> On 2013-08-02 14:00, Schneider wrote: > I have to write a small SMTP-Relay script (+ some statistic infos) > and I'm wondering, if this > can be done in python (in terms of performance, of course not in > terms of possibility ;) ). > > It has to handle around 2000 mails per hour for at least 8hours a > day (which does not mean, that it is allowed not to respond the > rest of the day. > > Can this be done? or should I better use some other programming > language? My second choice would be erlang. I suspect it depends on a lot of factors: - will your network connection support that much traffic? (And an ISP that will grant you permission to spew that volume of email?) - are these simple text emails, or are they large with lots of attachments, inline images, PDFs, or whatever? - are the statistics that you're gathering simple, or do they require complex analysis of the documents passing through? - is the load 8hr straight of spewing email, or is it bursty? If it's bursty, you can internally queue them up when load gets high, delivering them from that queue when load diminishes. Given the store-and-forward nature of email, there's no guarantee that if you spewed them at ~33/minute (that/s a little faster than one every two seconds), they'd arrive at their destination any faster than if you'd queued them up and sent them at a more steady rate. That said, sending ~33 text emails per minute doesn't seem impossible, it just depends on your configuration (network, DNS, and receiving SMTP server(s) throttling/setup) -tkc From js at globe.de Fri Aug 2 09:16:05 2013 From: js at globe.de (Schneider) Date: Fri, 02 Aug 2013 15:16:05 +0200 Subject: Python performance In-Reply-To: <20130802075926.454bd391@bigbox.christie.dr> References: <51FB9F49.9040209@globe.de> <20130802075926.454bd391@bigbox.christie.dr> Message-ID: <51FBB115.4070608@globe.de> On Fri 02 Aug 2013 02:59:26 PM CEST, Tim Chase wrote: > On 2013-08-02 14:00, Schneider wrote: >> I have to write a small SMTP-Relay script (+ some statistic infos) >> and I'm wondering, if this >> can be done in python (in terms of performance, of course not in >> terms of possibility ;) ). >> >> It has to handle around 2000 mails per hour for at least 8hours a >> day (which does not mean, that it is allowed not to respond the >> r est of the day. >> >> Can this be done? or should I better use some other programming >> language? My second choice would be erlang. > > I suspect it depends on a lot of factors: > > - will your network connection support that much traffic? (And an > ISP that will grant you permission to spew that volume of email?) yes, because we are the ISP. > - are these simple text emails, or are they large with lots of > attachments, inline images, PDFs, or whatever? any kind of mail. No restrictions allowed. > - are the statistics that you're gathering simple, or do they require > complex analysis of the documents passing through? very simple statistics, mostly counting and time statistics. > - is the load 8hr straight of spewing email, or is it bursty? If > it's bursty, you can internally queue them up when load gets high, > delivering them from that queue when load diminishes. Given the > store-and-forward nature of email, there's no guarantee that if > you spewe d them at ~33/minute (that/s a little faster than one > every two seconds), they'd arrive at their destination any faster > than if you'd queued them up and sent them at a more steady rate. I guess it's bursty. I don't have finer granulated information about their time distribution. > -tkc > Queuing the mails for a while is not possible, because the tool should sit between the client and smtp-server. It should act as proxy, not as server. 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 rosuav at gmail.com Fri Aug 2 09:43:33 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 2 Aug 2013 14:43:33 +0100 Subject: Python performance In-Reply-To: <51FBB115.4070608@globe.de> References: <51FB9F49.9040209@globe.de> <20130802075926.454bd391@bigbox.christie.dr> <51FBB115.4070608@globe.de> Message-ID: On Fri, Aug 2, 2013 at 2:16 PM, Schneider wrote: > Queuing the mails for a while is not possible, because the tool should sit > between the client and smtp-server. > It should act as proxy, not as server. I've written an SMTP proxy (primary purpose: check SPF records; secondary purpose: rate-limit one particular PHP app) that can handle fairly large throughput; it could probably saturate the network connection it's working on (10Mbit) without the proxy making a particularly notable blip on the CPU or RAM usage. Most of the work is elsewhere. How bursty will the mail be? If we're talking 2000 all at once and then an hour of quietness, you may have some issues; but I'd say you could do 100-200 in a second, on reasonable hardware. Of course, this is assuming you're writing your logs to a write-cached volume - which means you'll lose some stats if the power fails. If that's a problem to you, you'll want to record your stats more reliably (personally I'd let PostgreSQL worry about the details, ie store into a database), which will cost throughput. But otherwise, basic stats shouldn't take more than 5-10ms. ChrisA From rosuav at gmail.com Fri Aug 2 08:57:55 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 2 Aug 2013 13:57:55 +0100 Subject: Python performance In-Reply-To: <51FB9F49.9040209@globe.de> References: <51FB9F49.9040209@globe.de> Message-ID: On Fri, Aug 2, 2013 at 1:00 PM, Schneider wrote: > Hi list, > > I have to write a small SMTP-Relay script (+ some statistic infos) and I'm > wondering, if this > can be done in python (in terms of performance, of course not in terms of > possibility ;) ). > > It has to handle around 2000 mails per hour for at least 8hours a day (which > does not mean, that it is allowed not to respond the rest of the day. 2000 an hour is less than one a second. I don't know how much hardware you're going to devote to this or how many other services are going to be on the same computer, but you should be able to do that no problem at all; basic statisticking isn't difficult. The slowest part is likely to be networking (esp if you have to do DNS lookups), so you'll want to make sure everything's done asynchronously - either with threads or with async queries and so on. I'd say this will be fairly easy. ChrisA From rgacote at appropriatesolutions.com Fri Aug 2 09:09:37 2013 From: rgacote at appropriatesolutions.com (Ray Cote) Date: Fri, 2 Aug 2013 09:09:37 -0400 (EDT) Subject: Python performance In-Reply-To: <51FB9F49.9040209@globe.de> Message-ID: <24373948.472.1375448972239.JavaMail.rgacote@raymac.appropriatesolutions.com> ----- Original Message ----- > From: "Schneider" > To: python-list at python.org > Sent: Friday, August 2, 2013 8:00:09 AM > Subject: Python performance > > Hi list, > > I have to write a small SMTP-Relay script (+ some statistic infos) > and > I'm wondering, if this > can be done in python (in terms of performance, of course not in > terms > of possibility ;) ). > > It has to handle around 2000 mails per hour for at least 8hours a day > (which does not mean, that it is allowed not to respond the rest of > the day. > A quick calculation shows that 2,000 mails per hour is less than 1 email every second. Plenty of time to handle individual emails. > Can this be done? or should I better use some other programming > language? > My second choice would be erlang. > > bg, > Johannes > > > -- > GLOBE Development GmbH > K?nigsberger Strasse 260 > 48157 M?nsterGLOBE Development GmbH > K?nigsber > ger Strasse 260 > 48157 M?nster > 0251/5205 390 > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Ray Cote, President Appropriate Solutions, Inc. We Build Software 603.924.6079 From js at globe.de Fri Aug 2 09:17:35 2013 From: js at globe.de (Schneider) Date: Fri, 02 Aug 2013 15:17:35 +0200 Subject: Python performance In-Reply-To: <24373948.472.1375448972239.JavaMail.rgacote@raymac.appropriatesolutions.com> References: <24373948.472.1375448972239.JavaMail.rgacote@raymac.appropriatesolutions.com> Message-ID: <51FBB16F.3070400@globe.de> On 08/02/2013 03:09 PM, Ray Cote wrote: > ----- Original Message ----- >> From: "Schneider" >> To: python-list at python.org >> Sent: Friday, August 2, 2013 8:00:09 AM >> Subject: Python performance >> >> Hi list, >> >> I have to write a small SMTP-Relay script (+ some statistic infos) >> and >> I'm wondering, if this >> can be done in python (in terms of performance, of course not in >> terms >> of possi bility ;) ). >> >> It has to handle around 2000 mails per hour for at least 8hours a day >> (which does not mean, that it is allowed not to respond the rest of >> the day. >> > A quick calculation shows that 2,000 mails per hour is less than 1 email every second. > Plenty of time to handle individual emails. This is just true, if the mails arrive equally distributed. We cannot ensure this. From invalid at invalid.invalid Fri Aug 2 10:04:00 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Fri, 2 Aug 2013 14:04:00 +0000 (UTC) Subject: Python performance References: Message-ID: On 2013-08-02, Schneider wrote: > I have to write a small SMTP-Relay script (+ some statistic infos) > and I'm wondering, if this can be done in python (in terms of > performance, of course not in terms of possibility ;) ). > > It has to handle around 2000 mails per hour for at least 8hours a day > (which does not mean, that it is allowed not to respond the rest of > the day. > > Can this be done? That's only a little over 2 per second, so I doubt Python is going to be the bottleneck. -- Grant Edwards grant.b.edwards Yow! I can't decide which at WRONG TURN to make first!! gmail.com I wonder if BOB GUCCIONE has these problems! From invalid at invalid.invalid Fri Aug 2 10:06:38 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Fri, 2 Aug 2013 14:06:38 +0000 (UTC) Subject: Python performance References: Message-ID: On 2013-08-02, Grant Edwards wrote: > On 2013-08-02, Schneider wrote: > >> I have to write a small SMTP-Relay script (+ some statistic infos) >> and I'm wondering, if this can be done in python (in terms of >> performance, of course not in terms of possibility ;) ). >> >> It has to handle around 2000 mails per hour for at least 8hours a day >> (which does not mean, that it is allowed not to respond the rest of >> the day. >> >> Can this be done? > > That's only a little over 2 per second, so I doubt Python is going to > be the bottleneck. Doh! I meant 2 seconds per message. That's certainly not a problem. But, as others have pointed out it's probably the peak rate not the average rate that matters. -- Grant Edwards grant.b.edwards Yow! Actually, what I'd at like is a little toy gmail.com spaceship!! From wayne at waynewerner.com Sat Aug 3 09:09:13 2013 From: wayne at waynewerner.com (Wayne Werner) Date: Sat, 3 Aug 2013 08:09:13 -0500 (CDT) Subject: Python performance In-Reply-To: <51FB9F49.9040209@globe.de> References: <51FB9F49.9040209@globe.de> Message-ID: On Fri, 2 Aug 2013, Schneider wrote: > Hi list, > > I have to write a small SMTP-Relay script (+ some statistic infos) and > I'm wondering, if this > can be done in python (in terms of performance, of course not in terms > of possibility ;) ). > > It has to handle around 2000 mails per hour for at least 8hours a day > (which does not mean, that it is allowed not to respond the rest of the day. > > Can this be done? or should I better use some other programming language? > My second choice would be erlang. Check out Kenneth Rietz's inbox.py[1] "It's quite quick. One instance should handle over one thousand emails per second." So it should be able to handle your hour's load in oh, say 2 seconds? ;) HTH, W [1]: https://crate.io/packages/inbox/ From stefan_ml at behnel.de Sat Aug 3 13:04:17 2013 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sat, 03 Aug 2013 19:04:17 +0200 Subject: Python performance In-Reply-To: References: <51FB9F49.9040209@globe.de> Message-ID: Wayne Werner, 03.08.2013 15:09: > On Fri, 2 Aug 2013, Schneider wrote: >> I have to write a small SMTP-Relay script (+ some statistic infos) and >> I'm wondering, if this >> can be done in python (in terms of performance, of course not in terms of >> possibility ;) ). >> >> It has to handle around 2000 mails per hour for at least 8hours a day >> (which does not mean, that it is allowed not to respond the rest of > the day. >> >> Can this be done? or should I better use some other programming language? >> My second choice would be erlang. > > Check out Kenneth Rietz's inbox.py[1] > > "It's quite quick. One instance should handle over one thousand emails per > second." Well, this has little to do with Kenneth Reitz. The smtpd implementation is part of the stdlib in Python. It uses asyncore and asynchat to scale, so you can generally expect it to be fast enough for the load in question. Working with asyncore and asynchat isn't the most pleasant that Python can offer, but given that it's there and has been for years, it should be pretty ok. Alternatively, Twisted also has a server+client implementation of SMTP: https://twistedmatrix.com/trac/wiki/TwistedMail Haven't used either of the two yet, though, so I can't say which is a better start for the problem at hand. Stefan From sylvain.thenault at logilab.fr Fri Aug 2 09:13:22 2013 From: sylvain.thenault at logilab.fr (Sylvain =?utf-8?B?VGjDqW5hdWx0?=) Date: Fri, 2 Aug 2013 15:13:22 +0200 Subject: Astroid 1.0 released Message-ID: <20130802131322.GD2580@logilab.fr> Astroid_ is the new name of former logilab-astng library. It's an AST library, used as the basis of Pylint_ and including Python 2.5 -> 3.3 compatible tree representation, statical type inference and other features useful for advanced Python code analysis, such as an API to provide extra information when statistical inference can't overcome Python dynamic nature (see the `pylint-brain`_ project for instance). It has been renamed and hosted to bitbucket to make clear that this is not a Logilab dedicated project but a community project that could benefit to any people manipulating Python code (statistical analysis tools, IDE, browser, etc). Documentation is a bit rough but should quickly improve. Also a dedicated web-site is now online, visit www.astroid.org (or https://bitbucket.org/logilab/astroid for development). You may download and install it from Pypi or from `Logilab's debian repositories`_. .. _Pylint: http://www.pylint.org .. _`Logilab's debian repositories`: http://www.logilab.org/bookmark/4621/follow .. _Astroid: http://www.astroid.org .. _`pylint-brain`: https://bitbucket.org/logilab/pylint-brain Enjoy! -- Sylvain Th?nault, LOGILAB, Paris (01.45.32.03.12) - Toulouse (05.62.17.16.42) Formations Python, Debian, M?th. Agiles: http://www.logilab.fr/formations D?veloppement logiciel sur mesure: http://www.logilab.fr/services CubicWeb, the semantic web framework: http://www.cubicweb.org From kevin4fong at gmail.com Fri Aug 2 20:40:52 2013 From: kevin4fong at gmail.com (kevin4fong at gmail.com) Date: Fri, 2 Aug 2013 17:40:52 -0700 (PDT) Subject: Python: Code is ignoring the if and else Message-ID: I'm trying to create a game of Go Fish in Python. But I've stumbled onto a little problem that I can't seem to figure out how to deal with. There is a human player (player 0) and three computer players (from 1-3). The human player goes first and chooses a target player. And then a card rank (for example, the player could target player two and choose jacks, then the computer would have to give the player all its jacks). What I have so far is below but the problem I'm having is right at the bottom of the code. So far, the code generates a deck, creates hands for every player, and then shows the player his/her cards. Then the player is asked which computer player he/she wants to target as well as the rank of cards. The problem I'm having is with the last set of lines (the def player_0_hitman) at the bottom of the code. Any help would be much appreciated. There are basically three issues I'm having problems with. Basically, my code is ignoring the if's and else's. I don't get why. Everything appears to be positioned correctly, but for some odd reason, even after an if, the program also runs the else as well. the "hit" is not being returned. Even though in the definition, I have the hit set to hit = hit - 1 for the last else, it still reruns the whole definition again as if it the hit was 1 I'm trying to use the count line to count how many cards are being transferred so the program will tell the player how many cards he gains when he gets a successful guess but I only get a statement saying I got 1 card each time no matter what (whether I get no cards or I get more than one). I understand the basics of what I need to do but I can't seem to get a working code for this. I've tried changing the "for" to "if" but I get all sorts of errors so I don't think that will work. Then I tried converting "hit" into another code before entering the definition, changing it while inside, then converting it back before returning it but that also seems to do nothing, and I still get the same issues. CODE: import random def SetDeck(): suitList = ["s", "c", "d", "h"] rankList = ["a", "2", "3", "4", "5", "6", "7", "8", "9", "t", "j", "q", "k"] deck = [] for suite in range(4): for rank in range(13): deck.append(rankList[rank]+suitList[suite]) return deck def Shuffle(deck): nCards = len(deck) for i in range(nCards): j = random.randint(i,nCards-1) deck[i], deck[j] = deck[j], deck[i] return deck def GetTopCard(shuffledDeck): return shuffledDeck.pop(0) def DealInitialCards(nPlayers,nCards,shuffledDeck): playersCards = [["" for j in range(nCards)] for i in range(nPlayers)] for j in range(nCards): for i in range(nPlayers): playersCards[i][j] = GetTopCard(shuffledDeck) return playersCards def Sort(cards): rankString = "a23456789tjqk" swapped=True while swapped: swapped = False for i in range(len(cards)-1): if rankString.find(cards[i][0])>rankString.find(cards[i+1][0]): cards[i],cards[i+1]=cards[i+1],cards[i] swapped = True return def ShowCards(player,cards): print("\n****************************************************") print("************Player "+str(player)+" has**************") print("****************************************************\n") for i in range(len(cards)): print(cards[i]), print("\n") return def ShowMessage(msg): print("****************************************************") print(str(msg)) print("****************************************************\n") return def remove_suits(player): new_hand = [] for card in pHands[player]: new_card = card[0] new_hand.append(new_card) return new_hand def choosing_target(): target_player = raw_input ("Who do you want to ask? (1-3)") while target_player.isdigit() == False or 1 > int(target_player) or 3 < int(target_player): print "Error: Must type a valid player id (from 1 to 3)" target_player = raw_input ("Who do you want to ask? (1-3)") target_player = int(target_player) return target_player def target_rank(): target_card = raw_input ("What rank are you seeking? (" + str(",".join(new_hand)) + ")") while target_card not in new_hand: print "Error: Must type one of the follow valid single character card ranks" print str(",".join(new_hand)) target_card = raw_input ("What rank are you seeking? (" + str(",".join(new_hand)) + ")") return target_card print("~"*70) print("~"*25+"WELCOME TO GO FISH!"+"~"*26) print("~"*70) nPlayers = 4 nCards = 10 deck = SetDeck() sDeck = Shuffle(deck[:]) pHands = DealInitialCards(nPlayers,nCards,sDeck) Sort(pHands[0]) ShowCards(0,pHands[0]) hit = 1 while hit == 1 : ShowMessage("TURN: Player 0, its your turn.") target_player = choosing_target() new_hand = remove_suits(0) target_card = target_rank() ShowMessage("Target: Player " + str(target_player) + " is being targeted for the rank <" + str(target_card) + ">") temp_hit = player_0_hitman(hit) print "hit = " + str(hit) def player_0_hitman(hit): for card in pHands[target_player]: if target_card[0] == card[0]: count = pHands[target_player].count(card) pHands[0].append(card) pHands[target_player].remove(card) ShowMessage("HIT: " + str(count) + " card(s) transferred") else: if target_card[0] != card[0]: top_card = GetTopCard(sDeck) pHands[0].append(top_card) if top_card[0] == target_card[0]: ShowMessage("HIT: LUCKILY Player 0 has fished up a rank <" + str(top_card[0]) + ">!!!") else: ShowMessage("MISS: You fished up the rank <" + str(top_card[0]) + ">") hit = hit - 1 return hit From python at mrabarnett.plus.com Fri Aug 2 21:38:02 2013 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 03 Aug 2013 02:38:02 +0100 Subject: Python: Code is ignoring the if and else In-Reply-To: References: Message-ID: <51FC5EFA.5090108@mrabarnett.plus.com> On 03/08/2013 01:40, kevin4fong at gmail.com wrote: > I'm trying to create a game of Go Fish in Python. But I've stumbled onto a little problem that I can't seem to figure out how to deal with. > > There is a human player (player 0) and three computer players (from 1-3). The human player goes first and chooses a target player. And then a card rank (for example, the player could target player two and choose jacks, then the computer would have to give the player all its jacks). > > What I have so far is below but the problem I'm having is right at the bottom of the code. So far, the code generates a deck, creates hands for every player, and then shows the player his/her cards. Then the player is asked which computer player he/she wants to target as well as the rank of cards. > > The problem I'm having is with the last set of lines (the def player_0_hitman) at the bottom of the code. Any help would be much appreciated. There are basically three issues I'm having problems with. > > Basically, my code is ignoring the if's and else's. I don't get why. Everything appears to be positioned correctly, but for some odd reason, even after an if, the program also runs the else as well. > > the "hit" is not being returned. Even though in the definition, I have the hit set to hit = hit - 1 for the last else, it still reruns the whole definition again as if it the hit was 1 > > I'm trying to use the count line to count how many cards are being transferred so the program will tell the player how many cards he gains when he gets a successful guess but I only get a statement saying I got 1 card each time no matter what (whether I get no cards or I get more than one). > > I understand the basics of what I need to do but I can't seem to get a working code for this. I've tried changing the "for" to "if" but I get all sorts of errors so I don't think that will work. Then I tried converting "hit" into another code before entering the definition, changing it while inside, then converting it back before returning it but that also seems to do nothing, and I still get the same issues. > [snip] > def player_0_hitman(hit): > for card in pHands[target_player]: > if target_card[0] == card[0]: > count = pHands[target_player].count(card) > pHands[0].append(card) > pHands[target_player].remove(card) > ShowMessage("HIT: " + str(count) + " card(s) transferred") > else: > if target_card[0] != card[0]: > top_card = GetTopCard(sDeck) > pHands[0].append(top_card) > if top_card[0] == target_card[0]: > ShowMessage("HIT: LUCKILY Player 0 has fished up a rank <" + str(top_card[0]) + ">!!!") > else: > ShowMessage("MISS: You fished up the rank <" + str(top_card[0]) + ">") > hit = hit - 1 > return hit > You have "else" lined up with "for". In Python a "for" loop can have an "else" clause, which is run if it didn't "break" out of the loop but finished. From john_ladasky at sbcglobal.net Fri Aug 2 21:39:43 2013 From: john_ladasky at sbcglobal.net (John Ladasky) Date: Fri, 2 Aug 2013 18:39:43 -0700 (PDT) Subject: Python: Code is ignoring the if and else In-Reply-To: References: Message-ID: On Friday, August 2, 2013 5:40:52 PM UTC-7, kevin... at gmail.com wrote: > Basically, my code is ignoring the if's and else's. I don't get why. > Everything appears to be positioned correctly, but for some odd reason, even > after an if, the program also runs the else as well. Look carefully at your indentation. One "else" statement is at the same indentation as a "for" statement rather than an "if" statement. So what, you say? http://docs.python.org/2/tutorial/controlflow.html "Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement." I don't know of any other computer programming language besides Python which has the "for...break...else" idiom. However, I know quite a few that do not. I find it useful in many situations. From kevin4fong at gmail.com Fri Aug 2 21:44:44 2013 From: kevin4fong at gmail.com (kevin4fong at gmail.com) Date: Fri, 2 Aug 2013 18:44:44 -0700 (PDT) Subject: Python: Code is ignoring the if and else In-Reply-To: References: Message-ID: On Friday, August 2, 2013 6:39:43 PM UTC-7, John Ladasky wrote: > On Friday, August 2, 2013 5:40:52 PM UTC-7, kevin... at gmail.com wrote: > > > Basically, my code is ignoring the if's and else's. I don't get why. > > > Everything appears to be positioned correctly, but for some odd reason, even > > > after an if, the program also runs the else as well. > > > > Look carefully at your indentation. One "else" statement is at the same indentation as a "for" statement rather than an "if" statement. So what, you say? > > > > http://docs.python.org/2/tutorial/controlflow.html > > > > "Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement." > > > > I don't know of any other computer programming language besides Python which has the "for...break...else" idiom. However, I know quite a few that do not. I find it useful in many situations. Yeah, I already know about that. But if I try to change it, I'm not even able to start the program. If I try to change the if statement that it corresponds with, I get a an error saying "card" is not a global. And if I try to shift it in, for some reason...the program runs through the MISS line multiple times. From rosuav at gmail.com Fri Aug 2 21:56:26 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 3 Aug 2013 02:56:26 +0100 Subject: Python: Code is ignoring the if and else In-Reply-To: References: Message-ID: On Sat, Aug 3, 2013 at 2:44 AM, wrote: > Yeah, I already know about that. But if I try to change it, I'm not even able to start the program. If I try to change the if statement that it corresponds with, I get a an error saying "card" is not a global. And if I try to shift it in, for some reason...the program runs through the MISS line multiple times. Okay. Stop, take a step back, and simplify your problems. You're currently exhibiting a technique of shotgun programming that may be getting in your way; you're just trying things without really knowing what you're doing. Play with individual control structures in interactive Python (eg IDLE), and get to know what's really happening. Read the docs. Be sure you're structuring your code the way you think you are. You'll find everything easier once you understand why your code is doing what it's doing. ChrisA From joshua at landau.ws Fri Aug 2 22:11:37 2013 From: joshua at landau.ws (Joshua Landau) Date: Sat, 3 Aug 2013 03:11:37 +0100 Subject: Python: Code is ignoring the if and else In-Reply-To: References: Message-ID: On 3 August 2013 02:44, wrote: > Yeah, I already know about that. But if I try to change it, I'm not even > able to start the program. If I try to change the if statement that it > corresponds with, I get a an error saying "card" is not a global. And if I > try to shift it in, for some reason...the program runs through the MISS > line multiple times. > You have a car with a broken engine and a broken tire and are telling us that you refuse to fix the engine because it highlights the problem of the broken tire. Take the fix and move on to the next problem. One piece of advice is about scoping. This is perhaps the hardest "gotcha" of Python conceptually, but it's sensible once you understand the justifications. Run the four commands below and try and understand why this applies to your code. a = 1 def access_global(): print(a) def set_variable(): a = 2 print(a) def broken_set(): a = a + 1 print(a) def also_broken(): print(a) return a = 1 # Never run! The fix for the broken variants is to start the functions with "global a". -------------- next part -------------- An HTML attachment was scrubbed... URL: From kevin4fong at gmail.com Fri Aug 2 22:24:56 2013 From: kevin4fong at gmail.com (kevin4fong at gmail.com) Date: Fri, 2 Aug 2013 19:24:56 -0700 (PDT) Subject: Python: Code is ignoring the if and else In-Reply-To: References: Message-ID: <0b558115-68c4-460b-863c-fef4b844bb57@googlegroups.com> On Friday, August 2, 2013 7:11:37 PM UTC-7, Joshua Landau wrote: > On 3 August 2013 02:44, wrote: > > > > Yeah, I already know about that. But if I try to change it, I'm not even able to start the program. If I try to change the if statement that it corresponds with, I get a an error saying "card" is not a global. And if I try to shift it in, for some reason...the program runs through the MISS line multiple times. > > > > > > You have a car with a broken engine and a broken tire and are telling us that you refuse to fix the engine because it highlights the problem of the broken tire. > > > Take the fix and move on to the next problem. > > > > > One piece of advice is about scoping. This is perhaps the hardest "gotcha" of Python conceptually, but it's sensible once you understand the justifications. > > > Run the four commands below and try and understand why this applies to your code. > > > > > a = 1 > > > def access_global(): > ? ? print(a) > > > def set_variable(): > ? ? a = 2 > ? ? print(a) > > > def broken_set(): > > > ? ? a = a + 1 > ? ? print(a) > > > def also_broken(): > ? ? print(a) > ? ? return > ? ? a = 1 # Never run! > > > The fix for the broken variants is to start the functions with "global a". I'll take a look at those. I used that fix you brought up as well but the main issue I get after that is: I'll try to explain the main issue I'm having as accurately as possible. Basically, I've changed the indentations around but the main issue I get is that the GetTopCard(sDeck) line is being run multiple times for some reason. So when I run the program, I get "MISS: You fished up the rank......" multiple times and my list pHands[0] is increased by several number combinations when I only want it to increase by one. I also updated that last definition in my main code. def player_0_hitman(hit): for card in pHands[target_player]: if target_card[0] == card[0]: count = pHands[target_player].count(card) pHands[0].append(card) pHands[target_player].remove(card) ShowMessage("HIT: " + str(count) + " card(s) transferred") else: top_card = GetTopCard(sDeck) pHands[0].append(top_card) if top_card[0] == target_card[0]: ShowMessage("HIT: LUCKILY Player 0 has fished up a rank <" + str(top_card[0]) + ">!!!") else: ShowMessage("MISS: You fished up the rank <" + str(top_card[0]) + ">") hit = hit - 1 From tjreedy at udel.edu Sat Aug 3 01:04:56 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 03 Aug 2013 01:04:56 -0400 Subject: Python: Code is ignoring the if and else In-Reply-To: <0b558115-68c4-460b-863c-fef4b844bb57@googlegroups.com> References: <0b558115-68c4-460b-863c-fef4b844bb57@googlegroups.com> Message-ID: On 8/2/2013 10:24 PM, kevin4fong at gmail.com wrote: Looking at this again, I believe you actually had the structure almost right before. You want to look through *all* of the target players cards and if *none* of them match, (ie the search fails), you want to draw 1 card. What you were missing before is break or return def player_0_hitman(hit): for card in pHands[target_player]: if target_card[0] == card[0]: count = pHands[target_player].count(card) pHands[0].append(card) pHands[target_player].remove(card) ShowMessage("HIT: " + str(count) + " card(s) transferred") return True # else: needed if just break above, but not with return top_card = GetTopCard(sDeck) pHands[0].append(top_card) if top_card[0] == target_card[0]: ShowMessage("HIT: LUCKILY Player 0 has fished up a rank <" + str(top_card[0]) + ">!!!") return True else: ShowMessage("MISS: You fished up the rank <" + str(top_card[0]) + ">") hit = hit - 1 return False The returns are based on what I remember of the rules from decades ago, that a hit either in the hand or the draw allowed another turn by the player. -- Terry Jan Reedy From kevin4fong at gmail.com Sat Aug 3 16:12:17 2013 From: kevin4fong at gmail.com (kevin4fong at gmail.com) Date: Sat, 3 Aug 2013 13:12:17 -0700 (PDT) Subject: Python: Code is ignoring the if and else In-Reply-To: References: <0b558115-68c4-460b-863c-fef4b844bb57@googlegroups.com> Message-ID: <65bd3363-8d39-4713-8e31-13ec8629f3a8@googlegroups.com> On Friday, August 2, 2013 10:04:56 PM UTC-7, Terry Reedy wrote: > On 8/2/2013 10:24 PM, kevin4fong at gmail.com wrote: > > > > Looking at this again, I believe you actually had the structure almost > > right before. You want to look through *all* of the target players cards > > and if *none* of them match, (ie the search fails), you want to draw 1 > > card. What you were missing before is break or return > > > > def player_0_hitman(hit): > > for card in pHands[target_player]: > > if target_card[0] == card[0]: > > count = pHands[target_player].count(card) > > pHands[0].append(card) > > pHands[target_player].remove(card) > > ShowMessage("HIT: " + str(count) + " card(s) transferred") > > return True > > > > # else: needed if just break above, but not with return > > > > top_card = GetTopCard(sDeck) > > pHands[0].append(top_card) > > if top_card[0] == target_card[0]: > > ShowMessage("HIT: LUCKILY Player 0 has fished up a rank <" + > > str(top_card[0]) + ">!!!") > > return True > > else: > > ShowMessage("MISS: You fished up the rank <" + > > str(top_card[0]) + ">") > > hit = hit - 1 > > return False > > > > The returns are based on what I remember of the rules from decades ago, > > that a hit either in the hand or the draw allowed another turn by the > > player. > > > > -- > > Terry Jan Reedy Thank you, that information worked quite well and is much appreciated. From tjreedy at udel.edu Fri Aug 2 21:42:30 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 02 Aug 2013 21:42:30 -0400 Subject: Python: Code is ignoring the if and else In-Reply-To: References: Message-ID: Nonsense: they are executed just as you ask, even though what you ask is not what you meant. On 8/2/2013 8:40 PM, kevin4fong at gmail.com wrote: > def player_0_hitman(hit): > for card in pHands[target_player]: > if target_card[0] == card[0]: > count = pHands[target_player].count(card) > pHands[0].append(card) > pHands[target_player].remove(card) > ShowMessage("HIT: " + str(count) + " card(s) transferred") > else: else: # indent to match if > if target_card[0] != card[0]: # delete this if line, see below. > top_card = GetTopCard(sDeck) > pHands[0].append(top_card) > if top_card[0] == target_card[0]: > ShowMessage("HIT: LUCKILY Player 0 has fished up a rank <" + str(top_card[0]) + ">!!!") > else: > ShowMessage("MISS: You fished up the rank <" + str(top_card[0]) + ">") > hit = hit - 1 > return hit This executes the for loop multiple times and the the else: clause of the *for* statement (see the ref manual). I believe you want the whole else: clause indented so that it will be executed when the if condition is false. If so, the second if is redundant and should just be removed. -- Terry Jan Reedy From kevin4fong at gmail.com Fri Aug 2 21:46:24 2013 From: kevin4fong at gmail.com (kevin4fong at gmail.com) Date: Fri, 2 Aug 2013 18:46:24 -0700 (PDT) Subject: Python: Code is ignoring the if and else In-Reply-To: References: Message-ID: <3ded73b2-e493-4726-9272-dd79610cd834@googlegroups.com> On Friday, August 2, 2013 6:42:30 PM UTC-7, Terry Reedy wrote: > Nonsense: they are executed just as you ask, even though what you ask is > > not what you meant. > > > > On 8/2/2013 8:40 PM, kevin4fong at gmail.com wrote: > > > > > def player_0_hitman(hit): > > > for card in pHands[target_player]: > > > if target_card[0] == card[0]: > > > count = pHands[target_player].count(card) > > > pHands[0].append(card) > > > pHands[target_player].remove(card) > > > ShowMessage("HIT: " + str(count) + " card(s) transferred") > > > else: > > else: # indent to match if > > > if target_card[0] != card[0]: > > # delete this if line, see below. > > > > > top_card = GetTopCard(sDeck) > > > pHands[0].append(top_card) > > > if top_card[0] == target_card[0]: > > > ShowMessage("HIT: LUCKILY Player 0 has fished up a rank <" + str(top_card[0]) + ">!!!") > > > else: > > > ShowMessage("MISS: You fished up the rank <" + str(top_card[0]) + ">") > > > hit = hit - 1 > > > return hit > > > > This executes the for loop multiple times and the the else: clause of > > the *for* statement (see the ref manual). I believe you want the whole > > else: clause indented so that it will be executed when the if condition > > is false. If so, the second if is redundant and should just be removed. > > Yeah, thanks for the advice. But unfortunately, I already tried that and I ended up with a whole bunch of new errors. > > -- > > Terry Jan Reedy From davea at davea.name Fri Aug 2 23:34:56 2013 From: davea at davea.name (Dave Angel) Date: Sat, 3 Aug 2013 03:34:56 +0000 (UTC) Subject: Python: Code is ignoring the if and else References: Message-ID: kevin4fong at gmail.com wrote: > I'm trying to create a game of Go Fish in Python. But I've stumbled onto a little problem that I can't seem to figure out how to deal with. > Please list the program the way you are actually running it. The present one will not run very long before producing the error: Traceback (most recent call last): File "kevin.py", line 100, in temp_hit = player_0_hitman(hit) NameError: name 'player_0_hitman' is not defined Next, tell us the environment it runs in. In this case that means the version of Python you're using. In some places you include parens around the print arguments, like needed in Python 3, but in others your code could only work up to version 2.7 So we have to do some detective work just to discover you're using version 2.x Next, when telling us of an error, quote the exact traceback, don't just say something like: "I get a an error saying "card" is not a global" Next, what on earth does the following mean: " And if I try to shift it in" ?? Are you perhaps referring to Perl grammar, or to DOS batch files? Next, comments like: "tried that and I ended up with a whole bunch of new errors" don't tell us much. Likewise " I'm not even able to start the program. If I try to change the if statement that it corresponds with" The last noun was "program" so I don't know which of the program's if statements you're changing. There are multiple things wrong with the code, and you can't expect any one of us to be able to spot them all. Some are clear by inspection, and others would probably require actually running the code, after you fix the problem with player_0_hitman above. Example of inspection: you have the line: temp_hit = player_0_hitman(hit) but never actually use the temp_hit variable. Similarly, the line: pHands[target_player].remove(card) is going to mess up the loop it's in, When you're iterating over a list, you can't normally add or remove items from that list. Even after you fix that, the loop is still all wrong. The count method will never get more than one, since all the cards are unique. You want to count up the cards that match a particular rank, regardless of suit. And that loop effectively does that, transferring one at a time. But you don't do anything to keep track of how many you transferred. As for the misplaced else, it doesn't belong there at all. What you presumably want to do is after exiting the loop, you want to check the corrected count value to decide how many cards were transferred. If that is non zero, then the logic should be executed, including decrementing the hit value and returning it. Is this your own program? If so, you should be able to siimplify it to ask a question. Few of us are willing to figure out the logic of go-fish, but many of us are willing to figure out what indentation makes sense for a given function. -- DaveA From me at davecotter.com Fri Aug 2 20:55:05 2013 From: me at davecotter.com (David M. Cotter) Date: Fri, 2 Aug 2013 17:55:05 -0700 (PDT) Subject: PyArg_ParseTuple() when the type could be anything? Message-ID: I'd like to be able to use PyArg_ParseTuple() in a generic way. for example, i'd like to have all commands start with 1 integer parameter, and this "commandID" will inform me of what parameters come next (via LUT). knowing that i can then call ParseTuple again with the proper parameters. like this: if (PyArg_ParseTuple(args, "i|", &commandID)) { switch (commandID) { case cmd_with_str: { const char *strZ = NULL; if (PyArg_ParseTuple(args, "is", &commandID, &strZ)) { // do something with string } break; } case cmd_with_float: { float valF = -1; if (PyArg_ParseTuple(args, "if", &commandID, &valF)) { // do something with float } break; } } } is there a way to achieve this? the "i|" at the start is not working From stefan_ml at behnel.de Sat Aug 3 10:31:37 2013 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sat, 03 Aug 2013 16:31:37 +0200 Subject: PyArg_ParseTuple() when the type could be anything? In-Reply-To: References: Message-ID: David M. Cotter, 03.08.2013 02:55: > I'd like to be able to use PyArg_ParseTuple() in a generic way. > > for example, i'd like to have all commands start with 1 integer parameter, and this "commandID" will inform me of what parameters come next (via LUT). > > knowing that i can then call ParseTuple again with the proper parameters. > > like this: > > if (PyArg_ParseTuple(args, "i|", &commandID)) { > > switch (commandID) { > > case cmd_with_str: { > const char *strZ = NULL; > > if (PyArg_ParseTuple(args, "is", &commandID, &strZ)) { > // do something with string > } > break; > } > > case cmd_with_float: { > float valF = -1; > > if (PyArg_ParseTuple(args, "if", &commandID, &valF)) { > // do something with float > } > break; > } > } > } > > is there a way to achieve this? the "i|" at the start is not working If you're willing to switch to Cython, here's an (untested) example: cdef enum: cmd_with_str = 1 cmd_with_float = 2 cdef int command_id = args[0] if command_id == cmd_with_str: str_z = args[1] # it's an object, so use it as such print(str_z) elif command_id == cmd_with_float: val_f = args[1] # converting to C float here ... else: raise ValueError("unknown command") Two comments: 1) you can obviously do the same in C, by writing a bit more code. It would likely be a lot slower, though, and you'd have to take care of error handling etc. 2) you might want to rethink your design as this is a rather unpythonic API. Although it depends on who (or what) you are expecting to use it. Stefan From me at davecotter.com Mon Aug 5 18:55:59 2013 From: me at davecotter.com (David M. Cotter) Date: Mon, 5 Aug 2013 15:55:59 -0700 (PDT) Subject: PyArg_ParseTuple() when the type could be anything? In-Reply-To: References: Message-ID: <7756ab34-3545-4dba-8a7e-c94d469681ce@googlegroups.com> i was able to get what i wanted by simply iterating over the tupile instead of using ParseTupile, then just query the type, then convert the type to C and move on to the next. totally great, now i can pass N different argument types to a single function, and have the C side deal gracefully with whatever types are sent. From arnaud.fontaine at nexedi.com Fri Aug 2 21:01:19 2013 From: arnaud.fontaine at nexedi.com (Arnaud Fontaine) Date: Sat, 03 Aug 2013 10:01:19 +0900 Subject: Dealing with ImportLock deadlock in Import Hooks Message-ID: <87fvurvb8g.fsf@duckcorp.org> Hello, I'm currently working on implementing Import Hooks (PEP302) with Python 2.7 to be able to import modules whose code is in ZODB. However, I have stumbled upon a widely known issue about import deadlock[0][1] (note that this issue is not directly related to ZODB, but a more general question about dealing with ImportLock deadlock for Import Hooks), basically: Thread 1 is trying to import a module 'foo.bar' (where 'foo' is a package containing dynamic modules) handled by Import Hooks I implemented, so ImportLock is acquired before even running the hooks (Python/import.c:PyImport_ImportModuleLevel()). Then, these import hooks try to load objects from ZODB and a request is sent and handled by another thread (Thread 2) which itself tries to import another module. Of course, this causes a deadlock because the first thread still holds ImportLock. I have thought about the following solutions: * Backport the patch applied in python 3.3 from issue 9260[0]. This would be the best option because it would mean that even when trying to import any module from package 'foo', other modules and packages can be imported, which would solve my issue. However, I'm not sure it could be released into python 2.7? * Within the hooks, protect the Import Hooks with a separate lock for the loader method. This would prevent any other thread to import any modules from 'foo' package but still allows to call the finder method (ignoring module fullname not starting with 'foo') along with other finder methods, so that other ZODB modules can be imported. Then, in the loader method, until the module is actually inserted into sys.modules and then other load_module() PEP302 responsabilities being taken care of (such as exec the code), release the ImportLock so that Thread 2 can process requests and send objects back to Thread 1. However, even after trying to understand import.c, I'm not sure at all that this is enough and that releasing ImportLock would not have nasty side-effects, any thoughts about that? * Fix the code in ZODB to not avoid import but to me this seems like a dirty hack because it could happen again and I would prefer to fix this issue once and for all. Any thoughts or suggestion welcome, thanks! Regards, -- Arnaud Fontaine [0] http://bugs.python.org/issue9260 [1] http://mail.python.org/pipermail/python-dev/2003-February/033436.html From alokmahor at gmail.com Sat Aug 3 00:22:45 2013 From: alokmahor at gmail.com (Alok Singh Mahor) Date: Sat, 3 Aug 2013 09:52:45 +0530 Subject: web development in python without using any webframework Message-ID: Hello everyone, few months back I started learning python and now I got nice familiarity. now i want to use python for creating dynamic database driven websites. and I dont want to use existing web frameworks for my work. I am learning things so I wont feel lazy to write all the code myself because I want to learn. could anyone suggest me any books/site from where I can start. I want to follow MVC architecture. so please suggest me some links/book or anything thank you in advance -------------- next part -------------- An HTML attachment was scrubbed... URL: From rodrick.brown at gmail.com Sat Aug 3 01:15:17 2013 From: rodrick.brown at gmail.com (Rodrick Brown) Date: Sat, 3 Aug 2013 01:15:17 -0400 Subject: web development in python without using any webframework In-Reply-To: References: Message-ID: <-3586102023561772528@unknownmsgid> On Aug 3, 2013, at 12:37 AM, Alok Singh Mahor wrote: > Hello everyone, > few months back I started learning python and now I got nice familiarity. now i want to use python for creating dynamic database driven websites. and I dont want to use existing web frameworks for my work. I am learning things so I wont feel lazy to write all the code myself because I want to learn. > > could anyone suggest me any books/site from where I can start. I want to follow MVC architecture. so please suggest me some links/book or anything > > thank you in advance I believe the best way to go about this is by reading and learning from other frameworks and learning how others implement core aspects of a particular approach or routine by building dynamic web frameworks. This will not only will help you learn from a working example but you will also learn how to improve upon things and make improvements over time. I would start with a simple framework like flask. Flask is extremely simple easy to use and high extendable. If one wanted to learn about building cars they wouldn't start off with spare car-parts and a engine manual -- you would be better of taking apart an old Civic and learning about the inner workings and try putting it back together this is just my 2 cents. Sent from my iPhone > -- > http://mail.python.org/mailman/listinfo/python-list From dieter at handshake.de Sat Aug 3 01:42:03 2013 From: dieter at handshake.de (dieter) Date: Sat, 03 Aug 2013 07:42:03 +0200 Subject: web development in python without using any webframework References: Message-ID: <87y58j8h5g.fsf@handshake.de> Alok Singh Mahor writes: > Hello everyone, > few months back I started learning python and now I got nice familiarity. > now i want to use python for creating dynamic database driven websites. and > I dont want to use existing web frameworks for my work. I am learning > things so I wont feel lazy to write all the code myself because I want to > learn. You likely want to use some (existing) web server (and not develop this as well)? In this case, you could look at WSGI ("Web Server Gateway Interface"). It is a specification intended to interface between a web server and a web application - with the motivation to facilitate factoring out common tasks (session handling, authentication, authorization, output transformations, ...). From jj at yuzao.org Sat Aug 3 01:32:07 2013 From: jj at yuzao.org (=?ISO-8859-1?B?amo=?=) Date: Sat, 3 Aug 2013 13:32:07 +0800 Subject: web development in python without using any webframework Message-ID: you could check the codes of flask ,bottle ,web.py to read codes ,the learn what you want to know . ------------------ Original ------------------ From: "Alok Singh Mahor"; Date: Sat, Aug 3, 2013 12:52 PM To: "python-list"; Subject: web development in python without using any webframework Hello everyone, few months back I started learning python and now I got nice familiarity. now i want to use python for creating dynamic database driven websites. and I dont want to use existing web frameworks for my work. I am learning things so I wont feel lazy to write all the code myself because I want to learn. could anyone suggest me any books/site from where I can start. I want to follow MVC architecture. so please suggest me some links/book or anything thank you in advance -------------- next part -------------- An HTML attachment was scrubbed... URL: From punk.sagar at gmail.com Sat Aug 3 02:18:47 2013 From: punk.sagar at gmail.com (punk.sagar at gmail.com) Date: Fri, 2 Aug 2013 23:18:47 -0700 (PDT) Subject: Correct Way to Write in Python Message-ID: Hi All, Im new to Python. Im coming from C# background and want to learn Python. I was used to do following thing in C# in my previous experiences. I want to know how do I implement below example in Python. How these things are done in Python. [code] public class Bank { public List lstCustomers = new List(); private string micrcode; public void Bank() { customer } } public class Customer { private srting customername; public string CustomerName { get { return customername; } set { customername = value; } } } main() { Customer objCustomer = new Customer; objCustomer.CustomerName = "XYZ" Bank objBank = new Bank(); objBank.lstCustomer.Add(objCustomer); } [/code] From __peter__ at web.de Sat Aug 3 02:47:49 2013 From: __peter__ at web.de (Peter Otten) Date: Sat, 03 Aug 2013 08:47:49 +0200 Subject: Correct Way to Write in Python References: Message-ID: punk.sagar at gmail.com wrote: > Hi All, > > Im new to Python. Im coming from C# background and want to learn Python. > I was used to do following thing in C# in my previous experiences. I want > to know how do I implement below example in Python. How these things are > done in Python. > [code] > public class Bank > { > > public List lstCustomers = new List(); > private string micrcode; > > public void Bank() > { > customer > } > > } > > public class Customer > { > private srting customername; > > public string CustomerName > > { > get { return customername; } > set { customername = value; } > } > } > > main() > { > Customer objCustomer = new Customer; > objCustomer.CustomerName = "XYZ" > > Bank objBank = new Bank(); > objBank.lstCustomer.Add(objCustomer); > > } > [/code] While I don't know C# I doubt that this is good C# code ;) Here's a moderately cleaned-up Python version: class DuplicateCustomerError(Exception): pass class Customer: def __init__(self, name): self.name = name class Bank: def __init__(self): self.customers = {} def add_customer(self, name): if name in self.customers: raise DuplicateCustomerError customer = Customer(name) self.customers[name] = customer return customer if __name__ == "__main__": bank = Bank() bank.add_customer("XYZ") I'm assuming a tiny bank where every customer has a unique name and only one program is running so that you can ignore concurrency issues. From punk.sagar at gmail.com Sat Aug 3 03:09:35 2013 From: punk.sagar at gmail.com (Sagar Varule) Date: Sat, 3 Aug 2013 00:09:35 -0700 (PDT) Subject: Correct Way to Write in Python In-Reply-To: References: Message-ID: On Saturday, August 3, 2013 12:17:49 PM UTC+5:30, Peter Otten wrote: > punk.sagar at gmail.com wrote: > > > > > Hi All, > > > > > > Im new to Python. Im coming from C# background and want to learn Python. > > > I was used to do following thing in C# in my previous experiences. I want > > > to know how do I implement below example in Python. How these things are > > > done in Python. > > > [code] > > > public class Bank > > > { > > > > > > public List lstCustomers = new List(); > > > private string micrcode; > > > > > > public void Bank() > > > { > > > customer > > > } > > > > > > } > > > > > > public class Customer > > > { > > > private srting customername; > > > > > > public string CustomerName > > > > > > { > > > get { return customername; } > > > set { customername = value; } > > > } > > > } > > > > > > main() > > > { > > > Customer objCustomer = new Customer; > > > objCustomer.CustomerName = "XYZ" > > > > > > Bank objBank = new Bank(); > > > objBank.lstCustomer.Add(objCustomer); > > > > > > } > > > [/code] > > > > While I don't know C# I doubt that this is good C# code ;) > > Here's a moderately cleaned-up Python version: > > > > class DuplicateCustomerError(Exception): > > pass > > > > class Customer: > > def __init__(self, name): > > self.name = name > > > > class Bank: > > def __init__(self): > > self.customers = {} > > def add_customer(self, name): > > if name in self.customers: > > raise DuplicateCustomerError > > customer = Customer(name) > > self.customers[name] = customer > > return customer > > > > if __name__ == "__main__": > > bank = Bank() > > bank.add_customer("XYZ") > > > > I'm assuming a tiny bank where every customer has a unique name and only one > > program is running so that you can ignore concurrency issues. Thanks a lot Peter. I appreciate your Help. You mentioned that C# code above is not good. If you can point me why it is not good, would help me learn new approaches as this type of Code I use to see long back(when i was fresher). There may be better approaches or concepts i am not aware of. If you can point me in that direction it would be gr8. From __peter__ at web.de Sat Aug 3 05:04:10 2013 From: __peter__ at web.de (Peter Otten) Date: Sat, 03 Aug 2013 11:04:10 +0200 Subject: Correct Way to Write in Python References: Message-ID: Sagar Varule wrote: > On Saturday, August 3, 2013 12:17:49 PM UTC+5:30, Peter Otten wrote: >> punk.sagar at gmail.com wrote: > Thanks a lot Peter. I appreciate your Help. You mentioned that C# code > above is not good. If you can point me why it is not good, would help me > learn new approaches as this type of Code I use to see long back(when i > was fresher). There may be better approaches or concepts i am not aware > of. If you can point me in that direction it would be gr8. As I said, I don't know C# -- but I already tried to fix some of the potential issues in my code snippet. - A list is not the best choice to store the customers -- there should be a lookup by some kind of ID (I picked the name to keep it simple) - Is it really necessary to expose that container in a language that provides "privacy"? - Is there ever a customer without name/ID? I'd say no, so these should be passed as constructor arguments. - Renaming a customer is a delicate process, you may need to keep track of the old name, the reason for the name, update your database etc., so I wouldn't allow setting the attribute and instead add a method Bank.rename_customer(...) or Bank.customers.rename_customer(...) Asking to translate code might make sense if you are a wizzard in the "other" language and want to see how a particular construct is written idomatically in Python, but to rewrite very basic C# code in Python is a bit like trying to learn a natural language by replacing one word after another in a text with the word in the new language that you looked up in a dict. The result tends to be underwhelming. I recommend that you read the tutorial and then try to solve a simple task in Python. Whenever you run into a problem you can come here for help. Because there's a real problem behind your code there will be different ways to solve it in Python, and you'll learn much more about the possibilites Python has to offer while your code gradually becomes more idiomatic. From punk.sagar at gmail.com Sat Aug 3 12:07:03 2013 From: punk.sagar at gmail.com (Sagar Varule) Date: Sat, 3 Aug 2013 09:07:03 -0700 (PDT) Subject: Correct Way to Write in Python In-Reply-To: References: Message-ID: On Saturday, August 3, 2013 2:34:10 PM UTC+5:30, Peter Otten wrote: > Sagar Varule wrote: > > > > > On Saturday, August 3, 2013 12:17:49 PM UTC+5:30, Peter Otten wrote: > > >> punk.sagar at gmail.com wrote: > > > > > Thanks a lot Peter. I appreciate your Help. You mentioned that C# code > > > above is not good. If you can point me why it is not good, would help me > > > learn new approaches as this type of Code I use to see long back(when i > > > was fresher). There may be better approaches or concepts i am not aware > > > of. If you can point me in that direction it would be gr8. > > > > As I said, I don't know C# -- but I already tried to fix some of the > > potential issues in my code snippet. > > > > - A list is not the best choice to store the customers -- there should be a > > lookup by some kind of ID (I picked the name to keep it simple) > > - Is it really necessary to expose that container in a language that > > provides "privacy"? > > - Is there ever a customer without name/ID? I'd say no, so these should be > > passed as constructor arguments. > > - Renaming a customer is a delicate process, you may need to keep track of > > the old name, the reason for the name, update your database etc., so I > > wouldn't allow setting the attribute and instead add a method > > > > Bank.rename_customer(...) > > > > or > > > > Bank.customers.rename_customer(...) > > > > Asking to translate code might make sense if you are a wizzard in the > > "other" language and want to see how a particular construct is written > > idomatically in Python, but to rewrite very basic C# code in Python is a bit > > like trying to learn a natural language by replacing one word after another > > in a text with the word in the new language that you looked up in a dict. > > The result tends to be underwhelming. > > > > I recommend that you read the tutorial and then try to solve a simple task > > in Python. Whenever you run into a problem you can come here for help. > > Because there's a real problem behind your code there will be different ways > > to solve it in Python, and you'll learn much more about the possibilites > > Python has to offer while your code gradually becomes more idiomatic. Thanks Peter for helping me out, Your Questions and suggestions are thoughts provoking and will help me every time I write a new Class. I will keep your suggestions. I am happy and amazed that Im getting help from strangers, But I got none when I approached programmers in my office....Thanks a Lot...!!!!! From steve+comp.lang.python at pearwood.info Sat Aug 3 04:20:41 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 Aug 2013 08:20:41 GMT Subject: Correct Way to Write in Python References: Message-ID: <51fcbd58$0$30000$c3e8da3$5496439d@news.astraweb.com> On Fri, 02 Aug 2013 23:18:47 -0700, punk.sagar wrote: > Hi All, > > Im new to Python. Im coming from C# background and want to learn Python. > I was used to do following thing in C# in my previous experiences. I > want to know how do I implement below example in Python. How these > things are done in Python. I am not an expert on C#, but I'll try to translate the following code to Python. > [code] > public class Bank > { > > public List lstCustomers = new List(); > private string micrcode; > > public void Bank() > { > customer > } > > } > > public class Customer > { > private srting customername; > public string CustomerName Do you mean "private string" rather than "private srting"? > { > get { return customername; } > set { customername = value; } > } > } > > main() > { > Customer objCustomer = new Customer; > objCustomer.CustomerName = "XYZ" > > Bank objBank = new Bank(); > objBank.lstCustomer.Add(objCustomer); > > } > [/code] Here is a literally translation, as best as I can understand the C# code. (But note that this is not the best Python code.) class Bank: def __init__(self): self.lstCustomers = [] # Empty list of customers. self._micrcode = '' # Does this actually get used? class Customer: def __init__(self): self._customername = '' @property def CustomerName(self): return self._customername @CustomerName.setter def CustomerName(self, value): if not instance(value, str): raise TypeError('names must be strings') self._customername = value if __name__ == '__main__': # Running as a script, call the main function. objCustomer = Customer() objCustomer.CustomerName = "XYZ" objBank = Bank() objBank.lstCustomers.append(objCustomer) But this isn't how I would write it in Python. For starters, our naming conventions are different. Everything in Python is an object, even simple types like ints and strings, and even classes, so it isn't meaningful to prefix instances with "obj". We tend to avoid anything which even vaguely looks like Hungarian Notation, so "lstCustomer" is right out. Instead, we use plural for collections (lists, sets, dicts, whatever) of things, and singular for individual instances. Also, while we can use the "property" decorator to make computed attributes, we very rarely do just to enforce private/public variables. Our philosophy is, if you want to shoot yourself in the foot, we're not going to stop you. (People spend far too much time trying to work around private names in other languages for Python to spend too much effort in this area.) Instead, we have "private by convention": names starting with a single underscore are "private", so don't touch them, and if you do, you have nobody but yourself to blame when you shoot yourself in the foot. Similarly, the language doesn't spend much time enforcing type restrictions. Python is a dynamic language, and type restrictions go against that philosophy. If you have a good reason to put a non-string as the customer name, you can do so, but don't come crying to me if you break your code. So here is how I would write the above: class Bank: def __init__(self): self.customers = [] self._micrcode = '' # Does this actually get used? class Customer: def __init__(self, name): # This type-check is optional. if not instance(name, str): raise TypeError('names must be strings') self.name = name if __name__ == '__main__': # Running as a script, call the main function. customer = Customer("XYX") bank = Bank() bank.customers.append(customer) The above is still not what I call professional quality -- no doc strings (documentation), and the bank doesn't actually do anything, but it's a start. -- Steven From punk.sagar at gmail.com Sat Aug 3 11:59:02 2013 From: punk.sagar at gmail.com (Sagar Varule) Date: Sat, 3 Aug 2013 08:59:02 -0700 (PDT) Subject: Correct Way to Write in Python In-Reply-To: <51fcbd58$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <51fcbd58$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Saturday, August 3, 2013 1:50:41 PM UTC+5:30, Steven D'Aprano wrote: > On Fri, 02 Aug 2013 23:18:47 -0700, punk.sagar wrote: > > > > > Hi All, > > > > > > Im new to Python. Im coming from C# background and want to learn Python. > > > I was used to do following thing in C# in my previous experiences. I > > > want to know how do I implement below example in Python. How these > > > things are done in Python. > > > > I am not an expert on C#, but I'll try to translate the following code to > > Python. > > > > > > > [code] > > > public class Bank > > > { > > > > > > public List lstCustomers = new List(); > > > private string micrcode; > > > > > > public void Bank() > > > { > > > customer > > > } > > > > > > } > > > > > > public class Customer > > > { > > > private srting customername; > > > public string CustomerName > > > > Do you mean "private string" rather than "private srting"? > > > > > > > { > > > get { return customername; } > > > set { customername = value; } > > > } > > > } > > > > > > main() > > > { > > > Customer objCustomer = new Customer; > > > objCustomer.CustomerName = "XYZ" > > > > > > Bank objBank = new Bank(); > > > objBank.lstCustomer.Add(objCustomer); > > > > > > } > > > [/code] > > > > > > Here is a literally translation, as best as I can understand the C# code. > > (But note that this is not the best Python code.) > > > > > > class Bank: > > def __init__(self): > > self.lstCustomers = [] # Empty list of customers. > > self._micrcode = '' # Does this actually get used? > > > > class Customer: > > def __init__(self): > > self._customername = '' > > > > @property > > def CustomerName(self): > > return self._customername > > > > @CustomerName.setter > > def CustomerName(self, value): > > if not instance(value, str): > > raise TypeError('names must be strings') > > self._customername = value > > > > > > if __name__ == '__main__': > > # Running as a script, call the main function. > > objCustomer = Customer() > > objCustomer.CustomerName = "XYZ" > > > > objBank = Bank() > > objBank.lstCustomers.append(objCustomer) > > > > > > > > But this isn't how I would write it in Python. For starters, our naming > > conventions are different. Everything in Python is an object, even simple > > types like ints and strings, and even classes, so it isn't meaningful to > > prefix instances with "obj". > > > > We tend to avoid anything which even vaguely looks like Hungarian > > Notation, so "lstCustomer" is right out. Instead, we use plural for > > collections (lists, sets, dicts, whatever) of things, and singular for > > individual instances. > > > > Also, while we can use the "property" decorator to make computed > > attributes, we very rarely do just to enforce private/public variables. > > Our philosophy is, if you want to shoot yourself in the foot, we're not > > going to stop you. (People spend far too much time trying to work around > > private names in other languages for Python to spend too much effort in > > this area.) Instead, we have "private by convention": names starting with > > a single underscore are "private", so don't touch them, and if you do, > > you have nobody but yourself to blame when you shoot yourself in the foot. > > > > Similarly, the language doesn't spend much time enforcing type > > restrictions. Python is a dynamic language, and type restrictions go > > against that philosophy. If you have a good reason to put a non-string as > > the customer name, you can do so, but don't come crying to me if you > > break your code. So here is how I would write the above: > > > > > > > > class Bank: > > def __init__(self): > > self.customers = [] > > self._micrcode = '' # Does this actually get used? > > > > class Customer: > > def __init__(self, name): > > # This type-check is optional. > > if not instance(name, str): > > raise TypeError('names must be strings') > > self.name = name > > > > > > if __name__ == '__main__': > > # Running as a script, call the main function. > > customer = Customer("XYX") > > > > bank = Bank() > > bank.customers.append(customer) > > > > > > The above is still not what I call professional quality -- no doc strings > > (documentation), and the bank doesn't actually do anything, but it's a > > start. > > > > > > -- > > Steven Thanks Steven for your Time and Effort. You have cleared many doubts and concepts for that I was struggling, since I started learning python. But I am falling in love for Python. Your explanation for private and public access modifier was awesome as I was having harding time finding why we dont have access modifier for python....Thanks a lot From rosuav at gmail.com Sat Aug 3 12:21:07 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 3 Aug 2013 17:21:07 +0100 Subject: Correct Way to Write in Python In-Reply-To: References: <51fcbd58$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, Aug 3, 2013 at 4:59 PM, Sagar Varule wrote: > Your explanation for private and public access modifier was awesome as I was having harding time finding why we dont have access modifier for python....Thanks a lot It's a huge saving in time and effort. The C++ convention is: Make everything private, then hand-write getters and setters for them all, just in case you want to put extra code onto them. (I don't know C# but it seems to be pretty much the same.) The Python convention is: We're all consenting adults. Make stuff public, then if you need to add code to something, make a @property that simulates the old behaviour. Personally, I've started to adopt the Python style in my C++ code as well. I use struct instead of class, avoid making anything private, and document the things that must not be done - for instance, I created a class that had one particular pointer that must never be advanced other than by the provided member function, but may be retarded. No reason to fiddle with member privacy even there. (The same technique has benefit in a quite different area, too: separation of code and data. Not in C++, but I have systems that let me load new code into a running process, and there it's extremely helpful to just do everything as a simple structure, and then the new code can slide in and work with the old data structure, happily extending it with whatever it now needs. Again, everything's public.) ChrisA From reality3d at gmail.com Sat Aug 3 06:16:09 2013 From: reality3d at gmail.com (Borja Morales) Date: Sat, 3 Aug 2013 03:16:09 -0700 (PDT) Subject: Minions are Python Powered! Message-ID: <58692490-e199-41b6-9adb-c14fda60d029@googlegroups.com> Everytime I watched the minions from Despicable Me something was hitting my unconscious mind. Finally I figured it out... Minions are Python Powered! I couldn't resist to make an image :) https://www.dropbox.com/s/t8kaba619vi6q82/minion_powered_by_python_reality3d.jpg From steve+comp.lang.python at pearwood.info Sat Aug 3 07:53:55 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 Aug 2013 11:53:55 GMT Subject: Minions are Python Powered! References: <58692490-e199-41b6-9adb-c14fda60d029@googlegroups.com> Message-ID: <51fcef53$0$30000$c3e8da3$5496439d@news.astraweb.com> On Sat, 03 Aug 2013 03:16:09 -0700, Borja Morales wrote: > Everytime I watched the minions from Despicable Me something was hitting > my unconscious mind. Finally I figured it out... Minions are Python > Powered! > > I couldn't resist to make an image :) > > https://www.dropbox.com/s/t8kaba619vi6q82/ minion_powered_by_python_reality3d.jpg "The Dropbox website requires JavaScript." Why the hell does some site need to run code in my browser just to display a JPEG? That's rubbish. What's even more rubbish is that Dropbox's 404 Page Not Found tries to set a cookie that doesn't expire for FIVE YEARS. -- Steven From none at mailinator.com Sat Aug 3 09:35:25 2013 From: none at mailinator.com (mm0fmf) Date: Sat, 03 Aug 2013 14:35:25 +0100 Subject: Minions are Python Powered! In-Reply-To: <51fcef53$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <58692490-e199-41b6-9adb-c14fda60d029@googlegroups.com> <51fcef53$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Sat, 03 Aug 2013 03:16:09 -0700, Borja Morales wrote: > >> Everytime I watched the minions from Despicable Me something was hitting >> my unconscious mind. Finally I figured it out... Minions are Python >> Powered! >> >> I couldn't resist to make an image :) >> >> https://www.dropbox.com/s/t8kaba619vi6q82/ > minion_powered_by_python_reality3d.jpg > > > "The Dropbox website requires JavaScript." > > Why the hell does some site need to run code in my browser just to > display a JPEG? That's rubbish. > Works for me with javascript disabled. From cmpython at gmail.com Sat Aug 3 13:21:23 2013 From: cmpython at gmail.com (CM) Date: Sat, 3 Aug 2013 10:21:23 -0700 (PDT) Subject: Minions are Python Powered! In-Reply-To: <58692490-e199-41b6-9adb-c14fda60d029@googlegroups.com> References: <58692490-e199-41b6-9adb-c14fda60d029@googlegroups.com> Message-ID: <99818c6e-90a7-495f-a369-83be3537820a@googlegroups.com> On Saturday, August 3, 2013 6:16:09 AM UTC-4, Borja Morales wrote: > Everytime I watched the minions from Despicable Me something was hitting my unconscious mind. Finally I figured it out... Minions are Python Powered! > > > > I couldn't resist to make an image :) I haven't even seen either of those movies (though have seen those characters in their advertising) and despite that, upon seeing your image, was unable to do anything but smile really hard. Killer job! Brightened my day. From kmacphail at gmail.com Sun Aug 4 08:02:55 2013 From: kmacphail at gmail.com (Kevin MacPhail) Date: Sun, 4 Aug 2013 09:02:55 -0300 Subject: Minions are Python Powered! In-Reply-To: <58692490-e199-41b6-9adb-c14fda60d029@googlegroups.com> References: <58692490-e199-41b6-9adb-c14fda60d029@googlegroups.com> Message-ID: This statement might be more true than you realize. I can't speak for the tools Blue Sky uses for their films, but when the studio I worked at created the "Minion Mayem" ride for Universal Orlando we used a number of tools that relied heavily on Python for everything from character rigging to rendering and compositing. I think it would be fair to say the Minions (and most animated characters these days) are quite literally "Python Powered". BTW, great image! On Sat, Aug 3, 2013 at 7:16 AM, Borja Morales wrote: > Everytime I watched the minions from Despicable Me something was hitting > my unconscious mind. Finally I figured it out... Minions are Python Powered! > > I couldn't resist to make an image :) > > > https://www.dropbox.com/s/t8kaba619vi6q82/minion_powered_by_python_reality3d.jpg > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From asmbansal2 at gmail.com Sat Aug 3 13:53:59 2013 From: asmbansal2 at gmail.com (Aseem Bansal) Date: Sat, 3 Aug 2013 10:53:59 -0700 (PDT) Subject: Where to suggest improvements in the official Python documentation? Message-ID: I have a suggestion about the Python tutorial for improvement. Specifically about in Python tutorial 4.7.5 lambda forms. http://docs.python.org/3/tutorial/controlflow.html#lambda-forms It is not very clear from the tutorial what lambda forms are for someone who doesn't know functional programming. I think placing a link of "functional Programming HOWTO" of Python documentation can take out much confusion for Python newbies. I would like to suggest this because as I newbiw I had much confusion 2 months back before I could figure out its proper use. Where do I suggest this improvement? From joel.goldstick at gmail.com Sat Aug 3 14:23:55 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Sat, 3 Aug 2013 14:23:55 -0400 Subject: Where to suggest improvements in the official Python documentation? In-Reply-To: References: Message-ID: On Sat, Aug 3, 2013 at 1:53 PM, Aseem Bansal wrote: > I have a suggestion about the Python tutorial for improvement. Specifically about in Python tutorial 4.7.5 lambda forms. > http://docs.python.org/3/tutorial/controlflow.html#lambda-forms > > It is not very clear from the tutorial what lambda forms are for someone who doesn't know functional programming. I think placing a link of "functional Programming HOWTO" of Python documentation can take out much confusion for Python newbies. > > I would like to suggest this because as I newbiw I had much confusion 2 months back before I could figure out its proper use. Where do I suggest this improvement? > -- > http://mail.python.org/mailman/listinfo/python-list http://docs.python.org/2/bugs.html -- Joel Goldstick http://joelgoldstick.com From tjreedy at udel.edu Sat Aug 3 16:43:14 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 03 Aug 2013 16:43:14 -0400 Subject: Where to suggest improvements in the official Python documentation? In-Reply-To: References: Message-ID: On 8/3/2013 2:23 PM, Joel Goldstick wrote: > On Sat, Aug 3, 2013 at 1:53 PM, Aseem Bansal > wrote: >> I have a suggestion about the Python tutorial for improvement. >> Specifically about in Python tutorial 4.7.5 lambda forms. >> http://docs.python.org/3/tutorial/controlflow.html#lambda-forms >> >> It is not very clear from the tutorial what lambda forms are for They are mostly for passing a one-use function as an argument to another function. The tutorial should say that and give an example. http://bugs.python.org/issue18646 >> someone who doesn't know functional programming. I think placing a >> link of "functional Programming HOWTO" of Python documentation can >> take out much confusion for Python newbies. That document is not about lambda. The word 'lambda' does not appear until near the end, and some of the current examples violate PEP 8. >> I would like to suggest this because as I newbiw I had much >> confusion 2 months back before I could figure out its proper use. >> Where do I suggest this improvement? -- > http://docs.python.org/2/bugs.html -- Terry Jan Reedy From asmbansal2 at gmail.com Sun Aug 4 12:30:35 2013 From: asmbansal2 at gmail.com (Aseem Bansal) Date: Sun, 4 Aug 2013 09:30:35 -0700 (PDT) Subject: Where to suggest improvements in the official Python documentation? In-Reply-To: References: Message-ID: @ Terry Jan Reedy If there is an issue in place for improving the lambda forms then that's good. I wanted a link about functional programming because it is mentioned as if it were a household word. From joel.goldstick at gmail.com Sun Aug 4 14:34:14 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Sun, 4 Aug 2013 14:34:14 -0400 Subject: Where to suggest improvements in the official Python documentation? In-Reply-To: References: Message-ID: On Sun, Aug 4, 2013 at 12:30 PM, Aseem Bansal wrote: > @ Terry Jan Reedy > > If there is an issue in place for improving the lambda forms then that's good. I wanted a link about functional programming because it is mentioned as if it were a household word. It depends on your household I suppose! Have you tried google? I am having a hard time understanding what your specific question is. Do you have one? > -- > http://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com From asmbansal2 at gmail.com Tue Aug 20 10:22:16 2013 From: asmbansal2 at gmail.com (Aseem Bansal) Date: Tue, 20 Aug 2013 07:22:16 -0700 (PDT) Subject: Where to suggest improvements in the official Python documentation? In-Reply-To: References: Message-ID: @Joel Goldstick > Joel Goldstick > > http://joelgoldstick.com My specific question was that the Python documentation's tutorial isn't clear when it comes to lambda forms. I just wanted something to be done so it becomes clear for future readers who are not familiar with functional paradigm. I wanted to make a suggestion about adding a link to functional programming to add some clarity. From joel.goldstick at gmail.com Tue Aug 20 10:57:05 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 20 Aug 2013 10:57:05 -0400 Subject: Where to suggest improvements in the official Python documentation? In-Reply-To: References: Message-ID: On Tue, Aug 20, 2013 at 10:22 AM, Aseem Bansal wrote: > @Joel Goldstick >> Joel Goldstick >> >> http://joelgoldstick.com > > My specific question was that the Python documentation's tutorial isn't clear when it comes to lambda forms. I just wanted something to be done so it becomes clear for future readers who are not familiar with functional paradigm. I wanted to make a suggestion about adding a link to functional programming to add some clarity. In my first post above I provided the link for bug reports for python (including documentation). This is a volunteer list which is not officially part of the python.org site. You might try going to that link to learn how to submit change requests. > -- > http://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com From asmbansal2 at gmail.com Sat Aug 3 13:57:32 2013 From: asmbansal2 at gmail.com (Aseem Bansal) Date: Sat, 3 Aug 2013 10:57:32 -0700 (PDT) Subject: Can someone suggest better resources for learning sqlite3? I wanted to use the Python library but I don't know sql. Message-ID: <2183b50a-b3d7-459f-bc00-5868fba5c7c3@googlegroups.com> I was writing a Python script for getting the user stats of a website(Specifically codereview.stackexchange). I wanted to store the stats in a database. I found Python3's sqlite3 library. I found that I needed sql commands for using it. I have tried sql.learncodethehardway but it isn't complete yet. I tired looking on stackoverflow's sql tag also but nothing much there. Can someone suggest me better resources for learning sql/sqlite3? From vito.detullio at gmail.com Sat Aug 3 14:47:29 2013 From: vito.detullio at gmail.com (Vito De Tullio) Date: Sat, 03 Aug 2013 20:47:29 +0200 Subject: Can someone suggest better resources for learning sqlite3? I wanted to use the Python library but I don't know sql. References: <2183b50a-b3d7-459f-bc00-5868fba5c7c3@googlegroups.com> Message-ID: Aseem Bansal wrote: > I have tried sql.learncodethehardway but it isn't complete yet. I tired > looking on stackoverflow's sql tag also but nothing much there. Can > someone suggest me better resources for learning sql/sqlite3? a start is the sqlite homepage: http://www.sqlite.org/docs.html -- By ZeD From joel.goldstick at gmail.com Sat Aug 3 14:53:11 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Sat, 3 Aug 2013 14:53:11 -0400 Subject: Can someone suggest better resources for learning sqlite3? I wanted to use the Python library but I don't know sql. In-Reply-To: <2183b50a-b3d7-459f-bc00-5868fba5c7c3@googlegroups.com> References: <2183b50a-b3d7-459f-bc00-5868fba5c7c3@googlegroups.com> Message-ID: On Sat, Aug 3, 2013 at 1:57 PM, Aseem Bansal wrote: > I was writing a Python script for getting the user stats of a website(Specifically codereview.stackexchange). I wanted to store the stats in a database. I found Python3's sqlite3 library. I found that I needed sql commands for using it. > > I have tried sql.learncodethehardway but it isn't complete yet. I tired looking on stackoverflow's sql tag also but nothing much there. Can someone suggest me better resources for learning sql/sqlite3? Have you tried the sqlite home page at http://www.sqlite.org/ > -- > http://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com From cousinstanley at gmail.com Sat Aug 3 15:06:12 2013 From: cousinstanley at gmail.com (Cousin Stanley) Date: Sat, 3 Aug 2013 19:06:12 +0000 (UTC) Subject: Can someone suggest better resources for learning sqlite3? I wanted to use the Python library but I don't know sql. References: <2183b50a-b3d7-459f-bc00-5868fba5c7c3@googlegroups.com> Message-ID: Aseem Bansal wrote: > .... > Can someone suggest me better resources > for learning sql/sqlite3 ? http://docs.python.org/3/library/sqlite3.html http://wiki.python.org/moin/DbApiCheatSheet http://www.w3schools.com/sql/default.asp http://www.sqlcourse.com/index.html http://sqlite.org/docs.html http://zetcode.com/db/sqlite/ -- Stanley C. Kitching Human Being Phoenix, Arizona From cmpython at gmail.com Sat Aug 3 20:53:06 2013 From: cmpython at gmail.com (CM) Date: Sat, 3 Aug 2013 17:53:06 -0700 (PDT) Subject: Can someone suggest better resources for learning sqlite3? I wanted to use the Python library but I don't know sql. In-Reply-To: <2183b50a-b3d7-459f-bc00-5868fba5c7c3@googlegroups.com> References: <2183b50a-b3d7-459f-bc00-5868fba5c7c3@googlegroups.com> Message-ID: ave tried sql.learncodethehardway but it isn't complete yet. I tired looking on stackoverflow's sql tag also but nothing much there. Can someone suggest me better resources for learning sql/sqlite3? There are a lot of nice small tutorials out there found by Googling. One resource that you might not find easily, though, and that helped me a while back and I liked is: http://sqlzoo.net/howto/source/u.cgi/tip241028/sqlite The thing about that page is, if you go to the main site, sqlzoo.net, it seems that SQLite is no longer one of the options. But if you go through the link I gave above, you can find the older site that does treat SQLite. To find the other command examples, I guess use Google like so: sqlite zoo INSERT sqlite zoo UPDATE and others. I don't know why SQLite was dropped from the Zoo's roster; I really liked that format. From inq1ltd at inqvista.com Sun Aug 4 11:01:10 2013 From: inq1ltd at inqvista.com (inq1ltd) Date: Sun, 04 Aug 2013 11:01:10 -0400 Subject: Can someone suggest better resources for learning sqlite3? I wanted to use the Python library but I don't know sql. In-Reply-To: <2183b50a-b3d7-459f-bc00-5868fba5c7c3@googlegroups.com> References: <2183b50a-b3d7-459f-bc00-5868fba5c7c3@googlegroups.com> Message-ID: <1489290.XDaGr2X6cf@mach-114-20> On Saturday, August 03, 2013 10:57:32 AM Aseem Bansal wrote: > I was writing a Python script for getting the user stats of a > website(Specifically codereview.stackexchange). I wanted to store the stats > in a database. I found Python3's sqlite3 library. I found that I needed sql > commands for using it. > > I have tried sql.learncodethehardway but it isn't complete yet. I tired > looking on stackoverflow's sql tag also but nothing much there. Can > someone suggest me better resources for learning sql/sqlite3? Go to: sqlite-users at sqlite.org and join. Ask any question, you will get an answer. Print out the manual. For someone that is not familiar with Relational DB programming it may look like a newspaper from another planet, but you should have it. You mentioned creating a database for stats. Learn how to create a table, and populate it. that is basic. Ask how to on the users help site, then compare the method sent to you to the manual. You need to know how the answer relates to the manual. The sqlite manual is in the standard format that is the accepted world wide. Once you can understand the manual format you can work your way through a lot of what you want to do. jimonlinux inqvista.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From nospam at nospam.com Tue Aug 6 06:51:01 2013 From: nospam at nospam.com (Gilles) Date: Tue, 06 Aug 2013 12:51:01 +0200 Subject: Can someone suggest better resources for learning sqlite3? I wanted to use the Python library but I don't know sql. References: <2183b50a-b3d7-459f-bc00-5868fba5c7c3@googlegroups.com> Message-ID: On Sat, 3 Aug 2013 10:57:32 -0700 (PDT), Aseem Bansal wrote: >I found Python3's sqlite3 library. I found that I needed sql commands for using it. > >I have tried sql.learncodethehardway but it isn't complete yet. I tired looking on stackoverflow's sql tag also but nothing much there. It'll be easier to read tutorials specifically meant to use Sqlite with Python: www.google.com/search?q=python+sqlite+examples From kender.jr at gmail.com Tue Aug 6 07:05:02 2013 From: kender.jr at gmail.com (Jordi Riera) Date: Tue, 6 Aug 2013 13:05:02 +0200 Subject: Can someone suggest better resources for learning sqlite3? I wanted to use the Python library but I don't know sql. In-Reply-To: References: <2183b50a-b3d7-459f-bc00-5868fba5c7c3@googlegroups.com> Message-ID: Hey, can't you use django to deal with your sqlite? If so, django modelsare a smart way to do. Regards, Jordi On Tue, Aug 6, 2013 at 12:51 PM, Gilles wrote: > On Sat, 3 Aug 2013 10:57:32 -0700 (PDT), Aseem Bansal > wrote: > >I found Python3's sqlite3 library. I found that I needed sql commands for > using it. > > > >I have tried sql.learncodethehardway but it isn't complete yet. I tired > looking on stackoverflow's sql tag also but nothing much there. > > It'll be easier to read tutorials specifically meant to use Sqlite > with Python: > > www.google.com/search?q=python+sqlite+examples > -- > http://mail.python.org/mailman/listinfo/python-list > -- - Jordi Riera, Connecting people. +33 662217507 -------------- next part -------------- An HTML attachment was scrubbed... URL: From memilanuk at gmail.com Tue Aug 6 23:22:46 2013 From: memilanuk at gmail.com (memilanuk) Date: Tue, 06 Aug 2013 20:22:46 -0700 Subject: Can someone suggest better resources for learning sqlite3? I wanted to use the Python library but I don't know sql. In-Reply-To: <2183b50a-b3d7-459f-bc00-5868fba5c7c3@googlegroups.com> References: <2183b50a-b3d7-459f-bc00-5868fba5c7c3@googlegroups.com> Message-ID: On 08/03/2013 10:57 AM, Aseem Bansal wrote: > I was writing a Python script for getting the user stats of a > website(Specifically codereview.stackexchange). I wanted to store the > stats in a database. I found Python3's sqlite3 library. I found that > I needed sql commands for using it. > > I have tried sql.learncodethehardway but it isn't complete yet. I > tired looking on stackoverflow's sql tag also but nothing much > there. Can someone suggest me better resources for learning > sql/sqlite3? > https://www.youtube.com/watch?v=__eI1sbEfLw&feature=c4-overview-vl&list=PLAA9B7C174493EC68 The same author has some other videos on working with sqlite in general, sql, etc. that may also prove useful. From skip at pobox.com Wed Aug 7 06:59:45 2013 From: skip at pobox.com (Skip Montanaro) Date: Wed, 7 Aug 2013 05:59:45 -0500 Subject: Can someone suggest better resources for learning sqlite3? I wanted to use the Python library but I don't know sql. In-Reply-To: <2183b50a-b3d7-459f-bc00-5868fba5c7c3@googlegroups.com> References: <2183b50a-b3d7-459f-bc00-5868fba5c7c3@googlegroups.com> Message-ID: > Can someone suggest me better resources for learning sql/sqlite3? The concepts behind the Structured Query Language haven't changed much since Edgar Codd first developed them in the 1970s. (He received the Turing Award in 1981 for this work.) Building and querying databases is very easy to do very badly, especially if you are new to its concepts. This is a case where a textbook might be helpful. Since these ideas have been around for a long while, there are plenty of good books on the subject. I have one on my desk at work whose name I can't remember off the top of my head. I still refer to it from time-to-time. If you'd like a reference, let me know and I'll check on it at work. Skip From skip at pobox.com Wed Aug 7 13:43:55 2013 From: skip at pobox.com (Skip Montanaro) Date: Wed, 7 Aug 2013 12:43:55 -0500 Subject: Can someone suggest better resources for learning sqlite3? I wanted to use the Python library but I don't know sql. In-Reply-To: References: <2183b50a-b3d7-459f-bc00-5868fba5c7c3@googlegroups.com> Message-ID: > I have one on my desk at work whose name I can't remember off the > top of my head. I still refer to it from time-to-time. If you'd > like a reference, let me know and I'll check on it at work. While I think of it: "The Practical SQL Handbook; Using Structured Query Language," by Bowman, Emerson, and Darnovsky Mine's the third edition. It even has a Sybase SQL Runtime CDROM inside the back cover. How quaint. :-) There is a fourth edition available. Skip From usharma01 at gmail.com Sat Aug 3 15:01:54 2013 From: usharma01 at gmail.com (Umesh Sharma) Date: Sat, 3 Aug 2013 12:01:54 -0700 (PDT) Subject: Crawl Quora Message-ID: <154b7f76-9491-4eb5-813b-c1d8c76cf054@googlegroups.com> Hello, I am writing a crawler in python, which crawl quora. I can't read the content of quora without login. But google/bing crawls quora. One thing i can do is use browser automation and login in my account and the go links by link and crawl content, but this method is slow. So can any one tell me how should i start in writing this crawler. Thanks, Umesh Kumar Sharma From davea at davea.name Sat Aug 3 17:09:05 2013 From: davea at davea.name (Dave Angel) Date: Sat, 3 Aug 2013 21:09:05 +0000 (UTC) Subject: Crawl Quora References: <154b7f76-9491-4eb5-813b-c1d8c76cf054@googlegroups.com> Message-ID: Umesh Sharma wrote: > Hello, > > I am writing a crawler in python, which crawl quora. I can't read the content of quora without login. But google/bing crawls quora. One thing i can do is use browser automation and login in my account and the go links by link and crawl content, but this method is slow. So can any one tell me how should i start in writing this crawler. > > I had never heard of quora. And I had to hunt a bit to find a link to this website. When you post a question here which refers to a non-Python site, you really should include a link to it. You start with reading the page: http://www.quora.com/about/tos which you agreed to when you created your account with them. At one place it seems pretty clear that unless you make specific arrangements with Quora, you're limited to using their API. I suspect that they bend over backwards to get Google and the other big names to index their stuff. But that doesn't make it legal for you to do the same. In particular, the section labeled "Rules" makes constraints on automated crawling. And so do other parts of the TOS. Crawling is permissible, but not scraping. What's that mean? I dunno. Perhaps scraping is what you're describing above as "method is slow." I'm going to be looking to see what API's they offer, if any. I'm creating an account now. -- DaveA From dwightdhutto at gmail.com Wed Aug 7 23:51:51 2013 From: dwightdhutto at gmail.com (David Hutto) Date: Wed, 7 Aug 2013 23:51:51 -0400 Subject: Crawl Quora In-Reply-To: References: <154b7f76-9491-4eb5-813b-c1d8c76cf054@googlegroups.com> Message-ID: Never tried this, but if it's not data you're after, but a search term type of app, then ip address crawl, and if keyword/metadata, then crawl, and parse, just as it seems you are doing, for keywords, and url's associated with them, then eliminate url's without that specified keyword parameter into your function. Then, of course, just as stated above, some sites won't let you have access in other ways, which you should be able to circumvent some way. On Sat, Aug 3, 2013 at 5:09 PM, Dave Angel wrote: > Umesh Sharma wrote: > > > Hello, > > > > I am writing a crawler in python, which crawl quora. I can't read the > content of quora without login. But google/bing crawls quora. One thing i > can do is use browser automation and login in my account and the go links > by link and crawl content, but this method is slow. So can any one tell me > how should i start in writing this crawler. > > > > > I had never heard of quora. And I had to hunt a bit to find a link to > this website. When you post a question here which refers to a > non-Python site, you really should include a link to it. > > You start with reading the page: http://www.quora.com/about/tos > > which you agreed to when you created your account with them. At one > place it seems pretty clear that unless you make specific arrangements > with Quora, you're limited to using their API. > > I suspect that they bend over backwards to get Google and the other big > names to index their stuff. But that doesn't make it legal for you to > do the same. > > In particular, the section labeled "Rules" makes constraints on > automated crawling. And so do other parts of the TOS. Crawling is > permissible, but not scraping. What's that mean? I dunno. Perhaps > scraping is what you're describing above as "method is slow." > > I'm going to be looking to see what API's they offer, if any. I'm > creating an account now. > > -- > DaveA > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Best Regards, David Hutto *CEO:* *http://www.hitwebdevelopment.com* -------------- next part -------------- An HTML attachment was scrubbed... URL: From kevin4fong at gmail.com Sat Aug 3 16:12:55 2013 From: kevin4fong at gmail.com (kevin4fong at gmail.com) Date: Sat, 3 Aug 2013 13:12:55 -0700 (PDT) Subject: Removing matching items from a list? Message-ID: <6c0bdea5-23bd-4854-8016-4bf0af3b7d0e@googlegroups.com> Basically, I'm trying to find out how to remove matching items from a list. But there doesn't seem to be any information on how to go about doing this specific function. For example, what I want is: let's say there is a list: pHands[0] = ['ad', 'ac', 'as', 'ah', '7d', '8s', '9d', 'td', 'js', 'jd'] So up there, my list, which is named pHands[0] has ten items in it. I'm trying to make a function where a search is initiated into the list and any matching items with a first matching number/letter reaching four are removed So in the end, ad, ac, as, ah (the four a's) will all be deleted/removed from the list. I need the list to automatically detect if there are four matching first letter/numbers in the items in the list. The remaining list will be: pHands[0] = ['7d', '8s', '9d', 'td', 'js', 'jd'] From kevin4fong at gmail.com Sat Aug 3 16:16:52 2013 From: kevin4fong at gmail.com (kevin4fong at gmail.com) Date: Sat, 3 Aug 2013 13:16:52 -0700 (PDT) Subject: Removing matching items from a list? In-Reply-To: <6c0bdea5-23bd-4854-8016-4bf0af3b7d0e@googlegroups.com> References: <6c0bdea5-23bd-4854-8016-4bf0af3b7d0e@googlegroups.com> Message-ID: On Saturday, August 3, 2013 1:12:55 PM UTC-7, kevin... at gmail.com wrote: > Basically, I'm trying to find out how to remove matching items from a list. But there doesn't seem to be any information on how to go about doing this specific function. > > > > For example, what I want is: > > > > let's say there is a list: > > > > pHands[0] = ['ad', 'ac', 'as', 'ah', '7d', '8s', '9d', 'td', 'js', 'jd'] > > > > So up there, my list, which is named pHands[0] has ten items in it. > > > > I'm trying to make a function where a search is initiated into the list and any matching items with a first matching number/letter reaching four are removed > > > > So in the end, ad, ac, as, ah (the four a's) will all be deleted/removed from the list. I need the list to automatically detect if there are four matching first letter/numbers in the items in the list. > > > > The remaining list will be: pHands[0] = ['7d', '8s', '9d', 'td', 'js', 'jd'] Ah, I forgot to mention. This function will be used for other lists to so it can't specifically target "a" and needs to auto search for four matching first letters. If it helps, the two letters/numbers in each item are strung together by the following function: for suite in range(4): for rank in range(13): deck.append(rankList[rank]+suitList[suite]) So they can be directly accessed using: card[0] for the first letter/number card[1] for the second From woooee at gmail.com Sat Aug 3 16:27:34 2013 From: woooee at gmail.com (woooee at gmail.com) Date: Sat, 3 Aug 2013 13:27:34 -0700 (PDT) Subject: Removing matching items from a list? In-Reply-To: <6c0bdea5-23bd-4854-8016-4bf0af3b7d0e@googlegroups.com> References: <6c0bdea5-23bd-4854-8016-4bf0af3b7d0e@googlegroups.com> Message-ID: Use a dictionary to count the number of times each first letter appears, then place any first letters==4 in a list or set and remove any items that have a first letter in the list/set (or keep items that are not in the set which is probably faster if using list comprehension). From roy at panix.com Sat Aug 3 17:02:45 2013 From: roy at panix.com (Roy Smith) Date: Sat, 03 Aug 2013 17:02:45 -0400 Subject: Removing matching items from a list? References: <6c0bdea5-23bd-4854-8016-4bf0af3b7d0e@googlegroups.com> Message-ID: In article <6c0bdea5-23bd-4854-8016-4bf0af3b7d0e at googlegroups.com>, kevin4fong at gmail.com wrote: > let's say there is a list: > > pHands[0] = ['ad', 'ac', 'as', 'ah', '7d', '8s', '9d', 'td', 'js', 'jd'] I assume this is a card game, and these are cards (ad = Ace of Diamonds, etc). > I'm trying to make a function where a search is initiated into the list and > any matching items with a first matching number/letter reaching four are > removed I'm not quite sure how to parse that, but I think what you're saying (using the deck of cards model) is you want to find all the sets of 4 cards of the same rank. In the example above, you've got all four aces (which, in the wrong saloon, can get you shot). I think I would do something like: ----------------------------------------- import collections def remove_four_of_a_kind(hand): ranks = [card[0] for card in hand] counts = collections.Counter(ranks) fours = [rank for rank, count in counts.items() if count == 4] new_hand = [card for card in hand if card[0] not in fours] return new_hand hand = ['ad', 'ac', 'as', 'ah', '7d', '8s', '9d', 'td', 'js', 'jd'] print remove_four_of_a_kind(hand) ----------------------------------------- $ python cards.py ['7d', '8s', '9d', 'td', 'js', 'jd'] I just gave a class yesterday where we covered list comprehensions and Counters, so maybe I just have comprehensions on the brain today :-) From python at mrabarnett.plus.com Sat Aug 3 17:06:15 2013 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 03 Aug 2013 22:06:15 +0100 Subject: Removing matching items from a list? In-Reply-To: <6c0bdea5-23bd-4854-8016-4bf0af3b7d0e@googlegroups.com> References: <6c0bdea5-23bd-4854-8016-4bf0af3b7d0e@googlegroups.com> Message-ID: <51FD70C7.6030007@mrabarnett.plus.com> On 03/08/2013 21:12, kevin4fong at gmail.com wrote: > > > Basically, I'm trying to find out how to remove matching items from a list. But there doesn't seem to be any information on how to go about doing this specific function. > > For example, what I want is: > > let's say there is a list: > > pHands[0] = ['ad', 'ac', 'as', 'ah', '7d', '8s', '9d', 'td', 'js', 'jd'] > > So up there, my list, which is named pHands[0] has ten items in it. > > I'm trying to make a function where a search is initiated into the list and any matching items with a first matching number/letter reaching four are removed > > So in the end, ad, ac, as, ah (the four a's) will all be deleted/removed from the list. I need the list to automatically detect if there are four matching first letter/numbers in the items in the list. > > The remaining list will be: pHands[0] = ['7d', '8s', '9d', 'td', 'js', 'jd'] > >>> hands = ['ad', 'ac', 'as', 'ah', '7d', '8s', '9d', 'td', 'js', 'jd'] >>> from collections import Counter >>> counts = Counter(hand[0] for hand in hands) >>> counts Counter({'a': 4, 'j': 2, 't': 1, '7': 1, '9': 1, '8': 1}) >>> counts.most_common() [('a', 4), ('j', 2), ('t', 1), ('7', 1), ('9', 1), ('8', 1)] So there are 4 aces. >>> hands = [hand for hand in hands if hand[0] != 'a'] >>> hands ['7d', '8s', '9d', 'td', 'js', 'jd'] From steve+comp.lang.python at pearwood.info Sat Aug 3 18:37:41 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 Aug 2013 22:37:41 GMT Subject: Removing matching items from a list? References: <6c0bdea5-23bd-4854-8016-4bf0af3b7d0e@googlegroups.com> Message-ID: <51fd8635$0$30000$c3e8da3$5496439d@news.astraweb.com> On Sat, 03 Aug 2013 13:12:55 -0700, kevin4fong wrote: > Basically, I'm trying to find out how to remove matching items from a > list. But there doesn't seem to be any information on how to go about > doing this specific function. The documentation cannot possibly cover every single one of the infinite number of things somebody might want to do with Python. Programming is about figuring out how to do the things you want from the tools provided by the language. > For example, what I want is: > > let's say there is a list: > > pHands[0] = ['ad', 'ac', 'as', 'ah', '7d', '8s', '9d', 'td', 'js', 'jd'] > > So up there, my list, which is named pHands[0] has ten items in it. Ten items? I wouldn't have known that if you didn't say so! *wink* But seriously... you don't need to repeat obvious things like that. We can count as well as you can. But more importantly, it is irrelevant. Is "ten items" important to this problem? No. What if there were nine, or thirty five, or seven? Would the problem of "removing matching items" change? No, the problem remains the same regardless of how many items there are. > I'm trying to make a function where a search is initiated into the list > and any matching items with a first matching number/letter reaching four > are removed In general, rather than removing items from a list, it is faster and safer (less error-prone) to create a new list with the items not removed. But either way, when trying to program, start by thinking about how *you* would do this task by hand: 1) I would start by counting how many items start with each initial letter: in your example I would count those starting with 'a' (4), '7' (1), and so on. 2) Then go through those initial letters, and pick out the ones equal to 4 (or should that be "four or more"?). In this case, only 'a' is 4 or more, but there could be multiple examples, say, 4 'a's and 6 'b's. 3) Then, either delete those items from the original list, *or* make a new list with the remaining items. Making a new list is usually better. Now start turning each step into Python code. Step 1: Iterate over each item, grab the first character of that item, and add one to a counter. counts = {} # Start with an empty dict. for item in pHands[0]: c = item[0] # Grab the first character. if c in counts: counts[c] += 1 else: counts[c] = 1 This sets the counter to 1 the first time each character is seen, otherwise increments the counter by 1. There's a ready-made tool for doing that: from collections import Counter counts = Counter(item[0] for item in pHands[0]) but under the hood, it's essentially doing the same thing as I wrote above. Steps 2 and 3 can be done together: new_list = [] for item in pHand[0]: c = item[0] # Look up the character in the counter. if counts[c] < 4: new_list.append(item) pHand[0] = new_list -- Steven From roy at panix.com Sat Aug 3 19:06:05 2013 From: roy at panix.com (Roy Smith) Date: Sat, 03 Aug 2013 19:06:05 -0400 Subject: Removing matching items from a list? References: <6c0bdea5-23bd-4854-8016-4bf0af3b7d0e@googlegroups.com> <51fd8635$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <51fd8635$0$30000$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > 2) Then go through those initial letters, and pick out the ones equal to > 4 (or should that be "four or more"?). Assuming my earlier hunch is correct about these being cards in a deck, and the a's being aces, I would hope it's not "four or more". See my earlier comment about saloons and gunshot wounds. From rosuav at gmail.com Sat Aug 3 19:35:26 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 4 Aug 2013 00:35:26 +0100 Subject: Removing matching items from a list? In-Reply-To: References: <6c0bdea5-23bd-4854-8016-4bf0af3b7d0e@googlegroups.com> <51fd8635$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Aug 4, 2013 at 12:06 AM, Roy Smith wrote: > In article <51fd8635$0$30000$c3e8da3$5496439d at news.astraweb.com>, > Steven D'Aprano wrote: > >> 2) Then go through those initial letters, and pick out the ones equal to >> 4 (or should that be "four or more"?). > > Assuming my earlier hunch is correct about these being cards in a deck, > and the a's being aces, I would hope it's not "four or more". See my > earlier comment about saloons and gunshot wounds. Unless he's working with multiple decks. There are plenty of games played with a double deck. But we did get a peek at his creation code, which appears to make but a single deck's worth of cards. ChrisA From steve+comp.lang.python at pearwood.info Sat Aug 3 20:25:16 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 04 Aug 2013 00:25:16 GMT Subject: Removing matching items from a list? References: <6c0bdea5-23bd-4854-8016-4bf0af3b7d0e@googlegroups.com> <51fd8635$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: <51fd9f6b$0$30000$c3e8da3$5496439d@news.astraweb.com> On Sat, 03 Aug 2013 19:06:05 -0400, Roy Smith wrote: > In article <51fd8635$0$30000$c3e8da3$5496439d at news.astraweb.com>, > Steven D'Aprano wrote: > >> 2) Then go through those initial letters, and pick out the ones equal >> to 4 (or should that be "four or more"?). > > Assuming my earlier hunch is correct about these being cards in a deck, > and the a's being aces, I would hope it's not "four or more". See my > earlier comment about saloons and gunshot wounds. There are card games that involve more than four suits, or more than one pack. In principle, if you draw ten cards from an unknown number of packs, you could draw ten Aces, or even ten Aces of the same suit. Think of playing blackjack with ten packs shuffled together. Cripple Mr Onion, for example, uses hands of ten cards drawn from eight suits: http://discworld.wikia.com/wiki/Cripple_Mr_Onion (yes, it's a real game, based on a fictional game) The Indian Ganjifa deck uses ten "houses" (decks) of 12 cards each. Pinochle uses the standard four Western suits, but uses two copies of each card 9, 10, J, Q, K, A, for 48 cards in total. Sometimes people combine two pinochle decks for larger games, so with a double deck all ten cards could be Aces. In the 1930s, there was a fad for playing Bridge with a fifth suit, coloured green, called Royals, Leaves, Eagles or Rooks depending on the manufacturer. There are five- and six-suit versions of poker, with at least two still commercially available: the five-suit Stardeck pack (which adds Stars), and the six-suit Empire deck (which adds red Crowns and black Anchors). There is an eight-suit Euchre pack that adds red Moons, black Stars, red four-leaf Clovers and black Tears to the standard deck. -- Steven From kevin4fong at gmail.com Sat Aug 3 20:33:09 2013 From: kevin4fong at gmail.com (kevin4fong at gmail.com) Date: Sat, 3 Aug 2013 17:33:09 -0700 (PDT) Subject: Removing matching items from a list? In-Reply-To: <51fd9f6b$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <6c0bdea5-23bd-4854-8016-4bf0af3b7d0e@googlegroups.com> <51fd8635$0$30000$c3e8da3$5496439d@news.astraweb.com> <51fd9f6b$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Saturday, August 3, 2013 5:25:16 PM UTC-7, Steven D'Aprano wrote: > On Sat, 03 Aug 2013 19:06:05 -0400, Roy Smith wrote: > > > > > In article <51fd8635$0$30000$c3e8da3$5496439d at news.astraweb.com>, > > > Steven D'Aprano wrote: > > > > > >> 2) Then go through those initial letters, and pick out the ones equal > > >> to 4 (or should that be "four or more"?). > > > > > > Assuming my earlier hunch is correct about these being cards in a deck, > > > and the a's being aces, I would hope it's not "four or more". See my > > > earlier comment about saloons and gunshot wounds. > > > > There are card games that involve more than four suits, or more than one > > pack. In principle, if you draw ten cards from an unknown number of > > packs, you could draw ten Aces, or even ten Aces of the same suit. Think > > of playing blackjack with ten packs shuffled together. > > > > Cripple Mr Onion, for example, uses hands of ten cards drawn from eight > > suits: > > > > http://discworld.wikia.com/wiki/Cripple_Mr_Onion > > > > (yes, it's a real game, based on a fictional game) > > > > > > The Indian Ganjifa deck uses ten "houses" (decks) of 12 cards each. > > > > Pinochle uses the standard four Western suits, but uses two copies of > > each card 9, 10, J, Q, K, A, for 48 cards in total. Sometimes people > > combine two pinochle decks for larger games, so with a double deck all > > ten cards could be Aces. > > > > In the 1930s, there was a fad for playing Bridge with a fifth suit, > > coloured green, called Royals, Leaves, Eagles or Rooks depending on the > > manufacturer. > > > > There are five- and six-suit versions of poker, with at least two still > > commercially available: the five-suit Stardeck pack (which adds Stars), > > and the six-suit Empire deck (which adds red Crowns and black Anchors). > > > > There is an eight-suit Euchre pack that adds red Moons, black Stars, red > > four-leaf Clovers and black Tears to the standard deck. > > > > > > > > -- > > Steven Thank you for the advice. It was really helpful with the descriptions and steps. Would you also happen to know how I could set up a list that keeps track of the removed sets? Let's say there's 4 a's taken out. The list would show: ['a'] But if there was also 4 j's in addition to the 4 a's, it would go: ['a', 'j'] and so forth. From kevin4fong at gmail.com Sat Aug 3 20:34:54 2013 From: kevin4fong at gmail.com (kevin4fong at gmail.com) Date: Sat, 3 Aug 2013 17:34:54 -0700 (PDT) Subject: Removing matching items from a list? In-Reply-To: <51fd9f6b$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <6c0bdea5-23bd-4854-8016-4bf0af3b7d0e@googlegroups.com> <51fd8635$0$30000$c3e8da3$5496439d@news.astraweb.com> <51fd9f6b$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: <94bd4f71-8a73-4a41-8278-ba67da4343c9@googlegroups.com> Thank you, Steven, for the advice. It was really helpful with the descriptions and steps. Would you also happen to know how I could set up a list that keeps track of the removed sets? Let's say there's 4 a's taken out. The list would show: ['a'] But if there was also 4 j's in addition to the 4 a's, it would go: ['a', 'j'] and so forth. From kevin4fong at gmail.com Sat Aug 3 20:23:42 2013 From: kevin4fong at gmail.com (kevin4fong at gmail.com) Date: Sat, 3 Aug 2013 17:23:42 -0700 (PDT) Subject: Removing matching items from a list? In-Reply-To: <51fd8635$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <6c0bdea5-23bd-4854-8016-4bf0af3b7d0e@googlegroups.com> <51fd8635$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: <3e61fd68-23b3-4368-aa29-42ca82bfed61@googlegroups.com> On Saturday, August 3, 2013 3:37:41 PM UTC-7, Steven D'Aprano wrote: > On Sat, 03 Aug 2013 13:12:55 -0700, kevin4fong wrote: > > > > > Basically, I'm trying to find out how to remove matching items from a > > > list. But there doesn't seem to be any information on how to go about > > > doing this specific function. > > > > The documentation cannot possibly cover every single one of the infinite > > number of things somebody might want to do with Python. Programming is > > about figuring out how to do the things you want from the tools provided > > by the language. > > > > > > > For example, what I want is: > > > > > > let's say there is a list: > > > > > > pHands[0] = ['ad', 'ac', 'as', 'ah', '7d', '8s', '9d', 'td', 'js', 'jd'] > > > > > > So up there, my list, which is named pHands[0] has ten items in it. > > > > Ten items? I wouldn't have known that if you didn't say so! *wink* > > > > But seriously... you don't need to repeat obvious things like that. We > > can count as well as you can. > > > > But more importantly, it is irrelevant. Is "ten items" important to this > > problem? No. What if there were nine, or thirty five, or seven? Would the > > problem of "removing matching items" change? No, the problem remains the > > same regardless of how many items there are. > > > > > > > I'm trying to make a function where a search is initiated into the list > > > and any matching items with a first matching number/letter reaching four > > > are removed > > > > In general, rather than removing items from a list, it is faster and > > safer (less error-prone) to create a new list with the items not removed. > > But either way, when trying to program, start by thinking about how *you* > > would do this task by hand: > > > > 1) I would start by counting how many items start with each initial > > letter: in your example I would count those starting with 'a' (4), > > '7' (1), and so on. > > > > 2) Then go through those initial letters, and pick out the ones equal to > > 4 (or should that be "four or more"?). In this case, only 'a' is 4 or > > more, but there could be multiple examples, say, 4 'a's and 6 'b's. > > > > 3) Then, either delete those items from the original list, *or* make a > > new list with the remaining items. Making a new list is usually better. > > > > Now start turning each step into Python code. > > > > Step 1: > > > > Iterate over each item, grab the first character of that item, and add > > one to a counter. > > > > counts = {} # Start with an empty dict. > > for item in pHands[0]: > > c = item[0] # Grab the first character. > > if c in counts: > > counts[c] += 1 > > else: > > counts[c] = 1 > > > > > > This sets the counter to 1 the first time each character is seen, > > otherwise increments the counter by 1. There's a ready-made tool for > > doing that: > > > > from collections import Counter > > counts = Counter(item[0] for item in pHands[0]) > > > > but under the hood, it's essentially doing the same thing as I wrote > > above. > > > > > > Steps 2 and 3 can be done together: > > > > new_list = [] > > for item in pHand[0]: > > c = item[0] > > # Look up the character in the counter. > > if counts[c] < 4: > > new_list.append(item) > > > > pHand[0] = new_list > > > > > > > > -- > > Steven Ah, all of the replies were useful but I found yours the most useful because of all the descriptions and in depth steps so I could actually understand what the code did. Thank you. Would you also happen to know how to fit in a simple count for each set of cards that is removed? For example, 4a's gone would set the count to 1 but if there was also 4j's, the count would be at 2. From kevin4fong at gmail.com Sat Aug 3 20:29:28 2013 From: kevin4fong at gmail.com (kevin4fong at gmail.com) Date: Sat, 3 Aug 2013 17:29:28 -0700 (PDT) Subject: Removing matching items from a list? In-Reply-To: <51fd8635$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <6c0bdea5-23bd-4854-8016-4bf0af3b7d0e@googlegroups.com> <51fd8635$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: <6ba5a0cf-a845-42f3-a08d-27828535df2a@googlegroups.com> On Saturday, August 3, 2013 3:37:41 PM UTC-7, Steven D'Aprano wrote: > On Sat, 03 Aug 2013 13:12:55 -0700, kevin4fong wrote: > > > > > Basically, I'm trying to find out how to remove matching items from a > > > list. But there doesn't seem to be any information on how to go about > > > doing this specific function. > > > > The documentation cannot possibly cover every single one of the infinite > > number of things somebody might want to do with Python. Programming is > > about figuring out how to do the things you want from the tools provided > > by the language. > > > > > > > For example, what I want is: > > > > > > let's say there is a list: > > > > > > pHands[0] = ['ad', 'ac', 'as', 'ah', '7d', '8s', '9d', 'td', 'js', 'jd'] > > > > > > So up there, my list, which is named pHands[0] has ten items in it. > > > > Ten items? I wouldn't have known that if you didn't say so! *wink* > > > > But seriously... you don't need to repeat obvious things like that. We > > can count as well as you can. > > > > But more importantly, it is irrelevant. Is "ten items" important to this > > problem? No. What if there were nine, or thirty five, or seven? Would the > > problem of "removing matching items" change? No, the problem remains the > > same regardless of how many items there are. > > > > > > > I'm trying to make a function where a search is initiated into the list > > > and any matching items with a first matching number/letter reaching four > > > are removed > > > > In general, rather than removing items from a list, it is faster and > > safer (less error-prone) to create a new list with the items not removed. > > But either way, when trying to program, start by thinking about how *you* > > would do this task by hand: > > > > 1) I would start by counting how many items start with each initial > > letter: in your example I would count those starting with 'a' (4), > > '7' (1), and so on. > > > > 2) Then go through those initial letters, and pick out the ones equal to > > 4 (or should that be "four or more"?). In this case, only 'a' is 4 or > > more, but there could be multiple examples, say, 4 'a's and 6 'b's. > > > > 3) Then, either delete those items from the original list, *or* make a > > new list with the remaining items. Making a new list is usually better. > > > > Now start turning each step into Python code. > > > > Step 1: > > > > Iterate over each item, grab the first character of that item, and add > > one to a counter. > > > > counts = {} # Start with an empty dict. > > for item in pHands[0]: > > c = item[0] # Grab the first character. > > if c in counts: > > counts[c] += 1 > > else: > > counts[c] = 1 > > > > > > This sets the counter to 1 the first time each character is seen, > > otherwise increments the counter by 1. There's a ready-made tool for > > doing that: > > > > from collections import Counter > > counts = Counter(item[0] for item in pHands[0]) > > > > but under the hood, it's essentially doing the same thing as I wrote > > above. > > > > > > Steps 2 and 3 can be done together: > > > > new_list = [] > > for item in pHand[0]: > > c = item[0] > > # Look up the character in the counter. > > if counts[c] < 4: > > new_list.append(item) > > > > pHand[0] = new_list > > > > > > > > -- > > Steven Thank you. I found all the replies useful but yours the most useful because of the descriptions and indepth steps so I could understand what the code did. Would you also happen to know how I could go about setting up a list that keeps track of the removed sets? For example, if there were 4 a's removed, that would be one set. And the list would be: ['a'] but if there was also 4j's in addition to the a's. It would be: ['a', 'j'] And so forth.... From steve+comp.lang.python at pearwood.info Sat Aug 3 20:40:51 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 04 Aug 2013 00:40:51 GMT Subject: Removing matching items from a list? References: <6c0bdea5-23bd-4854-8016-4bf0af3b7d0e@googlegroups.com> <51fd8635$0$30000$c3e8da3$5496439d@news.astraweb.com> <6ba5a0cf-a845-42f3-a08d-27828535df2a@googlegroups.com> Message-ID: <51fda313$0$30000$c3e8da3$5496439d@news.astraweb.com> On Sat, 03 Aug 2013 17:29:28 -0700, kevin4fong wrote: > Would you also happen to know how I could go about setting up a list > that keeps track of the removed sets? > > For example, if there were 4 a's removed, that would be one set. And the > list would be: > > ['a'] > > but if there was also 4j's in addition to the a's. It would be: > > ['a', 'j'] > > And so forth.... How would *you* do it in person? Start with that, then turn it into Python code. For example, you might count the cards first, as in my previous post, then look at each counter. If the counter is four (or greater), add it to a list of cards to be removed. # counter is a dict like {'a': 4, '7': 1} as per previous post cards_to_remove = [] for card in counter: if counter[card] >= 4: cards_to_remove.append(card) Or you could do this at the same time as you build the counter. I leave it to you to work out how to do that. The important thing is, you have to start by asking the question, "How would I do this task?" before you can write Python code for it. -- Steven From kevin4fong at gmail.com Sat Aug 3 20:32:20 2013 From: kevin4fong at gmail.com (kevin4fong at gmail.com) Date: Sat, 3 Aug 2013 17:32:20 -0700 (PDT) Subject: Removing matching items from a list? In-Reply-To: <51fd8635$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <6c0bdea5-23bd-4854-8016-4bf0af3b7d0e@googlegroups.com> <51fd8635$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Saturday, August 3, 2013 3:37:41 PM UTC-7, Steven D'Aprano wrote: > On Sat, 03 Aug 2013 13:12:55 -0700, kevin4fong wrote: > > > > > Basically, I'm trying to find out how to remove matching items from a > > > list. But there doesn't seem to be any information on how to go about > > > doing this specific function. > > > > The documentation cannot possibly cover every single one of the infinite > > number of things somebody might want to do with Python. Programming is > > about figuring out how to do the things you want from the tools provided > > by the language. > > > > > > > For example, what I want is: > > > > > > let's say there is a list: > > > > > > pHands[0] = ['ad', 'ac', 'as', 'ah', '7d', '8s', '9d', 'td', 'js', 'jd'] > > > > > > So up there, my list, which is named pHands[0] has ten items in it. > > > > Ten items? I wouldn't have known that if you didn't say so! *wink* > > > > But seriously... you don't need to repeat obvious things like that. We > > can count as well as you can. > > > > But more importantly, it is irrelevant. Is "ten items" important to this > > problem? No. What if there were nine, or thirty five, or seven? Would the > > problem of "removing matching items" change? No, the problem remains the > > same regardless of how many items there are. > > > > > > > I'm trying to make a function where a search is initiated into the list > > > and any matching items with a first matching number/letter reaching four > > > are removed > > > > In general, rather than removing items from a list, it is faster and > > safer (less error-prone) to create a new list with the items not removed. > > But either way, when trying to program, start by thinking about how *you* > > would do this task by hand: > > > > 1) I would start by counting how many items start with each initial > > letter: in your example I would count those starting with 'a' (4), > > '7' (1), and so on. > > > > 2) Then go through those initial letters, and pick out the ones equal to > > 4 (or should that be "four or more"?). In this case, only 'a' is 4 or > > more, but there could be multiple examples, say, 4 'a's and 6 'b's. > > > > 3) Then, either delete those items from the original list, *or* make a > > new list with the remaining items. Making a new list is usually better. > > > > Now start turning each step into Python code. > > > > Step 1: > > > > Iterate over each item, grab the first character of that item, and add > > one to a counter. > > > > counts = {} # Start with an empty dict. > > for item in pHands[0]: > > c = item[0] # Grab the first character. > > if c in counts: > > counts[c] += 1 > > else: > > counts[c] = 1 > > > > > > This sets the counter to 1 the first time each character is seen, > > otherwise increments the counter by 1. There's a ready-made tool for > > doing that: > > > > from collections import Counter > > counts = Counter(item[0] for item in pHands[0]) > > > > but under the hood, it's essentially doing the same thing as I wrote > > above. > > > > > > Steps 2 and 3 can be done together: > > > > new_list = [] > > for item in pHand[0]: > > c = item[0] > > # Look up the character in the counter. > > if counts[c] < 4: > > new_list.append(item) > > > > pHand[0] = new_list > > > > > > > > -- > > Steven Thank you for the advice. It was really helpful with the descriptions and steps. Would you also happen to know how I could set up a list that keeps track of the removed sets? Let's say there's 4 a's taken out. The list would show: ['a'] But if there was also 4 j's in addition to the 4 a's, it would go: ['a', 'j'] and so forth. From kevin4fong at gmail.com Sat Aug 3 20:33:54 2013 From: kevin4fong at gmail.com (kevin4fong at gmail.com) Date: Sat, 3 Aug 2013 17:33:54 -0700 (PDT) Subject: Removing matching items from a list? In-Reply-To: <6c0bdea5-23bd-4854-8016-4bf0af3b7d0e@googlegroups.com> References: <6c0bdea5-23bd-4854-8016-4bf0af3b7d0e@googlegroups.com> Message-ID: On Saturday, August 3, 2013 1:12:55 PM UTC-7, kevin... at gmail.com wrote: > Basically, I'm trying to find out how to remove matching items from a list. But there doesn't seem to be any information on how to go about doing this specific function. > > > > For example, what I want is: > > > > let's say there is a list: > > > > pHands[0] = ['ad', 'ac', 'as', 'ah', '7d', '8s', '9d', 'td', 'js', 'jd'] > > > > So up there, my list, which is named pHands[0] has ten items in it. > > > > I'm trying to make a function where a search is initiated into the list and any matching items with a first matching number/letter reaching four are removed > > > > So in the end, ad, ac, as, ah (the four a's) will all be deleted/removed from the list. I need the list to automatically detect if there are four matching first letter/numbers in the items in the list. > > > > The remaining list will be: pHands[0] = ['7d', '8s', '9d', 'td', 'js', 'jd'] Thank you for the advice. It was really helpful with the descriptions and steps. Would you also happen to know how I could set up a list that keeps track of the removed sets? Let's say there's 4 a's taken out. The list would show: ['a'] But if there was also 4 j's in addition to the 4 a's, it would go: ['a', 'j'] and so forth. From davea at davea.name Sat Aug 3 21:53:02 2013 From: davea at davea.name (Dave Angel) Date: Sun, 4 Aug 2013 01:53:02 +0000 (UTC) Subject: Removing matching items from a list? References: <6c0bdea5-23bd-4854-8016-4bf0af3b7d0e@googlegroups.com> Message-ID: kevin4fong at gmail.com wrote: > > Would you also happen to know how I could set up a list that keeps track of the removed sets? > Let's see, i think that makes 5 times you've asked the same question, counting the dups you apparently sent to the same person. Instead of writing all these messages, could you perhaps actually write some code? Show us that you've learned something, and that you actually tried to get the next answer? Frame your question around one of the following scenarios: Here's some code I wrote, and it doesn't work because: 1) it gets an exception (show full traceback) 2) it hangs 3) it gets this answer: show actual answer instead of what I expected: show what you expected to get And when you post the code, paste it into your message, don't point to some temporary website. And if you must use googlegroups, please edit your messages to hide the bugs in googlegarbage. Go into the message and tediously delete all the stupid extra blank lines, because one person doing it is better than 1000 people seeing double-spaced nonsense. Many people just won't see a message sent from googlegroups because they filter them out. -- DaveA From kevin4fong at gmail.com Sat Aug 3 20:36:42 2013 From: kevin4fong at gmail.com (kevin4fong at gmail.com) Date: Sat, 3 Aug 2013 17:36:42 -0700 (PDT) Subject: Removing matching items from a list? Message-ID: I have no idea why but I'm unable to post in my original thread so I made a new one here. Thank you for the advice, everyone, especially Steven. It was really helpful with the descriptions and steps. Would you also happen to know how I could set up a list that keeps track of the removed sets? Let's say there's 4 a's taken out. The list would show: ['a'] But if there was also 4 j's in addition to the 4 a's, it would go: ['a', 'j'] and so forth. From kevin4fong at gmail.com Sat Aug 3 20:38:56 2013 From: kevin4fong at gmail.com (kevin4fong at gmail.com) Date: Sat, 3 Aug 2013 17:38:56 -0700 (PDT) Subject: Removing matching items from a list? In-Reply-To: References: Message-ID: On Saturday, August 3, 2013 5:36:42 PM UTC-7, kevin... at gmail.com wrote: > I have no idea why but I'm unable to post in my original thread so I made a new one here. > > > > > > Thank you for the advice, everyone, especially Steven. It was really helpful with the descriptions and steps. > > > > Would you also happen to know how I could set up a list that keeps track of the removed sets? > > > > Let's say there's 4 a's taken out. The list would show: > > > > ['a'] > > > > But if there was also 4 j's in addition to the 4 a's, it would go: > > > > ['a', 'j'] > > > > and so forth. Sorry for the repeated messages. I have no idea why I have such a long time delay. My messages didn't appear until just now after a few minutes (thought I was having some issues). From auriocus at gmx.de Sun Aug 4 02:52:51 2013 From: auriocus at gmx.de (Christian Gollwitzer) Date: Sun, 04 Aug 2013 08:52:51 +0200 Subject: Removing matching items from a list? In-Reply-To: References: Message-ID: Hi Kevin, Am 04.08.13 02:38, schrieb kevin4fong at gmail.com: > Sorry for the repeated messages. I have no idea why I have such a > long time delay. My messages didn't appear until just now after a few > minutes (thought I was having some issues). > you are posting to newsgroups from the USENET. It is typical that your posts need some time, and more time is needed for the message to show up *for other people* - many people see your message only after an hour or similar. Concerning your question, > Let's say there's 4 a's taken out. The list would show: > ['a'] > But if there was also 4 j's in addition to the 4 a's, it would go: > ['a', 'j'] well, when you take them out, as Steven has shown, you can also store them in an additional list, like takeouts.append(removedchar) If you are only interested in the number, you could count the items in the original list and subtract the number of items in you final list. Christian From sofia_prata at hotmail.com Sat Aug 3 16:31:03 2013 From: sofia_prata at hotmail.com (sofia prata) Date: Sat, 3 Aug 2013 17:31:03 -0300 Subject: Python 2.7.5 incompatible Message-ID: I use the Windows 7 and i'm trying to install the Python 2.7.5, even though it's working, it says it not compatible. So then, i can't use other programs that depend upon the Python like the Google App Engine. What do i do? -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Sat Aug 3 18:32:11 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 Aug 2013 22:32:11 GMT Subject: Python 2.7.5 incompatible References: Message-ID: <51fd84ea$0$30000$c3e8da3$5496439d@news.astraweb.com> Hi Sofia, and welcome! On Sat, 03 Aug 2013 17:31:03 -0300, sofia prata wrote: > I use the Windows 7 and i'm trying to install the Python 2.7.5, even > though it's working, it says it not compatible. So then, i can't use > other programs that depend upon the Python like the Google App Engine. > What do i do? Start by explaining the problem in more detail. We cannot see your computer or see what you are doing. How do you know Python is working? How are you running Python? Explain the steps that you use, e.g.: "I go to the Start Menu, choose Python 2.7>IDLE, then the IDLE window opens. If I type code in IDLE, the code runs correctly." Or: "I double-click my program 'test.py', and it runs the way I expect." Then explain what says Python is not compatible. What did you do to get that message? Where is the error message? Is it in IDLE? In a Windows dialog box? Somewhere else? If this is a Python error, it will almost certainly start with a line like: Traceback (most recent call last) and continue until an error like ImportError ValueError TypeError etc. followed by a message. Please COPY AND PASTE the entire traceback, starting from the first line, not just the last. Don't retype from memory or summarize it. If the error is somewhere else, and you cannot copy and paste it, then retype it *exactly* unless it is unreasonably long. If necessary, but only if absolutely necessary, try taking a screenshot and posting that here. But 99 times out of 100, you should be able to copy and paste the text, or at least re-type it if it is short. That will do for a start. If any of my instructions are unclear, feel free to ask for more help, but remember, more detail is usually better than vague comments. Good luck! -- Steven From tjreedy at udel.edu Sat Aug 3 20:00:58 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 03 Aug 2013 20:00:58 -0400 Subject: Python 2.7.5 incompatible In-Reply-To: <51fd84ea$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <51fd84ea$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 8/3/2013 6:32 PM, Steven D'Aprano wrote: > Hi Sofia, and welcome! > > > On Sat, 03 Aug 2013 17:31:03 -0300, sofia prata wrote: > >> I use the Windows 7 and i'm trying to install the Python 2.7.5, even >> though it's working, it says it not compatible. So then, i can't use >> other programs that depend upon the Python like the Google App Engine. >> What do i do? > > Start by explaining the problem in more detail. We cannot see your > computer or see what you are doing. > > How do you know Python is working? How are you running Python? Explain > the steps that you use, e.g.: > > "I go to the Start Menu, choose Python 2.7>IDLE, then the IDLE window > opens. If I type code in IDLE, the code runs correctly." > > Or: > > "I double-click my program 'test.py', and it runs the way I expect." > > > Then explain what says Python is not compatible. What did you do to get > that message? Where is the error message? Is it in IDLE? In a Windows > dialog box? Somewhere else? > > If this is a Python error, it will almost certainly start with a line > like: > > Traceback (most recent call last) > > and continue until an error like > > ImportError > ValueError > TypeError > > etc. followed by a message. Please COPY AND PASTE the entire traceback, > starting from the first line, not just the last. Don't retype from memory > or summarize it. > > If the error is somewhere else, and you cannot copy and paste it, then > retype it *exactly* unless it is unreasonably long. > > If necessary, but only if absolutely necessary, try taking a screenshot > and posting that here. But 99 times out of 100, you should be able to > copy and paste the text, or at least re-type it if it is short. > > That will do for a start. If any of my instructions are unclear, feel > free to ask for more help, but remember, more detail is usually better > than vague comments. In addition to what Steven said, what did you already have installed before you installed 2.7.5. Here is a guess. You had and still have 2.5 or 2.6 installed as the default python. You installed 2.7.5 and left the "[x] make this the default python" box checked. ??? -- Terry Jan Reedy From eschneider92 at comcast.net Sat Aug 3 18:17:42 2013 From: eschneider92 at comcast.net (eschneider92 at comcast.net) Date: Sat, 3 Aug 2013 15:17:42 -0700 (PDT) Subject: Print word from list Message-ID: <5711a28d-d180-4882-a2d3-c68eb958f06d@googlegroups.com> pie='apple keylime pecan meat pot cherry' pie.split() How can I print a word from the list other than this way: print(pie[0:5]) ? Thanks in advance. From rosuav at gmail.com Sat Aug 3 18:22:40 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 3 Aug 2013 23:22:40 +0100 Subject: Print word from list In-Reply-To: <5711a28d-d180-4882-a2d3-c68eb958f06d@googlegroups.com> References: <5711a28d-d180-4882-a2d3-c68eb958f06d@googlegroups.com> Message-ID: On Sat, Aug 3, 2013 at 11:17 PM, wrote: > pie='apple keylime pecan meat pot cherry' > pie.split() > > How can I print a word from the list other than this way: print(pie[0:5]) ? The split() method returns a list, it doesn't change the original string. Try: pies = pie.split() print(pie[2]) ChrisA From eschneider92 at comcast.net Sat Aug 3 18:30:37 2013 From: eschneider92 at comcast.net (eschneider92 at comcast.net) Date: Sat, 3 Aug 2013 15:30:37 -0700 (PDT) Subject: Print word from list In-Reply-To: <5711a28d-d180-4882-a2d3-c68eb958f06d@googlegroups.com> References: <5711a28d-d180-4882-a2d3-c68eb958f06d@googlegroups.com> Message-ID: <0008ce24-53c3-4e7a-b48d-3996ab9adfcc@googlegroups.com> Thanks! From steve+comp.lang.python at pearwood.info Sat Aug 3 18:35:32 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 Aug 2013 22:35:32 GMT Subject: Print word from list References: <5711a28d-d180-4882-a2d3-c68eb958f06d@googlegroups.com> Message-ID: <51fd85b4$0$30000$c3e8da3$5496439d@news.astraweb.com> On Sat, 03 Aug 2013 15:17:42 -0700, eschneider92 wrote: > pie='apple keylime pecan meat pot cherry' That sets pie to a string containing multiple words. > pie.split() This splits pie into individual words, then immediately throws the result away, leaving pie still set to a string. Instead, you want to do this: pie = pie.split() Now pie will be a list of individual words. You can print the entire list: print(pie) or print each word separately: for filling in pie: print(filling) Or pick out one specific filling: print(pie[2]) # should print 'pecan' -- Steven From francois.lafont at nospam.invalid Sat Aug 3 22:10:35 2013 From: francois.lafont at nospam.invalid (Francois Lafont) Date: Sun, 04 Aug 2013 04:10:35 +0200 Subject: [argparse] mutually exclusive group with 2 sets of options Message-ID: <51fdb81b$0$2423$426a74cc@news.free.fr> Hi, Is it possible with argparse to have this syntax for a script? my-script (-a -b VALUE-B | -c -d VALUE-D) I would like to do this with the argparse module. Thanks in advance. -- Fran?ois Lafont From francois.lafont at nospam.invalid Sun Aug 4 22:05:53 2013 From: francois.lafont at nospam.invalid (Francois Lafont) Date: Mon, 05 Aug 2013 04:05:53 +0200 Subject: [argparse] mutually exclusive group with 2 sets of options In-Reply-To: <51fdb81b$0$2423$426a74cc@news.free.fr> References: <51fdb81b$0$2423$426a74cc@news.free.fr> Message-ID: <51ff0881$0$2218$426a74cc@news.free.fr> Hello, Up. ;-) Le 04/08/2013 04:10, Francois Lafont a ?crit : > Is it possible with argparse to have this syntax for a script? > > my-script (-a -b VALUE-B | -c -d VALUE-D) > > I would like to do this with the argparse module. > > Thanks in advance. I have found this post: https://groups.google.com/forum/#!searchin/argparse-users/exclusive/argparse-users/-o6GOwhCjbQ/m-PfL4OxLAIJ It was in 2011 and at that time, it was impossible to have the syntax above. I have the impression that it's impossible now yet. Am I wrong? If it's impossible yet, I could try a hack. With some checks, I think I could have the "(-a -b VALUE-B | -c -d VALUE-D)" behavior but I would like this syntax appear in the help output too and I have no idea to do it. -- Fran?ois Lafont From joshua at landau.ws Sun Aug 4 23:53:43 2013 From: joshua at landau.ws (Joshua Landau) Date: Mon, 5 Aug 2013 04:53:43 +0100 Subject: [argparse] mutually exclusive group with 2 sets of options In-Reply-To: <51ff0881$0$2218$426a74cc@news.free.fr> References: <51fdb81b$0$2423$426a74cc@news.free.fr> <51ff0881$0$2218$426a74cc@news.free.fr> Message-ID: On 5 August 2013 03:05, Francois Lafont wrote: > Hello, > > Up. ;-) > > Le 04/08/2013 04:10, Francois Lafont a ?crit : > > > Is it possible with argparse to have this syntax for a script? > > > > my-script (-a -b VALUE-B | -c -d VALUE-D) > > > > I would like to do this with the argparse module. > > > > Thanks in advance. > > I have found this post: > > https://groups.google.com/forum/#!searchin/argparse-users/exclusive/argparse-users/-o6GOwhCjbQ/m-PfL4OxLAIJ > > It was in 2011 and at that time, it was impossible to have the syntax > above. I have the impression that it's impossible now yet. Am I wrong? > > If it's impossible yet, I could try a hack. With some checks, I think I > could have the "(-a -b VALUE-B | -c -d VALUE-D)" behavior but I would like > this syntax appear in the help output too and I have no idea to do it. If possible you could try docopt. That should be able to do it quite easily. You could write literally: My-Script Usage: my-script (-a -b VALUE-B | -c -d VALUE-D) Options: -b VALUE-B Description -d VALUE-D Description as the grammar, but: My-Script Usage: my-script -a -b VALUE-B my-script -c -d VALUE-D Options: -b VALUE-B Description -d VALUE-D Description would be preferred. -------------- next part -------------- An HTML attachment was scrubbed... URL: From miki.tebeka at gmail.com Mon Aug 5 10:11:01 2013 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Mon, 5 Aug 2013 07:11:01 -0700 (PDT) Subject: [argparse] mutually exclusive group with 2 sets of options In-Reply-To: <51fdb81b$0$2423$426a74cc@news.free.fr> References: <51fdb81b$0$2423$426a74cc@news.free.fr> Message-ID: <6db08670-52e8-4ca2-aff4-82d611a29da6@googlegroups.com> > Is it possible with argparse to have this syntax for a script? > my-script (-a -b VALUE-B | -c -d VALUE-D) > > I would like to do this with the argparse module. You can probably do something similar using sub commands (http://docs.python.org/2/library/argparse.html#sub-commands). From jcasale at activenetwerx.com Mon Aug 5 13:11:30 2013 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Mon, 5 Aug 2013 17:11:30 +0000 Subject: [argparse] mutually exclusive group with 2 sets of options In-Reply-To: <6db08670-52e8-4ca2-aff4-82d611a29da6@googlegroups.com> References: <51fdb81b$0$2423$426a74cc@news.free.fr> <6db08670-52e8-4ca2-aff4-82d611a29da6@googlegroups.com> Message-ID: > You can probably do something similar using sub commands > (http://docs.python.org/2/library/argparse.html#sub-commands). The problem here is that argparse does not pass the subparser into the parsed args and shared args between subparsers need to be declared each time. Come execution time, when you have shared args you end up testing for various incantations of the invoked code, you're better off omitting subparsers and performing conditional tests after parsing for incompatible combinations. It's a waste of typing to write out a mode followed by params to achieve the ops goal, I've hit the same limitation. Certainly a weakness of argparse in my opinion. It's also a tough sell to force a user to install a package just for the cli if you go with docopt. I'd love to see argparse expand or docopt get included... jlc From francois.lafont at nospam.invalid Mon Aug 5 18:13:16 2013 From: francois.lafont at nospam.invalid (Francois Lafont) Date: Tue, 06 Aug 2013 00:13:16 +0200 Subject: [argparse] mutually exclusive group with 2 sets of options In-Reply-To: <6db08670-52e8-4ca2-aff4-82d611a29da6@googlegroups.com> References: <51fdb81b$0$2423$426a74cc@news.free.fr> <6db08670-52e8-4ca2-aff4-82d611a29da6@googlegroups.com> Message-ID: <5200237c$0$2103$426a74cc@news.free.fr> Le 05/08/2013 16:11, Miki Tebeka a ?crit : > You can probably do something similar using sub commands (http://docs.python.org/2/library/argparse.html#sub-commands). Yes, but this is not the same syntax. I want this syntax : my-script (-a -b VALUE-B | -c -d VALUE-D) I don't want this syntax: my-script (subcommad-a -b VALUE-B | subcommand-c -d VALUE-D) In fact, in this post, I have simplified my question to put the stress just on my problem. In the real life, my script already uses the subcommands (and no problem with that). But I need to have mutually exclusive groups with 2 *sets* of options for compatibility reasons with another programs. -- Fran?ois Lafont From rafadurancastaneda at gmail.com Mon Aug 5 16:01:22 2013 From: rafadurancastaneda at gmail.com (=?UTF-8?B?UmFmYWVsIER1csOhbiBDYXN0YcOxZWRh?=) Date: Mon, 05 Aug 2013 22:01:22 +0200 Subject: [argparse] mutually exclusive group with 2 sets of options In-Reply-To: <51fdb81b$0$2423$426a74cc@news.free.fr> References: <51fdb81b$0$2423$426a74cc@news.free.fr> Message-ID: <52000492.5030502@gmail.com> El 04/08/13 04:10, Francois Lafont escribi?: > Hi, > > Is it possible with argparse to have this syntax for a script? > > my-script (-a -b VALUE-B | -c -d VALUE-D) > > I would like to do this with the argparse module. > > Thanks in advance. > > I think you are looking for exclusive groups: http://docs.python.org/2.7/library/argparse.html#argparse.add_mutually_exclusive_group HTH From jcasale at activenetwerx.com Mon Aug 5 18:15:47 2013 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Mon, 5 Aug 2013 22:15:47 +0000 Subject: [argparse] mutually exclusive group with 2 sets of options In-Reply-To: <52000492.5030502@gmail.com> References: <51fdb81b$0$2423$426a74cc@news.free.fr> <52000492.5030502@gmail.com> Message-ID: > I think you are looking for exclusive groups: > > http://docs.python.org/2.7/library/argparse.html#argparse.add_mutually_excl > usive_group No. That links first doc line in that method shows the very point we are all discussing: "Create a mutually exclusive group. argparse will make sure that only one of the arguments in the mutually exclusive group was present on the command line:" Op requires more than one per group, this method "make sure that only one of the arguments" is accepted. jlc From francois.lafont at nospam.invalid Mon Aug 5 18:34:16 2013 From: francois.lafont at nospam.invalid (Francois Lafont) Date: Tue, 06 Aug 2013 00:34:16 +0200 Subject: [argparse] mutually exclusive group with 2 sets of options In-Reply-To: References: <51fdb81b$0$2423$426a74cc@news.free.fr> Message-ID: <52002868$0$2551$426a74cc@news.free.fr> Le 05/08/2013 22:01, Rafael Dur?n Casta?eda a ?crit : > I think you are looking for exclusive groups: > > http://docs.python.org/2.7/library/argparse.html#argparse.add_mutually_exclusive_group Yes... but no. The doc explains you can do this: my-script (-b VALUE-B | -d VALUE-D) ie mutally exclusive group with *just* *one* option incompatible with another one. But, apparently, you can't do that: my-script (-a -b VALUE-B | -c -d VALUE-D) ie mutually exclusive group with one *set* of option*s* incompatible with another *set* of option*s*. This is why I have posted my message. I have read the documentation before to post it. ;-) I know docopt but I prefer argparse. My script have subcommands and some of them have common options, so I appreciate the parser objects and the inheritance between parser objects (with parents parameter of argparse.ArgumentParser class). -- Fran?ois Lafont From francois.lafont at nospam.invalid Tue Aug 6 19:18:17 2013 From: francois.lafont at nospam.invalid (Francois Lafont) Date: Wed, 07 Aug 2013 01:18:17 +0200 Subject: [argparse] mutually exclusive group with 2 sets of options In-Reply-To: <52002868$0$2551$426a74cc@news.free.fr> References: <51fdb81b$0$2423$426a74cc@news.free.fr> <52002868$0$2551$426a74cc@news.free.fr> Message-ID: <5201843a$0$2051$426a34cc@news.free.fr> Hi, On relfection, it's clear that: 1. the "(-a -b VALUE-B | -c -d VALUE-D)" syntax is not implemented by the argparse module; 2. and get this syntax with "argparse + hacking" is not very clean. So, finally I'll use the docopt module version 0.6.1. For the inheritance of common options, I'll used something like that (even if I prefer the oriented object side of the argparse module): [the main file] #----------------------------------- import importlib import sys from docopt import docopt help_message =""" Usage: my-script (-h | --help) my-script [...] Options: -h, --help Show this help message and exit. Available commands: command1 Description1... command2 Description2... See 'my-script -h' for more information on a specific command. """ if __name__ == '__main__': args = docopt(help_message, options_first=True) command = args[''] try: m = importlib.import_module('mymodule.' + command) except ImportError: print("Sorry, the %s command doesn't exist. See 'my-script -h'.") % (command,) sys.exit(1) args = docopt(m.help_message, options_first=False) m.run(args) #----------------------------------- [a file for each specific command, mymodule/command1.py etc.] #----------------------------------- import mymodule.common as common help_message = """ Usage: my-script subcommand1 (-h | --help) my-script subcommand1 %s -w -c Options: %s -w , --warning= Some help. -c , --critical= Some help. """ % (common.common_syntax, common.common_help,) def run(args): pass #----------------------------------- [and a file for the common syntax, mymodule/common.py] #----------------------------------- common_syntax = """-H -t (--v2c -C | -l -x -X -L )""" common_help = """ -h, --help Show this help message and exit. -H , --host= Some help. -t , --timeout= Some help. --v2c Some help. -C , --community= Set the community password for SNMP V2c. # etc. # etc. """ #----------------------------------- Thank you all. -- Fran?ois Lafont From francois.lafont at nospam.invalid Tue Aug 6 19:36:42 2013 From: francois.lafont at nospam.invalid (Francois Lafont) Date: Wed, 07 Aug 2013 01:36:42 +0200 Subject: [argparse] mutually exclusive group with 2 sets of options In-Reply-To: <5201843a$0$2051$426a34cc@news.free.fr> References: <51fdb81b$0$2423$426a74cc@news.free.fr> <52002868$0$2551$426a74cc@news.free.fr> <5201843a$0$2051$426a34cc@news.free.fr> Message-ID: <5201888a$0$2224$426a34cc@news.free.fr> Le 07/08/2013 01:18, Francois Lafont a ?crit : > For the inheritance of common options, I'll used something like > that (even if I prefer the oriented object side of the argparse > module): But I admit that this is a very simple and intelligent module. ;-) -- Fran?ois Lafont From naruto0.1 at live.cn Sun Aug 4 12:06:46 2013 From: naruto0.1 at live.cn (Jacknaruto) Date: Mon, 5 Aug 2013 00:06:46 +0800 Subject: In pyqt, some signals seems not work well Message-ID: Hi, Guys! I created a thread class based QThread, and defined some signals to update UI through main thread. the UI used a stackedWidget,someone page named 'progressPage' have a progressBar and a Label, the next page named 'resultsPage' shows results(some Labels). When the window showed progressPage, my code that updates progressBar was well, but the updating resultsPage's labels signals seems ignored by main thread.(I guess, because I added some prints in the slot, it wasn't printed) the code like this:class worker(QtCore.QThread): sig_jump_finish_page = QtCore.pyqtSignal() sig_set_label_text = QtCore.pyqtSignal(str, str) sig_set_progress_value = QtCore.pyqtSignal(int) for dirpath, dirnames, filenames in os.walk(self.root): for filename in filenames: self.sig_set_label_text.emit('current', self.current()) # work well # so something #self.dialog.set_found(str(self.found())) # work well #self.dialog.set_all(str(self.total())) # work well self.sig_set_label_text.emit('found', str(self.found())) # :( self.sig_set_label_text.emit('all', str(self.total())) # :( self.sig_jump_finish_page.emit() So I have to update UI directly in the thread(comments Line 11-12 shows), it looks work well. I found the difference between line 8 and line 13-14 is line 8 manipulates current page -progressPage, line 13-14 manipulates next page -resultsPage. Is some wrongs in there? -------------- next part -------------- An HTML attachment was scrubbed... URL: From vincent.vandevyvre at swing.be Sun Aug 4 14:20:56 2013 From: vincent.vandevyvre at swing.be (Vincent Vande Vyvre) Date: Sun, 04 Aug 2013 20:20:56 +0200 Subject: In pyqt, some signals seems not work well In-Reply-To: References: Message-ID: <51FE9B88.8090706@swing.be> Le 04/08/2013 18:06, Jacknaruto a ?crit : > Hi, Guys! > > I created a thread class based QThread, and defined some signals to > update UI through main thread. > > the UI used a stackedWidget,someone page named 'progressPage' have a > progressBar and a Label, the next page named 'resultsPage' shows > results(some Labels). > > When the window showed progressPage, my code that updates progressBar > was well, but the updating resultsPage's labels signals seems ignored > by main thread.(I guess, because I added some prints in the slot, it > wasn't printed) > > the code like this: > > 1. class worker(QtCore.QThread): > 2. sig_jump_finish_page = QtCore.pyqtSignal() > 3. sig_set_label_text = QtCore.pyqtSignal(str, str) > 4. sig_set_progress_value = QtCore.pyqtSignal(int) > 5. > > 6. for dirpath, dirnames, filenames in os.walk(self.root): > 7. for filename in filenames: > 8. self.sig_set_label_text.emit('current', self.current()) # work well > 9. # so something >10. > > 11. #self.dialog.set_found(str(self.found())) # work well > 12. #self.dialog.set_all(str(self.total())) # work well > 13. self.sig_set_label_text.emit('found', str(self.found())) # :( > 14. self.sig_set_label_text.emit('all', str(self.total())) # :( >15. > > 16. self.sig_jump_finish_page.emit() > > > So I have to update UI directly in the thread(comments Line 11-12 > shows), it looks work well. > > I found the difference between line 8 and line 13-14 is line 8 > manipulates current page -progressPage, line 13-14 manipulates next > page -resultsPage. > > Is some wrongs in there? > > > > > Have a look at my comments: class worker(QtCore.QThread): sig_jump_finish_page = QtCore.pyqtSignal() sig_set_label_text = QtCore.pyqtSignal(str, str) sig_set_progress_value = QtCore.pyqtSignal(int) for dirpath, dirnames, filenames in os.walk(self.root): # What is 'self' here ? for filename in filenames: self.sig_set_label_text.emit('current', self.current()) # where is 'current' ? # so something #self.dialog.set_found(str(self.found())) # What is self, and dialog, and found ? #self.dialog.set_all(str(self.total())) # ... self.sig_set_label_text.emit('found', str(self.found())) # again ? self.sig_set_label_text.emit('all', str(self.total())) # etc self.sig_jump_finish_page.emit() -- Vincent V.V. Oqapy . Qarte . PaQager From naruto0.1 at live.cn Sun Aug 4 21:37:46 2013 From: naruto0.1 at live.cn (Jack) Date: Mon, 5 Aug 2013 09:37:46 +0800 Subject: In pyqt, some signals seems not work well In-Reply-To: <51FE9B88.8090706@swing.be> References: <51FE9B88.8090706@swing.be> Message-ID: On 2013/8/5 2:20, Vincent Vande Vyvre wrote: > Le 04/08/2013 18:06, Jacknaruto a ?crit : >> Hi, Guys! >> >> I created a thread class based QThread, and defined some signals to >> update UI through main thread. >> >> the UI used a stackedWidget,someone page named 'progressPage' have a >> progressBar and a Label, the next page named 'resultsPage' shows >> results(some Labels). >> >> When the window showed progressPage, my code that updates progressBar >> was well, but the updating resultsPage's labels signals seems ignored >> by main thread.(I guess, because I added some prints in the slot, it >> wasn't printed) >> >> the code like this: >> >> 1. class worker(QtCore.QThread): >> 2. sig_jump_finish_page = QtCore.pyqtSignal() >> 3. sig_set_label_text = QtCore.pyqtSignal(str, str) >> 4. sig_set_progress_value = QtCore.pyqtSignal(int) >> 5. >> >> 6. for dirpath, dirnames, filenames in os.walk(self.root): >> 7. for filename in filenames: >> 8. self.sig_set_label_text.emit('current', self.current()) # work well >> 9. # so something >> 10. >> >> 11. #self.dialog.set_found(str(self.found())) # work well >> 12. #self.dialog.set_all(str(self.total())) # work well >> 13. self.sig_set_label_text.emit('found', str(self.found())) # :( >> 14. self.sig_set_label_text.emit('all', str(self.total())) # :( >> 15. >> >> 16. self.sig_jump_finish_page.emit() >> >> >> So I have to update UI directly in the thread(comments Line 11-12 >> shows), it looks work well. >> >> I found the difference between line 8 and line 13-14 is line 8 >> manipulates current page -progressPage, line 13-14 manipulates next >> page -resultsPage. >> >> Is some wrongs in there? >> >> >> >> >> > Have a look at my comments: > > class worker(QtCore.QThread): > sig_jump_finish_page = QtCore.pyqtSignal() > sig_set_label_text = QtCore.pyqtSignal(str, str) > sig_set_progress_value = QtCore.pyqtSignal(int) > I'm sorry. In run() function the code below > for dirpath, dirnames, filenames in os.walk(self.root): # What is > 'self' here ? self is current object, isn't it? self.root is a path which the user will specify. > for filename in filenames: > self.sig_set_label_text.emit('current', self.current()) # where is > 'current' ? > # so something self.current() is a getter, it just returns a member that shows the path of the current work. > > #self.dialog.set_found(str(self.found())) # What is self, and dialog, > and found ? the main thread is a dialog object. self.dialog was assigned the dialog object in the __init__ of worker class. self.found() is a getter also, it returns the number of meeting the conditions. self.total() means like this. > #self.dialog.set_all(str(self.total())) # ... > self.sig_set_label_text.emit('found', str(self.found())) # again ? > self.sig_set_label_text.emit('all', str(self.total())) # etc > > self.sig_jump_finish_page.emit() > dialog.set_all() and so on just sets the widget's text. My English is poor, I hopeyou will excuse me. From john at nowhere.com Sun Aug 4 14:20:27 2013 From: john at nowhere.com (JohnD) Date: 04 Aug 2013 18:20:27 GMT Subject: stupid simple scope issue Message-ID: <51fe9b6b$0$1689$e4fe514c@dreader35.news.xs4all.nl> After 5 year of no Python programming I decided that I needed to brush up my skills. Started writing on a reasonably complicated problem. Unfortunately my basic Python skill are gone. I present the bare-bore problem. This code does not produce the expected result: can anyone tell me why? As you will guess, I want the first three lines of output identical to the second three lines... Can anyone point out the solution? Thanks! #~/usr/bin/python import random class boys: state={} class boy: state={ 'name':'', 'age':'' } names=['a','b','c'] def add_names(): global boys for n in names: boy.state['name']=n boy.state['age']=random.randint(1, 1000) boys.state[n]=boy.state print boy.state['name'], boy.state['age'] add_names() for n in boys.state: boy.state=boys.state[n] print boy.state['name'], boy.state['age'] From rosuav at gmail.com Sun Aug 4 14:39:33 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 4 Aug 2013 19:39:33 +0100 Subject: stupid simple scope issue In-Reply-To: <51fe9b6b$0$1689$e4fe514c@dreader35.news.xs4all.nl> References: <51fe9b6b$0$1689$e4fe514c@dreader35.news.xs4all.nl> Message-ID: On Sun, Aug 4, 2013 at 7:20 PM, JohnD wrote: > #~/usr/bin/python If this is meant to be a Unix-style shebang, the second character needs to be ! not ~. This has no effect on Python though. > import random > class boys: > state={} > class boy: > state={ > 'name':'', > 'age':'' > } At no time do you actually instantiate any objects from these types. In fact, you may as well drop the class blocks and the ".state" usage and simply use: boys = {} boy = {'name':'', 'age':''} as this will achieve the exact same thing. > def add_names(): > global boys The global declaration is needed only if you assign to the name, eg "boys = <...>" - it's superfluous here. > for n in names: > boy.state['name']=n > boy.state['age']=random.randint(1, 1000) > boys.state[n]=boy.state > print boy.state['name'], boy.state['age'] Each time you do this, you're modifying the same 'boy' mapping, then putting another reference to it in 'boys'. I think possibly what you want here is to construct a new boy() instance for each one. > add_names() > > for n in boys.state: > boy.state=boys.state[n] > print boy.state['name'], boy.state['age'] I'd look at doing it more like this: class boy: def __init__(self,name,age): self.name=name; self.age=age boys = {} def add_name(n): b = boy(n,random.randint(1, 1000)) boys[n] = b print b.name, b.age for n in 'a','b','c': add_name(n) for n,b in boys.items(): print b.name, b.age Or possibly even dispense with the boy class altogether and simply use a dictionary - or simply map a name to an age, since (as you can see in the final loop) it's easy enough to iterate over the dictionary. (Note that the code above is untested and probably has an egregious bug in it somewhere.) ChrisA From john at nowhere.com Sun Aug 4 15:21:31 2013 From: john at nowhere.com (JohnD) Date: 04 Aug 2013 19:21:31 GMT Subject: stupid simple scope issue References: <51fe9b6b$0$1689$e4fe514c@dreader35.news.xs4all.nl> Message-ID: <51fea9bb$0$1677$e4fe514c@dreader35.news.xs4all.nl> On 2013-08-04, Chris Angelico wrote: [...] Thank you very much. The dust is slowly starting to move. The code posted is nothing like the real thing, but I tried to capture the essence. >From your commants I think I see my mistake. Thank you very much for your reply! From rosuav at gmail.com Sun Aug 4 16:03:22 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 4 Aug 2013 21:03:22 +0100 Subject: stupid simple scope issue In-Reply-To: <51fea9bb$0$1677$e4fe514c@dreader35.news.xs4all.nl> References: <51fe9b6b$0$1689$e4fe514c@dreader35.news.xs4all.nl> <51fea9bb$0$1677$e4fe514c@dreader35.news.xs4all.nl> Message-ID: On Sun, Aug 4, 2013 at 8:21 PM, JohnD wrote: > On 2013-08-04, Chris Angelico wrote: > [...] > Thank you very much. The dust is slowly starting to move. > The code posted is nothing like the real thing, but I tried > to capture the essence. > > From your commants I think I see my mistake. > > Thank you very much for your reply! No probs! Python does have a slightly odd (compared to other languages) interpretation of "variable assignments" (name bindings, really) inside a class block. Trips up a lot of people. ChrisA From john at nowhere.com Mon Aug 5 03:21:09 2013 From: john at nowhere.com (JohnD) Date: 05 Aug 2013 07:21:09 GMT Subject: stupid simple scope issue References: <51fe9b6b$0$1689$e4fe514c@dreader35.news.xs4all.nl> <51fea9bb$0$1677$e4fe514c@dreader35.news.xs4all.nl> Message-ID: <51ff5265$0$3165$e4fe514c@dreader36.news.xs4all.nl> On 2013-08-04, Chris Angelico wrote: > On Sun, Aug 4, 2013 at 8:21 PM, JohnD wrote: >> On 2013-08-04, Chris Angelico wrote: [...] > > Python does have a slightly odd (compared to other languages) > interpretation of "variable assignments" (name bindings, really) > inside a class block. Trips up a lot of people. Changed the code: >10% smaller, more elegant, and it seems to work! If it really works I am close to half-way... From python at markusrother.de Sun Aug 4 18:35:58 2013 From: python at markusrother.de (Markus Rother) Date: Mon, 05 Aug 2013 00:35:58 +0200 Subject: Bug? ( () == [] ) != ( ().__eq__([]) ) Message-ID: <51FED74E.6080003@markusrother.de> Hello, The following behaviour seen in 3.2 seems very strange to me: As expected: >>> () == [] False However: >>> ().__eq__([]) NotImplemented >>> [].__eq__(()) NotImplemented And: >>> bool(NotImplemented) True Hence: >>> bool(().__eq__([])) True >>> ( () == [] ) != ( ().__eq__([]) ) True How/why can this be intended? Thanks, everybody. Markus R. From python at mrabarnett.plus.com Sun Aug 4 19:05:46 2013 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 05 Aug 2013 00:05:46 +0100 Subject: Bug? ( () == [] ) != ( ().__eq__([]) ) In-Reply-To: <51FED74E.6080003@markusrother.de> References: <51FED74E.6080003@markusrother.de> Message-ID: <51FEDE4A.8090409@mrabarnett.plus.com> On 04/08/2013 23:35, Markus Rother wrote: > Hello, > > The following behaviour seen in 3.2 seems very strange to me: > > As expected: > >>> () == [] > False > > However: > >>> ().__eq__([]) > NotImplemented > >>> [].__eq__(()) > NotImplemented > > And: > >>> bool(NotImplemented) > True > > Hence: > >>> bool(().__eq__([])) > True > >>> ( () == [] ) != ( ().__eq__([]) ) > True > > How/why can this be intended? > The docs say: """NotImplemented This type has a single value. There is a single object with this value. This object is accessed through the built-in name NotImplemented. Numeric methods and rich comparison methods may return this value if they do not implement the operation for the operands provided. (The interpreter will then try the reflected operation, or some other fallback, depending on the operator.) Its truth value is true. """ From rosuav at gmail.com Sun Aug 4 19:06:16 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 5 Aug 2013 00:06:16 +0100 Subject: Bug? ( () == [] ) != ( ().__eq__([]) ) In-Reply-To: <51FED74E.6080003@markusrother.de> References: <51FED74E.6080003@markusrother.de> Message-ID: On Sun, Aug 4, 2013 at 11:35 PM, Markus Rother wrote: > Hello, > > The following behaviour seen in 3.2 seems very strange to me: > > As expected: >>>> () == [] > False > > However: >>>> ().__eq__([]) > NotImplemented >>>> [].__eq__(()) > NotImplemented You don't normally want to be calling dunder methods directly. The reasoning behind this behaviour goes back to a few things, including a way to handle "1 == Foo()" where Foo is a custom type that implements __eq__; obviously the integer 1 won't know whether it's equal to a Foo instance or not, so it has to defer to the second operand to get a result. This deferral is done by returning NotImplemented, which is an object, and so is true by default. I don't see any particular reason for it to be false, as you shouldn't normally be using it; it's more like a "null" state, it means "I don't know if we're equal or not". If neither side knows whether they're equal, then they're presumed to be unequal, but you can't determine that from a single call to __eq__. ChrisA From markus.rother at web.de Mon Aug 5 10:58:03 2013 From: markus.rother at web.de (Markus Rother) Date: Mon, 05 Aug 2013 16:58:03 +0200 Subject: Bug? ( () == [] ) != ( ().__eq__([]) ) In-Reply-To: References: <51FED74E.6080003@markusrother.de> Message-ID: <51FFBD7B.8000700@web.de> Thanks for the good explanation. My intention was to pass a custom method/function as a comparator to an object. My misconception was, that __eq__ is equivalent to the '==' operator, and could be passed as a first class function. Apparently, that is not possible without wrapping the comparison into another function/method. Best regards, Markus R. On 05.08.2013 01:06, Chris Angelico wrote: > On Sun, Aug 4, 2013 at 11:35 PM, Markus Rother wrote: >> Hello, >> >> The following behaviour seen in 3.2 seems very strange to me: >> >> As expected: >>>>> () == [] >> False >> >> However: >>>>> ().__eq__([]) >> NotImplemented >>>>> [].__eq__(()) >> NotImplemented > > You don't normally want to be calling dunder methods directly. The > reasoning behind this behaviour goes back to a few things, including a > way to handle "1 == Foo()" where Foo is a custom type that implements > __eq__; obviously the integer 1 won't know whether it's equal to a Foo > instance or not, so it has to defer to the second operand to get a > result. This deferral is done by returning NotImplemented, which is an > object, and so is true by default. I don't see any particular reason > for it to be false, as you shouldn't normally be using it; it's more > like a "null" state, it means "I don't know if we're equal or not". If > neither side knows whether they're equal, then they're presumed to be > unequal, but you can't determine that from a single call to __eq__. > > ChrisA > From ian.g.kelly at gmail.com Mon Aug 5 11:08:07 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 5 Aug 2013 09:08:07 -0600 Subject: Bug? ( () == [] ) != ( ().__eq__([]) ) In-Reply-To: <51FFBD7B.8000700@web.de> References: <51FED74E.6080003@markusrother.de> <51FFBD7B.8000700@web.de> Message-ID: On Mon, Aug 5, 2013 at 8:58 AM, Markus Rother wrote: > Thanks for the good explanation. > > My intention was to pass a custom method/function as a comparator > to an object. My misconception was, that __eq__ is equivalent to > the '==' operator, and could be passed as a first class function. > Apparently, that is not possible without wrapping the comparison > into another function/method. The operator.eq function is equivalent to the == operator. Use that. From i at introo.me Wed Aug 7 05:24:18 2013 From: i at introo.me (Shiyao Ma) Date: Wed, 7 Aug 2013 17:24:18 +0800 Subject: Bug? ( () == [] ) != ( ().__eq__([]) ) In-Reply-To: References: <51FED74E.6080003@markusrother.de> Message-ID: Sorry. I don't quite get it. As you said, it first tries, leftOperand.__eq__(rightOperand) then if it returns NotImplemented, it goes to invoke rightOperand.__eq__(leftOperand). But for any reason, [] == () returns false, why? On Mon, Aug 5, 2013 at 7:06 AM, Chris Angelico wrote: > On Sun, Aug 4, 2013 at 11:35 PM, Markus Rother > wrote: > > Hello, > > > > The following behaviour seen in 3.2 seems very strange to me: > > > > As expected: > >>>> () == [] > > False > > > > However: > >>>> ().__eq__([]) > > NotImplemented > >>>> [].__eq__(()) > > NotImplemented > > You don't normally want to be calling dunder methods directly. The > reasoning behind this behaviour goes back to a few things, including a > way to handle "1 == Foo()" where Foo is a custom type that implements > __eq__; obviously the integer 1 won't know whether it's equal to a Foo > instance or not, so it has to defer to the second operand to get a > result. This deferral is done by returning NotImplemented, which is an > object, and so is true by default. I don't see any particular reason > for it to be false, as you shouldn't normally be using it; it's more > like a "null" state, it means "I don't know if we're equal or not". If > neither side knows whether they're equal, then they're presumed to be > unequal, but you can't determine that from a single call to __eq__. > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://about.me/introom -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Wed Aug 7 05:43:07 2013 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 7 Aug 2013 10:43:07 +0100 Subject: Bug? ( () == [] ) != ( ().__eq__([]) ) In-Reply-To: References: <51FED74E.6080003@markusrother.de> Message-ID: On Wed, Aug 7, 2013 at 10:24 AM, Shiyao Ma wrote: > Sorry. I don't quite get it. As you said, it first tries, > leftOperand.__eq__(rightOperand) then if it returns NotImplemented, it goes > to invoke rightOperand.__eq__(leftOperand). But for any reason, [] == () > returns false, why? If neither of them has implemented a check, then it's assumed they're not equal. It wouldn't be helpful for the == operator to return anything other than True or False (except maybe NaN), so it returns False. ChrisA From ethan at stoneleaf.us Wed Aug 7 13:50:12 2013 From: ethan at stoneleaf.us (Ethan Furman) Date: Wed, 07 Aug 2013 10:50:12 -0700 Subject: Bug? ( () == [] ) != ( ().__eq__([]) ) In-Reply-To: References: <51FED74E.6080003@markusrother.de> Message-ID: <520288D4.4040406@stoneleaf.us> On 08/07/2013 02:24 AM, Shiyao Ma wrote: > > Sorry. I don't quite get it. As you said, it first tries, leftOperand.__eq__(rightOperand) then if it returns > NotImplemented, it goes to invoke rightOperand.__eq__(leftOperand). But for any reason, [] == () returns false, why? A list that is empty is not equal to a tuple that is empty, much like a car that is empty is not equal to a boat that is empty. -- ~Ethan~ From eschneider92 at comcast.net Mon Aug 5 01:11:35 2013 From: eschneider92 at comcast.net (eschneider92 at comcast.net) Date: Sun, 4 Aug 2013 22:11:35 -0700 (PDT) Subject: Hangman question Message-ID: <0d4e93ac-9df9-44f3-abdc-a1aaa3363ab7@googlegroups.com> I'm on chapter 9 of this guide to python: http://inventwithpython.com/chapter9.html but I don't quite understand why line 79 is what it is (blanks = blanks[:i] + secretWord[i] + blanks[i+1:]). I particularly don't get the [i+1:] part. Any additional information and help would be greatly appreciated! From joshua at landau.ws Mon Aug 5 02:11:21 2013 From: joshua at landau.ws (Joshua Landau) Date: Mon, 5 Aug 2013 07:11:21 +0100 Subject: Hangman question In-Reply-To: <0d4e93ac-9df9-44f3-abdc-a1aaa3363ab7@googlegroups.com> References: <0d4e93ac-9df9-44f3-abdc-a1aaa3363ab7@googlegroups.com> Message-ID: On 5 August 2013 06:11, wrote: > I'm on chapter 9 of this guide to python: > http://inventwithpython.com/chapter9.html but I don't quite > understand why line 79 is what it is (blanks = blanks[:i] + secretWord[i] + > blanks[i+1:]). I particularly don't get the [i+1:] part. Any additional > information and help would be greatly appreciated! > First you should realise that this isn't actually great code by my standards. There are quite a few un-idiomatic things done, but hopefully the learning you get outweighs the things you have to unlearn ;). That said, here's an explanation. This loop: for i in range(len(secretWord)): # replace blanks with correctly guessed letters if secretWord[i] in correctLetters: blanks = blanks[:i] + secretWord[i] + blanks[i+1:] wants to change blanks from a string like "____" to a string like "a_t_". It has a companion string, secretWord, of the same length (such as "acts"). the "for i in range(len(secretWord)):" loop? goes through all the numbers from 0 to 3 in this case. "if secretWord[i] in correctLetters:"? takes the letter from position 0 to 3, which is each of the letters in the secret word in turn, so "a", "c", "t" and "s" and checks if that letter is in correctLetters. If it is, we want to do the transformation like "____" -> "a___" and "a___" -> "a_t_". This brings us to line 79. blanks = blanks[:i] + secretWord[i] + blanks[i+1:]? Let "blanks" be "a___". When i == 2, secretWord[i] == "t" which we can assume is in correctLetters. "blanks[:2]" returns "a_" because you're slicing the string up to the second stop. I don't know how slices have been taught but I'd been taught like so: [0]a[1]_[2]_[3]_[4] so slicing from the start (by missing the start index in the slice) to 2 would return the "a" and "_". "secretWord[i]" returns "t" as before. "blanks[i+1:]" == "blanks[2+1:]" == "blanks[3:]" so return the rest *after skipping one*, so just "_". This means that the additions resolve to: blanks = "a_" + "t" + "_" which results in "a_t_". Does this help? ? You should really use "for i, letter in enumerate(secretWord?):" but I'll let it pass for simplicity. ? Really, "secret_word" is better than "secretWord", too, but w/e. ? Better written as "if letter in guessed_letters:" given [1] and a generalisation of [2]. ? This is *really* bad practice. Not only is the string rebuilt every time the loop happens but it's actually rebuilt in part about *three* times per loop. In this case it's not a big deal, but don't write things like this.? ? You should probably use a mutable list which you "".join() OR replace elements using an iterator (the second of those you can leave for later but they're not that hard). -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Mon Aug 5 02:18:50 2013 From: __peter__ at web.de (Peter Otten) Date: Mon, 05 Aug 2013 08:18:50 +0200 Subject: Hangman question References: <0d4e93ac-9df9-44f3-abdc-a1aaa3363ab7@googlegroups.com> Message-ID: eschneider92 at comcast.net wrote: > I'm on chapter 9 of this guide to python: > http://inventwithpython.com/chapter9.html but I don't quite understand > why line 79 is what it is (blanks = blanks[:i] + secretWord[i] + > blanks[i+1:]). I particularly don't get the [i+1:] part. Any additional > information and help would be greatly appreciated! When you have a list with and want to replace the third item with something else you can do it like this: >>> items = ["r", "e", "e", "d"] >>> items[2] = "a" >>> items ['r', 'e', 'a', 'd'] With a string that is not possible because strings cannot be modified (they are "immutable"): >>> s = "reed" >>> s[2] = "a" Traceback (most recent call last): File "", line 1, in TypeError: 'str' object does not support item assignment You could try replace() to build a new string >>> s.replace("e", "a") 'raad' but that replaces all occurrences. So to manually replace th third character you take the first two characters of the original >>> s[:2] 're' then the characters after the third >>> s[3:] 'd' and finally build the new string by putting the new character (or string) in between: >>> s[:2] + "a" + s[3:] 'read' A function to replace the n-th character would then look like this: >>> def replace_nth_char(s, n, replacement): ... return s[:n] + replacement + s[n+1:] ... >>> replace_nth_char("read", 2, "e") 'reed' From davea at davea.name Mon Aug 5 02:44:36 2013 From: davea at davea.name (Dave Angel) Date: Mon, 5 Aug 2013 06:44:36 +0000 (UTC) Subject: Hangman question References: <0d4e93ac-9df9-44f3-abdc-a1aaa3363ab7@googlegroups.com> Message-ID: eschneider92 at comcast.net wrote: > I'm on chapter 9 of this guide to python: http://inventwithpython.com/chapter9.html but I don't quite understand why line 79 is what it is (blanks = blanks[:i] + secretWord[i] + blanks[i+1:]). I particularly don't get the [i+1:] part. Any additional information and help would be greatly appreciated! (You're using googlegroups, which is painfully buggy. Just an alert that some folks won't even see a message from googlegroups, because they filter it out in protest. If you join this mailing list with more conventional means, you can avoid several problems.) The syntax you're describing is called a slice. it works on strings, and on lists, and some other places. For strings in particular, because they are immutable, something that ought to be easy becomes a strange-looking slice expression. it's worth taking the trouble to figure out the patterns of those, so you can just recognize them without too much thought. And of course, asking here is one good first step for that. A slice can specify up to 3 numeric values, which could be labeled as start, end, and step, just like range. However, they're separated by colons, rather than commas. Go to the debugger (by just typing python at the command prompt, without a script), and play a bit. "abcdefg"[:3] is analogous to range(3), which is equivalent to range(0,3). Those ranges produce a list containing 0, 1 and 2. So the string slice produces the characters at those 3 positions. In other words, "abc" (Remember Python is 99.9% zero-based). (If on Python 3.x, use list(range(qqq)) to expand it out and actually see the elements as a list) "abcdefg"[4:] is analogous to range(4, 7) and produces 'efg' Notice that character 3 is not in either string. The letter 'd' is missing. So this particular pattern: xxx[:i] + other + xxx[i+1:] lets you replace the ith character with whatever's in "other". if it's a single character, the net effect is just replacing the ith character with a different one. Does that help? -- DaveA From eschneider92 at comcast.net Tue Aug 6 01:01:29 2013 From: eschneider92 at comcast.net (eschneider92 at comcast.net) Date: Mon, 5 Aug 2013 22:01:29 -0700 (PDT) Subject: Hangman question In-Reply-To: <0d4e93ac-9df9-44f3-abdc-a1aaa3363ab7@googlegroups.com> References: <0d4e93ac-9df9-44f3-abdc-a1aaa3363ab7@googlegroups.com> Message-ID: <1edbdf31-497b-4ae2-a8f8-d4690aff1b0d@googlegroups.com> Thanks I get it now. From jarausch at igpm.rwth-aachen.de Mon Aug 5 03:34:39 2013 From: jarausch at igpm.rwth-aachen.de (Helmut Jarausch) Date: 5 Aug 2013 07:34:39 GMT Subject: __iadd__ for a subclass of array - howto Message-ID: Hi, I'd like to subclass array.array and implement operators like __iadd__ How can this be accomplished. I'tried from array import array class Vec(array) : def __new__(cls,Vinit) : return array.__new__(cls,'d',Vinit) def __init__(self,*args) : self.N = len(self) def __str__(self) : out=[ "{}".format(self.__getitem__(i)) for i in range(0,self.N) ] return ",".join(out) def __iadd__(self,Op) : # for i,x in enumerate(self) : # x+= Op[i] # this doesn't update self for i in range(0,self.N) : (self.__getitem__(i))+= Op[i] # __getitem__ doesn't return a "reference" (like in C++) return self Many thanks for a hint, Helmut From ian.g.kelly at gmail.com Mon Aug 5 03:59:09 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 5 Aug 2013 01:59:09 -0600 Subject: __iadd__ for a subclass of array - howto In-Reply-To: References: Message-ID: On Mon, Aug 5, 2013 at 1:34 AM, Helmut Jarausch wrote: > Hi, > > I'd like to subclass array.array and implement operators like __iadd__ > How can this be accomplished. > > I'tried > > from array import array > > class Vec(array) : > def __new__(cls,Vinit) : > return array.__new__(cls,'d',Vinit) > > def __init__(self,*args) : > self.N = len(self) > > def __str__(self) : > out=[ "{}".format(self.__getitem__(i)) for i in range(0,self.N) ] > return ",".join(out) > > def __iadd__(self,Op) : > # for i,x in enumerate(self) : > # x+= Op[i] # this doesn't update self > for i in range(0,self.N) : > (self.__getitem__(i))+= Op[i] # __getitem__ doesn't return a "reference" (like in C++) > return self for i in range(len(self)): self[i] += Op[i] There's no reason to be calling __getitem__ directly. Use the operators and let Python decide what to call. From luca.cerone at gmail.com Mon Aug 5 09:09:53 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Mon, 5 Aug 2013 06:09:53 -0700 (PDT) Subject: Simulate `bash` behaviour using Python and named pipes. Message-ID: Hi everybody, I am trying to understand how to use named pipes in python to launch external processes (in a Linux environment). As an example I am trying to "imitate" the behaviour of the following sets of commands is bash: > mkfifo named_pipe > ls -lah > named_pipe & > cat < named_pipe In Python I have tried the following commands: import os import subprocess as sp os.mkfifo("named_pipe",0777) #equivalent to mkfifo in bash.. fw = open("named_pipe",'w') #at this point the system hangs... My idea it was to use subprocess.Popen and redirect stdout to fw... next open named_pipe for reading and giving it as input to cat (still using Popen). I know it is a simple (and rather stupid) example, but I can't manage to make it work.. How would you implement such simple scenario? Thanks a lot in advance for the help!!! Luca From poalman at gmail.com Mon Aug 5 09:39:24 2013 From: poalman at gmail.com (Paul Wiseman) Date: Mon, 5 Aug 2013 14:39:24 +0100 Subject: Simulate `bash` behaviour using Python and named pipes. In-Reply-To: References: Message-ID: On 5 August 2013 14:09, Luca Cerone wrote: > Hi everybody, > I am trying to understand how to use named pipes in python to launch > external processes (in a Linux environment). > > As an example I am trying to "imitate" the behaviour of the following sets > of commands is bash: > > > mkfifo named_pipe > > ls -lah > named_pipe & > > cat < named_pipe > > In Python I have tried the following commands: > > import os > import subprocess as sp > > os.mkfifo("named_pipe",0777) #equivalent to mkfifo in bash.. > fw = open("named_pipe",'w') > #at this point the system hangs... > > My idea it was to use subprocess.Popen and redirect stdout to fw... > next open named_pipe for reading and giving it as input to cat (still > using Popen). > > I know it is a simple (and rather stupid) example, but I can't manage to > make it work.. > > > How would you implement such simple scenario? > > Thanks a lot in advance for the help!!! > > You can pipe using subprocess p1 = subprocess.Popen(["ls", "-lah"], stdout=subprocess.PIPE) p2 = subprocess.Popen(["cat"], stdin=p1.stdout) p1.wait() p2.wait() You can also pass a file object to p1's stdout and p2's stdin if you want to pipe via a file. with open("named_pipe", "rw") as named_pipe: p1 = subprocess.Popen(["ls", "-lah"], stdout=named_pipe) p2 = subprocess.Popen(["cat"], stdin=named_pipe) p1.wait() p2.wait() > Luca > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From luca.cerone at gmail.com Mon Aug 5 09:51:36 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Mon, 5 Aug 2013 06:51:36 -0700 (PDT) Subject: Simulate `bash` behaviour using Python and named pipes. In-Reply-To: References: Message-ID: <0b3fcb71-ce96-4f7e-b765-dff61bbdb119@googlegroups.com> Hi Paul, first of all thanks for the help. I am aware of the first solutions, just now I would like to experiment a bit with using named pipes (I also know that the example is trivial, but it just to grasp the main concepts) > > You can also pass a file object to p1's stdout and p2's stdin if you want to >pipe via a file. > > with open("named_pipe", "rw") as named_pipe: > ? ? p1 = subprocess.Popen(["ls", "-lah"], stdout=named_pipe) > ? ? p2 = subprocess.Popen(["cat"], stdin=named_pipe) > > ? ? p1.wait() > ? ? p2.wait() > ? Your second example doesn't work for me.. if named_file is not a file in the folder I get an error saying that there is not such a file. If I create named_pipe as a named pipe using os.mkfifo("named_file",0777) than the code hangs.. I think it is because there is not process that reads the content of the pipe, so the system waits for the pipe to be emptied. Thanks a lot in advance for the help in any case. Luca From python at mrabarnett.plus.com Mon Aug 5 10:03:02 2013 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 05 Aug 2013 15:03:02 +0100 Subject: Simulate `bash` behaviour using Python and named pipes. In-Reply-To: References: Message-ID: <51FFB096.2050506@mrabarnett.plus.com> On 05/08/2013 14:09, Luca Cerone wrote: > Hi everybody, > I am trying to understand how to use named pipes in python to launch external processes (in a Linux environment). > > As an example I am trying to "imitate" the behaviour of the following sets of commands is bash: > >> mkfifo named_pipe >> ls -lah > named_pipe & >> cat < named_pipe > > In Python I have tried the following commands: > > import os > import subprocess as sp > > os.mkfifo("named_pipe",0777) #equivalent to mkfifo in bash.. > fw = open("named_pipe",'w') > #at this point the system hangs... > > My idea it was to use subprocess.Popen and redirect stdout to fw... > next open named_pipe for reading and giving it as input to cat (still using Popen). > > I know it is a simple (and rather stupid) example, but I can't manage to make it work.. > > > How would you implement such simple scenario? > > Thanks a lot in advance for the help!!! > Opening the pipe for reading will block until it's also opened for writing, and vice versa. In your bash code, 'ls' blocked until you ran 'cat', but because you ran 'ls' in the background you didn't notice it! In your Python code, the Python thread blocked on opening the pipe for writing. It was waiting for another thread or process to open the pipe for reading. From luca.cerone at gmail.com Mon Aug 5 10:11:52 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Mon, 5 Aug 2013 07:11:52 -0700 (PDT) Subject: Simulate `bash` behaviour using Python and named pipes. In-Reply-To: References: Message-ID: <7f5c60c4-50c7-4a93-af22-5c8549f87461@googlegroups.com> Hi MRAB, thanks for the reply! > > Opening the pipe for reading will block until it's also opened for > > writing, and vice versa. > OK. > > > In your bash code, 'ls' blocked until you ran 'cat', but because you > > ran 'ls' in the background you didn't notice it! > > Right. > > In your Python code, the Python thread blocked on opening the pipe for > > writing. It was waiting for another thread or process to open the pipe > > for reading. OK. What you have written makes sense to me. So how do I overcome the block? As you said in bash I run the ls process in background. How do I do that in Python? Thanks again for the help, Luca From python at mrabarnett.plus.com Mon Aug 5 10:51:35 2013 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 05 Aug 2013 15:51:35 +0100 Subject: Simulate `bash` behaviour using Python and named pipes. In-Reply-To: <7f5c60c4-50c7-4a93-af22-5c8549f87461@googlegroups.com> References: <7f5c60c4-50c7-4a93-af22-5c8549f87461@googlegroups.com> Message-ID: <51FFBBF7.50407@mrabarnett.plus.com> On 05/08/2013 15:11, Luca Cerone wrote: > Hi MRAB, thanks for the reply! >> >> Opening the pipe for reading will block until it's also opened for >> writing, and vice versa. >> > > OK. > >> >> In your bash code, 'ls' blocked until you ran 'cat', but because you >> ran 'ls' in the background you didn't notice it! >> >> > Right. >> >> In your Python code, the Python thread blocked on opening the pipe for >> writing. It was waiting for another thread or process to open the pipe >> for reading. > > OK. What you have written makes sense to me. So how do I overcome the block? > As you said in bash I run the ls process in background. How do I do that in Python? > You need to ensure that the pipe is already open at the other end. Why are you using a named pipe anyway? If you're talking to another program, then that needs to be running already, waiting for the connection, at the point that you open the named pipe from this end. If you're using a pipe _within_ a program (a queue would be better), then you should opening for writing in one thread and for reading in another. From luca.cerone at gmail.com Mon Aug 5 11:27:46 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Mon, 5 Aug 2013 08:27:46 -0700 (PDT) Subject: Simulate `bash` behaviour using Python and named pipes. In-Reply-To: References: <7f5c60c4-50c7-4a93-af22-5c8549f87461@googlegroups.com> Message-ID: Thanks MRAB, > > You need to ensure that the pipe is already open at the other end. So I need to open the process that reads the pipe before writing in it? > > > > Why are you using a named pipe anyway? For some bug in ipython (see my previous email) I can't use subprocess.Popen and pipe in the standard way. One of Ipython developers has suggested me to use named pipes as a temporary workaround. So I am taking the occasion to learn :) > > If you're talking to another program, then that needs to be running > > already, waiting for the connection, at the point that you open the > > named pipe from this end. I am not entirely sure I got this: ideally I would like to have a function that runs an external tool (the equivalent of ls in my example) redirecting its output in a named pipe. A second function (the cat command in my example) would read the named_pipe, parse it and extract some features from the output. I also would like that the named_pipe is deleted when the whole communication is ended. > > If you're using a pipe _within_ a program (a queue would be better), > > then you should opening for writing in one thread and for reading in > > another. Let's stick with the pipe :) I will ask about the queue when I manage to use pipes ;) I should have explained better that I have no idea how to run threads in Python :): how do I open a thread that executes "ls -lah" in background and writes into a named pipe? And how do I open a thread that reads from the named pipe? Can you please post a small example, so that I have something to work on? Thanks a lot in advance for your help! Luca From python at mrabarnett.plus.com Mon Aug 5 12:08:32 2013 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 05 Aug 2013 17:08:32 +0100 Subject: Simulate `bash` behaviour using Python and named pipes. In-Reply-To: References: <7f5c60c4-50c7-4a93-af22-5c8549f87461@googlegroups.com> Message-ID: <51FFCE00.1000806@mrabarnett.plus.com> On 05/08/2013 16:27, Luca Cerone wrote: > Thanks MRAB, >> >> You need to ensure that the pipe is already open at the other end. > > So I need to open the process that reads the pipe before writing in > it? > >> >> Why are you using a named pipe anyway? > > For some bug in ipython (see my previous email) I can't use > subprocess.Popen and pipe in the standard way. One of Ipython > developers has suggested me to use named pipes as a temporary > workaround. So I am taking the occasion to learn :) > An alternative workaround is to use CPython. :-) >> If you're talking to another program, then that needs to be >> running already, waiting for the connection, at the point that you >> open the named pipe from this end. > > I am not entirely sure I got this: ideally I would like to have a > function that runs an external tool (the equivalent of ls in my > example) redirecting its output in a named pipe. > > A second function (the cat command in my example) would read the > named_pipe, parse it and extract some features from the output. > > I also would like that the named_pipe is deleted when the whole > communication is ended. > >> If you're using a pipe _within_ a program (a queue would be >> better), then you should opening for writing in one thread and for >> reading in another. > > Let's stick with the pipe :) I will ask about the queue when I > manage to use pipes ;) > > I should have explained better that I have no idea how to run > threads in Python :): how do I open a thread that executes "ls -lah" > in background and writes into a named pipe? And how do I open a > thread that reads from the named pipe? > > Can you please post a small example, so that I have something to > work on? > You could try something like this: os.mkfifo("named_pipe", 0777) ls_process = subprocess.Popen("ls -lah > named_pipe") pipe = open("named_pipe", "r") # Read the output of the subprocess from the pipe. When the subprocess terminates (look at the docs for Popen objects), close and delete the fifo. From luca.cerone at gmail.com Mon Aug 5 12:54:24 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Mon, 5 Aug 2013 09:54:24 -0700 (PDT) Subject: Simulate `bash` behaviour using Python and named pipes. In-Reply-To: References: <7f5c60c4-50c7-4a93-af22-5c8549f87461@googlegroups.com> Message-ID: Thanks this works (if you add shell=True in Popen). If I don't want to use shell = True, how can I redirect the stdout to named_pipe? Popen accepts an open file handle for stdout, which I can't open for writing because that blocks the process... > > > os.mkfifo("named_pipe", 0777) > > ls_process = subprocess.Popen("ls -lah > named_pipe") > > pipe = open("named_pipe", "r") > > # Read the output of the subprocess from the pipe. > > > > When the subprocess terminates (look at the docs for Popen objects), > > close and delete the fifo. From python at mrabarnett.plus.com Mon Aug 5 15:45:05 2013 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 05 Aug 2013 20:45:05 +0100 Subject: Simulate `bash` behaviour using Python and named pipes. In-Reply-To: References: <7f5c60c4-50c7-4a93-af22-5c8549f87461@googlegroups.com> Message-ID: <520000C1.5000501@mrabarnett.plus.com> On 05/08/2013 17:54, Luca Cerone wrote: > Thanks this works (if you add shell=True in Popen). > If I don't want to use shell = True, how can I redirect the stdout to named_pipe? Popen accepts an open file handle for stdout, which I can't open for writing because that blocks the process... > You're back to using separate threads for the reader and the writer. The one that opens the pipe first will block until the other one opens the other end. >> >> >> os.mkfifo("named_pipe", 0777) >> >> ls_process = subprocess.Popen("ls -lah > named_pipe") >> >> pipe = open("named_pipe", "r") >> >> # Read the output of the subprocess from the pipe. >> >> >> >> When the subprocess terminates (look at the docs for Popen objects), >> >> close and delete the fifo. > From luca.cerone at gmail.com Mon Aug 5 17:47:45 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Mon, 5 Aug 2013 14:47:45 -0700 (PDT) Subject: Simulate `bash` behaviour using Python and named pipes. In-Reply-To: References: <7f5c60c4-50c7-4a93-af22-5c8549f87461@googlegroups.com> Message-ID: > You're back to using separate threads for the reader and the writer. > And how do I create separate threads in Python? I was trying to use the threading library without not too success.. From python at mrabarnett.plus.com Mon Aug 5 18:42:57 2013 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 05 Aug 2013 23:42:57 +0100 Subject: Simulate `bash` behaviour using Python and named pipes. In-Reply-To: References: <7f5c60c4-50c7-4a93-af22-5c8549f87461@googlegroups.com> Message-ID: <52002A71.1020205@mrabarnett.plus.com> On 05/08/2013 22:47, Luca Cerone wrote: >> You're back to using separate threads for the reader and the writer. >> > And how do I create separate threads in Python? I was trying to use the threading library without not too success.. > To run a function in a separate thread: import threading def my_func(arg_1, arg_2): ... my_thread = threading.Thread(target=my_func, args=(arg_1, arg_2)) my_thread.start() Is the thread still running? if my_thread.is_alive(): ... Wait for the thread to terminate: my_thread.join() From luca.cerone at gmail.com Tue Aug 6 12:25:08 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Tue, 6 Aug 2013 09:25:08 -0700 (PDT) Subject: Simulate `bash` behaviour using Python and named pipes. In-Reply-To: References: <7f5c60c4-50c7-4a93-af22-5c8549f87461@googlegroups.com> Message-ID: <92074090-ae9e-4219-ae0f-21592e8f0133@googlegroups.com> > my_thread.join() Thanks! I managed to make it work using the threading library :) From greg.ewing at canterbury.ac.nz Fri Aug 9 21:07:03 2013 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Sat, 10 Aug 2013 13:07:03 +1200 Subject: Simulate `bash` behaviour using Python and named pipes. In-Reply-To: <92074090-ae9e-4219-ae0f-21592e8f0133@googlegroups.com> References: <7f5c60c4-50c7-4a93-af22-5c8549f87461@googlegroups.com> <92074090-ae9e-4219-ae0f-21592e8f0133@googlegroups.com> Message-ID: Luca Cerone wrote: > Thanks! I managed to make it work using the threading library :) If at least one of the external programs can accept the source or destination as a filename argument instead of redirecting its stdin or stdout, you can also do something like this: import subprocess p2 = subprocess.Popen(["cat", "named_pipe"]) pipe = open("named_pipe", "w") p1 = subprocess.Popen(["ls", "-lah"], stdout = pipe) pipe.close() p1.wait() p2.wait() That works because opening the reading end of the pipe is done by the subprocess executing cat, leaving the main process free to open the other end. -- Greg From alister.ware at ntlworld.com Mon Aug 5 10:29:51 2013 From: alister.ware at ntlworld.com (Alister) Date: Mon, 05 Aug 2013 14:29:51 GMT Subject: Simulate `bash` behaviour using Python and named pipes. References: Message-ID: On Mon, 05 Aug 2013 06:09:53 -0700, Luca Cerone wrote: > Hi everybody, > I am trying to understand how to use named pipes in python to launch > external processes (in a Linux environment). > > As an example I am trying to "imitate" the behaviour of the following > sets of commands is bash: > >> mkfifo named_pipe ls -lah > named_pipe & >> cat < named_pipe > > In Python I have tried the following commands: > > import os import subprocess as sp > > os.mkfifo("named_pipe",0777) #equivalent to mkfifo in bash.. > fw = open("named_pipe",'w') > #at this point the system hangs... > > My idea it was to use subprocess.Popen and redirect stdout to fw... > next open named_pipe for reading and giving it as input to cat (still > using Popen). > > I know it is a simple (and rather stupid) example, but I can't manage to > make it work.. > > > How would you implement such simple scenario? > > Thanks a lot in advance for the help!!! > > Luca Are you sure you are using the correct tool for the task? I tend to find that in most cases if you are trying to execute bash commands from Python you are doing it wrong. certainly if you are trying to pipe one bash command into another you would probably be better of with a bash script. -- When I was crossing the border into Canada, they asked if I had any firearms with me. I said, "Well, what do you need?" -- Steven Wright From luca.cerone at gmail.com Mon Aug 5 10:59:04 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Mon, 5 Aug 2013 07:59:04 -0700 (PDT) Subject: Simulate `bash` behaviour using Python and named pipes. In-Reply-To: References: Message-ID: <38f81870-666f-430f-adb3-0df5cdba0572@googlegroups.com> Hi Alister, > Are you sure you are using the correct tool for the task? Yes. For two reasons: 1. I want to learn how to do this in Python :) 2. for an application I have in mind I will need to run external tools (not developed by me) and process the output using some tools that I have written in Python. For technical reasons I can't use the subprocess.communicate() (the output to process is very large) method, and due to a bug in the interactive shell I am using (https://github.com/ipython/ipython/issues/3884) I cannot pipe processes just using the standard subprocess.Popen() approach. > > I tend to find that in most cases if you are trying to execute bash > > commands from Python you are doing it wrong. As I said, the example in my question is just for learning purposes, I don't want to reproduce ls and cat in Python... I just would like to learn how to handle named pipes in Python, which I find it easier to do by using a simple example that I am comfortable to use :) Thanks in any case for your answer, Luca From neilc at norwich.edu Mon Aug 5 12:00:31 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 5 Aug 2013 16:00:31 GMT Subject: Simulate `bash` behaviour using Python and named pipes. References: <38f81870-666f-430f-adb3-0df5cdba0572@googlegroups.com> Message-ID: On 2013-08-05, Luca Cerone wrote: > I just would like to learn how to handle named pipes in Python, > which I find it easier to do by using a simple example that I > am comfortable to use :) Names pipes are a unix concept that saves you the hassle and limitations of writing to and reading from a temp file. You'll have to create the temp file and manage attaching processes to it yourself. -- Neil Cerutti From diolu.remove_this_part at bigfoot.com Mon Aug 5 15:38:11 2013 From: diolu.remove_this_part at bigfoot.com (Olive) Date: Mon, 5 Aug 2013 21:38:11 +0200 Subject: Module for dialoging with intercative programs, sockets, files, etc. Message-ID: <20130805213811.48631a88@pcolivier.chezmoi.net> I have found telnetlib which make very easy to interact with a telnet server, especially the read_until command. I wonder if something similar exits for other things that a telnet server. I for the moment have in mind interacting with a GSM modem (we do it by reading and writing a pseudo serial device file). But we could also want to interact with an interactive program or a socket, etc... Olive From ulrich.eckhardt at dominolaser.com Tue Aug 6 04:44:16 2013 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Tue, 06 Aug 2013 10:44:16 +0200 Subject: Module for dialoging with intercative programs, sockets, files, etc. In-Reply-To: <20130805213811.48631a88@pcolivier.chezmoi.net> References: <20130805213811.48631a88@pcolivier.chezmoi.net> Message-ID: <0kp6da-e84.ln1@satorlaser.homedns.org> Am 05.08.2013 21:38, schrieb Olive: > I have found telnetlib which make very easy to interact with a telnet > server, especially the read_until command. I wonder if something > similar exits for other things that a telnet server. It's not Python and I haven't played with it extensively, but there is a tool named "expect": https://en.wikipedia.org/wiki/Expect Uli From wrw at mac.com Tue Aug 6 09:28:36 2013 From: wrw at mac.com (William Ray Wing) Date: Tue, 06 Aug 2013 09:28:36 -0400 Subject: Module for dialoging with intercative programs, sockets, files, etc. In-Reply-To: <0kp6da-e84.ln1@satorlaser.homedns.org> References: <20130805213811.48631a88@pcolivier.chezmoi.net> <0kp6da-e84.ln1@satorlaser.homedns.org> Message-ID: <94B763D3-E0F1-46F5-9645-18C1118D7731@mac.com> On Aug 6, 2013, at 4:44 AM, Ulrich Eckhardt wrote: > Am 05.08.2013 21:38, schrieb Olive: >> I have found telnetlib which make very easy to interact with a telnet >> server, especially the read_until command. I wonder if something >> similar exits for other things that a telnet server. > > It's not Python and I haven't played with it extensively, but there is a tool named "expect": https://en.wikipedia.org/wiki/Expect > > Uli > -- > http://mail.python.org/mailman/listinfo/python-list Ah, but there _is_ a Python equivalent called pexpect with lots of examples and documentation via Google. You can start here: http://www.pythonforbeginners.com/systems-programming/how-to-use-the-pexpect-module-in-python/ -Bill From rui.maciel at gmail.com Mon Aug 5 16:46:57 2013 From: rui.maciel at gmail.com (Rui Maciel) Date: Mon, 05 Aug 2013 21:46:57 +0100 Subject: Newbie: static typing? Message-ID: Is there any pythonic way to perform static typing? After searching the web I've stumbled on a significant number of comments that appear to cover static typing as a proof of concept , but in the process I've found no tutorial on how to implement it. Does anyone care to enlighten a newbie? Thanks in advance, Rui Maciel From gary.herron at islandtraining.com Mon Aug 5 17:07:42 2013 From: gary.herron at islandtraining.com (Gary Herron) Date: Mon, 05 Aug 2013 14:07:42 -0700 Subject: Newbie: static typing? In-Reply-To: References: Message-ID: <5200141E.2040705@islandtraining.com> On 08/05/2013 01:46 PM, Rui Maciel wrote: > Is there any pythonic way to perform static typing? After searching the web > I've stumbled on a significant number of comments that appear to cover > static typing as a proof of concept , but in the process I've found no > tutorial on how to implement it. > > Does anyone care to enlighten a newbie? > > > Thanks in advance, > Rui Maciel The Pythonic way is to *enjoy* the freedom and flexibility and power of dynamic typing. If you are stepping out of a static typing language into Python, don't step just half way. Embrace dynamic typing. (Like any freedom, it can bite you at times, but that's no reason to hobble Python with static typing.) Python is both dynamically typed and strongly typed. If you are confusing dynamic/static typing with week/strong typing, see http://wiki.python.org/moin/Why%20is%20Python%20a%20dynamic%20language%20and%20also%20a%20strongly%20typed%20language Gary Herron From ian.g.kelly at gmail.com Mon Aug 5 19:38:30 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 5 Aug 2013 17:38:30 -0600 Subject: Newbie: static typing? In-Reply-To: References: Message-ID: On Mon, Aug 5, 2013 at 2:46 PM, Rui Maciel wrote: > Is there any pythonic way to perform static typing? After searching the web > I've stumbled on a significant number of comments that appear to cover > static typing as a proof of concept , but in the process I've found no > tutorial on how to implement it. > > Does anyone care to enlighten a newbie? Python 3 has support for function annotations, but it leaves it entirely up to the user how they wish to use these annotations (if at all). In theory, a Python IDE could use function annotations to perform static type checking, but I am not aware of any IDE that has actually implemented this. From ben+python at benfinney.id.au Mon Aug 5 20:35:46 2013 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 06 Aug 2013 10:35:46 +1000 Subject: Newbie: static typing? References: Message-ID: <7wwqnzpsf1.fsf@benfinney.id.au> Rui Maciel writes: > Is there any pythonic way to perform static typing? I think no; static typing is inherently un-Pythonic. Python provides strong, dynamic typing. Enjoy it! > Does anyone care to enlighten a newbie? Is there some specific problem you think needs static typing? Perhaps you could start a new thread, giving an example where you are having trouble and you think static typing would help. -- \ ?Pinky, are you pondering what I'm pondering?? ?Wuh, I think | `\ so, Brain, but will they let the Cranberry Duchess stay in the | _o__) Lincoln Bedroom?? ?_Pinky and The Brain_ | Ben Finney From steve at pearwood.info Tue Aug 6 01:21:33 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 06 Aug 2013 05:21:33 GMT Subject: Newbie: static typing? References: Message-ID: <520087dc$0$21718$c3e8da3@news.astraweb.com> On Mon, 05 Aug 2013 21:46:57 +0100, Rui Maciel wrote: > Is there any pythonic way to perform static typing? After searching the > web I've stumbled on a significant number of comments that appear to > cover static typing as a proof of concept , but in the process I've > found no tutorial on how to implement it. Try Cobra instead. It's Python-like syntax, but allows static typing. http://cobra-language.com/ -- Steven From rui.maciel at gmail.com Tue Aug 6 05:04:10 2013 From: rui.maciel at gmail.com (Rui Maciel) Date: Tue, 06 Aug 2013 10:04:10 +0100 Subject: Newbie: static typing? References: <520087dc$0$21718$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Mon, 05 Aug 2013 21:46:57 +0100, Rui Maciel wrote: > >> Is there any pythonic way to perform static typing? After searching the >> web I've stumbled on a significant number of comments that appear to >> cover static typing as a proof of concept , but in the process I've >> found no tutorial on how to implement it. > > Try Cobra instead. It's Python-like syntax, but allows static typing. > > http://cobra-language.com/ Thanks for the suggestion, but switching Python for another programming language would defeat the purpose of learning Python. Rui Maciel From rui.maciel at gmail.com Tue Aug 6 05:01:31 2013 From: rui.maciel at gmail.com (Rui Maciel) Date: Tue, 06 Aug 2013 10:01:31 +0100 Subject: Newbie: static typing? References: Message-ID: Ben Finney wrote: > Rui Maciel writes: > >> Is there any pythonic way to perform static typing? > > I think no; static typing is inherently un-Pythonic. > > Python provides strong, dynamic typing. Enjoy it! Bummer. >> Does anyone care to enlighten a newbie? > > Is there some specific problem you think needs static typing? Perhaps > you could start a new thread, giving an example where you are having > trouble and you think static typing would help. It would be nice if some functions threw an error if they were passed a type they don't support or weren't designed to handle. That would avoid having to deal with some bugs which otherwise would never happen. To avoid this sort of error, I've been testing arguments passed to some functions based on their type, and raising TypeError when necessariy, but surely there must be a better, more pythonic way to handle this issue. Rui Maciel From joshua at landau.ws Tue Aug 6 05:19:55 2013 From: joshua at landau.ws (Joshua Landau) Date: Tue, 6 Aug 2013 10:19:55 +0100 Subject: Newbie: static typing? In-Reply-To: References: Message-ID: On 6 August 2013 10:01, Rui Maciel wrote: > Ben Finney wrote: > >> Rui Maciel writes: >> >>> Is there any pythonic way to perform static typing? >> >> I think no; static typing is inherently un-Pythonic. >> >> Python provides strong, dynamic typing. Enjoy it! > > Bummer. It's really not. >>> Does anyone care to enlighten a newbie? >> >> Is there some specific problem you think needs static typing? Perhaps >> you could start a new thread, giving an example where you are having >> trouble and you think static typing would help. > > It would be nice if some functions threw an error if they were passed a type > they don't support or weren't designed to handle. That would avoid having > to deal with some bugs which otherwise would never happen. > > To avoid this sort of error, I've been testing arguments passed to some > functions based on their type, and raising TypeError when necessariy, but > surely there must be a better, more pythonic way to handle this issue. Unless you have a very good reason, don't do this. It's a damn pain when functions won't accept my custom types with equivalent functionality -- Python's a duck-typed language and it should behave like one. From rosuav at gmail.com Tue Aug 6 05:29:40 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Aug 2013 10:29:40 +0100 Subject: Newbie: static typing? In-Reply-To: References: Message-ID: On Tue, Aug 6, 2013 at 10:01 AM, Rui Maciel wrote: > It would be nice if some functions threw an error if they were passed a type > they don't support or weren't designed to handle. That would avoid having > to deal with some bugs which otherwise would never happen. > > To avoid this sort of error, I've been testing arguments passed to some > functions based on their type, and raising TypeError when necessariy, but > surely there must be a better, more pythonic way to handle this issue. def add_three_values(x,y,z): return x+y+z Do you want to test these values for compatibility? Remember, you could take a mixture of types, as most of the numeric types can safely be added. You can also add strings, or lists, but you can't mix them. And look! It already raises TypeError if it's given something unsuitable: >>> add_three_values(1,"foo",[4,6]) Traceback (most recent call last): File "", line 1, in add_three_values(1,"foo",[4,6]) File "", line 2, in add_three_values return x+y+z TypeError: unsupported operand type(s) for +: 'int' and 'str' The Pythonic way is to not care what the objects' types are, but to simply use them. In C++ and Java, it's usually assumed that the person writing a function/class is different from the person writing the code that uses it, and that each needs to be protected from each other. In Python, it's assumed that either you're writing both halves yourself, or at least you're happy to be aware of the implementation on the other side. It saves a HUGE amount of trouble; for instance, abolishing private members makes everything easier. This philosophical difference does take some getting used to, but is so freeing. The worst it can do is give you a longer traceback when a problem is discovered deep in the call tree, and if your call stack takes more than a page to display, that's code smell for another reason. (I've seen Ruby tracebacks that are like that. I guess Ruby programmers get used to locating the important part.) ChrisA From rui.maciel at gmail.com Tue Aug 6 06:07:56 2013 From: rui.maciel at gmail.com (Rui Maciel) Date: Tue, 06 Aug 2013 11:07:56 +0100 Subject: Newbie: static typing? References: Message-ID: Joshua Landau wrote: > Unless you have a very good reason, don't do this. It's a damn pain > when functions won't accept my custom types with equivalent > functionality -- Python's a duck-typed language and it should behave > like one. In that case what's the pythonic way to deal with standard cases like this one? class SomeModel(object): def __init__(self): self.label = "this is a label attribute" def accept(self, visitor): visitor.visit(self) print("visited: ", self.label) class AbstractVisitor(object): def visit(self, element): pass class ConcreteVisitorA(AbstractVisitor): def visit(self, element): element.label = "ConcreteVisitorA operated on this model" class ConcreteVisitorB(AbstractVisitor): def visit(self, element): element.label = "ConcreteVisitorB operated on this model" model = SomeModel() operatorA = ConcreteVisitorA() model.accept(operatorA) operatorB = ConcreteVisitorB() model.accept(operatorA) not_a_valid_type = "foo" model.accept(not_a_valid_type) Rui Maciel From sg552 at hotmail.co.uk Tue Aug 6 10:25:26 2013 From: sg552 at hotmail.co.uk (Rotwang) Date: Tue, 06 Aug 2013 15:25:26 +0100 Subject: Newbie: static typing? In-Reply-To: References: Message-ID: On 06/08/2013 11:07, Rui Maciel wrote: > Joshua Landau wrote: > >> Unless you have a very good reason, don't do this [i.e. checking >> arguments for type at runtime and raising TypeError]. It's a damn pain >> when functions won't accept my custom types with equivalent >> functionality -- Python's a duck-typed language and it should behave >> like one. > > In that case what's the pythonic way to deal with standard cases like this > one? > > > class SomeModel(object): > def __init__(self): > self.label = "this is a label attribute" > > def accept(self, visitor): > visitor.visit(self) > print("visited: ", self.label) > > > class AbstractVisitor(object): > def visit(self, element): > pass > > > class ConcreteVisitorA(AbstractVisitor): > def visit(self, element): > element.label = "ConcreteVisitorA operated on this model" > > class ConcreteVisitorB(AbstractVisitor): > def visit(self, element): > element.label = "ConcreteVisitorB operated on this model" > > > model = SomeModel() > > operatorA = ConcreteVisitorA() > > model.accept(operatorA) > > operatorB = ConcreteVisitorB() > > model.accept(operatorA) > > not_a_valid_type = "foo" > > model.accept(not_a_valid_type) > The Pythonic way to deal with it is exactly how you deal with it above. When the script attempts to call model.accept(not_a_valid_type) an exception is raised, and the exception's traceback will tell you exactly what the problem was (namely that not_a_valid_type does not have a method called "visit"). In what way would runtime type-checking be any better than this? There's an obvious way in which it would be worse, namely that it would prevent the user from passing a custom object to SomeModel.accept() that has a visit() method but is not one of the types for which you thought to check. From ben+python at benfinney.id.au Tue Aug 6 18:34:20 2013 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 07 Aug 2013 08:34:20 +1000 Subject: Newbie: static typing? References: Message-ID: <7wd2pqbg9f.fsf@benfinney.id.au> Rui Maciel writes: > class AbstractVisitor(object): > def visit(self, element): > pass A small improvement to your code: If you want an abstract method ? that is, a method which should not be called directly but which sub-classes should over-ride ? then your abstract method should not ?pass?. Instead, it should ?raise NotImplementedError? so this will be clear to anyone if the MRO falls back to the abstract method. > not_a_valid_type = "foo" > > model.accept(not_a_valid_type) At this point, the ?not_a_valid_type? object is asked for its ?visit? method, and a TypeError is raised. That's good, because it informs the person looking at the code that this object doesn't support the needed behaviour. This is as it's meant to be; what problem are you pointing out here? -- \ ?Software patents provide one more means of controlling access | `\ to information. They are the tool of choice for the internet | _o__) highwayman.? ?Anthony Taylor | Ben Finney From rui.maciel at gmail.com Tue Aug 6 06:28:07 2013 From: rui.maciel at gmail.com (Rui Maciel) Date: Tue, 06 Aug 2013 11:28:07 +0100 Subject: Newbie: static typing? References: Message-ID: Chris Angelico wrote: > On Tue, Aug 6, 2013 at 10:01 AM, Rui Maciel wrote: >> It would be nice if some functions threw an error if they were passed a >> type >> they don't support or weren't designed to handle. That would avoid >> having to deal with some bugs which otherwise would never happen. >> >> To avoid this sort of error, I've been testing arguments passed to some >> functions based on their type, and raising TypeError when necessariy, but >> surely there must be a better, more pythonic way to handle this issue. > > def add_three_values(x,y,z): > return x+y+z > > Do you want to test these values for compatibility? Remember, you > could take a mixture of types, as most of the numeric types can safely > be added. You can also add strings, or lists, but you can't mix them. > And look! It already raises TypeError if it's given something > unsuitable: If the type problems aren't caught right away when the invalid types are passed to a function then the problem may only manifest itself in some far away point in the code, making this bug needlessly harder to spot and fix, and making the whole ordeal needlessly too time consuming. Rui Maciel From rosuav at gmail.com Tue Aug 6 06:50:38 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Aug 2013 11:50:38 +0100 Subject: Newbie: static typing? In-Reply-To: References: Message-ID: On Tue, Aug 6, 2013 at 11:28 AM, Rui Maciel wrote: > Chris Angelico wrote: >> def add_three_values(x,y,z): >> return x+y+z >> >> Do you want to test these values for compatibility? Remember, you >> could take a mixture of types, as most of the numeric types can safely >> be added. You can also add strings, or lists, but you can't mix them. >> And look! It already raises TypeError if it's given something >> unsuitable: > > If the type problems aren't caught right away when the invalid types are > passed to a function then the problem may only manifest itself in some far > away point in the code, making this bug needlessly harder to spot and fix, > and making the whole ordeal needlessly too time consuming. There are two problems that can result from not checking: 1) The traceback will be deeper and may be less clear. 2) Some code will be executed and then an exception thrown. If #2 is a problem, then you write checks in. (But be aware that exceptions can be thrown from all sorts of places. It's usually better to write your code to cope with exceptions than to write it to check its args.) But that's a highly unusual case. With >99% of Python scripts, it won't matter; programming errors are corrected by editing the source and rerunning the program from the top. (There ARE exceptions to this, but in Python they're relatively rare. In some of my serverside programming (usually in Pike), I have to code in *much* better protection than this. But if you're doing that sort of thing, you'll know.) So the real problem here is that, when there's a bug, the traceback is longer and perhaps unclear. This is at times a problem, but it's not as big a problem as the maintenance burden of all those extra type checks. You might have a bug that takes you an extra few minutes to diagnose because it's actually caused half way up the call stack (or, worse, it doesn't come up in testing at all and it slips through into production), but you save hours and hours of fiddling with the type checks, and perhaps outright fighting them when you want to do something more unusual. Or you write six versions of a function, with different type checking. Any of these scenarios is, in my opinion, far worse than the occasional bit of extra debugging work. Like everything, it's a tradeoff. And if your function signatures are sufficiently simple, you won't often get the args wrong anyway. ChrisA From tjreedy at udel.edu Tue Aug 6 19:02:34 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 06 Aug 2013 19:02:34 -0400 Subject: Newbie: static typing? In-Reply-To: References: Message-ID: On 8/6/2013 6:50 AM, Chris Angelico wrote: > On Tue, Aug 6, 2013 at 11:28 AM, Rui Maciel wrote: >> Chris Angelico wrote: >>> def add_three_values(x,y,z): >>> return x+y+z >>> >>> Do you want to test these values for compatibility? Remember, you >>> could take a mixture of types, as most of the numeric types can safely >>> be added. You can also add strings, or lists, but you can't mix them. >>> And look! It already raises TypeError if it's given something >>> unsuitable: >> >> If the type problems aren't caught right away when the invalid types are >> passed to a function then the problem may only manifest itself in some far >> away point in the code, making this bug needlessly harder to spot and fix, >> and making the whole ordeal needlessly too time consuming. > > There are two problems that can result from not checking: > > 1) The traceback will be deeper and may be less clear. > > 2) Some code will be executed and then an exception thrown. 3) The code falls into an infinite loop or recursion. The solution is to think before looping or recursing. This often involves value checking (non-negative int or non-fractional float, for instance) rather than type checking in the usual static type-checking sense. One also needs to be careful about passing unbounded iterators to other functions and remember that unboundedness is contagious. (filter(pred, unbounded_iterator) is an unbounded iterator). Again, this is a 'value' or implicit sub-type issue rather than a explicit, visible 'type' issue. Infinite recursion will be caught eventually when memory runs out (and that is an advantage of recursion over iteration ;-), but I prefer to avoid it anyway. -- Terry Jan Reedy From rosuav at gmail.com Tue Aug 6 20:16:39 2013 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 7 Aug 2013 01:16:39 +0100 Subject: Newbie: static typing? In-Reply-To: References: Message-ID: On Wed, Aug 7, 2013 at 12:02 AM, Terry Reedy wrote: > 3) The code falls into an infinite loop or recursion. > > The solution is to think before looping or recursing. This often involves > value checking (non-negative int or non-fractional float, for instance) > rather than type checking in the usual static type-checking sense. Yeah, there aren't many languages that let you declare that the argument must be a positive integer. That's just something you have to test for manually. > One also needs to be careful about passing unbounded iterators to other > functions and remember that unboundedness is contagious. (filter(pred, > unbounded_iterator) is an unbounded iterator). Again, this is a 'value' or > implicit sub-type issue rather than a explicit, visible 'type' issue. Not quite always; I'd say that unboundedness is as contagious as IEEE Infinity. Lots of operations on infinity will yield infinity, but a few won't. itertools.islice can guarantee a finite iterator, and takewhile may terminate. But yes, with filter() it certainly is. ChrisA From ramit.prasad at jpmorgan.com.dmarc.invalid Thu Aug 8 12:46:03 2013 From: ramit.prasad at jpmorgan.com.dmarc.invalid (Prasad, Ramit) Date: Thu, 8 Aug 2013 16:46:03 +0000 Subject: Newbie: static typing? In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF474186261C3@SCACMX008.exchad.jpmchase.net> Rui Maciel wrote: > Chris Angelico wrote: > > > On Tue, Aug 6, 2013 at 10:01 AM, Rui Maciel wrote: > >> It would be nice if some functions threw an error if they were passed a > >> type > >> they don't support or weren't designed to handle. That would avoid > >> having to deal with some bugs which otherwise would never happen. > >> > >> To avoid this sort of error, I've been testing arguments passed to some > >> functions based on their type, and raising TypeError when necessariy, but > >> surely there must be a better, more pythonic way to handle this issue. > > > > def add_three_values(x,y,z): > > return x+y+z > > > > Do you want to test these values for compatibility? Remember, you > > could take a mixture of types, as most of the numeric types can safely > > be added. You can also add strings, or lists, but you can't mix them. > > And look! It already raises TypeError if it's given something > > unsuitable: > > If the type problems aren't caught right away when the invalid types are > passed to a function then the problem may only manifest itself in some far > away point in the code, making this bug needlessly harder to spot and fix, > and making the whole ordeal needlessly too time consuming. > > > Rui Maciel This can be true, but in personal experience does not happen often. I will say that dynamic typing ends up usually being more future proof as I can later create a similar object (but not in the same inheritance hierarchy) that will work with older functions because the functions don't look for certain types but rather just rely on duck typing. I find this especially useful when testing or mocking. I can create a test object and attach methods/attributes to the test object to duck type as I desire to test the my desired code. I think the following reads are very interesting for people new to Python from other languages (not just Java). http://dirtsimple.org/2004/12/python-is-not-java.html (and the flip side) http://dirtsimple.org/2004/12/java-is-not-python-either.html ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From rui.maciel at gmail.com Tue Aug 6 05:05:57 2013 From: rui.maciel at gmail.com (Rui Maciel) Date: Tue, 06 Aug 2013 10:05:57 +0100 Subject: Newbie: static typing? References: Message-ID: Gary Herron wrote: > The Pythonic way is to *enjoy* the freedom and flexibility and power of > dynamic typing. If you are stepping out of a static typing language > into Python, don't step just half way. Embrace dynamic typing. (Like > any freedom, it can bite you at times, but that's no reason to hobble > Python with static typing.) What's the Python way of dealing with objects being passed to a function that aren't of a certain type, have specific attributes of a specific type, nor support a specific interface? Rui Maciel From joshua at landau.ws Tue Aug 6 05:29:13 2013 From: joshua at landau.ws (Joshua Landau) Date: Tue, 6 Aug 2013 10:29:13 +0100 Subject: Newbie: static typing? In-Reply-To: References: Message-ID: On 6 August 2013 10:05, Rui Maciel wrote: > Gary Herron wrote: > >> The Pythonic way is to *enjoy* the freedom and flexibility and power of >> dynamic typing. If you are stepping out of a static typing language >> into Python, don't step just half way. Embrace dynamic typing. (Like >> any freedom, it can bite you at times, but that's no reason to hobble >> Python with static typing.) > > What's the Python way of dealing with objects being passed to a function > that aren't of a certain type, have specific attributes of a specific type, > nor support a specific interface? What's the actual problem you're facing? Where do you feel that you need to verify types? From steve at pearwood.info Tue Aug 6 05:26:50 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 06 Aug 2013 09:26:50 GMT Subject: Newbie: static typing? References: Message-ID: <5200c15a$0$29986$c3e8da3$5496439d@news.astraweb.com> On Tue, 06 Aug 2013 10:05:57 +0100, Rui Maciel wrote: > What's the Python way of dealing with objects being passed to a function > that aren't of a certain type, have specific attributes of a specific > type, nor support a specific interface? Raise TypeError, or just let the failure occurs however it occurs, depending on how much you care about early failure. Worst: if type(obj) is not int: raise TypeError("obj must be an int") Better, because it allows subclasses: if not isinstance(obj, int): raise TypeError("obj must be an int") Better still: import numbers if not isinstance(obj, numbers.Integer): raise TypeError("obj must be an integer number") All of the above is "look before you leap". Under many circumstances, it is "better to ask for forgiveness than permission" by just catching the exception: try: flag = obj & 8 except TypeError: flag = False Or just don't do anything: flag = obj & 8 If obj is the wrong type, you will get a perfectly fine exception at run- time. Why do extra work do try to detect the failure when Python will do it for you? -- Steven From rui.maciel at gmail.com Tue Aug 6 06:12:29 2013 From: rui.maciel at gmail.com (Rui Maciel) Date: Tue, 06 Aug 2013 11:12:29 +0100 Subject: Newbie: static typing? References: Message-ID: Joshua Landau wrote: > What's the actual problem you're facing? Where do you feel that you > need to verify types? A standard case would be when there's a function which is designed expecting that all operands support a specific interface or contain specific attributes. In other words, when passing an unsupported type causes problems. Rui Maciel From burak.arslan at arskom.com.tr Tue Aug 6 09:27:10 2013 From: burak.arslan at arskom.com.tr (Burak Arslan) Date: Tue, 06 Aug 2013 16:27:10 +0300 Subject: Newbie: static typing? In-Reply-To: References: Message-ID: <5200F9AE.9000805@arskom.com.tr> On 08/06/13 13:12, Rui Maciel wrote: > Joshua Landau wrote: > >> What's the actual problem you're facing? Where do you feel that you >> need to verify types? > A standard case would be when there's a function which is designed expecting > that all operands support a specific interface or contain specific > attributes. > > In other words, when passing an unsupported type causes problems. > Hi, First, let's get over the fact that, with dynamic typing, code fails at runtime. Irrespective of language, you just shouldn't ship untested code, so I say that's not an argument against dynamic typing. This behaviour is only a problem when code fails *too late* into the runtime -- i.e. when you don't see the offending value in the stack trace. For example, consider you append values to a list and the values in that list get processed somewhere else. If your code fails because of an invalid value, your stack trace is useless, because that value should not be there in the first place. The code should fail when appending to that list and not when processing it. The "too late" case is a bit tough to illustrate. This could be a rough example: https://gist.github.com/plq/6163839 Imagine that the list there is progressively constructed somewhere else in the code and later processed by the sq_all function. As you can see, the stack trace is pretty useless as we don't see how that value got there. In such cases, you do need manual type checking. Yet, as someone else noted, naively using isinstance() for type checking breaks duck typing. So you should read up on abstract base classes: http://docs.python.org/2/glossary.html#term-abstract-base-class These said, I've been writing Python for several years now, and I only needed to resort to this technique only once. (i was working on a compiler) Most of the time, you'll be just fine without any manual type checking. Best regards, Burak From antoon.pardon at rece.vub.ac.be Tue Aug 6 09:57:26 2013 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Tue, 06 Aug 2013 15:57:26 +0200 Subject: Newbie: static typing? In-Reply-To: <5200F9AE.9000805@arskom.com.tr> References: <5200F9AE.9000805@arskom.com.tr> Message-ID: <520100C6.30909@rece.vub.ac.be> Op 06-08-13 15:27, Burak Arslan schreef: > On 08/06/13 13:12, Rui Maciel wrote: >> Joshua Landau wrote: >> >>> What's the actual problem you're facing? Where do you feel that you >>> need to verify types? >> A standard case would be when there's a function which is designed expecting >> that all operands support a specific interface or contain specific >> attributes. >> >> In other words, when passing an unsupported type causes problems. >> > > Hi, > > First, let's get over the fact that, with dynamic typing, code fails at > runtime. Irrespective of language, you just shouldn't ship untested > code, so I say that's not an argument against dynamic typing. Why not? Can ease of development not be a consideration? So if some kind of faults are easier to detect at compile time if you have static typing than if you have to design a test for them, I don't see why that can't be an argument. -- Antoon Pardon From rosuav at gmail.com Tue Aug 6 10:06:40 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Aug 2013 15:06:40 +0100 Subject: Newbie: static typing? In-Reply-To: <520100C6.30909@rece.vub.ac.be> References: <5200F9AE.9000805@arskom.com.tr> <520100C6.30909@rece.vub.ac.be> Message-ID: On Tue, Aug 6, 2013 at 2:57 PM, Antoon Pardon wrote: > Op 06-08-13 15:27, Burak Arslan schreef: >> On 08/06/13 13:12, Rui Maciel wrote: >>> Joshua Landau wrote: >>> >>>> What's the actual problem you're facing? Where do you feel that you >>>> need to verify types? >>> A standard case would be when there's a function which is designed expecting >>> that all operands support a specific interface or contain specific >>> attributes. >>> >>> In other words, when passing an unsupported type causes problems. >>> >> >> Hi, >> >> First, let's get over the fact that, with dynamic typing, code fails at >> runtime. Irrespective of language, you just shouldn't ship untested >> code, so I say that's not an argument against dynamic typing. > > Why not? Can ease of development not be a consideration? So if some > kind of faults are easier to detect at compile time if you have static > typing than if you have to design a test for them, I don't see why that > can't be an argument. Sure, which is why I like working in Pike, which does have static type declarations (when you want them; they can get out the way when you don't). But there will always be, regardless of your language, criteria that static typing cannot adequately handle, so just write your code to cope with exceptions - much easier. If the exception's never thrown, the bug can't be all that serious; otherwise, just deal with it when you find it, whether that be in initial testing or years later in production. There WILL BE such errors - that's a given. Deal with them, rather than trying to eliminate them. ChrisA From esj at harvee.org Tue Aug 6 09:58:53 2013 From: esj at harvee.org (Eric S. Johansson) Date: Tue, 06 Aug 2013 09:58:53 -0400 Subject: Newbie: static typing? In-Reply-To: <5200F9AE.9000805@arskom.com.tr> References: <5200F9AE.9000805@arskom.com.tr> Message-ID: On Tue, 06 Aug 2013 09:27:10 -0400, Burak Arslan wrote: > First, let's get over the fact that, with dynamic typing, code fails at > runtime. Irrespective of language, you just shouldn't ship untested > code, so I say that's not an argument against dynamic typing. It's not so much shipping untested code as not having or unable to test all the pathways in the code shipped. I ran into this problem with a server I built. I ended up solving the problem by building a testing scaffolding that let me control all inputs. It would've been much easier with static typing to make sure all the pieces lined up. The other technique I've used is a properly set up exception handling environment. Do it right and you can log all of the errors so that you have useful information. Part of "doing it right" includes a system that tells you when exceptions happened right away so the server doesn't run for days or more failing at random but nobody notices because your exceptions keep the system for failing completely. I guess this is a long way of saying instrument your software so that it can be tested and or give you enough information about the internal state. This is sort of like building a specialized integrated circuit. You need to design it so it can be tested/observed after it's been embedded in epoxy and not just count on being able to probe the wafer in the lab. From rosuav at gmail.com Tue Aug 6 10:38:27 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Aug 2013 15:38:27 +0100 Subject: Newbie: static typing? In-Reply-To: References: <5200F9AE.9000805@arskom.com.tr> Message-ID: On Tue, Aug 6, 2013 at 2:58 PM, Eric S. Johansson wrote: > I guess this is a long way of saying instrument your software so that it can > be tested and or give you enough information about the internal state. This > is sort of like building a specialized integrated circuit. You need to > design it so it can be tested/observed after it's been embedded in epoxy and > not just count on being able to probe the wafer in the lab. In software, that's easy: just have a way to execute arbitrary code in the context of the running server. A *hugely* beneficial debugging tool. Of course, it's also a security concern, so you have to put a good password [1] on it, or have some other system for guaranteeing that untrusted persons can't execute arbitrary code. ChrisA [1] By which I mean http://xkcd.com/936/ compliant. From ben+python at benfinney.id.au Tue Aug 6 18:23:04 2013 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 07 Aug 2013 08:23:04 +1000 Subject: Easier to Ask Forgiveness than Permission (was: Newbie: static typing?) References: Message-ID: <7whaf2bgs7.fsf_-_@benfinney.id.au> Rui Maciel writes: > Gary Herron wrote: > > > The Pythonic way is to *enjoy* the freedom and flexibility and power > > of dynamic typing. If you are stepping out of a static typing > > language into Python, don't step just half way. Embrace dynamic > > typing. (Like any freedom, it can bite you at times, but that's no > > reason to hobble Python with static typing.) > > What's the Python way of dealing with objects being passed to a > function that aren't of a certain type, have specific attributes of a > specific type, nor support a specific interface? Python has strong typing. That means every Python object (and in Python, every value is an object) knows what its type is and therefore what behaviours it supports. Types (that is, classes; the two terms refer to the same thing in Python) are the correct place to put checks on whether the type's instances are being used properly. Don't check types of objects in every place where you use those objects. (This is a sub-set of Look Before You Leap programming, which is discouraged because it makes your code far too heavy on checking for problems rather than the purpose of the code.) Check for type appropriate usage in the type itself. So the Pythonic way to deal with objects that don't support particular behaviour is: Use the object on the assumption that it supports the behaviour you want ? that is, assume the caller of your function is giving you an object that your function can use. If the object doesn't support that behaviour, an error will be raised from the type. Sometimes your function will know exactly what to do with an error, and can give more specific information about the problem. In those cases, you should catch the exception and ?raise MoreSpecificError("foo") from exc?. But only in those cases where your function *actually does* know more about the problem. In the majority of cases, don't check the type at all, and allow the type-specific error to raise back to the caller, who then has to deal with the fact they've passed your function an object that doesn't support the necessary behaviour. This principle ? of assuming the behaviour is supported, and having a robust infrastructure for dealing with errors ? is known as Easier to Ask Forgiveness than Permission. In Python, we discourage LBYL and encourage EAFP. Our code tends to have a lot less boiler-plate and obfuscatory error-checking as a result, and tends to be more expressive than statically-typed languages. -- \ ?If you make people think they're thinking, they'll love you; | `\ but if you really make them think, they'll hate you.? ?Donald | _o__) Robert Perry Marquis | Ben Finney From invalid at invalid.invalid Tue Aug 6 11:05:54 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 6 Aug 2013 15:05:54 +0000 (UTC) Subject: Newbie: static typing? References: Message-ID: On 2013-08-05, Rui Maciel wrote: > Is there any pythonic way to perform static typing? No. One of the fundamental characteristics of Python is dynamic typing. Without dynamic typing, it wouldn't _be_ Python. -- 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 thcerbla.netsrot at googlemail.com Mon Aug 5 18:24:04 2013 From: thcerbla.netsrot at googlemail.com (thcerbla.netsrot at googlemail.com) Date: Mon, 5 Aug 2013 15:24:04 -0700 (PDT) Subject: undefined method []' for nil:NilClass (NoMethodError) Message-ID: <511eb972-a9ad-43c2-9a22-50605bb587e6@googlegroups.com> Hi, I have this script to monitor solr4 with munin. But I get an error and I hove no idea what's the problem. Hope someone can help me... Thanks, Torsten Error: /etc/munin/plugins/solr4_multicore_avgRequestsPerSecond:60: undefined method []' for nil:NilClass (NoMethodError) from /etc/munin/plugins/solr4_multicore_avgRequestsPerSecond:50:in each' from /etc/munin/plugins/solr4_multicore_avgRequestsPerSecond:50 Script: #%# family=auto #%# capabilities=autoconf require 'open-uri' SOLR_PORT = 8983 SOLR_HOST = "localhost" target = __FILE__.split("_")[-1] cores_url = "http://#{SOLR_HOST}:#{SOLR_PORT}/solr/admin/cores?action=STATUS&wt=ruby" error = false begin cores = eval(open(cores_url).read)['status'].keys.sort rescue error = true end case ARGV[0] when 'autoconf' puts error ? "no" : "yes" when 'config' if target.end_with?("Cache") puts "graph_title #{target} hitratio" puts "graph_args --base 1000 -r --lower-limit 0 --upper-limit 100" puts "graph_vlabel %" else puts "graph_title #{target}" puts "graph_args --base 1000" puts "graph_vlabel Size #{target}" end puts "graph_category Solr" puts "graph_info Info for cores: #{cores.join(",")}" cores.each do |core| puts "#{core}.label #{core}" puts "#{core}.type GAUGE" puts "#{core}.min 0" end else cores.each do |core| url = "http://#{SOLR_HOST}:#{SOLR_PORT}/solr/#{core}/admin/mbeans?stats=true&wt=ruby" res = eval(open(url).read) if target.end_with?('Cache') cache = Hash[*res['solr-mbeans']]['CACHE'] hitratio = cache[target]['stats']['hitratio'] stat = (hitratio.to_f * 100).round else handler = Hash[*res['solr-mbeans']]["QUERYHANDLER"] stat = handler['/select']['stats'][target] end puts "#{core}.value #{stat}" end end From rosuav at gmail.com Mon Aug 5 18:29:28 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 5 Aug 2013 23:29:28 +0100 Subject: undefined method []' for nil:NilClass (NoMethodError) In-Reply-To: <511eb972-a9ad-43c2-9a22-50605bb587e6@googlegroups.com> References: <511eb972-a9ad-43c2-9a22-50605bb587e6@googlegroups.com> Message-ID: On Mon, Aug 5, 2013 at 11:24 PM, wrote: > I have this script to monitor solr4 with munin. > > But I get an error and I hove no idea what's the problem. > > Error: > /etc/munin/plugins/solr4_multicore_avgRequestsPerSecond:60: undefined method []' for nil:NilClass (NoMethodError) > > cores_url = "http://#{SOLR_HOST}:#{SOLR_PORT}/solr/admin/cores?action=STATUS&wt=ruby" Your first problem, I think, is that that's not Python code. My crystal ball suggests it's probably Ruby. ChrisA From dbarrosop at dravetech.com Mon Aug 5 18:56:59 2013 From: dbarrosop at dravetech.com (David Barroso) Date: Tue, 6 Aug 2013 00:56:59 +0200 Subject: Working with XML/XSD Message-ID: Hello, I was wondering if someone could point me in the right direction. I would like to develop some scripts to manage Cisco routers and switches using XML. However, I am not sure where to start. Does someone have some experience working with XML, Schemas and things like that? Which libraries do you use? Do you know of any good tutorial? Thanks! David -------------- next part -------------- An HTML attachment was scrubbed... URL: From tdldev at gmail.com Tue Aug 6 08:00:05 2013 From: tdldev at gmail.com (Verde Denim) Date: Tue, 06 Aug 2013 08:00:05 -0400 Subject: Working with XML/XSD In-Reply-To: References: Message-ID: <5200E545.2010204@gmail.com> On 08/05/2013 06:56 PM, David Barroso wrote: > Hello, > I was wondering if someone could point me in the right direction. I > would like to develop some scripts to manage Cisco routers and > switches using XML. However, I am not sure where to start. Does > someone have some experience working with XML, Schemas and things like > that? Which libraries do you use? Do you know of any good tutorial? > > Thanks! > > David > > W3C has a decent primer for XML (w3schools.com/xml/). I have a couple of O'Reilly publications on the topic that are ok as well. There's a site dedicated to the subject at xmlnews.org, and someone told me about a course available at xmlmaster.org. I also found a short preso at oreilly.com called the Eight Minute XML tutorial on automating system administration so that's probably got some promise in the area you're looking for. hth -- Regards Jack Boston Tea Party, Coercive Acts, Powder Alarm, Revolution Lessons (Mistakes) not learned are bound to be repeated. From burak.arslan at arskom.com.tr Tue Aug 6 09:33:16 2013 From: burak.arslan at arskom.com.tr (Burak Arslan) Date: Tue, 06 Aug 2013 16:33:16 +0300 Subject: Working with XML/XSD In-Reply-To: References: Message-ID: <5200FB1C.1060803@arskom.com.tr> On 08/06/13 01:56, David Barroso wrote: > Hello, > I was wondering if someone could point me in the right direction. I > would like to develop some scripts to manage Cisco routers and > switches using XML. However, I am not sure where to start. Does > someone have some experience working with XML, Schemas and things like > that? Which libraries do you use? Do you know of any good tutorial? > Hi, I develop Spyne (http://spyne.io), it does let you define Xml Schema types, generate the Schema documents, (and in the upcoming release, parse them) and also does both serialization and deserialization of python objects from and to xml according to definitions in the xml schema. It also does RPC :) In case you don't want to use a framework, use lxml, it's a very good xml manipulation library based on libxml2/libxslt. You can use lxml.objectify for xml serialization as well. Best, Burak From dieter at handshake.de Wed Aug 7 02:00:13 2013 From: dieter at handshake.de (dieter) Date: Wed, 07 Aug 2013 08:00:13 +0200 Subject: Working with XML/XSD References: Message-ID: <87y58em45u.fsf@handshake.de> David Barroso writes: > I was wondering if someone could point me in the right direction. I would > like to develop some scripts to manage Cisco routers and switches using > XML. However, I am not sure where to start. Does someone have some > experience working with XML, Schemas and things like that? Which libraries > do you use? Do you know of any good tutorial? I made good experience with "PyXB". This package takes an XML-schema definition and generates Python classes from it. XML documents (conforming to the schema) can be parsed into instances of those classes and instances of those classes can be serialized as XML documents. It hides well (many of) the complexities of XML-schema (if the schemas are given). From dbarrosop at dravetech.com Wed Aug 7 03:06:25 2013 From: dbarrosop at dravetech.com (David Barroso) Date: Wed, 7 Aug 2013 09:06:25 +0200 Subject: Working with XML/XSD In-Reply-To: <87y58em45u.fsf@handshake.de> References: <87y58em45u.fsf@handshake.de> Message-ID: I will start looking to the links and libraries you pointed out. As the schema definitions are provided by the vendor I think I will go for PyXB. Thanks! On Wed, Aug 7, 2013 at 8:00 AM, dieter wrote: > David Barroso writes: > > > I was wondering if someone could point me in the right direction. I would > > like to develop some scripts to manage Cisco routers and switches using > > XML. However, I am not sure where to start. Does someone have some > > experience working with XML, Schemas and things like that? Which > libraries > > do you use? Do you know of any good tutorial? > > I made good experience with "PyXB". > > This package takes an XML-schema definition and generates Python classes > from it. XML documents (conforming to the schema) can be parsed into > instances of those classes and instances of those classes can > be serialized as XML documents. > > It hides well (many of) the complexities of XML-schema (if the > schemas are given). > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From gratedmedia at gmail.com Mon Aug 5 21:01:09 2013 From: gratedmedia at gmail.com (gratedmedia at gmail.com) Date: Mon, 5 Aug 2013 18:01:09 -0700 (PDT) Subject: Creating a running tally/ definitely new to this Message-ID: <9e9e6a5a-5f8e-46b4-91dc-757fc74348d9@googlegroups.com> I currently working on a game, where I need to maintain a running tally of money, as the player makes purchases as they navigate thru game. I not exactly sure how to do this in python. I know it is a fairly basic step, nonetheless. Any assistance would be greatly appreciated. From joshua at landau.ws Mon Aug 5 21:56:08 2013 From: joshua at landau.ws (Joshua Landau) Date: Tue, 6 Aug 2013 02:56:08 +0100 Subject: Creating a running tally/ definitely new to this In-Reply-To: <9e9e6a5a-5f8e-46b4-91dc-757fc74348d9@googlegroups.com> References: <9e9e6a5a-5f8e-46b4-91dc-757fc74348d9@googlegroups.com> Message-ID: On 6 August 2013 02:01, wrote: > > I currently working on a game, where I need to maintain a running tally of > money, as the player makes purchases as they navigate thru game. I not > exactly sure how to do this in python. I know it is a fairly basic step, > nonetheless. Any assistance would be greatly appreciated. > If I understand correctly you want to store the amount of money in a variable as a number that can be changed from many places within the code. Say you have: money = 0 def do_some_adventuring(): ... # Add some money You can "add some money" with: global money # Allow changing money from within the function money += 10 # for example Is that it? ---- Note that normally you'd have some class of player with a money attribute which can be accessed, but as a guess from the question I'm not sure you know about classes yet. -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Mon Aug 5 22:15:30 2013 From: davea at davea.name (Dave Angel) Date: Tue, 6 Aug 2013 02:15:30 +0000 (UTC) Subject: Creating a running tally/ definitely new to this References: <9e9e6a5a-5f8e-46b4-91dc-757fc74348d9@googlegroups.com> Message-ID: gratedmedia at gmail.com wrote: > > I currently working on a game, where I need to maintain a running tally of money, as the player makes purchases as they navigate thru game. I not exactly sure how to do this in python. I know it is a fairly basic step, nonetheless. Any assistance would be greatly appreciated. (just to save you the pain later: http://wiki.python.org/moin/GoogleGroupsPython ) So what have you written so far? Is this a homework assignment and you've been covering certain parts of Python in a certain order? Is it part of learning some tutorial? There are so many ways of accomplishing this sort of thing, that without some constraints, there are a dozen reasonable responses. I'll try one: in the class Player, you make a pair of methods, purchase() and income(), which manipulate the instance attribute assets. Then you make a property that returns the assets. Class Player: .... def purchase(self, amount): self.assets -= amount def income(self, amount): self.assets += amount def wealth(self): return self.assets -- DaveA From gratedmedia at gmail.com Tue Aug 6 07:00:30 2013 From: gratedmedia at gmail.com (gratedmedia at gmail.com) Date: Tue, 6 Aug 2013 04:00:30 -0700 (PDT) Subject: Creating a running tally/ definitely new to this In-Reply-To: References: <9e9e6a5a-5f8e-46b4-91dc-757fc74348d9@googlegroups.com> Message-ID: <591973bb-477e-48bd-a9a2-e644f342e1a8@googlegroups.com> Yes I want to store an amount of money which will change from many places within the code. Absolutely correct. I am very "green" to this, if you're familiar, with "dopewars" the concept is very similar. for my practice trials I used.. selection_b = input() and manually input an amount of money, and used a loop of: loop = 1 while loop < 5: just to get a feel of how it would work but, every time I travel to a new destination and buy something the amount of money is reset. And I am working to prevent this so that I can maintain a running tally until the loop is complete. > > > > > I currently working on a game, where I need to maintain a running tally of money, as the player makes purchases as they navigate thru game. ? I not exactly sure how to do this in python. ?I know it is a fairly basic step, nonetheless. ?Any assistance would be greatly appreciated. > > > > > > If I understand correctly you want to store the amount of money in a variable as a number that can be changed from many places within the code. > > > Say you have: > > > > > ? ? money = 0 > > > ? ? def do_some_adventuring(): > ? ? ? ? ... # Add some money > > > You can "add some money" with: > > > > ? ? ? ? global money # Allow changing money from within the function > > ? ? ? ? money += 10 # for example > > > Is that it? > > > ---- > > > Note that normally you'd have some class of player with a money attribute which can be accessed, but as a guess from the question I'm not sure you know about classes yet. From gratedmedia at gmail.com Tue Aug 6 11:24:42 2013 From: gratedmedia at gmail.com (gratedmedia at gmail.com) Date: Tue, 6 Aug 2013 08:24:42 -0700 (PDT) Subject: Creating a running tally/ definitely new to this In-Reply-To: References: <9e9e6a5a-5f8e-46b4-91dc-757fc74348d9@googlegroups.com> Message-ID: <8b372a3b-c71d-4e26-88ae-ce5e6239fc36@googlegroups.com> On Monday, August 5, 2013 10:15:30 PM UTC-4, Dave Angel wrote: > gratedmedia at gmail.com wrote: > > > > > > > > I currently working on a game, where I need to maintain a running tally of money, as the player makes purchases as they navigate thru game. I not exactly sure how to do this in python. I know it is a fairly basic step, nonetheless. Any assistance would be greatly appreciated. > > > > (just to save you the pain later: > > http://wiki.python.org/moin/GoogleGroupsPython > > ) > > > > So what have you written so far? Is this a homework assignment and > > you've been covering certain parts of Python in a certain order? Is it > > part of learning some tutorial? > > > > There are so many ways of accomplishing this sort of thing, that without > > some constraints, there are a dozen reasonable responses. I'll try one: > > > > in the class Player, you make a pair of methods, purchase() and > > income(), which manipulate the instance attribute assets. Then you > > make a property that returns the assets. > > > > > > > > Class Player: > > .... > > def purchase(self, amount): > > self.assets -= amount > > def income(self, amount): > > self.assets += amount > > def wealth(self): > > return self.assets > > > > > > -- > > DaveA This a project I am am working on. I am using "Learn Python the Hard Way". To best explain. I'm working on a game with a similar format to John Dell's Dopewars, but on Python. SO I've created the several destinations to travel, but now maintaining the "running tally (money)" has been my issue. I'm going to take your advice and play with code you posted. Please contact me with any more suggestions. From joshua at landau.ws Wed Aug 7 08:02:20 2013 From: joshua at landau.ws (Joshua Landau) Date: Wed, 7 Aug 2013 13:02:20 +0100 Subject: Creating a running tally/ definitely new to this In-Reply-To: <8b372a3b-c71d-4e26-88ae-ce5e6239fc36@googlegroups.com> References: <9e9e6a5a-5f8e-46b4-91dc-757fc74348d9@googlegroups.com> <8b372a3b-c71d-4e26-88ae-ce5e6239fc36@googlegroups.com> Message-ID: On 6 August 2013 16:24, wrote: > On Monday, August 5, 2013 10:15:30 PM UTC-4, Dave Angel wrote: >> gratedmedia at gmail.com wrote: >> >> > I currently working on a game, where I need to maintain a running tally of money, as the player makes purchases as they navigate thru game. I not exactly sure how to do this in python. I know it is a fairly basic step, nonetheless. Any assistance would be greatly appreciated. >> >> (just to save you the pain later: >> >> http://wiki.python.org/moin/GoogleGroupsPython >> >> ) Look! A link! Read it! > This a project I am am working on. I am using "Learn Python the Hard Way". To best explain. I'm working on a game with a similar format to John Dell's Dopewars, but on Python. SO I've created the several destinations to travel, but now maintaining the "running tally (money)" has been my issue. I'm going to take your advice and play with code you posted. Please contact me with any more suggestions. You're doing something wrong. No-one on this list knows what it is. Hence no-one can help you until you give us some way of finding out. From devyncjohnson at gmail.com Mon Aug 5 22:00:55 2013 From: devyncjohnson at gmail.com (Devyn Collier Johnson) Date: Mon, 05 Aug 2013 22:00:55 -0400 Subject: Sort lines in a plain text file alphanumerically Message-ID: <520058D7.10105@Gmail.com> I am wanting to sort a plain text file alphanumerically by the lines. I have tried this code, but I get an error. I assume this command does not accept newline characters. >>> file = open('/home/collier/pytest/sort.TXT', 'r').read() >>> print(file) z c w r h s d >>> file.sort() #The first blank line above is from the file. I do not know where the second comes from. Traceback (most recent call last): File "", line 1, in AttributeError: 'str' object has no attribute 'sort' I had the parameters (key=str.casefold, reverse=True), but I took those out to make sure the error was not with my parameters. Specifically, I need something that will sort the lines. They may contain one word or one sentence with punctuation. I need to reverse the sorting ('z' before 'a'). The case does not matter ('a' = 'A'). I have also tried this without success: >>> file.sort(key=str.casefold, reverse=True) Traceback (most recent call last): File "", line 1, in AttributeError: 'str' object has no attribute 'sort' Mahalo, DevynCJohnson at Gmail.com From python at mrabarnett.plus.com Mon Aug 5 22:19:38 2013 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 06 Aug 2013 03:19:38 +0100 Subject: Sort lines in a plain text file alphanumerically In-Reply-To: <520058D7.10105@Gmail.com> References: <520058D7.10105@Gmail.com> Message-ID: <52005D3A.2020602@mrabarnett.plus.com> On 06/08/2013 03:00, Devyn Collier Johnson wrote: > I am wanting to sort a plain text file alphanumerically by the lines. I > have tried this code, but I get an error. I assume this command does not > accept newline characters. > > > >>> file = open('/home/collier/pytest/sort.TXT', 'r').read() That returns the file as a single string. > >>> print(file) > z > c > w > r > h > s > d > > > >>> file.sort() #The first blank line above is from the file. I do not > know where the second comes from. > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'str' object has no attribute 'sort' > Strings don't have a sort method. > I had the parameters (key=str.casefold, reverse=True), but I took those > out to make sure the error was not with my parameters. > > Specifically, I need something that will sort the lines. They may > contain one word or one sentence with punctuation. I need to reverse the > sorting ('z' before 'a'). The case does not matter ('a' = 'A'). > > I have also tried this without success: > > >>> file.sort(key=str.casefold, reverse=True) > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'str' object has no attribute 'sort' > Try this: lines = open('/home/collier/pytest/sort.TXT', 'r').readlines() lines.sort() Actually, a more Pythonic way these days is to use the 'with' statement: with open('/home/collier/pytest/sort.TXT') as file: lines = file.readlines() lines.sort() From devyncjohnson at gmail.com Tue Aug 6 06:35:34 2013 From: devyncjohnson at gmail.com (Devyn Collier Johnson) Date: Tue, 06 Aug 2013 06:35:34 -0400 Subject: Sort lines in a plain text file alphanumerically In-Reply-To: <52005D3A.2020602@mrabarnett.plus.com> References: <520058D7.10105@Gmail.com> <52005D3A.2020602@mrabarnett.plus.com> Message-ID: <5200D176.70607@Gmail.com> On 08/05/2013 10:19 PM, MRAB wrote: > On 06/08/2013 03:00, Devyn Collier Johnson wrote: >> I am wanting to sort a plain text file alphanumerically by the lines. I >> have tried this code, but I get an error. I assume this command does not >> accept newline characters. >> >> >> >>> file = open('/home/collier/pytest/sort.TXT', 'r').read() > > That returns the file as a single string. > >> >>> print(file) >> z >> c >> w >> r >> h >> s >> d >> >> >> >>> file.sort() #The first blank line above is from the file. I do not >> know where the second comes from. >> Traceback (most recent call last): >> File "", line 1, in >> AttributeError: 'str' object has no attribute 'sort' >> > Strings don't have a sort method. > >> I had the parameters (key=str.casefold, reverse=True), but I took those >> out to make sure the error was not with my parameters. >> >> Specifically, I need something that will sort the lines. They may >> contain one word or one sentence with punctuation. I need to reverse the >> sorting ('z' before 'a'). The case does not matter ('a' = 'A'). >> >> I have also tried this without success: >> >> >>> file.sort(key=str.casefold, reverse=True) >> Traceback (most recent call last): >> File "", line 1, in >> AttributeError: 'str' object has no attribute 'sort' >> > Try this: > > lines = open('/home/collier/pytest/sort.TXT', 'r').readlines() > lines.sort() > > > Actually, a more Pythonic way these days is to use the 'with' statement: > > with open('/home/collier/pytest/sort.TXT') as file: > lines = file.readlines() > > lines.sort() > Thanks! That works well. After I run your command, I run print(''.join(lines)) to get the sorted output. Even the parameters work without issues. lines.sort(reverse=True, key=str.casefold) Mahalo, DCJ From devyncjohnson at gmail.com Tue Aug 6 06:35:46 2013 From: devyncjohnson at gmail.com (Devyn Collier Johnson) Date: Tue, 06 Aug 2013 06:35:46 -0400 Subject: Sort lines in a plain text file alphanumerically In-Reply-To: <52005D3A.2020602@mrabarnett.plus.com> References: <520058D7.10105@Gmail.com> <52005D3A.2020602@mrabarnett.plus.com> Message-ID: <5200D182.7010104@Gmail.com> On 08/05/2013 10:19 PM, MRAB wrote: > On 06/08/2013 03:00, Devyn Collier Johnson wrote: >> I am wanting to sort a plain text file alphanumerically by the lines. I >> have tried this code, but I get an error. I assume this command does not >> accept newline characters. >> >> >> >>> file = open('/home/collier/pytest/sort.TXT', 'r').read() > > That returns the file as a single string. > >> >>> print(file) >> z >> c >> w >> r >> h >> s >> d >> >> >> >>> file.sort() #The first blank line above is from the file. I do not >> know where the second comes from. >> Traceback (most recent call last): >> File "", line 1, in >> AttributeError: 'str' object has no attribute 'sort' >> > Strings don't have a sort method. > >> I had the parameters (key=str.casefold, reverse=True), but I took those >> out to make sure the error was not with my parameters. >> >> Specifically, I need something that will sort the lines. They may >> contain one word or one sentence with punctuation. I need to reverse the >> sorting ('z' before 'a'). The case does not matter ('a' = 'A'). >> >> I have also tried this without success: >> >> >>> file.sort(key=str.casefold, reverse=True) >> Traceback (most recent call last): >> File "", line 1, in >> AttributeError: 'str' object has no attribute 'sort' >> > Try this: > > lines = open('/home/collier/pytest/sort.TXT', 'r').readlines() > lines.sort() > > > Actually, a more Pythonic way these days is to use the 'with' statement: > > with open('/home/collier/pytest/sort.TXT') as file: > lines = file.readlines() > > lines.sort() > Thanks! That works well. After I run your command, I run print(''.join(lines)) to get the sorted output. Even the parameters work without issues. lines.sort(reverse=True, key=str.casefold) Mahalo, DCJ From joshua at landau.ws Mon Aug 5 23:12:35 2013 From: joshua at landau.ws (Joshua Landau) Date: Tue, 6 Aug 2013 04:12:35 +0100 Subject: Sort lines in a plain text file alphanumerically In-Reply-To: <520058D7.10105@Gmail.com> References: <520058D7.10105@Gmail.com> Message-ID: On 6 August 2013 03:00, Devyn Collier Johnson wrote: > I am wanting to sort a plain text file alphanumerically by the lines. I > have tried this code, but I get an error. I assume this command does not > accept newline characters. > HINT #1: Don't assume that without a reason. It's wrong. > >>> file = open('/home/collier/pytest/**sort.TXT', 'r').read() > HINT #2: Don't lie. "file" is not a file so you probably shouldn't call it one. It's the contents of a file object. > >>> print(file) > z > c > w > r > h > s > d > > > >>> file.sort() #The first blank line above is from the file. I do not > know where the second comes from. > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'str' object has no attribute 'sort' > HINT #3: *Read*. What does it say? AttributeError: 'str' object has no attribute 'sort' Probably your problem, then, is that a 'str' object has no attribute 'sort'. That's what it says. "file" is a "'str' object". You are accessing the 'sort' attribute which it doesn't have. I had the parameters (key=str.casefold, reverse=True), but I took those out > to make sure the error was not with my parameters. > HINT #4: Don't just guess what the problem is. The answer is in the error. > Specifically, I need something that will sort the lines. They may contain > one word or one sentence with punctuation. I need to reverse the sorting > ('z' before 'a'). The case does not matter ('a' = 'A'). > > I have also tried this without success: > > >>> file.sort(key=str.casefold, reverse=True) > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'str' object has no attribute 'sort' > So you want to sort your string by lines. Rather than trying to abuse a .sort attribute that patently doesn't exist, just use sorted OR convert to a list first. sorted(open('/home/collier/pytest/**sort.TXT'), key=str.casefold, reverse=True) Because it's bad to open files without a with unless you know what you're doing, use a with: with open('/home/collier/pytest/**sort.TXT') as file: sorted(file, key=str.casefold, reverse=True) -------------- next part -------------- An HTML attachment was scrubbed... URL: From devyncjohnson at gmail.com Tue Aug 6 06:38:08 2013 From: devyncjohnson at gmail.com (Devyn Collier Johnson) Date: Tue, 06 Aug 2013 06:38:08 -0400 Subject: Sort lines in a plain text file alphanumerically In-Reply-To: References: <520058D7.10105@Gmail.com> Message-ID: <5200D210.9030003@Gmail.com> On 08/05/2013 11:12 PM, Joshua Landau wrote: > On 6 August 2013 03:00, Devyn Collier Johnson > wrote: > > I am wanting to sort a plain text file alphanumerically by the > lines. I have tried this code, but I get an error. I assume this > command does not accept newline characters. > > > HINT #1: Don't assume that without a reason. It's wrong. > > >>> file = open('/home/collier/pytest/sort.TXT', 'r').read() > > > HINT #2: Don't lie. "file" is not a file so you probably shouldn't > call it one. It's the contents of a file object. > > >>> print(file) > z > c > w > r > h > s > d > > > >>> file.sort() #The first blank line above is from the file. I do > not know where the second comes from. > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'str' object has no attribute 'sort' > > > HINT #3: *Read*. What does it say? > > AttributeError: 'str' object has no attribute 'sort' > > Probably your problem, then, is that a 'str' object has no attribute > 'sort'. That's what it says. > > "file" is a "'str' object". You are accessing the 'sort' attribute > which it doesn't have. > > I had the parameters (key=str.casefold, reverse=True), but I took > those out to make sure the error was not with my parameters. > > > HINT #4: Don't just guess what the problem is. The answer is in the error. > > Specifically, I need something that will sort the lines. They may > contain one word or one sentence with punctuation. I need to > reverse the sorting ('z' before 'a'). The case does not matter > ('a' = 'A'). > > I have also tried this without success: > > >>> file.sort(key=str.casefold, reverse=True) > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'str' object has no attribute 'sort' > > > > > So you want to sort your string by lines. Rather than trying to abuse > a .sort attribute that patently doesn't exist, just use sorted OR > convert to a list first. > > sorted(open('/home/collier/pytest/sort.TXT'), key=str.casefold, > reverse=True) > > Because it's bad to open files without a with unless you know what > you're doing, use a with: > > with open('/home/collier/pytest/sort.TXT') as file: > sorted(file, key=str.casefold, reverse=True) Thanks for the advice Joshua. I find these tips very useful. However, how would I close the files, or would they close after the "with" construct is complete? Mahalo, DCJ -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Tue Aug 6 06:52:14 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Aug 2013 11:52:14 +0100 Subject: Sort lines in a plain text file alphanumerically In-Reply-To: <5200D210.9030003@Gmail.com> References: <520058D7.10105@Gmail.com> <5200D210.9030003@Gmail.com> Message-ID: On Tue, Aug 6, 2013 at 11:38 AM, Devyn Collier Johnson wrote: > with open('/home/collier/pytest/sort.TXT') as file: > sorted(file, key=str.casefold, reverse=True) > > > Thanks for the advice Joshua. I find these tips very useful. However, how > would I close the files, or would they close after the "with" construct is > complete? That's the whole point of 'with'. It calls open(), then calls __enter__, and it guarantees to call __exit__ before executing any code following the with block. With a file object, __exit__ will close the file. ChrisA From joshua at landau.ws Tue Aug 6 07:44:34 2013 From: joshua at landau.ws (Joshua Landau) Date: Tue, 6 Aug 2013 12:44:34 +0100 Subject: Sort lines in a plain text file alphanumerically In-Reply-To: References: <520058D7.10105@Gmail.com> <5200D210.9030003@Gmail.com> Message-ID: On 6 August 2013 11:52, Chris Angelico wrote: > On Tue, Aug 6, 2013 at 11:38 AM, Devyn Collier Johnson > wrote: >> with open('/home/collier/pytest/sort.TXT') as file: >> sorted(file, key=str.casefold, reverse=True) >> >> >> Thanks for the advice Joshua. I find these tips very useful. However, how >> would I close the files, or would they close after the "with" construct is >> complete? > > > That's the whole point of 'with'. It calls open(), To be pedantic, it does not. open is called by the time the with gets involved. > then calls > __enter__, and it guarantees to call __exit__ before executing any > code following the with block. With a file object, __exit__ will close > the file. To make it more obvious for Devyn?, with is used any time you need a simple guarantee of when things are "closed", for any particular meaning. One popular usage, just as a taster, is with changing directories so that the original directory is restored upon completion of the sub-tasks. ? And so it doesn't look like I wrote a whole post to be pedantic From rosuav at gmail.com Tue Aug 6 09:15:06 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Aug 2013 14:15:06 +0100 Subject: Sort lines in a plain text file alphanumerically In-Reply-To: References: <520058D7.10105@Gmail.com> <5200D210.9030003@Gmail.com> Message-ID: On Tue, Aug 6, 2013 at 12:44 PM, Joshua Landau wrote: > On 6 August 2013 11:52, Chris Angelico wrote: >> On Tue, Aug 6, 2013 at 11:38 AM, Devyn Collier Johnson >> wrote: >>> with open('/home/collier/pytest/sort.TXT') as file: >>> sorted(file, key=str.casefold, reverse=True) >>> >>> >>> Thanks for the advice Joshua. I find these tips very useful. However, how >>> would I close the files, or would they close after the "with" construct is >>> complete? >> >> >> That's the whole point of 'with'. It calls open(), > > To be pedantic, it does not. open is called by the time the with gets involved. > The entire statement does. Yes, it's not 'with' that does that, but it is called. Anyway, that's detaily stuff :) ChrisA From devyncjohnson at gmail.com Tue Aug 6 08:29:46 2013 From: devyncjohnson at gmail.com (Devyn Collier Johnson) Date: Tue, 06 Aug 2013 08:29:46 -0400 Subject: Sort lines in a plain text file alphanumerically In-Reply-To: References: <520058D7.10105@Gmail.com> <5200D210.9030003@Gmail.com> Message-ID: <5200EC3A.1060204@Gmail.com> On 08/06/2013 06:52 AM, Chris Angelico wrote: > On Tue, Aug 6, 2013 at 11:38 AM, Devyn Collier Johnson > wrote: >> with open('/home/collier/pytest/sort.TXT') as file: >> sorted(file, key=str.casefold, reverse=True) >> >> >> Thanks for the advice Joshua. I find these tips very useful. However, how >> would I close the files, or would they close after the "with" construct is >> complete? > > That's the whole point of 'with'. It calls open(), then calls > __enter__, and it guarantees to call __exit__ before executing any > code following the with block. With a file object, __exit__ will close > the file. > > ChrisA Thanks! Now I see why using "with" is a better way to write the code. DCJ From edu4madh at gmail.com Mon Aug 5 23:39:13 2013 From: edu4madh at gmail.com (edu4madh at gmail.com) Date: Mon, 5 Aug 2013 20:39:13 -0700 (PDT) Subject: Sort lines in a plain text file alphanumerically In-Reply-To: References: Message-ID: On Monday, August 5, 2013 10:00:55 PM UTC-4, Devyn Collier Johnson wrote: > I am wanting to sort a plain text file alphanumerically by the lines. I > > have tried this code, but I get an error. I assume this command does not > > accept newline characters. > > > > > > >>> file = open('/home/collier/pytest/sort.TXT', 'r').read() > > >>> print(file) > > z > > c > > w > > r > > h > > s > > d > > > > > > >>> file.sort() #The first blank line above is from the file. I do not > > know where the second comes from. > > Traceback (most recent call last): > > File "", line 1, in > > AttributeError: 'str' object has no attribute 'sort' > > > > I had the parameters (key=str.casefold, reverse=True), but I took those > > out to make sure the error was not with my parameters. > > > > Specifically, I need something that will sort the lines. They may > > contain one word or one sentence with punctuation. I need to reverse the > > sorting ('z' before 'a'). The case does not matter ('a' = 'A'). > > > > I have also tried this without success: > > > > >>> file.sort(key=str.casefold, reverse=True) > > Traceback (most recent call last): > > File "", line 1, in > > AttributeError: 'str' object has no attribute 'sort' > > > > > > Mahalo, > > > > DevynCJohnson at Gmail.com fileName = open('test.txt') lines = fileName.readlines() lines.sort() print lines From wuwei23 at gmail.com Mon Aug 5 23:49:34 2013 From: wuwei23 at gmail.com (alex23) Date: Tue, 06 Aug 2013 13:49:34 +1000 Subject: Sort lines in a plain text file alphanumerically In-Reply-To: References: <520058D7.10105@Gmail.com> Message-ID: On 6/08/2013 1:12 PM, Joshua Landau wrote: > Because it's bad to open files without a with unless you know what > you're doing, use a with: > > with open('/home/collier/pytest/__sort.TXT') as file: > sorted(file, key=str.casefold, reverse=True) Shouldn't that be: with open('/home/collier/pytest/__sort.TXT') as file: data = file.readlines() sorted(data, key=str.casefold, reverse=True) I'm tempted to say "HINT #5: don't provide a solution without testing it first" but that would be pretty obnoxious. From wuwei23 at gmail.com Mon Aug 5 23:51:31 2013 From: wuwei23 at gmail.com (alex23) Date: Tue, 06 Aug 2013 13:51:31 +1000 Subject: Sort lines in a plain text file alphanumerically In-Reply-To: References: <520058D7.10105@Gmail.com> Message-ID: On 6/08/2013 1:49 PM, alex23 wrote: > Shouldn't that be: > > with open('/home/collier/pytest/__sort.TXT') as file: > data = file.readlines() > sorted(data, key=str.casefold, reverse=True) > > I'm tempted to say "HINT #5: don't provide a solution without testing it > first" but that would be pretty obnoxious. Even more so when I got it wrong myself :) data = sorted(file.readlines(), key=str.casefold, reverse=True) I can never remember which one sorts in place and which doesn't. From wuwei23 at gmail.com Tue Aug 6 00:33:17 2013 From: wuwei23 at gmail.com (alex23) Date: Tue, 06 Aug 2013 14:33:17 +1000 Subject: Sort lines in a plain text file alphanumerically In-Reply-To: References: <520058D7.10105@Gmail.com> Message-ID: On 6/08/2013 1:49 PM, alex23 wrote: > On 6/08/2013 1:12 PM, Joshua Landau wrote: >> Because it's bad to open files without a with unless you know what >> you're doing, use a with: >> >> with open('/home/collier/pytest/__sort.TXT') as file: >> sorted(file, key=str.casefold, reverse=True) > > Shouldn't that be: > > with open('/home/collier/pytest/__sort.TXT') as file: > data = file.readlines() > sorted(data, key=str.casefold, reverse=True) Hmm, I take that back entirely. Your version does work. Weirdly, I tried yours under both 2.7 & 3.2 without it working at all, but a subsequent attempt did. Sorry for the noise. From devyncjohnson at gmail.com Tue Aug 6 06:40:03 2013 From: devyncjohnson at gmail.com (Devyn Collier Johnson) Date: Tue, 06 Aug 2013 06:40:03 -0400 Subject: Sort lines in a plain text file alphanumerically In-Reply-To: References: <520058D7.10105@Gmail.com> Message-ID: <5200D283.4000500@Gmail.com> On 08/05/2013 11:49 PM, alex23 wrote: > On 6/08/2013 1:12 PM, Joshua Landau wrote: >> Because it's bad to open files without a with unless you know what >> you're doing, use a with: >> >> with open('/home/collier/pytest/__sort.TXT') as file: >> sorted(file, key=str.casefold, reverse=True) > > Shouldn't that be: > > with open('/home/collier/pytest/__sort.TXT') as file: > data = file.readlines() > sorted(data, key=str.casefold, reverse=True) > > I'm tempted to say "HINT #5: don't provide a solution without testing > it first" but that would be pretty obnoxious. I tried Joshua's suggestion in Python3.3 and it worked. What version of Python are you using? DCJ From lakshmipathi.g at gmail.com Tue Aug 6 02:45:22 2013 From: lakshmipathi.g at gmail.com (Lakshmipathi.G) Date: Tue, 6 Aug 2013 12:15:22 +0530 Subject: Reg secure python environment with web terminal emulator Message-ID: Hi - We have a server running a web-based terminal emulator (based on shellinabox for screen-casting check www.webminal.org) that allows users to learn simple bash commands. This Linux environment secured by things like quota, selinux,ulimit etc Now some users are requesting python access. How to ensure python is executed in a restricted environment. I came across http://docs.python.org/2/library/restricted.html but it seems like disabled in 2.3. Any thoughts on how we can safely provide python access to users. I hope this is the correct mailing-list, If not please redirect me to the right one. Thanks for any help. -- ---- Cheers, Lakshmipathi.G FOSS Programmer. www.giis.co.in From dieter at handshake.de Wed Aug 7 02:05:39 2013 From: dieter at handshake.de (dieter) Date: Wed, 07 Aug 2013 08:05:39 +0200 Subject: Reg secure python environment with web terminal emulator References: Message-ID: <87txj2m3ws.fsf@handshake.de> "Lakshmipathi.G" writes: > We have a server running a web-based terminal emulator (based on shellinabox > for screen-casting check www.webminal.org) that allows users to learn > simple bash commands. This Linux environment secured by things like quota, > selinux,ulimit etc > > Now some users are requesting python access. How to ensure python is executed > in a restricted environment. I came across > http://docs.python.org/2/library/restricted.html > but it seems like disabled in 2.3. Any thoughts on how we can safely > provide python access > to users. When you are satisfied with the protection you have achieved for bash commands, those same protection might be sufficient for Python as well. I assume that you used operating system facilities to restrict what the (system) user can do on the operating system level: the same restriction would apply to the (same) user executing Python code. From lakshmipathi.g at gmail.com Wed Aug 7 05:15:44 2013 From: lakshmipathi.g at gmail.com (Lakshmipathi.G) Date: Wed, 7 Aug 2013 14:45:44 +0530 Subject: Reg secure python environment with web terminal emulator In-Reply-To: <87txj2m3ws.fsf@handshake.de> References: <87txj2m3ws.fsf@handshake.de> Message-ID: Hi - Thanks for the response. Yes, we used OS features to restrict the system user accounts. We don't allow gcc - this helped us to avoid kernel exploits via C code like : https://www.centos.org/modules/newbb/viewtopic.php?viewmode=flat&topic_id=42827&forum=59 https://bugzilla.redhat.com/show_bug.cgi?id=962792 We are concerned whether user may try C exploits via Python code and break the system. What's the minimal python set-up you would suggest? I'm thinking something like: 1- Uninstall python-devel packages 2- Remove easy_install or pip (any such install utilities) 3- Keep only very basic modules under /usr/lib/python<>/site-packages and delete the others. Thanks. -- ---- Cheers, Lakshmipathi.G FOSS Programmer. www.giis.co.in On Wed, Aug 7, 2013 at 11:35 AM, dieter wrote: > "Lakshmipathi.G" writes: > >> We have a server running a web-based terminal emulator (based on shellinabox >> for screen-casting check www.webminal.org) that allows users to learn >> simple bash commands. This Linux environment secured by things like quota, >> selinux,ulimit etc >> >> Now some users are requesting python access. How to ensure python is executed >> in a restricted environment. I came across >> http://docs.python.org/2/library/restricted.html >> but it seems like disabled in 2.3. Any thoughts on how we can safely >> provide python access >> to users. > > When you are satisfied with the protection you have achieved > for bash commands, those same protection might be sufficient > for Python as well. I assume that you used operating system > facilities to restrict what the (system) user can do on the > operating system level: the same restriction would apply to the > (same) user executing Python code. > > -- > http://mail.python.org/mailman/listinfo/python-list From lakshmipathi.g at gmail.com Thu Aug 8 00:14:41 2013 From: lakshmipathi.g at gmail.com (Lakshmipathi.G) Date: Thu, 8 Aug 2013 09:44:41 +0530 Subject: Reg secure python environment with web terminal emulator In-Reply-To: References: <87txj2m3ws.fsf@handshake.de> Message-ID: > If you permit file I/O and anything that can spawn a process, it is > possible to create a raw binary executable and trigger its execution. > -- Yes,we permit file i/o with quota limits and spawning a process is allowed upto a limit. If I'm not wrong, we will be safe if user invokes subprocess or os.system('sudo') calls due to system constraints. Could you please share more info about creating raw binary executable and its potential problem. Thanks for your response. -- ---- Cheers, Lakshmipathi.G FOSS Programmer. www.giis.co.in From dieter at handshake.de Thu Aug 8 02:23:22 2013 From: dieter at handshake.de (dieter) Date: Thu, 08 Aug 2013 08:23:22 +0200 Subject: Reg secure python environment with web terminal emulator References: <87txj2m3ws.fsf@handshake.de> Message-ID: <87zjssem5h.fsf@handshake.de> "Lakshmipathi.G" writes: > Could you please share more info about creating raw binary executable > and its potential > problem. In an earlier message, you reported to have banned "gcc" to avoid "C" level exploits. A "raw binary executable" would allow the same exploits. Think of a binary generated elsewhere (where "gcc" is available) and put into your environment. I am convinced that 100 % security is impossible - and correspondingly would use a pragmatic approach: I would rely on OS level constraints (user with very restricted rights, process running in an isolated "box") - and ensure the OS is kept up to date to reduce the risk of exploits of OS security weaknesses. From lakshmipathi.g at gmail.com Thu Aug 8 03:40:46 2013 From: lakshmipathi.g at gmail.com (Lakshmipathi.G) Date: Thu, 8 Aug 2013 13:10:46 +0530 Subject: Reg secure python environment with web terminal emulator In-Reply-To: <87zjssem5h.fsf@handshake.de> References: <87txj2m3ws.fsf@handshake.de> <87zjssem5h.fsf@handshake.de> Message-ID: >> the same exploits. Think of a binary generated elsewhere (where > "gcc" is available) and put into your environment. That's pretty bad news :( > > I am convinced that 100 % security is impossible - and correspondingly > would use a pragmatic approach: I would rely on OS level > constraints (user with very restricted rights, process running > in an isolated "box") - and ensure the OS is kept up to date > to reduce the risk of exploits of OS security weaknesses. > Yes,agree 100% security will never be possible. I'll explore about running process as an isolated box. Thanks for the suggestions and inputs. -- ---- Cheers, Lakshmipathi.G FOSS Programmer. www.giis.co.in From rosuav at gmail.com Tue Aug 6 04:25:15 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Aug 2013 09:25:15 +0100 Subject: [Python-ideas] Allow filter(items) In-Reply-To: References: Message-ID: On Tue, Aug 6, 2013 at 9:23 AM, Serhiy Storchaka wrote: > 06.08.13 10:34, Chris Angelico ???????(??): > >> Okay. Sounds like there's already an answer to those who want more >> readability: Just use filter(bool,...). Maybe I'm just not seeing the >> obvious problem with this version? > > > Are `if bool(...)` or `if bool(...) == True` more readable than `if ...`? They're more readable than 'if None(...)' is. ChrisA From brjohan at gmail.com Tue Aug 6 05:10:17 2013 From: brjohan at gmail.com (BrJohan) Date: Tue, 06 Aug 2013 11:10:17 +0200 Subject: Class hierarchy problem Message-ID: I'm in need of help to solve this Python (ver. 3.3) problem: I have a hierarchy of classes (SubA, SubAB, SubB, ..., SubBCA, SubC,...), each of which is inheriting from a chain of superclasses with a common baseclass(Sup) on top. (So far, no problem) Now, I want to create instances of the correct subclasstype as decided by the common baseclass, like this: i = Sup(args_allowing_the_baseclass_to_deduce_correct_subclass) where i can be of any class except Sup itself (as decided by Sup) Now, the problem: How to design the __new__() and __init__() methods for the various classes in order to achieve what I want? (Some ten years I had the same problem (in a different context) and was helped by asking in this group. However, the solution has disappeared. Maybe the 2.x solution is not the same as in 3.x?) From rosuav at gmail.com Tue Aug 6 05:30:39 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Aug 2013 10:30:39 +0100 Subject: Class hierarchy problem In-Reply-To: References: Message-ID: On Tue, Aug 6, 2013 at 10:10 AM, BrJohan wrote: > Now, I want to create instances of the correct subclasstype as decided by > the common baseclass, like this: > > i = Sup(args_allowing_the_baseclass_to_deduce_correct_subclass) > > where i can be of any class except Sup itself (as decided by Sup) Can you do this as a factory function instead of the class itself? Then all you need to do is call the appropriate class. ChrisA From __peter__ at web.de Tue Aug 6 05:34:58 2013 From: __peter__ at web.de (Peter Otten) Date: Tue, 06 Aug 2013 11:34:58 +0200 Subject: Class hierarchy problem References: Message-ID: BrJohan wrote: > I'm in need of help to solve this Python (ver. 3.3) problem: > > I have a hierarchy of classes (SubA, SubAB, SubB, ..., SubBCA, > SubC,...), each of which is inheriting from a chain of superclasses with > a common baseclass(Sup) on top. (So far, no problem) > > Now, I want to create instances of the correct subclasstype as decided > by the common baseclass, like this: > > i = Sup(args_allowing_the_baseclass_to_deduce_correct_subclass) > > where i can be of any class except Sup itself (as decided by Sup) > > Now, the problem: > > How to design the __new__() and __init__() methods for the various > classes in order to achieve what I want? > > (Some ten years I had the same problem (in a different context) and was > helped by asking in this group. However, the solution has disappeared. > Maybe the 2.x solution is not the same as in 3.x?) Keep it simple, use a function: def make(*args): class_ = deduce_correct_class(*args) return class_(*args) That way you won't even need any __new__() methods. From steve at pearwood.info Tue Aug 6 05:36:20 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 06 Aug 2013 09:36:20 GMT Subject: Class hierarchy problem References: Message-ID: <5200c393$0$29986$c3e8da3$5496439d@news.astraweb.com> On Tue, 06 Aug 2013 11:10:17 +0200, BrJohan wrote: > I'm in need of help to solve this Python (ver. 3.3) problem: > > I have a hierarchy of classes (SubA, SubAB, SubB, ..., SubBCA, > SubC,...), each of which is inheriting from a chain of superclasses with > a common baseclass(Sup) on top. (So far, no problem) Well, I don't know about that, a deep and complex chain of super- and sub- classes already sounds like a problem. But putting that aside... > Now, I want to create instances of the correct subclasstype as decided > by the common baseclass, like this: > > i = Sup(args_allowing_the_baseclass_to_deduce_correct_subclass) Inheritance doesn't, or at least shouldn't, work like that. The superclasses shouldn't be expected to know about their subclasses. Knowledge goes the other way: each class knows its parents, and so recursively can go all the way to the common base class. > where i can be of any class except Sup itself (as decided by Sup) > > Now, the problem: > > How to design the __new__() and __init__() methods for the various > classes in order to achieve what I want? You don't :-) Instead, have a factory function: def sup(args): if args: return Sup(...) elif condition(): return SupA(...) elif condition2() or condition3(): return SupB(...) ... sort of thing. Another possibility would be to start with the child class, and give it the ability to return an instance of itself, or its direct parent. Completely untested: class SupABC(SupABB): def __new__(cls, args): if condition(): # Return an instance of myself. ... else: # Let my parent (maybe) return an instance. return SupABB(args) Either way, the design risks becoming a horrible mess, but you might be able to use it. Good luck! -- Steven From rosuav at gmail.com Tue Aug 6 10:02:23 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Aug 2013 15:02:23 +0100 Subject: Class hierarchy problem In-Reply-To: <520100F9.4010805@gmail.com> References: <520100F9.4010805@gmail.com> Message-ID: On Tue, Aug 6, 2013 at 2:58 PM, BrJohan wrote: > On 06/08/2013 11:30, Chris Angelico wrote: >> >> On Tue, Aug 6, 2013 at 10:10 AM, BrJohan wrote: >>> >>> Now, I want to create instances of the correct subclasstype as decided by >>> the common baseclass, like this: >>> >>> i = Sup(args_allowing_the_baseclass_to_deduce_correct_subclass) >>> >>> where i can be of any class except Sup itself (as decided by Sup) >> >> >> Can you do this as a factory function instead of the class itself? >> Then all you need to do is call the appropriate class. >> >> ChrisA >> > > My classhierarchy is like a multilevel tree where each non-leaf node (class) > is given knowledge about its nearest subclasses and their 'capacities'. > > So, my idea is to let the 'upper' class recursively choose which of its > nearest subclasses is the 'correct' one, until approaching a 'leaf' class > from which the instance should be created. And, given my knowledge that a > solution along the lines of this idea has been designed and was working, I'm > still hopeful ... (or I'll have to investigate all those old backup-DVDs) [ responding on-list - I hope it was mere oversight that had this come privately to me alone ] This is code smell; this recursive search for the "right" class seems likely to be wrong. Can you have the classes perhaps register themselves in some way? On what basis is a superclass to determine that one of its subclasses should handle this request? ChrisA From inq1ltd at inqvista.com Tue Aug 6 11:05:18 2013 From: inq1ltd at inqvista.com (inq1ltd) Date: Tue, 06 Aug 2013 11:05:18 -0400 Subject: pexpect, loading an entry field In-Reply-To: References: <520100F9.4010805@gmail.com> Message-ID: <4559802.M7tbXMqQd4@mach-114-20> python help; I am using pexpect to open my program. Can someone tell me how to get data to appear in an entry field. After pexpect opens the my program I have tried to use send, sendline, and write functions to try to put data into the program's entry field. However, the data is going to the terminal window, the window that is used to initiate the call to pexpect but not to the entry field in the open program. I would appreciate suggestions. jol -------------- next part -------------- An HTML attachment was scrubbed... URL: From lakshmipathi.g at gmail.com Tue Aug 6 12:34:54 2013 From: lakshmipathi.g at gmail.com (Lakshmipathi.G) Date: Tue, 6 Aug 2013 22:04:54 +0530 Subject: pexpect, loading an entry field In-Reply-To: <4559802.M7tbXMqQd4@mach-114-20> References: <520100F9.4010805@gmail.com> <4559802.M7tbXMqQd4@mach-114-20> Message-ID: pexpect looks simple to use. Please check this example http://www.pythonforbeginners.com/systems-programming/how-to-use-the-pexpect-module-in-python/ -- ---- Cheers, Lakshmipathi.G FOSS Programmer. www.giis.co.in On Tue, Aug 6, 2013 at 8:35 PM, inq1ltd wrote: > python help; > > > > I am using pexpect to open my program. > > Can someone tell me how to get data to appear in > > an entry field. > > > > After pexpect opens the my program I have tried to use > > send, sendline, and write functions to try to put data into > > the program's entry field. > > > > However, the data is going to the terminal window, > > the window that is used to initiate the call to pexpect > > but not to the entry field in the open program. > > > > I would appreciate suggestions. > > jol > > > > > -- > http://mail.python.org/mailman/listinfo/python-list > From inq1ltd at inqvista.com Tue Aug 6 14:03:38 2013 From: inq1ltd at inqvista.com (inq1ltd) Date: Tue, 06 Aug 2013 14:03:38 -0400 Subject: pexpect, loading an entry field In-Reply-To: References: <4559802.M7tbXMqQd4@mach-114-20> Message-ID: <3630058.GVOTG9cGGT@mach-114-20> > pexpect looks simple to use. Please check this example > http://www.pythonforbeginners.com/systems-programming/how-to-use-the- pexpect > -module-in-python/ > > python help; > > > > I am using pexpect to open my program. > > Can someone tell me how to get data to appear in > > an entry field. > > After pexpect opens the my program I have tried to use > > send, sendline, and write functions to try to put data into > > the program's entry field. > > However, the data is going to the terminal window, > > the window that is used to initiate the call to pexpect > > but not to the entry field in the open program. > > I would appreciate suggestions. > > > > jol > > Thanks for the response. I have been there but that site gives me the same information that I get from noah.org. I have the pexpect docs and they presume that this problem doesn't exist. None of the information in the NOAH site or the FAQ's address this question. I'm using suse linux running python 2.7 and pexpect. The data, when using send() method is going to the terminal window, This is the same window that is used to initiate the call to pexpect. Pexpect opens the program but the send method sends data to the terminal window, not the entry field located in the child spawned by pexpect. interact works, as does close(), TIMEOUT, EOF, before, after, expect_exact and methods. But the entry field does not load. So my question is; Why?? > > > > > > > > -- > > http://mail.python.org/mailman/listinfo/python-list -------------- next part -------------- An HTML attachment was scrubbed... URL: From lakshmipathi.g at gmail.com Wed Aug 7 04:10:46 2013 From: lakshmipathi.g at gmail.com (Lakshmipathi.G) Date: Wed, 7 Aug 2013 13:40:46 +0530 Subject: pexpect, loading an entry field In-Reply-To: <3630058.GVOTG9cGGT@mach-114-20> References: <4559802.M7tbXMqQd4@mach-114-20> <3630058.GVOTG9cGGT@mach-114-20> Message-ID: Hi - I'm using Python 2.7.3 (Fedora 17) . I tried a simple example with pexpect to copy a file to remote system. It works $ cat pex.py import pexpect s = pexpect.spawn ('scp pex.py root at 10.30.77.244:/tmp') s.expect ('Password:') s.sendline ('a') s.expect(pexpect.EOF,timeout=20) Execute above program (change remote ip and password) - If it works then its not an issue with pexpect or python environment. -- ---- Cheers, Lakshmipathi.G FOSS Programmer. www.giis.co.in On Tue, Aug 6, 2013 at 11:33 PM, inq1ltd wrote: > > >> pexpect looks simple to use. Please check this example > >> >> http://www.pythonforbeginners.com/systems-programming/how-to-use-the-pexpect > >> -module-in-python/ > >> > python help; > >> > > >> > I am using pexpect to open my program. > >> > Can someone tell me how to get data to appear in > >> > an entry field. > >> > After pexpect opens the my program I have tried to use > >> > send, sendline, and write functions to try to put data into > >> > the program's entry field. > >> > However, the data is going to the terminal window, > >> > the window that is used to initiate the call to pexpect > >> > but not to the entry field in the open program. > >> > I would appreciate suggestions. > >> > > >> > jol > >> > > > > > Thanks for the response. > > > > I have been there but that site gives me > > the same information that I get from > > noah.org. I have the pexpect docs and they presume > > that this problem doesn't exist. > > > > None of the information in the NOAH site or the FAQ's > > address this question. > > > > I'm using suse linux running python 2.7 and pexpect. > > > > The data, when using send() method is going to the terminal window, > > This is the same window that is used to initiate the call to pexpect. > > > > Pexpect opens the program but the send method sends > > data to the terminal window, not the entry field located > > in the child spawned by pexpect. > > > > interact works, as does close(), TIMEOUT, EOF, before, after, > > expect_exact and methods. But the entry field does not > > load. > > > > So my question is; > > > > Why?? > > > > > > > > > >> > > >> > > >> > > >> > -- > >> > http://mail.python.org/mailman/listinfo/python-list From brjohan at gmail.com Tue Aug 6 11:36:53 2013 From: brjohan at gmail.com (BrJohan) Date: Tue, 06 Aug 2013 17:36:53 +0200 Subject: Class hierarchy problem In-Reply-To: References: <520100F9.4010805@gmail.com> Message-ID: On 06/08/2013 16:02, Chris Angelico wrote: >> My classhierarchy is like a multilevel tree where each non-leaf node (class) >> is given knowledge about its nearest subclasses and their 'capacities'. >> >> So, my idea is to let the 'upper' class recursively choose which of its >> nearest subclasses is the 'correct' one, until approaching a 'leaf' class >> from which the instance should be created. And, given my knowledge that a >> solution along the lines of this idea has been designed and was working, I'm >> still hopeful ... (or I'll have to investigate all those old backup-DVDs) > > [ responding on-list - I hope it was mere oversight that had this come > privately to me alone ] > > This is code smell; this recursive search for the "right" class seems > likely to be wrong. Can you have the classes perhaps register > themselves in some way? On what basis is a superclass to determine > that one of its subclasses should handle this request? > > ChrisA > Consider a botanical classification system (somewhat analogous to my 'problem' as it effectively is related to classification of entities): A Domain should know about its Kingdoms, a Kingdom should know about its Phylums, ... a Genus should know about its Species. Of course it is possible to implement such a decision tree as a 'factory'. However, I would rather prefer to encapsulate those decisions at the class level where they 'belong'. BrJohan From kender.jr at gmail.com Tue Aug 6 12:13:55 2013 From: kender.jr at gmail.com (Jordi Riera) Date: Tue, 6 Aug 2013 18:13:55 +0200 Subject: Class hierarchy problem In-Reply-To: References: <520100F9.4010805@gmail.com> Message-ID: Are you using a db for that? Django models would handle that pretty easily: class Species(models.Model): name = models.CharField() class Genus(models.Model): name = models.CharField() species = models.ManyToManyField( Species, related_name = 'genus_species' ) then you can access species from genus by: Genus.objects.all()[0].species.all() and genus from species by: Species.objects.all()[0].genus_species.all() if you can afford this depends that would solve your issue I think. more details: http://stackoverflow.com/questions/9352662/how-to-use-the-reverse-of-a-django-manytomany-relationship Regards Jordi On Tue, Aug 6, 2013 at 5:36 PM, BrJohan wrote: > On 06/08/2013 16:02, Chris Angelico wrote: > > My classhierarchy is like a multilevel tree where each non-leaf node >>> (class) >>> is given knowledge about its nearest subclasses and their 'capacities'. >>> >>> So, my idea is to let the 'upper' class recursively choose which of its >>> nearest subclasses is the 'correct' one, until approaching a 'leaf' class >>> from which the instance should be created. And, given my knowledge that a >>> solution along the lines of this idea has been designed and was working, >>> I'm >>> still hopeful ... (or I'll have to investigate all those old backup-DVDs) >>> >> >> [ responding on-list - I hope it was mere oversight that had this come >> privately to me alone ] >> >> This is code smell; this recursive search for the "right" class seems >> likely to be wrong. Can you have the classes perhaps register >> themselves in some way? On what basis is a superclass to determine >> that one of its subclasses should handle this request? >> >> ChrisA >> >> > > Consider a botanical classification system (somewhat analogous to my > 'problem' as it effectively is related to classification of entities): > > A Domain should know about its Kingdoms, > a Kingdom should know about its Phylums, > ... > a Genus should know about its Species. > > Of course it is possible to implement such a decision tree as a 'factory'. > However, I would rather prefer to encapsulate those decisions at the class > level where they 'belong'. > > BrJohan > -- > http://mail.python.org/**mailman/listinfo/python-list > -- - Jordi Riera, Connecting people. +33 662217507 -------------- next part -------------- An HTML attachment was scrubbed... URL: From joe.fbs.junior at gmail.com Tue Aug 6 12:23:58 2013 From: joe.fbs.junior at gmail.com (Joe Junior) Date: Tue, 6 Aug 2013 13:23:58 -0300 Subject: Class hierarchy problem In-Reply-To: References: <520100F9.4010805@gmail.com> Message-ID: On Tue, Aug 6, 2013 at 12:36 PM, BrJohan wrote: > On 06/08/2013 16:02, Chris Angelico wrote: > >>> My classhierarchy is like a multilevel tree where each non-leaf node >>> (class) >>> is given knowledge about its nearest subclasses and their 'capacities'. >>> >>> So, my idea is to let the 'upper' class recursively choose which of its >>> nearest subclasses is the 'correct' one, until approaching a 'leaf' class >>> from which the instance should be created. And, given my knowledge that a >>> solution along the lines of this idea has been designed and was working, >>> I'm >>> still hopeful ... (or I'll have to investigate all those old backup-DVDs) >> >> >> [ responding on-list - I hope it was mere oversight that had this come >> privately to me alone ] >> >> This is code smell; this recursive search for the "right" class seems >> likely to be wrong. Can you have the classes perhaps register >> themselves in some way? On what basis is a superclass to determine >> that one of its subclasses should handle this request? >> >> ChrisA >> > > > Consider a botanical classification system (somewhat analogous to my > 'problem' as it effectively is related to classification of entities): > > A Domain should know about its Kingdoms, > a Kingdom should know about its Phylums, > ... > a Genus should know about its Species. > > Of course it is possible to implement such a decision tree as a 'factory'. > However, I would rather prefer to encapsulate those decisions at the class > level where they 'belong'. > > BrJohan > -- I think it's a "has a" vs a "is a" problem. A Domain has a Kingdom, a Kingdom is not a Domain, so it shouldn't actually inherit Domain. In this case you should use composition instead of inheritance. When you say that a A Domain should know about it's Kingdons note that you're talking about a specific Domain and it's specific Kingdons, which means, a Domain instance and various Kingdom instances. JoeS From rosuav at gmail.com Tue Aug 6 13:12:36 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Aug 2013 18:12:36 +0100 Subject: Class hierarchy problem In-Reply-To: References: <520100F9.4010805@gmail.com> Message-ID: On Tue, Aug 6, 2013 at 4:36 PM, BrJohan wrote: > Consider a botanical classification system (somewhat analogous to my > 'problem' as it effectively is related to classification of entities): > > A Domain should know about its Kingdoms, > a Kingdom should know about its Phylums, > ... > a Genus should know about its Species. I don't believe it's that clear. In some situations it may be best to work that way; in others, each tier should know only those above it. Does the generic Widget know about being a PushButton, a ListBox, and a Slider? No, but the Slider knows what a Widget is. ChrisA From tjreedy at udel.edu Tue Aug 6 19:13:40 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 06 Aug 2013 19:13:40 -0400 Subject: Class hierarchy problem In-Reply-To: References: <520100F9.4010805@gmail.com> Message-ID: On 8/6/2013 11:36 AM, BrJohan wrote: > Consider a botanical classification system (somewhat analogous to my > 'problem' as it effectively is related to classification of entities): > > A Domain should know about its Kingdoms, > a Kingdom should know about its Phylums, > ... > a Genus should know about its Species. As some already said, 'a domain' is an instance of Domain (or possibly generic Taxon). We have on Earth one instance of 'Life'. > > Of course it is possible to implement such a decision tree as a > 'factory'. However, I would rather prefer to encapsulate those decisions > at the class level where they 'belong'. Each instance could have a .classify function that assigns instances to sub-instance. The master classifier function would only know how to use the .classify functions and have no specific content knowledge in itself. -- Terry Jan Reedy From ben+python at benfinney.id.au Tue Aug 6 21:58:51 2013 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 07 Aug 2013 11:58:51 +1000 Subject: Class hierarchy problem References: <520100F9.4010805@gmail.com> Message-ID: <7w7gfyb6sk.fsf@benfinney.id.au> Terry Reedy writes: > On 8/6/2013 11:36 AM, BrJohan wrote: > > > Consider a botanical classification system (somewhat analogous to my > > 'problem' as it effectively is related to classification of entities): > > > > A Domain should know about its Kingdoms, > > a Kingdom should know about its Phylums, > > ... > > a Genus should know about its Species. > > As some already said, 'a domain' is an instance of Domain (or possibly > generic Taxon). We have on Earth one instance of 'Life'. I think the term for a taxonomic grouping of life, if you want to avoid specifying some specific level of the taxonomy, is ?clade? . -- \ ?First they came for the verbs, and I said nothing, for verbing | `\ weirds language. Then, they arrival for the nouns and I speech | _o__) nothing, for I no verbs.? ?Peter Ellis | Ben Finney From nospam at nospam.com Tue Aug 6 06:35:40 2013 From: nospam at nospam.com (Gilles) Date: Tue, 06 Aug 2013 12:35:40 +0200 Subject: [GUI] Good frameworks for Windows/Mac? Message-ID: Hello I need to write a small GUI application that should run on Windows and Mac. What open-source framework would you recommend? I just need basic widgets (button, listbox, etc.) and would rather a solution that can get me up and running fast. I know about wxWidgets and Qt: Are there other good options I should know about? Thank you. From kender.jr at gmail.com Tue Aug 6 07:03:23 2013 From: kender.jr at gmail.com (Jordi Riera) Date: Tue, 6 Aug 2013 13:03:23 +0200 Subject: [GUI] Good frameworks for Windows/Mac? In-Reply-To: References: Message-ID: Hey you can build GUIs with tkinter . Easy but not as powerful than PyQt can be. I think it is os agnostic. Regards, Jordi On Tue, Aug 6, 2013 at 12:35 PM, Gilles wrote: > Hello > > I need to write a small GUI application that should run on Windows and > Mac. > > What open-source framework would you recommend? I just need basic > widgets (button, listbox, etc.) and would rather a solution that can > get me up and running fast. > > I know about wxWidgets and Qt: Are there other good options I should > know about? > > Thank you. > -- > http://mail.python.org/mailman/listinfo/python-list > -- - Jordi Riera, Connecting people. +33 662217507 -------------- next part -------------- An HTML attachment was scrubbed... URL: From vlastimil.brom at gmail.com Tue Aug 6 07:22:01 2013 From: vlastimil.brom at gmail.com (Vlastimil Brom) Date: Tue, 6 Aug 2013 13:22:01 +0200 Subject: [GUI] Good frameworks for Windows/Mac? In-Reply-To: References: Message-ID: 2013/8/6 Gilles : > Hello > > I need to write a small GUI application that should run on Windows and > Mac. > > What open-source framework would you recommend? I just need basic > widgets (button, listbox, etc.) and would rather a solution that can > get me up and running fast. > > I know about wxWidgets and Qt: Are there other good options I should > know about? > > Thank you. > -- > http://mail.python.org/mailman/listinfo/python-list Hi, I mostly use wxPython myself, but if you just need some basic widgets and not some very complex or non-standard layouts, the tkinter - available in the standard library - might be perfectly viable. http://docs.python.org/3.3/library/tk.html The more recent versions of python also support ttk and Tix which has further possibilities, styling etc. There is further e.g. Python GUI http://www.cosc.canterbury.ac.nz/greg.ewing/python_gui/ and several others http://wiki.python.org/moin/GuiProgramming hth, vbr From nospam at nospam.com Tue Aug 6 18:13:43 2013 From: nospam at nospam.com (Gilles) Date: Wed, 07 Aug 2013 00:13:43 +0200 Subject: [GUI] Good frameworks for Windows/Mac? References: Message-ID: On Tue, 6 Aug 2013 13:22:01 +0200, Vlastimil Brom wrote: >I mostly use wxPython myself, but if you just need some basic widgets >and not some very complex or non-standard layouts, the tkinter - >available in the standard library - might be perfectly viable. >http://docs.python.org/3.3/library/tk.html >The more recent versions of python also support ttk and Tix which has >further possibilities, styling etc. >There is further e.g. Python GUI >http://www.cosc.canterbury.ac.nz/greg.ewing/python_gui/ > >and several others >http://wiki.python.org/moin/GuiProgramming Thanks for the links. I'll play with Tkinter, and if it's not good enough, check the alternatives. From sagar.varule at gmail.com Thu Aug 8 04:47:21 2013 From: sagar.varule at gmail.com (sagar varule) Date: Thu, 8 Aug 2013 01:47:21 -0700 (PDT) Subject: [GUI] Good frameworks for Windows/Mac? In-Reply-To: References: Message-ID: On Tuesday, August 6, 2013 4:05:40 PM UTC+5:30, Gilles wrote: > Hello > > > > I need to write a small GUI application that should run on Windows and > > Mac. > > > > What open-source framework would you recommend? I just need basic > > widgets (button, listbox, etc.) and would rather a solution that can > > get me up and running fast. > > > > I know about wxWidgets and Qt: Are there other good options I should > > know about? > > > > Thank you. Pyside is also Good. It has a Designer which can be helpful. From nospam at nospam.com Thu Aug 8 05:17:32 2013 From: nospam at nospam.com (Gilles) Date: Thu, 08 Aug 2013 11:17:32 +0200 Subject: [GUI] Good frameworks for Windows/Mac? References: Message-ID: <0ho609t93l22evbjfrsf0813ffm66n57am@4ax.com> On Thu, 8 Aug 2013 01:47:21 -0700 (PDT), sagar varule wrote: >Pyside is also Good. It has a Designer which can be helpful. Thanks for the info. From sylvain.thenault at logilab.fr Tue Aug 6 10:12:17 2013 From: sylvain.thenault at logilab.fr (Sylvain =?utf-8?B?VGjDqW5hdWx0?=) Date: Tue, 6 Aug 2013 16:12:17 +0200 Subject: Pylint 1.0 released Message-ID: <20130806141217.GD5676@logilab.fr> Hi there, at last, Pylint 1.0 is out! See http://www.logilab.org/blogentry/163292 for (much) more info and enjoy! And many thanks to every one who contributed to this release... -- Sylvain Th?nault, LOGILAB, Paris (01.45.32.03.12) - Toulouse (05.62.17.16.42) Formations Python, Debian, M?th. Agiles: http://www.logilab.fr/formations D?veloppement logiciel sur mesure: http://www.logilab.fr/services CubicWeb, the semantic web framework: http://www.cubicweb.org From kender.jr at gmail.com Tue Aug 6 10:32:16 2013 From: kender.jr at gmail.com (Jordi Riera) Date: Tue, 6 Aug 2013 16:32:16 +0200 Subject: Pylint 1.0 released In-Reply-To: <20130806141217.GD5676@logilab.fr> References: <20130806141217.GD5676@logilab.fr> Message-ID: Great tool Thanks! On Tue, Aug 6, 2013 at 4:12 PM, Sylvain Th?nault < sylvain.thenault at logilab.fr> wrote: > Hi there, > > at last, Pylint 1.0 is out! See http://www.logilab.org/blogentry/163292 > for (much) more info and enjoy! > > And many thanks to every one who contributed to this release... > -- > Sylvain Th?nault, LOGILAB, Paris (01.45.32.03.12) - Toulouse > (05.62.17.16.42) > Formations Python, Debian, M?th. Agiles: http://www.logilab.fr/formations > D?veloppement logiciel sur mesure: http://www.logilab.fr/services > CubicWeb, the semantic web framework: http://www.cubicweb.org > -- > http://mail.python.org/mailman/listinfo/python-list > -- - Jordi Riera, Connecting people. +33 662217507 -------------- next part -------------- An HTML attachment was scrubbed... URL: From yimr00 at gmail.com Tue Aug 6 11:56:31 2013 From: yimr00 at gmail.com (Yimr Wong) Date: Tue, 6 Aug 2013 23:56:31 +0800 Subject: Python-list Digest, Vol 119, Issue 37 In-Reply-To: References: Message-ID: >I found Python3's sqlite3 library. I found that I needed sql commands for using it. > >I have tried sql.learncodethehardway but it isn't complete yet. I tired looking on stackoverflow's sql tag also but nothing much there. There is some useful references in the Python's documents. If you want to connect to the mysql, you should import the packages related. On Tue, Aug 6, 2013 at 10:11 PM, wrote: > Send Python-list mailing list submissions to > python-list at python.org > > To subscribe or unsubscribe via the World Wide Web, visit > http://mail.python.org/mailman/listinfo/python-list > or, via email, send a message with subject or body 'help' to > python-list-request at python.org > > You can reach the person managing the list at > python-list-owner at python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Python-list digest..." > > Today's Topics: > > 1. Re: Can someone suggest better resources for learning > sqlite3? I wanted to use the Python library but I don't know > sql. > (Jordi Riera) > 2. Re: Sort lines in a plain text file alphanumerically > (Devyn Collier Johnson) > 3. Re: Sort lines in a plain text file alphanumerically > (Chris Angelico) > 4. Re: Newbie: static typing? (Burak Arslan) > 5. Re: Working with XML/XSD (Burak Arslan) > 6. Re: Newbie: static typing? (Antoon Pardon) > 7. Re: Class hierarchy problem (Chris Angelico) > 8. Re: Newbie: static typing? (Chris Angelico) > 9. Re: Newbie: static typing? (Eric S. Johansson) > > > ---------- Forwarded message ---------- > From: Jordi Riera > To: Gilles > Cc: python-list at python.org > Date: Tue, 6 Aug 2013 13:05:02 +0200 > Subject: Re: Can someone suggest better resources for learning sqlite3? I > wanted to use the Python library but I don't know sql. > Hey, > > can't you use django to deal with your sqlite? > If so, django modelsare a smart way to do. > > Regards, > Jordi > > > On Tue, Aug 6, 2013 at 12:51 PM, Gilles wrote: > >> On Sat, 3 Aug 2013 10:57:32 -0700 (PDT), Aseem Bansal >> wrote: >> >I found Python3's sqlite3 library. I found that I needed sql commands >> for using it. >> > >> >I have tried sql.learncodethehardway but it isn't complete yet. I tired >> looking on stackoverflow's sql tag also but nothing much there. >> >> It'll be easier to read tutorials specifically meant to use Sqlite >> with Python: >> >> www.google.com/search?q=python+sqlite+examples >> -- >> http://mail.python.org/mailman/listinfo/python-list >> > > > > -- > - Jordi Riera, Connecting people. > +33 662217507 > > > ---------- Forwarded message ---------- > From: Devyn Collier Johnson > To: Python Mailing List > Cc: > Date: Tue, 06 Aug 2013 08:29:46 -0400 > Subject: Re: Sort lines in a plain text file alphanumerically > > On 08/06/2013 06:52 AM, Chris Angelico wrote: > >> On Tue, Aug 6, 2013 at 11:38 AM, Devyn Collier Johnson >> wrote: >> >>> with open('/home/collier/pytest/**sort.TXT') as file: >>> sorted(file, key=str.casefold, reverse=True) >>> >>> >>> Thanks for the advice Joshua. I find these tips very useful. However, how >>> would I close the files, or would they close after the "with" construct >>> is >>> complete? >>> >> >> That's the whole point of 'with'. It calls open(), then calls >> __enter__, and it guarantees to call __exit__ before executing any >> code following the with block. With a file object, __exit__ will close >> the file. >> >> ChrisA >> > > Thanks! Now I see why using "with" is a better way to write the code. > > DCJ > > > > ---------- Forwarded message ---------- > From: Chris Angelico > To: python-list at python.org > Cc: > Date: Tue, 6 Aug 2013 14:15:06 +0100 > Subject: Re: Sort lines in a plain text file alphanumerically > On Tue, Aug 6, 2013 at 12:44 PM, Joshua Landau wrote: > > On 6 August 2013 11:52, Chris Angelico wrote: > >> On Tue, Aug 6, 2013 at 11:38 AM, Devyn Collier Johnson > >> wrote: > >>> with open('/home/collier/pytest/sort.TXT') as file: > >>> sorted(file, key=str.casefold, reverse=True) > >>> > >>> > >>> Thanks for the advice Joshua. I find these tips very useful. However, > how > >>> would I close the files, or would they close after the "with" > construct is > >>> complete? > >> > >> > >> That's the whole point of 'with'. It calls open(), > > > > To be pedantic, it does not. open is called by the time the with gets > involved. > > > > The entire statement does. Yes, it's not 'with' that does that, but it > is called. Anyway, that's detaily stuff :) > > ChrisA > > > > ---------- Forwarded message ---------- > From: Burak Arslan > To: python-list at python.org > Cc: > Date: Tue, 06 Aug 2013 16:27:10 +0300 > Subject: Re: Newbie: static typing? > On 08/06/13 13:12, Rui Maciel wrote: > > Joshua Landau wrote: > > > >> What's the actual problem you're facing? Where do you feel that you > >> need to verify types? > > A standard case would be when there's a function which is designed > expecting > > that all operands support a specific interface or contain specific > > attributes. > > > > In other words, when passing an unsupported type causes problems. > > > > Hi, > > First, let's get over the fact that, with dynamic typing, code fails at > runtime. Irrespective of language, you just shouldn't ship untested > code, so I say that's not an argument against dynamic typing. > > This behaviour is only a problem when code fails *too late* into the > runtime -- i.e. when you don't see the offending value in the stack trace. > > For example, consider you append values to a list and the values in that > list get processed somewhere else. If your code fails because of an > invalid value, your stack trace is useless, because that value should > not be there in the first place. The code should fail when appending to > that list and not when processing it. > > The "too late" case is a bit tough to illustrate. This could be a rough > example: https://gist.github.com/plq/6163839 Imagine that the list there > is progressively constructed somewhere else in the code and later > processed by the sq_all function. As you can see, the stack trace is > pretty useless as we don't see how that value got there. > > In such cases, you do need manual type checking. > > Yet, as someone else noted, naively using isinstance() for type checking > breaks duck typing. So you should read up on abstract base classes: > http://docs.python.org/2/glossary.html#term-abstract-base-class > > These said, I've been writing Python for several years now, and I only > needed to resort to this technique only once. (i was working on a > compiler) Most of the time, you'll be just fine without any manual type > checking. > > Best regards, > Burak > > > > > > ---------- Forwarded message ---------- > From: Burak Arslan > To: python-list at python.org > Cc: > Date: Tue, 06 Aug 2013 16:33:16 +0300 > Subject: Re: Working with XML/XSD > On 08/06/13 01:56, David Barroso wrote: > > Hello, > > I was wondering if someone could point me in the right direction. I > > would like to develop some scripts to manage Cisco routers and > > switches using XML. However, I am not sure where to start. Does > > someone have some experience working with XML, Schemas and things like > > that? Which libraries do you use? Do you know of any good tutorial? > > > > Hi, > > I develop Spyne (http://spyne.io), it does let you define Xml Schema > types, generate the Schema documents, (and in the upcoming release, > parse them) and also does both serialization and deserialization of > python objects from and to xml according to definitions in the xml > schema. It also does RPC :) > > In case you don't want to use a framework, use lxml, it's a very good > xml manipulation library based on libxml2/libxslt. You can use > lxml.objectify for xml serialization as well. > > Best, > Burak > > > > > ---------- Forwarded message ---------- > From: Antoon Pardon > To: python-list at python.org > Cc: > Date: Tue, 06 Aug 2013 15:57:26 +0200 > Subject: Re: Newbie: static typing? > Op 06-08-13 15:27, Burak Arslan schreef: > > On 08/06/13 13:12, Rui Maciel wrote: > >> Joshua Landau wrote: > >> > >>> What's the actual problem you're facing? Where do you feel that you > >>> need to verify types? > >> A standard case would be when there's a function which is designed > expecting > >> that all operands support a specific interface or contain specific > >> attributes. > >> > >> In other words, when passing an unsupported type causes problems. > >> > > > > Hi, > > > > First, let's get over the fact that, with dynamic typing, code fails at > > runtime. Irrespective of language, you just shouldn't ship untested > > code, so I say that's not an argument against dynamic typing. > > Why not? Can ease of development not be a consideration? So if some > kind of faults are easier to detect at compile time if you have static > typing than if you have to design a test for them, I don't see why that > can't be an argument. > > -- > Antoon Pardon > > > > ---------- Forwarded message ---------- > From: Chris Angelico > To: python-list at python.org > Cc: > Date: Tue, 6 Aug 2013 15:02:23 +0100 > Subject: Re: Class hierarchy problem > On Tue, Aug 6, 2013 at 2:58 PM, BrJohan wrote: > > On 06/08/2013 11:30, Chris Angelico wrote: > >> > >> On Tue, Aug 6, 2013 at 10:10 AM, BrJohan wrote: > >>> > >>> Now, I want to create instances of the correct subclasstype as decided > by > >>> the common baseclass, like this: > >>> > >>> i = Sup(args_allowing_the_baseclass_to_deduce_correct_subclass) > >>> > >>> where i can be of any class except Sup itself (as decided by Sup) > >> > >> > >> Can you do this as a factory function instead of the class itself? > >> Then all you need to do is call the appropriate class. > >> > >> ChrisA > >> > > > > My classhierarchy is like a multilevel tree where each non-leaf node > (class) > > is given knowledge about its nearest subclasses and their 'capacities'. > > > > So, my idea is to let the 'upper' class recursively choose which of its > > nearest subclasses is the 'correct' one, until approaching a 'leaf' class > > from which the instance should be created. And, given my knowledge that a > > solution along the lines of this idea has been designed and was working, > I'm > > still hopeful ... (or I'll have to investigate all those old backup-DVDs) > > [ responding on-list - I hope it was mere oversight that had this come > privately to me alone ] > > This is code smell; this recursive search for the "right" class seems > likely to be wrong. Can you have the classes perhaps register > themselves in some way? On what basis is a superclass to determine > that one of its subclasses should handle this request? > > ChrisA > > > > ---------- Forwarded message ---------- > From: Chris Angelico > To: python-list at python.org > Cc: > Date: Tue, 6 Aug 2013 15:06:40 +0100 > Subject: Re: Newbie: static typing? > On Tue, Aug 6, 2013 at 2:57 PM, Antoon Pardon > wrote: > > Op 06-08-13 15:27, Burak Arslan schreef: > >> On 08/06/13 13:12, Rui Maciel wrote: > >>> Joshua Landau wrote: > >>> > >>>> What's the actual problem you're facing? Where do you feel that you > >>>> need to verify types? > >>> A standard case would be when there's a function which is designed > expecting > >>> that all operands support a specific interface or contain specific > >>> attributes. > >>> > >>> In other words, when passing an unsupported type causes problems. > >>> > >> > >> Hi, > >> > >> First, let's get over the fact that, with dynamic typing, code fails at > >> runtime. Irrespective of language, you just shouldn't ship untested > >> code, so I say that's not an argument against dynamic typing. > > > > Why not? Can ease of development not be a consideration? So if some > > kind of faults are easier to detect at compile time if you have static > > typing than if you have to design a test for them, I don't see why that > > can't be an argument. > > Sure, which is why I like working in Pike, which does have static type > declarations (when you want them; they can get out the way when you > don't). But there will always be, regardless of your language, > criteria that static typing cannot adequately handle, so just write > your code to cope with exceptions - much easier. If the exception's > never thrown, the bug can't be all that serious; otherwise, just deal > with it when you find it, whether that be in initial testing or years > later in production. There WILL BE such errors - that's a given. Deal > with them, rather than trying to eliminate them. > > ChrisA > > > > ---------- Forwarded message ---------- > From: "Eric S. Johansson" > To: python-list at python.org, "Burak Arslan" > Cc: > Date: Tue, 06 Aug 2013 09:58:53 -0400 > Subject: Re: Newbie: static typing? > On Tue, 06 Aug 2013 09:27:10 -0400, Burak Arslan < > burak.arslan at arskom.com.tr> wrote: > > First, let's get over the fact that, with dynamic typing, code fails at >> runtime. Irrespective of language, you just shouldn't ship untested >> code, so I say that's not an argument against dynamic typing. >> > > It's not so much shipping untested code as not having or unable to test > all the pathways in the code shipped. I ran into this problem with a server > I built. I ended up solving the problem by building a testing scaffolding > that let me control all inputs. It would've been much easier with static > typing to make sure all the pieces lined up. > > The other technique I've used is a properly set up exception handling > environment. Do it right and you can log all of the errors so that you have > useful information. Part of "doing it right" includes a system that tells > you when exceptions happened right away so the server doesn't run for days > or more failing at random but nobody notices because your exceptions keep > the system for failing completely. > > I guess this is a long way of saying instrument your software so that it > can be tested and or give you enough information about the internal state. > This is sort of like building a specialized integrated circuit. You need to > design it so it can be tested/observed after it's been embedded in epoxy > and not just count on being able to probe the wafer in the lab. > > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From luca.cerone at gmail.com Tue Aug 6 13:12:26 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Tue, 6 Aug 2013 10:12:26 -0700 (PDT) Subject: Using Pool map with a method of a class and a list Message-ID: <96c575da-7601-4023-aa91-e80664f90333@googlegroups.com> Hi guys, I would like to apply the Pool.map method to a member of a class. Here is a small example that shows what I would like to do: from multiprocessing import Pool class A(object): def __init__(self,x): self.value = x def fun(self,x): return self.value**x l = range(10) p = Pool(4) op = p.map(A.fun,l) #using this with the normal map doesn't cause any problem This fails because it says that the methods can't be pickled. (I assume it has something to do with the note in the documentation: "functionality within this package requires that the __main__ module be importable by the children.", which is obscure to me). I would like to understand two things: why my code fails and when I can expect it to fail? what is a possible workaround? Thanks a lot in advance to everybody for the help! Cheers, Luca From rosuav at gmail.com Tue Aug 6 13:38:54 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Aug 2013 18:38:54 +0100 Subject: Using Pool map with a method of a class and a list In-Reply-To: <96c575da-7601-4023-aa91-e80664f90333@googlegroups.com> References: <96c575da-7601-4023-aa91-e80664f90333@googlegroups.com> Message-ID: On Tue, Aug 6, 2013 at 6:12 PM, Luca Cerone wrote: > from multiprocessing import Pool > > class A(object): > def __init__(self,x): > self.value = x > def fun(self,x): > return self.value**x > > > l = range(10) > > p = Pool(4) > > op = p.map(A.fun,l) Do you ever instantiate any A() objects? You're attempting to call an unbound method without passing it a 'self'. You may find the results completely different in Python 2 vs Python 3, and between bound and unbound methods. In Python 3, an unbound method is simply a function. In both versions, a bound method carries its first argument around, so it has to be something different. Play around with it a bit. ChrisA From luca.cerone at gmail.com Tue Aug 6 15:37:45 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Tue, 6 Aug 2013 12:37:45 -0700 (PDT) Subject: Using Pool map with a method of a class and a list In-Reply-To: <96c575da-7601-4023-aa91-e80664f90333@googlegroups.com> References: <96c575da-7601-4023-aa91-e80664f90333@googlegroups.com> Message-ID: On Tuesday, 6 August 2013 18:12:26 UTC+1, Luca Cerone wrote: > Hi guys, > > I would like to apply the Pool.map method to a member of a class. > > > > Here is a small example that shows what I would like to do: > > > > from multiprocessing import Pool > > > > class A(object): > > def __init__(self,x): > > self.value = x > > def fun(self,x): > > return self.value**x > > > > > > l = range(10) > > > > p = Pool(4) > > > > op = p.map(A.fun,l) > > > > #using this with the normal map doesn't cause any problem > > > > This fails because it says that the methods can't be pickled. > > (I assume it has something to do with the note in the documentation: "functionality within this package requires that the __main__ module be importable by the children.", which is obscure to me). > > > > I would like to understand two things: why my code fails and when I can expect it to fail? what is a possible workaround? > > > > Thanks a lot in advance to everybody for the help! > > > > Cheers, > > Luca On Tuesday, 6 August 2013 18:12:26 UTC+1, Luca Cerone wrote: > Hi guys, > > I would like to apply the Pool.map method to a member of a class. > > > > Here is a small example that shows what I would like to do: > > > > from multiprocessing import Pool > > > > class A(object): > > def __init__(self,x): > > self.value = x > > def fun(self,x): > > return self.value**x > > > > > > l = range(10) > > > > p = Pool(4) > > > > op = p.map(A.fun,l) > > > > #using this with the normal map doesn't cause any problem > > > > This fails because it says that the methods can't be pickled. > > (I assume it has something to do with the note in the documentation: "functionality within this package requires that the __main__ module be importable by the children.", which is obscure to me). > > > > I would like to understand two things: why my code fails and when I can expect it to fail? what is a possible workaround? > > > > Thanks a lot in advance to everybody for the help! > > > > Cheers, > > Luca From luca.cerone at gmail.com Tue Aug 6 15:42:31 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Tue, 6 Aug 2013 12:42:31 -0700 (PDT) Subject: Using Pool map with a method of a class and a list In-Reply-To: References: <96c575da-7601-4023-aa91-e80664f90333@googlegroups.com> Message-ID: <4cff0d5e-33ab-42cd-b6d4-2b4fe235a274@googlegroups.com> Hi Chris, thanks > Do you ever instantiate any A() objects? You're attempting to call an > > unbound method without passing it a 'self'. I have tried a lot of variations, instantiating the object, creating lambda functions that use the unbound version of fun (A.fun.__func__) etc etc.. I have played around it quite a bit before posting. As far as I have understood the problem is due to the fact that Pool pickle the function and copy it in the various pools.. But since the methods cannot be pickled this fails.. The same example I posted won't run in Python 3.2 neither (I am mostly interested in a solution for Python 2.7, sorry I forgot to mention that). Thanks in any case for the help, hopefully there will be some other advice in the ML :) Cheers, Luca From joshua at landau.ws Wed Aug 7 02:48:32 2013 From: joshua at landau.ws (Joshua Landau) Date: Wed, 7 Aug 2013 07:48:32 +0100 Subject: Using Pool map with a method of a class and a list In-Reply-To: <4cff0d5e-33ab-42cd-b6d4-2b4fe235a274@googlegroups.com> References: <96c575da-7601-4023-aa91-e80664f90333@googlegroups.com> <4cff0d5e-33ab-42cd-b6d4-2b4fe235a274@googlegroups.com> Message-ID: On 6 August 2013 20:42, Luca Cerone wrote: > Hi Chris, thanks > >> Do you ever instantiate any A() objects? You're attempting to call an >> >> unbound method without passing it a 'self'. > > I have tried a lot of variations, instantiating the object, creating lambda functions that use the unbound version of fun (A.fun.__func__) etc etc.. > I have played around it quite a bit before posting. > > As far as I have understood the problem is due to the fact that Pool pickle the function and copy it in the various pools.. > But since the methods cannot be pickled this fails.. > > The same example I posted won't run in Python 3.2 neither (I am mostly interested in a solution for Python 2.7, sorry I forgot to mention that). > > Thanks in any case for the help, hopefully there will be some other advice in the ML :) I think you might not understand what Chris said. Currently this does *not* work with Python 2.7 as you suggested it would. >>> op = map(A.fun,l) Traceback (most recent call last): File "", line 1, in TypeError: unbound method fun() must be called with A instance as first argument (got int instance instead) This, however, does: >>> op = map(A(3).fun,l) >>> op [1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683] Chris might have also been confused because once you fix that it works in Python 3. You will find that http://stackoverflow.com/questions/1816958/cant-pickle-type-instancemethod-when-using-pythons-multiprocessing-pool-ma explains the problem in more detail than I understand. I suggest reading it and relaying further questions back to us. Or use Python 3 ;). From luca.cerone at gmail.com Wed Aug 7 04:33:05 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Wed, 7 Aug 2013 01:33:05 -0700 (PDT) Subject: Using Pool map with a method of a class and a list In-Reply-To: References: <96c575da-7601-4023-aa91-e80664f90333@googlegroups.com> <4cff0d5e-33ab-42cd-b6d4-2b4fe235a274@googlegroups.com> Message-ID: <021dfe24-af83-4307-856e-441cf35cb93a@googlegroups.com> Hi Joshua thanks! > I think you might not understand what Chris said. > Currently this does *not* work with Python 2.7 as you suggested it would. > >>> op = map(A.fun,l) Yeah actually that wouldn't work even in Python 3, since value attribute used by fun has not been set. It was my mistake in the example, but it is not the source of the problem.. > This, however, does: > >>> op = map(A(3).fun,l) > > >>> op > > [1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683] > > This works fine (and I knew that).. but is not what I want... You are using the map() function that comes with Python. I want to use the map() method of the Pool class (available in the multiprocessing module). And there are differences between map() and Pool.map() apparently, so that if something works fine with map() it may not work with Pool.map() (as in my case). To correct my example: from multiprocessing import Pool class A(object): def __init__(self,x): self.value = x def fun(self,x): return self.value**x l = range(100) p = Pool(4) op = p.map(A(3).fun, l) doesn't work neither in Python 2.7, nor 3.2 (by the way I can't use Python 3 for my application). > You will find that > http://stackoverflow.com/questions/1816958/cant-pickle-type-instancemethod-> > when-using-pythons-multiprocessing-pool-ma > explains the problem in more detail than I understand. I suggest > reading it and relaying further questions back to us. Or use Python 3 :) Thanks, but of course I googled and found this link before posting. I don't understand much of the details as well, that's why I posted here. Anyway, thanks for the attempt :) Luca From joshua at landau.ws Wed Aug 7 05:47:06 2013 From: joshua at landau.ws (Joshua Landau) Date: Wed, 7 Aug 2013 10:47:06 +0100 Subject: Using Pool map with a method of a class and a list In-Reply-To: <021dfe24-af83-4307-856e-441cf35cb93a@googlegroups.com> References: <96c575da-7601-4023-aa91-e80664f90333@googlegroups.com> <4cff0d5e-33ab-42cd-b6d4-2b4fe235a274@googlegroups.com> <021dfe24-af83-4307-856e-441cf35cb93a@googlegroups.com> Message-ID: On 7 August 2013 09:33, Luca Cerone wrote: > To correct my example: > > from multiprocessing import Pool > > class A(object): > def __init__(self,x): > self.value = x > def fun(self,x): > return self.value**x > > l = range(100) > p = Pool(4) > op = p.map(A(3).fun, l) > > doesn't work neither in Python 2.7, nor 3.2 (by the way I can't use Python 3 for my application). Are you using Windows? Over here on 3.3 on Linux it does. Not on 2.7 though. >> You will find that >> http://stackoverflow.com/questions/1816958/cant-pickle-type-instancemethod-> > when-using-pythons-multiprocessing-pool-ma >> explains the problem in more detail than I understand. I suggest >> reading it and relaying further questions back to us. Or use Python 3 > > :) Thanks, but of course I googled and found this link before posting. I don't understand much of the details as well, that's why I posted here. > > Anyway, thanks for the attempt :) Reading there, the simplest method seems to be, in effect: from multiprocessing import Pool from functools import partial class A(object): def __init__(self,x): self.value = x def fun(self,x): return self.value**x def _getattr_proxy_partialable(instance, name, arg): return getattr(instance, name)(arg) def getattr_proxy(instance, name): """ A version of getattr that returns a proxy function that can be pickled. Only function calls will work on the proxy. """ return partial(_getattr_proxy_partialable, instance, name) l = range(100) p = Pool(4) op = p.map(getattr_proxy(A(3), "fun"), l) print(op) From luca.cerone at gmail.com Wed Aug 7 06:10:51 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Wed, 7 Aug 2013 03:10:51 -0700 (PDT) Subject: Using Pool map with a method of a class and a list In-Reply-To: References: <96c575da-7601-4023-aa91-e80664f90333@googlegroups.com> <4cff0d5e-33ab-42cd-b6d4-2b4fe235a274@googlegroups.com> <021dfe24-af83-4307-856e-441cf35cb93a@googlegroups.com> Message-ID: <13807c2e-7f9f-45dd-b36e-4cdc7cde6709@googlegroups.com> > > doesn't work neither in Python 2.7, nor 3.2 (by the way I can't use Python 3 for my application). > > Are you using Windows? Over here on 3.3 on Linux it does. Not on 2.7 though. No I am using Ubuntu (12.04, 64 bit).. maybe things changed from 3.2 to 3.3? > from multiprocessing import Pool > > from functools import partial > > > > class A(object): > > def __init__(self,x): > > self.value = x > > def fun(self,x): > > return self.value**x > > > > def _getattr_proxy_partialable(instance, name, arg): > > return getattr(instance, name)(arg) > > > > def getattr_proxy(instance, name): > > """ > > A version of getattr that returns a proxy function that can > > be pickled. Only function calls will work on the proxy. > > """ > > return partial(_getattr_proxy_partialable, instance, name) > > > > l = range(100) > > p = Pool(4) > > op = p.map(getattr_proxy(A(3), "fun"), l) > > print(op) I can't try it now, I'll let you know later if it works! (Though just by reading I can't really understand what the code does). Thanks for the help, Luca From joshua at landau.ws Wed Aug 7 07:53:40 2013 From: joshua at landau.ws (Joshua Landau) Date: Wed, 7 Aug 2013 12:53:40 +0100 Subject: Using Pool map with a method of a class and a list In-Reply-To: <13807c2e-7f9f-45dd-b36e-4cdc7cde6709@googlegroups.com> References: <96c575da-7601-4023-aa91-e80664f90333@googlegroups.com> <4cff0d5e-33ab-42cd-b6d4-2b4fe235a274@googlegroups.com> <021dfe24-af83-4307-856e-441cf35cb93a@googlegroups.com> <13807c2e-7f9f-45dd-b36e-4cdc7cde6709@googlegroups.com> Message-ID: On 7 August 2013 11:10, Luca Cerone wrote: > I can't try it now, I'll let you know later if it works! > (Though just by reading I can't really understand what the code does). Well, >> from multiprocessing import Pool >> from functools import partial >> >> class A(object): >> def __init__(self,x): >> self.value = x >> def fun(self,x): >> return self.value**x This is all the same, as with >> l = range(100) >> p = Pool(4) You then wanted to do: > op = p.map(A(3).fun, l) but bound methods can't be pickled, it seems. However, A(3) *can* be pickled. So what we want is a function: def proxy(arg): A(3).fun(arg) so we can write: > op = p.map(proxy, l) To generalise you might be tempted to write: def generic_proxy(instance, name): def proxy(arg): # Equiv. of instance.name(arg) getattr(instance, name)(arg) but the inner function won't work as functions-in-functions can't be pickled either. So we use: >> def _getattr_proxy_partialable(instance, name, arg): >> return getattr(instance, name)(arg) Which takes all instance, name and arg. Of course we only want our function to take arg, so we partial it: >> def getattr_proxy(instance, name): >> """ >> A version of getattr that returns a proxy function that can >> be pickled. Only function calls will work on the proxy. >> """ >> return partial(_getattr_proxy_partialable, instance, name) partial objects are picklable, btw. >> op = p.map(getattr_proxy(A(3), "fun"), l) >> print(op) :) From __peter__ at web.de Wed Aug 7 10:46:09 2013 From: __peter__ at web.de (Peter Otten) Date: Wed, 07 Aug 2013 16:46:09 +0200 Subject: Using Pool map with a method of a class and a list References: <96c575da-7601-4023-aa91-e80664f90333@googlegroups.com> <4cff0d5e-33ab-42cd-b6d4-2b4fe235a274@googlegroups.com> <021dfe24-af83-4307-856e-441cf35cb93a@googlegroups.com> <13807c2e-7f9f-45dd-b36e-4cdc7cde6709@googlegroups.com> Message-ID: Joshua Landau wrote: > On 7 August 2013 11:10, Luca Cerone wrote: >> I can't try it now, I'll let you know later if it works! >> (Though just by reading I can't really understand what the code does). > > Well, > >>> from multiprocessing import Pool >>> from functools import partial >>> >>> class A(object): >>> def __init__(self,x): >>> self.value = x >>> def fun(self,x): >>> return self.value**x > > This is all the same, as with > >>> l = range(100) >>> p = Pool(4) > > You then wanted to do: > >> op = p.map(A(3).fun, l) > > but bound methods can't be pickled, it seems. > > However, A(3) *can* be pickled. So what we want is a function: > > def proxy(arg): > A(3).fun(arg) > > so we can write: > >> op = p.map(proxy, l) > > To generalise you might be tempted to write: > > def generic_proxy(instance, name): > def proxy(arg): > # Equiv. of instance.name(arg) > getattr(instance, name)(arg) > > but the inner function won't work as functions-in-functions can't be > pickled either. > > So we use: > >>> def _getattr_proxy_partialable(instance, name, arg): >>> return getattr(instance, name)(arg) > > Which takes all instance, name and arg. Of course we only want our > function to take arg, so we partial it: > >>> def getattr_proxy(instance, name): >>> """ >>> A version of getattr that returns a proxy function that can >>> be pickled. Only function calls will work on the proxy. >>> """ >>> return partial(_getattr_proxy_partialable, instance, name) > > partial objects are picklable, btw. > >>> op = p.map(getattr_proxy(A(3), "fun"), l) >>> print(op) > > :) There is also the copy_reg module. Adapting you get: import copy_reg import multiprocessing import new def make_instancemethod(inst, methodname): return getattr(inst, methodname) def pickle_instancemethod(method): return make_instancemethod, (method.im_self, method.im_func.__name__) copy_reg.pickle( new.instancemethod, pickle_instancemethod, make_instancemethod) class A(object): def __init__(self, a): self.a = a def fun(self, b): return self.a**b if __name__ == "__main__": items = range(10) pool = multiprocessing.Pool(4) print pool.map(A(3).fun, items) From joshua at landau.ws Wed Aug 7 11:52:45 2013 From: joshua at landau.ws (Joshua Landau) Date: Wed, 7 Aug 2013 16:52:45 +0100 Subject: Using Pool map with a method of a class and a list In-Reply-To: References: <96c575da-7601-4023-aa91-e80664f90333@googlegroups.com> <4cff0d5e-33ab-42cd-b6d4-2b4fe235a274@googlegroups.com> <021dfe24-af83-4307-856e-441cf35cb93a@googlegroups.com> <13807c2e-7f9f-45dd-b36e-4cdc7cde6709@googlegroups.com> Message-ID: On 7 August 2013 15:46, Peter Otten <__peter__ at web.de> wrote: > import copy_reg > import multiprocessing > import new "new" is deprecated from 2.6+; use types.MethodType instead of new.instancemethod. > def make_instancemethod(inst, methodname): > return getattr(inst, methodname) This is just getattr -- you can replace the two uses of make_instancemethod with getattr and delete this ;). > def pickle_instancemethod(method): > return make_instancemethod, (method.im_self, method.im_func.__name__) > > copy_reg.pickle( > new.instancemethod, pickle_instancemethod, make_instancemethod) > > class A(object): > def __init__(self, a): > self.a = a > def fun(self, b): > return self.a**b > > if __name__ == "__main__": > items = range(10) > pool = multiprocessing.Pool(4) > print pool.map(A(3).fun, items) Well that was easy. The Stackoverflow link made that look *hard*. -1 to my hack, +1 to this. You can do this in one statement: copy_reg.pickle( types.MethodType, lambda method: (getattr, (method.im_self, method.im_func.__name__)), getattr ) From __peter__ at web.de Wed Aug 7 12:15:14 2013 From: __peter__ at web.de (Peter Otten) Date: Wed, 07 Aug 2013 18:15:14 +0200 Subject: Using Pool map with a method of a class and a list References: <96c575da-7601-4023-aa91-e80664f90333@googlegroups.com> <4cff0d5e-33ab-42cd-b6d4-2b4fe235a274@googlegroups.com> <021dfe24-af83-4307-856e-441cf35cb93a@googlegroups.com> <13807c2e-7f9f-45dd-b36e-4cdc7cde6709@googlegroups.com> Message-ID: Joshua Landau wrote: > On 7 August 2013 15:46, Peter Otten <__peter__ at web.de> wrote: >> def make_instancemethod(inst, methodname): >> return getattr(inst, methodname) > > This is just getattr -- you can replace the two uses of > make_instancemethod with getattr and delete this ;). D'oh ;) From luca.cerone at gmail.com Wed Aug 7 19:31:54 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Wed, 7 Aug 2013 16:31:54 -0700 (PDT) Subject: Using Pool map with a method of a class and a list In-Reply-To: References: <96c575da-7601-4023-aa91-e80664f90333@googlegroups.com> <4cff0d5e-33ab-42cd-b6d4-2b4fe235a274@googlegroups.com> <021dfe24-af83-4307-856e-441cf35cb93a@googlegroups.com> <13807c2e-7f9f-45dd-b36e-4cdc7cde6709@googlegroups.com> Message-ID: Thanks for the help Peter! > > > > >> def make_instancemethod(inst, methodname): > > >> return getattr(inst, methodname) > > > > > > This is just getattr -- you can replace the two uses of > > > make_instancemethod with getattr and delete this ;). > > > > D'oh ;) From luca.cerone at gmail.com Wed Aug 7 18:26:37 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Wed, 7 Aug 2013 15:26:37 -0700 (PDT) Subject: Using Pool map with a method of a class and a list In-Reply-To: References: <96c575da-7601-4023-aa91-e80664f90333@googlegroups.com> <4cff0d5e-33ab-42cd-b6d4-2b4fe235a274@googlegroups.com> <021dfe24-af83-4307-856e-441cf35cb93a@googlegroups.com> <13807c2e-7f9f-45dd-b36e-4cdc7cde6709@googlegroups.com> Message-ID: Thanks for the post. I actually don't know exactly what can and can't be pickles.. not what partialing a function means.. Maybe can you link me to some resources? I still can't understand all the details in your code :) From joshua at landau.ws Wed Aug 7 18:49:47 2013 From: joshua at landau.ws (Joshua Landau) Date: Wed, 7 Aug 2013 23:49:47 +0100 Subject: Using Pool map with a method of a class and a list In-Reply-To: References: <96c575da-7601-4023-aa91-e80664f90333@googlegroups.com> <4cff0d5e-33ab-42cd-b6d4-2b4fe235a274@googlegroups.com> <021dfe24-af83-4307-856e-441cf35cb93a@googlegroups.com> <13807c2e-7f9f-45dd-b36e-4cdc7cde6709@googlegroups.com> Message-ID: On 7 August 2013 23:26, Luca Cerone wrote: > Thanks for the post. > I actually don't know exactly what can and can't be pickles.. I just try it and see what works ;). The general idea is that if it is module-level it can be pickled and if it is defined inside of something else it cannot. It depends though. > not what partialing a function means.. "partial" takes a function and returns it with arguments "filled in": from functools import partial def add(a, b): return a + b add5 = partial(add, 5) print(add5(10)) # Returns 15 == 5 + 10 > Maybe can you link me to some resources? http://docs.python.org/2/library/functools.html#functools.partial > I still can't understand all the details in your code :) Never mind that, though, as Peter Otten's code (with my very minor suggested modifications) if by far the cleanest method of the two and is arguably more correct too. From andrea.crotti.0 at gmail.com Tue Aug 6 13:38:17 2013 From: andrea.crotti.0 at gmail.com (andrea crotti) Date: Tue, 6 Aug 2013 18:38:17 +0100 Subject: lxml tostring quoting too much Message-ID: I would really like to do the following: from lxml import etree as ET from lxml.builder import E url = "http://something?x=10&y=20" l = E.link(url) ET.tostring(l) -> "http://something?x=10&y=20" However the lxml tostring always quotes the &, I can't find a way to tell it to avoid quoting it. Is it possible? Thanks From chris at chrisdown.name Tue Aug 6 16:28:09 2013 From: chris at chrisdown.name (Chris Down) Date: Tue, 6 Aug 2013 22:28:09 +0200 Subject: lxml tostring quoting too much In-Reply-To: References: Message-ID: <20130806202809.GC17412@gopher> On 2013-08-06 18:38, andrea crotti wrote: > I would really like to do the following: > > from lxml import etree as ET > from lxml.builder import E > > url = "http://something?x=10&y=20" > l = E.link(url) > ET.tostring(l) -> "http://something?x=10&y=20" > > However the lxml tostring always quotes the &, I can't find a way to > tell it to avoid quoting it. You're probably aware, but without the escaping, it is no longer well formed XML. Why do you want to do that? Is there a larger underlying problem that should be solved instead? Either way, you can use "unescape" from the xml.sax.saxutils module[0]. Chris 0: http://docs.python.org/2/library/xml.sax.utils.html -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From andrea.crotti.0 at gmail.com Wed Aug 7 06:49:52 2013 From: andrea.crotti.0 at gmail.com (andrea crotti) Date: Wed, 7 Aug 2013 11:49:52 +0100 Subject: lxml tostring quoting too much In-Reply-To: <20130806202809.GC17412@gopher> References: <20130806202809.GC17412@gopher> Message-ID: 2013/8/6 Chris Down : > On 2013-08-06 18:38, andrea crotti wrote: >> I would really like to do the following: >> >> from lxml import etree as ET >> from lxml.builder import E >> >> url = "http://something?x=10&y=20" >> l = E.link(url) >> ET.tostring(l) -> "http://something?x=10&y=20" >> >> However the lxml tostring always quotes the &, I can't find a way to >> tell it to avoid quoting it. > > You're probably aware, but without the escaping, it is no longer well formed > XML. Why do you want to do that? Is there a larger underlying problem that > should be solved instead? > > Either way, you can use "unescape" from the xml.sax.saxutils module[0]. > > Chris > > 0: http://docs.python.org/2/library/xml.sax.utils.html Yes I know it's not correct, I thought that I still had to send that anyway but luckily the problem was somewhere else, so encoding was actually necessary and I don't need to do something strange.. From eschneider92 at comcast.net Tue Aug 6 17:35:44 2013 From: eschneider92 at comcast.net (eschneider92 at comcast.net) Date: Tue, 6 Aug 2013 14:35:44 -0700 (PDT) Subject: Beginner question Message-ID: <6e80b2f8-0b14-43cd-b8af-211ef65d73ba@googlegroups.com> Why won't the 'goodbye' part of this code work right? it prints 'ok' no matter what is typed. Much thanks. def thing(): print('go again?') goagain=input() if goagain=='y' or 'yes': print('ok') elif goagain!='y' or 'yes': print('goodbye') sys.exit() thing() From davea at davea.name Tue Aug 6 18:03:57 2013 From: davea at davea.name (Dave Angel) Date: Tue, 6 Aug 2013 22:03:57 +0000 (UTC) Subject: Beginner question References: <6e80b2f8-0b14-43cd-b8af-211ef65d73ba@googlegroups.com> Message-ID: eschneider92 at comcast.net wrote: > Why won't the 'goodbye' part of this code work right? it prints 'ok' no matter what is typed. Much thanks. > > def thing(): > print('go again?') > goagain=input() > if goagain=='y' or 'yes': This expression doesn't do what you think. The comparison binds more tightly, so it first evaluates (goagain=="y"). The results of that are either True or False. Then it or's that logical value with 'yes'. The result is either True or it's 'yes'. 'yes' is considered truthy, so the if will always succeed. What you meant to use was: if goagain == "y" or goagain == "yes": Alternatively, you could use if goagain in ("y", "yes"): > print('ok') > elif goagain!='y' or 'yes': Same here. > print('goodbye') > sys.exit() > thing() -- DaveA From rosuav at gmail.com Tue Aug 6 18:10:32 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Aug 2013 23:10:32 +0100 Subject: Beginner question In-Reply-To: <6e80b2f8-0b14-43cd-b8af-211ef65d73ba@googlegroups.com> References: <6e80b2f8-0b14-43cd-b8af-211ef65d73ba@googlegroups.com> Message-ID: On Tue, Aug 6, 2013 at 10:35 PM, wrote: > Why won't the 'goodbye' part of this code work right? it prints 'ok' no matter what is typed. Much thanks. > > def thing(): > print('go again?') > goagain=input() > if goagain=='y' or 'yes': > print('ok') > elif goagain!='y' or 'yes': > print('goodbye') > sys.exit() > thing() When you use 'or' in this way, it's not doing what you think it does. It actually first computes >>> goagain=='y' which is either True or False, and then evaluates the next part: >>> True or 'yes' >>> False or 'yes' Try out those two in interactive Python and see what they do. You probably want to compare in both halves of the test, or possibly use the 'in' operator: if goagain in ('y', 'yes'): Also, you don't need an 'elif' there; just use a plain else. Repeating the condition just introduces the chance of bugs - for instance, would you notice the error in this? if goagain=='y' or goagain=='yes': print('ok') elif goagain!='y' or goagain!='yes': print('goodbye') Using a plain 'else' guarantees that exactly one of the branches will be executed, rather than having the possibility that neither will, which isn't the intention here. Hope that helps! ChrisA From chris at chrisdown.name Tue Aug 6 17:46:51 2013 From: chris at chrisdown.name (Chris Down) Date: Tue, 6 Aug 2013 23:46:51 +0200 Subject: Beginner question In-Reply-To: <6e80b2f8-0b14-43cd-b8af-211ef65d73ba@googlegroups.com> References: <6e80b2f8-0b14-43cd-b8af-211ef65d73ba@googlegroups.com> Message-ID: <20130806214651.GD17412@gopher> On 2013-08-06 14:35, eschneider92 at comcast.net wrote: > Why won't the 'goodbye' part of this code work right? it prints 'ok' no > matter what is typed. Much thanks. "if" statements do not fall through, because the first statement was matched, no other ones in the same chain will be evaluted. "elif" means "else if", where "else" means "if nothing previous matched". -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From rhodri at wildebst.demon.co.uk Tue Aug 6 18:14:37 2013 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Tue, 06 Aug 2013 23:14:37 +0100 Subject: Beginner question References: <6e80b2f8-0b14-43cd-b8af-211ef65d73ba@googlegroups.com> Message-ID: On Tue, 06 Aug 2013 22:35:44 +0100, wrote: > Why won't the 'goodbye' part of this code work right? it prints 'ok' no > matter what is typed. Much thanks. > > def thing(): > print('go again?') > goagain=input() > if goagain=='y' or 'yes': This line doesn't do what you think it does :-) Typing that condition at an interactive prompt reveals something interesting: >>> goagain = "n" >>> goagain=="y" False >>> goagain=="y" or "yes" 'yes' Oho. What's actually happening is that you've mistaken the operator precedence. "==" has a higher precedence than "or", so your condition is equivalent to '(goagain=="y") or "yes"'. Since it's left-hand argument is False, "or" returns its right-hand argument, which has the value 'yes', which in a boolean context is "True". What you seem to want to do is to have your condition be true if goagain is either "y" or "yes". Probably the easiest way of doing this and learning something new at the same time is to ask if goagain appears in a list (or rather a tuple) of strings: if goagain in ('y', 'yes'): print('ok') elif goagain not in ('y', 'yes'): print('goodbye') sys.exit() or better, if goagain in ('y', 'yes', 'ohdeargodyes', 'you get the idea'): print('ok') else: print('goodbye') sys.exit() -- Rhodri James *-* Wildebeest Herder to the Masses From eschneider92 at comcast.net Tue Aug 6 23:06:51 2013 From: eschneider92 at comcast.net (eschneider92 at comcast.net) Date: Tue, 6 Aug 2013 20:06:51 -0700 (PDT) Subject: Beginner question In-Reply-To: <6e80b2f8-0b14-43cd-b8af-211ef65d73ba@googlegroups.com> References: <6e80b2f8-0b14-43cd-b8af-211ef65d73ba@googlegroups.com> Message-ID: Thanks that helped a lot! From nospam at nospam.com Tue Aug 6 18:12:35 2013 From: nospam at nospam.com (Gilles) Date: Wed, 07 Aug 2013 00:12:35 +0200 Subject: [ActivePython] Upgrading fails Message-ID: Hello, I already posted in their forum, but got no reply and figured some people here might have experienced this too. I'm currently running ActivePython 2.5.1.1 on my XP Pro host. After downloading and running the installer to 2.7.2.5, I get the following error message: "Windows Installer: The Windows Installer Service could not be accessed. This can occur if the Windows Installer is not correctly installed." Has someone seen this, and knows what to do? Thank you. From tjreedy at udel.edu Tue Aug 6 19:42:00 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 06 Aug 2013 19:42:00 -0400 Subject: [ActivePython] Upgrading fails In-Reply-To: References: Message-ID: On 8/6/2013 6:12 PM, Gilles wrote: > Hello, > > I already posted in their forum, but got no reply and figured some > people here might have experienced this too. > > I'm currently running ActivePython 2.5.1.1 on my XP Pro host. > > After downloading and running the installer to 2.7.2.5, I get the > following error message: > > "Windows Installer: The Windows Installer Service could not be > accessed. This can occur if the Windows Installer is not correctly > installed." > > Has someone seen this, and knows what to do? No and no. I would search "Windows Installer Service could not be accessed" (I just did and there are lots of relevant hits.) Try the same for any Windows error message. -- Terry Jan Reedy From mikelhamer at gmail.com Tue Aug 6 18:28:00 2013 From: mikelhamer at gmail.com (snakeinmyboot) Date: Tue, 6 Aug 2013 15:28:00 -0700 (PDT) Subject: [tkinter] trouble running imported modules in main program Message-ID: <6f47e052-ec39-4894-a527-0d68653d5375@googlegroups.com> Hello, I am currently just starting to learn the basics of the tkinter module and ive run into a problem. To teach myself I am messing around by creating separate modules containing basic GUI apps like a tip calculator and kilometer converter, then creating a main program that displays buttons you can click that will create instances of the modules (tip calculator, kilometer converter). So far ive managed to successfully make the basic GUI apps works just fine, and have been able to import them into my main program and get them to launch in a new window when i click the buttons and everything, but heres my problem. Everything displays ok, like the labels, entry boxes, and buttons, but when I actually click the buttons (which are supposed to trigger a method within the module and display a label containing the calculated data).. nothing happens. When I run the modules by themselves everything works just fine (testing by making the module create an instance of itself but i # them out before running the main program). I have posted all my code to this forum if you need to look at it http://python-forum.org/viewtopic.php?f=12&t=5620 Any idea what im doing wrong here? From tjreedy at udel.edu Tue Aug 6 19:38:55 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 06 Aug 2013 19:38:55 -0400 Subject: [tkinter] trouble running imported modules in main program In-Reply-To: <6f47e052-ec39-4894-a527-0d68653d5375@googlegroups.com> References: <6f47e052-ec39-4894-a527-0d68653d5375@googlegroups.com> Message-ID: On 8/6/2013 6:28 PM, snakeinmyboot wrote: > Hello, > > I am currently just starting to learn the basics of the tkinter module and ive run into a problem. To teach myself I am messing around by creating separate modules containing basic GUI apps like a tip calculator and kilometer converter, then creating a main program that displays buttons you can click that will create instances of the modules (tip calculator, kilometer converter). So far ive managed to successfully make the basic GUI apps works just fine, and have been able to import them into my main program and get them to launch in a new window when i click the buttons and everything, but heres my problem. > > Everything displays ok, like the labels, entry boxes, and buttons, but when I actually click the buttons (which are supposed to trigger a method within the module and display a label containing the calculated data).. nothing happens. When I run the modules by themselves everything works just fine (testing by making the module create an instance of itself but i # them out before running the main program) Make the boilerplate test code conditional, something like if __name__ == '__main__': root = tkinter.Tk() app = MainClass(root) # 'MainClass' depends on the module. root.mainloop root.destroy and you do not need to comment it out as it will be ignored when the module is imported. > I have posted all my code to this forum if you need to look at it > > http://python-forum.org/viewtopic.php?f=12&t=5620 Code comments: double and triple spacing code make it painful to read, especiallly in a 10 line box. self.miles_button = tkinter.Button(self.frame3, \ text = 'Miles', \ command = self.conv_miles) Leave off the unneeded \s. > Any idea what im doing wrong here? You got the answer there -- only one mainloop. Revising the code as suggested above will do that. Note: I am a tkinter beginner too, so I would also have to experiment with details to get importable custom widgets, built from tk widgets, right. -- Terry Jan Reedy From mikelhamer at gmail.com Wed Aug 7 00:25:05 2013 From: mikelhamer at gmail.com (snakeinmyboot) Date: Tue, 6 Aug 2013 21:25:05 -0700 (PDT) Subject: [tkinter] trouble running imported modules in main program In-Reply-To: References: <6f47e052-ec39-4894-a527-0d68653d5375@googlegroups.com> Message-ID: On Tuesday, August 6, 2013 7:38:55 PM UTC-4, Terry Reedy wrote: > On 8/6/2013 6:28 PM, snakeinmyboot wrote: > > > Hello, > > > > > > I am currently just starting to learn the basics of the tkinter module and ive run into a problem. To teach myself I am messing around by creating separate modules containing basic GUI apps like a tip calculator and kilometer converter, then creating a main program that displays buttons you can click that will create instances of the modules (tip calculator, kilometer converter). So far ive managed to successfully make the basic GUI apps works just fine, and have been able to import them into my main program and get them to launch in a new window when i click the buttons and everything, but heres my problem. > > > > > > Everything displays ok, like the labels, entry boxes, and buttons, but when I actually click the buttons (which are supposed to trigger a method within the module and display a label containing the calculated data).. nothing happens. When I run the modules by themselves everything works just fine (testing by making the module create an instance of itself but i # them out before running the main program) > > > > Make the boilerplate test code conditional, something like > > > > if __name__ == '__main__': > > root = tkinter.Tk() > > app = MainClass(root) # 'MainClass' depends on the module. > > root.mainloop > > root.destroy > > > Could you elaborate on this a little bit? Never even heard the term "boilerplate test code". Also, I have removed the mainloops from the other modules, but this didnt seem to affect anything. Thanks for the reply! From mikelhamer at gmail.com Wed Aug 7 00:26:57 2013 From: mikelhamer at gmail.com (snakeinmyboot) Date: Tue, 6 Aug 2013 21:26:57 -0700 (PDT) Subject: [tkinter] trouble running imported modules in main program In-Reply-To: References: <6f47e052-ec39-4894-a527-0d68653d5375@googlegroups.com> Message-ID: <66fbaae1-0097-4073-8643-524cef4ffbce@googlegroups.com> > Make the boilerplate test code conditional, something like > > > > if __name__ == '__main__': > > root = tkinter.Tk() > > app = MainClass(root) # 'MainClass' depends on the module. > > root.mainloop > > root.destroy > > > Could you elaborate on this a little bit? Never even heard the term "boilerplate test code". Also, I have removed the mainloops from the other modules, but this didnt seem to affect anything. Thanks for the reply! (the message I sent you was supposed to be this reply, my bad) From cousinstanley at gmail.com Wed Aug 7 16:08:52 2013 From: cousinstanley at gmail.com (Cousin Stanley) Date: Wed, 7 Aug 2013 20:08:52 +0000 (UTC) Subject: [tkinter] trouble running imported modules in main program References: <6f47e052-ec39-4894-a527-0d68653d5375@googlegroups.com> Message-ID: Terry Reedy wrote: > Code comments : > > double and triple spacing code > make it painful to read, Not for everyone :-) I prefer mostly double-spaced code in any language > especially in a 10 line box. Agree, but the 10 line box would not be used for routine code editing and viewing and could be made larger to accomodate viewing code posted online .... -- Stanley C. Kitching Human Being Phoenix, Arizona From mikelhamer at gmail.com Thu Aug 8 00:06:18 2013 From: mikelhamer at gmail.com (snakeinmyboot) Date: Wed, 7 Aug 2013 21:06:18 -0700 (PDT) Subject: [tkinter] trouble running imported modules in main program In-Reply-To: References: <6f47e052-ec39-4894-a527-0d68653d5375@googlegroups.com> Message-ID: <5d739907-50ec-4691-a4a3-5c10f2b7cfe2@googlegroups.com> >if __name__ == '__main__': > root = tkinter.Tk() > app = MainClass(root) # 'MainClass' depends on the module. > root.mainloop > root.destroy for REAL you guys...wtf does this even mean lol. what is a boilerplate test code? From bouncingcats at gmail.com Thu Aug 8 01:13:48 2013 From: bouncingcats at gmail.com (David) Date: Thu, 8 Aug 2013 15:13:48 +1000 Subject: [tkinter] trouble running imported modules in main program In-Reply-To: <5d739907-50ec-4691-a4a3-5c10f2b7cfe2@googlegroups.com> References: <6f47e052-ec39-4894-a527-0d68653d5375@googlegroups.com> <5d739907-50ec-4691-a4a3-5c10f2b7cfe2@googlegroups.com> Message-ID: On 8 August 2013 14:06, snakeinmyboot wrote: > > for REAL you guys...wtf does this even mean lol. what is a boilerplate test code? Did you try at all to find the answer to this yourself? I ask because it took me only a few seconds to go to wikipedia and search for "boilerplate" find this for you: http://en.wikipedia.org/wiki/Boilerplate_code Tip: To successfully use forums like this one (where a lot of very smart people read), the more care/effort/thought you demonstrate that you tried to solve your issue before asking, the more likely you are to receive a response. From mikelhamer at gmail.com Fri Aug 9 00:28:30 2013 From: mikelhamer at gmail.com (snakeinmyboot) Date: Thu, 8 Aug 2013 21:28:30 -0700 (PDT) Subject: [tkinter] trouble running imported modules in main program In-Reply-To: References: <6f47e052-ec39-4894-a527-0d68653d5375@googlegroups.com> <5d739907-50ec-4691-a4a3-5c10f2b7cfe2@googlegroups.com> Message-ID: <8039efc1-5fa1-46be-a834-46bd7981f7f5@googlegroups.com> On Thursday, August 8, 2013 1:13:48 AM UTC-4, David wrote: > On 8 August 2013 14:06, snakeinmyboot wrote: > > > > > > for REAL you guys...wtf does this even mean lol. what is a boilerplate test code? > > > > Did you try at all to find the answer to this yourself? > > > > I ask because it took me only a few seconds to go to wikipedia and > > search for "boilerplate" find this for you: > > http://en.wikipedia.org/wiki/Boilerplate_code > > > > Tip: To successfully use forums like this one (where a lot of very > > smart people read), the more care/effort/thought you demonstrate that > > you tried to solve your issue before asking, the more likely you are > > to receive a response. I did read that article but I couldnt really figure out how to apply it to the code he was giving as an example of making it "conditional". From bouncingcats at gmail.com Fri Aug 9 01:02:52 2013 From: bouncingcats at gmail.com (David) Date: Fri, 9 Aug 2013 15:02:52 +1000 Subject: [tkinter] trouble running imported modules in main program In-Reply-To: <8039efc1-5fa1-46be-a834-46bd7981f7f5@googlegroups.com> References: <6f47e052-ec39-4894-a527-0d68653d5375@googlegroups.com> <5d739907-50ec-4691-a4a3-5c10f2b7cfe2@googlegroups.com> <8039efc1-5fa1-46be-a834-46bd7981f7f5@googlegroups.com> Message-ID: On 9 August 2013 14:28, snakeinmyboot wrote: > On Thursday, August 8, 2013 1:13:48 AM UTC-4, David wrote: >> On 8 August 2013 14:06, snakeinmyboot wrote: >> >> Did you try at all to find the answer to this yourself? >> >> I ask because it took me only a few seconds to go to wikipedia and >> search for "boilerplate" find this for you: >> >> http://en.wikipedia.org/wiki/Boilerplate_code >> >> Tip: To successfully use forums like this one (where a lot of very >> smart people read), the more care/effort/thought you demonstrate that >> you tried to solve your issue before asking, the more likely you are >> to receive a response. > > I did read that article but I couldnt really figure out how to apply it to the code he was giving as an example of making it "conditional". Did you try the research method I gave you in my previous answer? From ron.eggler at gmail.com Tue Aug 6 18:52:19 2013 From: ron.eggler at gmail.com (cerr) Date: Tue, 6 Aug 2013 15:52:19 -0700 (PDT) Subject: HTTP post with urllib2 Message-ID: <2e3c176d-6cb9-42f9-91a9-6bf0832a69cb@googlegroups.com> Hi, Why does this code: #!/usr/bin/python import urllib2 from binascii import hexlify, unhexlify host = "localhost" uri="/test.php" data ="\x48\x65\x6C\x6C\x6F\x57\x6F\x72\x6C\x64" #Hello World url="http://{0}{1}?f=test".format(host, uri) req = urllib2.Request(url, data,{'Content-Type': 'application/octet-stream'}) req.get_method = lambda: 'PUT' response = urllib2.urlopen(req, 120) retval = response.read() print "RETVAL "+retval return me this: ./post.py Traceback (most recent call last): File "./post.py", line 13, in response = urllib2.urlopen(req, 120) File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen return _opener.open(url, data, timeout) File "/usr/lib/python2.7/urllib2.py", line 398, in open req = meth(req) File "/usr/lib/python2.7/urllib2.py", line 1116, in do_request_ 'Content-length', '%d' % len(data)) I don't get it, what's going on here? Thank you! From joel.goldstick at gmail.com Tue Aug 6 19:08:34 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 6 Aug 2013 19:08:34 -0400 Subject: HTTP post with urllib2 In-Reply-To: <2e3c176d-6cb9-42f9-91a9-6bf0832a69cb@googlegroups.com> References: <2e3c176d-6cb9-42f9-91a9-6bf0832a69cb@googlegroups.com> Message-ID: On Tue, Aug 6, 2013 at 6:52 PM, cerr wrote: > Hi, > > Why does this code: > > #!/usr/bin/python > > > import urllib2 > from binascii import hexlify, unhexlify > > host = "localhost" > uri="/test.php" > data ="\x48\x65\x6C\x6C\x6F\x57\x6F\x72\x6C\x64" #Hello World > url="http://{0}{1}?f=test".format(host, uri) > req = urllib2.Request(url, data,{'Content-Type': 'application/octet-stream'}) > req.get_method = lambda: 'PUT' What does the above line do? is it the same as req.get_method = 'PUT' > response = urllib2.urlopen(req, 120) the docs say req should be a url. Is it? > retval = response.read() > print "RETVAL "+retval > > > > return me this: > > ./post.py > Traceback (most recent call last): > File "./post.py", line 13, in > response = urllib2.urlopen(req, 120) > File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen > return _opener.open(url, data, timeout) > File "/usr/lib/python2.7/urllib2.py", line 398, in open > req = meth(req) > File "/usr/lib/python2.7/urllib2.py", line 1116, in do_request_ > 'Content-length', '%d' % len(data)) > > > I don't get it, what's going on here? > > Thank you! > -- > http://mail.python.org/mailman/listinfo/python-list KInda of ducking your questions, but the requests module is a lot easier to use and understand:http://docs.python-requests.org/en/latest/ -- Joel Goldstick http://joelgoldstick.com From ron.eggler at gmail.com Tue Aug 6 19:35:26 2013 From: ron.eggler at gmail.com (cerr) Date: Tue, 6 Aug 2013 16:35:26 -0700 (PDT) Subject: HTTP post with urllib2 In-Reply-To: References: <2e3c176d-6cb9-42f9-91a9-6bf0832a69cb@googlegroups.com> Message-ID: <59b6f7db-a1e6-4326-8c87-49f2678b7db5@googlegroups.com> On Tuesday, August 6, 2013 4:08:34 PM UTC-7, Joel Goldstick wrote: > On Tue, Aug 6, 2013 at 6:52 PM, cerr wrote: > > > Hi, > > > > > > Why does this code: > > > > > > #!/usr/bin/python > > > > > > > > > import urllib2 > > > from binascii import hexlify, unhexlify > > > > > > host = "localhost" > > > uri="/test.php" > > > data ="\x48\x65\x6C\x6C\x6F\x57\x6F\x72\x6C\x64" #Hello World > > > url="http://{0}{1}?f=test".format(host, uri) > > > req = urllib2.Request(url, data,{'Content-Type': 'application/octet-stream'}) > > > req.get_method = lambda: 'PUT' > > > > What does the above line do? is it the same as req.get_method = 'PUT' I guess so, I got this from an example.... copy & paste :x > > > response = urllib2.urlopen(req, 120) > > > > the docs say req should be a url. Is it? no, it's an instance of req = urllib2.Request() > > > retval = response.read() > > > print "RETVAL "+retval > > > > > > > > > > > > return me this: > > > > > > ./post.py > > > Traceback (most recent call last): > > > File "./post.py", line 13, in > > > response = urllib2.urlopen(req, 120) > > > File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen > > > return _opener.open(url, data, timeout) > > > File "/usr/lib/python2.7/urllib2.py", line 398, in open > > > req = meth(req) > > > File "/usr/lib/python2.7/urllib2.py", line 1116, in do_request_ > > > 'Content-length', '%d' % len(data)) > > > > > > > > > I don't get it, what's going on here? > > > > > > Thank you! > > > -- > > > http://mail.python.org/mailman/listinfo/python-list > > > > KInda of ducking your questions, but the requests module is a lot > > easier to use and > > understand:http://docs.python-requests.org/en/latest/ But there must be a way to get this working with urllib alone... From joel.goldstick at gmail.com Tue Aug 6 19:48:58 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 6 Aug 2013 19:48:58 -0400 Subject: HTTP post with urllib2 In-Reply-To: <59b6f7db-a1e6-4326-8c87-49f2678b7db5@googlegroups.com> References: <2e3c176d-6cb9-42f9-91a9-6bf0832a69cb@googlegroups.com> <59b6f7db-a1e6-4326-8c87-49f2678b7db5@googlegroups.com> Message-ID: On Tue, Aug 6, 2013 at 7:35 PM, cerr wrote: > On Tuesday, August 6, 2013 4:08:34 PM UTC-7, Joel Goldstick wrote: >> On Tue, Aug 6, 2013 at 6:52 PM, cerr wrote: >> >> > Hi, >> >> > >> >> > Why does this code: >> >> > >> >> > #!/usr/bin/python >> >> > >> >> > >> >> > import urllib2 >> >> > from binascii import hexlify, unhexlify >> >> > >> >> > host = "localhost" >> >> > uri="/test.php" >> >> > data ="\x48\x65\x6C\x6C\x6F\x57\x6F\x72\x6C\x64" #Hello World >> >> > url="http://{0}{1}?f=test".format(host, uri) >> >> > req = urllib2.Request(url, data,{'Content-Type': 'application/octet-stream'}) >> >> > req.get_method = lambda: 'PUT' >> >> >> >> What does the above line do? is it the same as req.get_method = 'PUT' > > I guess so, I got this from an example.... copy & paste :x That's not a very good answer! Honest, but really. Just because you can cut and paste, doesn't mean you are learning to program. > >> >> > response = urllib2.urlopen(req, 120) >> >> >> >> the docs say req should be a url. Is it? > > no, it's an instance of req = urllib2.Request() >> >> > retval = response.read() >> >> > print "RETVAL "+retval >> >> > >> >> > >> >> > >> >> > return me this: >> >> > >> >> > ./post.py >> >> > Traceback (most recent call last): >> >> > File "./post.py", line 13, in >> >> > response = urllib2.urlopen(req, 120) >> >> > File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen >> >> > return _opener.open(url, data, timeout) >> >> > File "/usr/lib/python2.7/urllib2.py", line 398, in open >> >> > req = meth(req) >> >> > File "/usr/lib/python2.7/urllib2.py", line 1116, in do_request_ >> >> > 'Content-length', '%d' % len(data)) >> >> > >> >> > >> >> > I don't get it, what's going on here? >> >> > >> >> > Thank you! >> >> > -- >> >> > http://mail.python.org/mailman/listinfo/python-list >> >> >> >> KInda of ducking your questions, but the requests module is a lot >> >> easier to use and >> >> understand:http://docs.python-requests.org/en/latest/ > > But there must be a way to get this working with urllib alone... I'm sure there is. I'm not a pro at urllib, not even requests but when I have used it, it made a lot more sense. It got changed in python 3, so the python core group seemed not to like how the earlier modules worked. At any rate. wait a while. Someone on the list will give you more specific advice I'm sure > -- > http://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com From python at mrabarnett.plus.com Tue Aug 6 20:14:48 2013 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 07 Aug 2013 01:14:48 +0100 Subject: HTTP post with urllib2 In-Reply-To: <2e3c176d-6cb9-42f9-91a9-6bf0832a69cb@googlegroups.com> References: <2e3c176d-6cb9-42f9-91a9-6bf0832a69cb@googlegroups.com> Message-ID: <52019178.9020104@mrabarnett.plus.com> On 06/08/2013 23:52, cerr wrote: > Hi, > > Why does this code: > > #!/usr/bin/python > > > import urllib2 > from binascii import hexlify, unhexlify > > host = "localhost" > uri="/test.php" > data ="\x48\x65\x6C\x6C\x6F\x57\x6F\x72\x6C\x64" #Hello World > url="http://{0}{1}?f=test".format(host, uri) > req = urllib2.Request(url, data,{'Content-Type': 'application/octet-stream'}) > req.get_method = lambda: 'PUT' > response = urllib2.urlopen(req, 120) > retval = response.read() > print "RETVAL "+retval > > > > return me this: > > ./post.py > Traceback (most recent call last): > File "./post.py", line 13, in > response = urllib2.urlopen(req, 120) > File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen > return _opener.open(url, data, timeout) > File "/usr/lib/python2.7/urllib2.py", line 398, in open > req = meth(req) > File "/usr/lib/python2.7/urllib2.py", line 1116, in do_request_ > 'Content-length', '%d' % len(data)) > > > I don't get it, what's going on here? > The docs say """urllib2.urlopen(url[, data][, timeout])""". You're calling it as """urllib2.urlopen(req, 120)""". In other words, 'url' is req and 'data' is 120. It should be """urllib2.urlopen(req, None, 120)""". From rosuav at gmail.com Tue Aug 6 20:28:24 2013 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 7 Aug 2013 01:28:24 +0100 Subject: HTTP post with urllib2 In-Reply-To: <2e3c176d-6cb9-42f9-91a9-6bf0832a69cb@googlegroups.com> References: <2e3c176d-6cb9-42f9-91a9-6bf0832a69cb@googlegroups.com> Message-ID: On Tue, Aug 6, 2013 at 11:52 PM, cerr wrote: > ./post.py > Traceback (most recent call last): > File "./post.py", line 13, in > response = urllib2.urlopen(req, 120) > File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen > return _opener.open(url, data, timeout) > File "/usr/lib/python2.7/urllib2.py", line 398, in open > req = meth(req) > File "/usr/lib/python2.7/urllib2.py", line 1116, in do_request_ > 'Content-length', '%d' % len(data)) > > > I don't get it, what's going on here? > You've given a traceback without the actual error. MRAB happened to figure out the mistake just from what you posted, but in future, do try to copy a bit more down :) ChrisA From ron.eggler at gmail.com Wed Aug 7 12:20:58 2013 From: ron.eggler at gmail.com (cerr) Date: Wed, 7 Aug 2013 09:20:58 -0700 (PDT) Subject: HTTP post with urllib2 In-Reply-To: References: <2e3c176d-6cb9-42f9-91a9-6bf0832a69cb@googlegroups.com> Message-ID: <86b93813-50a2-41b8-b72f-efe20ac57182@googlegroups.com> On Tuesday, August 6, 2013 5:14:48 PM UTC-7, MRAB wrote: > On 06/08/2013 23:52, cerr wrote: > > > Hi, > > > > > > Why does this code: > > > > > > #!/usr/bin/python > > > > > > > > > import urllib2 > > > from binascii import hexlify, unhexlify > > > > > > host = "localhost" > > > uri="/test.php" > > > data ="\x48\x65\x6C\x6C\x6F\x57\x6F\x72\x6C\x64" #Hello World > > > url="http://{0}{1}?f=test".format(host, uri) > > > req = urllib2.Request(url, data,{'Content-Type': 'application/octet-stream'}) > > > req.get_method = lambda: 'PUT' > > > response = urllib2.urlopen(req, 120) > > > retval = response.read() > > > print "RETVAL "+retval > > > > > > > > > > > > return me this: > > > > > > ./post.py > > > Traceback (most recent call last): > > > File "./post.py", line 13, in > > > response = urllib2.urlopen(req, 120) > > > File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen > > > return _opener.open(url, data, timeout) > > > File "/usr/lib/python2.7/urllib2.py", line 398, in open > > > req = meth(req) > > > File "/usr/lib/python2.7/urllib2.py", line 1116, in do_request_ > > > 'Content-length', '%d' % len(data)) > > > > > > > > > I don't get it, what's going on here? > > > > > The docs say """urllib2.urlopen(url[, data][, timeout])""". > > > > You're calling it as """urllib2.urlopen(req, 120)""". > > > > In other words, 'url' is req and 'data' is 120. > > > > It should be """urllib2.urlopen(req, None, 120)""". Yes, great! That did it! :) Coming into the office in the morning, sitting down, changing this and get it working! Good way to start my day! :) Thanks MRAB! From ian.g.kelly at gmail.com Tue Aug 6 19:00:00 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 6 Aug 2013 17:00:00 -0600 Subject: Enum vs OrderedEnum Message-ID: Using the OrderedEnum recipe from the Python 3.4 docs, I have the following code: class Environment(OrderedEnum): gaia = 1 fertile = 2 terran, jungle, ocean, arid, steppe, desert, minimal = range(3, 10) barren, tundra, dead, inferno, toxic, radiated = range(10, 16) def is_standard(self): return Environment.terran <= self <= Environment.minimal def is_hostile(self): return Environment.barren <= self @property def growth_factor(self): if self.is_standard(): return 1.0 elif self.is_hostile(): return 0.5 elif self is Environment.fertile: return 1.5 elif self is Environment.gaia: return 2.0 else: raise AttributeError("Unknown growth_factor for %s" % self) This works, and the ordering is logical and intuitive, and I think result is quite readable. That said, really the only reason for subclassing OrderedEnum instead of Enum is to support the is_standard and is_hostile methods. In normal usage there is no reason for the members to be ordered. Can anyone suggest an alternative formulation that is as readable as the above without relying on OrderedEnum? From ethan at stoneleaf.us Tue Aug 6 19:12:49 2013 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 06 Aug 2013 16:12:49 -0700 Subject: Enum vs OrderedEnum In-Reply-To: References: Message-ID: <520182F1.2060709@stoneleaf.us> On 08/06/2013 04:00 PM, Ian Kelly wrote: Use the .value attribute instead. You could also substitute self for Environment. > class Environment(Enum): > > gaia = 1 > fertile = 2 > terran, jungle, ocean, arid, steppe, desert, minimal = range(3, 10) > barren, tundra, dead, inferno, toxic, radiated = range(10, 16) > > def is_standard(self): > return self.terran.value <= self.value <= self.minimal.value > > def is_hostile(self): > return self.barren.value <= self.value > > @property > def growth_factor(self): > if self.is_standard(): > return 1.0 > elif self.is_hostile(): > return 0.5 > elif self is self.fertile: > return 1.5 > elif self is self.gaia: > return 2.0 > else: > raise AttributeError("Unknown growth_factor for %s" % self) -- ~Ethan~ From ian.g.kelly at gmail.com Tue Aug 6 19:46:39 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 6 Aug 2013 17:46:39 -0600 Subject: Enum vs OrderedEnum In-Reply-To: <520182F1.2060709@stoneleaf.us> References: <520182F1.2060709@stoneleaf.us> Message-ID: On Aug 6, 2013 5:15 PM, "Ethan Furman" wrote: > > Use the .value attribute instead. You could also substitute self for Environment. It feels more natural and readable to compare the enum instances rather than their value attributes. If I am ordering the values then that seems to imply that the enumeration itself is ordered. So I guess my question is better stated: is there a better way to do this that doesn't involve ordered comparisons at all? -------------- next part -------------- An HTML attachment was scrubbed... URL: From ethan at stoneleaf.us Tue Aug 6 20:33:16 2013 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 06 Aug 2013 17:33:16 -0700 Subject: Enum vs OrderedEnum In-Reply-To: References: <520182F1.2060709@stoneleaf.us> Message-ID: <520195CC.7050506@stoneleaf.us> On 08/06/2013 04:46 PM, Ian Kelly wrote: > > On Aug 6, 2013 5:15 PM, "Ethan Furman" > wrote: >> >> Use the .value attribute instead. You could also substitute self for Environment. > > It feels more natural and readable to compare the enum instances rather than their value attributes. If I am ordering > the values then that seems to imply that the enumeration itself is ordered. So I guess my question is better stated: is > there a better way to do this that doesn't involve ordered comparisons at all? If the only time you are using their Ordered nature is inside the class, it's an implementation detail. As such, using the value seems fine to me. There are other options: - using sets, as Rhodri pointed out (extra work is needed, though, because a set would want to turn into an Enum member) - using AutoNumber instead of Ordered, and specifing the growth factor directly AutoNumber ---------- class AutoNumber(Enum): "ignore any arguments, __init__ can have them" def __new__(cls, *args): value = len(cls.__members__) + 1 obj = object.__new__(cls) obj._value_ = value return obj class Environment(AutoNumber): gaia = 2.0 fertile = 1.5 terran = 1.0 jungle = 1.0 ocean = 1.0 arid = 1.0 steppe = 1.0 desert = 1.0 minimal = 1.0 barren = 0.5 tundra = 0.5 dead = 0.5 inferno = 0.5 toxic = 0.5 radiated = 0.5 def __init__(self, growth_factor): self._growth_factor = growth_factor @property def growth_factor(self): return self._growth_factor This works because each Enum member gets its own integer value (1 - 15) in __new__, plus a growth factor that is stored by __init__. Whether you think this is better I have no idea. ;) -- ~Ethan~ From ian.g.kelly at gmail.com Wed Aug 7 03:35:28 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 7 Aug 2013 01:35:28 -0600 Subject: Enum vs OrderedEnum In-Reply-To: <520195CC.7050506@stoneleaf.us> References: <520182F1.2060709@stoneleaf.us> <520195CC.7050506@stoneleaf.us> Message-ID: On Tue, Aug 6, 2013 at 6:33 PM, Ethan Furman wrote: > class Environment(AutoNumber): > > gaia = 2.0 > fertile = 1.5 > terran = 1.0 > jungle = 1.0 > ocean = 1.0 > arid = 1.0 > steppe = 1.0 > desert = 1.0 > minimal = 1.0 > barren = 0.5 > tundra = 0.5 > dead = 0.5 > inferno = 0.5 > toxic = 0.5 > radiated = 0.5 > > def __init__(self, growth_factor): > self._growth_factor = growth_factor > > @property > def growth_factor(self): > return self._growth_factor > > This works because each Enum member gets its own integer value (1 - 15) in > __new__, plus a growth factor that is stored by __init__. Whether you think > this is better I have no idea. ;) Black magic, I like it. I think I'd write it like this, however: class Environment(AutoNumber): gaia = 2.0 fertile = 1.5 terran = jungle = ocean = arid = steppe = desert = minimal = 1.0 barren = tundra = dead = inferno = toxic = radiated = 0.5 def __init__(self, growth_factor): self.growth_factor = growth_factor From rhodri at wildebst.demon.co.uk Tue Aug 6 19:55:49 2013 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Wed, 07 Aug 2013 00:55:49 +0100 Subject: Enum vs OrderedEnum References: <520182F1.2060709@stoneleaf.us> Message-ID: On Wed, 07 Aug 2013 00:46:39 +0100, Ian Kelly wrote: > On Aug 6, 2013 5:15 PM, "Ethan Furman" wrote: >> >> Use the .value attribute instead. You could also substitute self for > Environment. > > It feels more natural and readable to compare the enum instances rather > than their value attributes. If I am ordering the values then that seems > to > imply that the enumeration itself is ordered. So I guess my question is > better stated: is there a better way to do this that doesn't involve > ordered comparisons at all? You could create sets (frozensets?) of standard and hostile environments as class variables. -- Rhodri James *-* Wildebeest Herder to the Masses From ben+python at benfinney.id.au Tue Aug 6 21:55:35 2013 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 07 Aug 2013 11:55:35 +1000 Subject: Enum vs OrderedEnum References: Message-ID: <7wbo5ab6y0.fsf@benfinney.id.au> Ian Kelly writes: > class Environment(OrderedEnum): I have nothing to add regarding the Python code, but I wanted to make a language correction: > gaia = 1 > fertile = 2 > terran, jungle, ocean, arid, steppe, desert, minimal = range(3, 10) > barren, tundra, dead, inferno, toxic, radiated = range(10, 16) Terrain that is ?radiated? would be terrain that has some kind of spokes spreading out from its centre. I think you mean ?irradiated?. Hope the game goes well :-) -- \ ?Saying that Java is nice because it works on all OSes is like | `\ saying that anal sex is nice because it works on all genders? | _o__) ?http://bash.org/ | Ben Finney From ian.g.kelly at gmail.com Wed Aug 7 03:36:25 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 7 Aug 2013 01:36:25 -0600 Subject: Enum vs OrderedEnum In-Reply-To: <7wbo5ab6y0.fsf@benfinney.id.au> References: <7wbo5ab6y0.fsf@benfinney.id.au> Message-ID: On Tue, Aug 6, 2013 at 7:55 PM, Ben Finney wrote: > Ian Kelly writes: > Terrain that is ?radiated? would be terrain that has some kind of spokes > spreading out from its centre. I think you mean ?irradiated?. > > Hope the game goes well :-) It's actually a reimplementation of a game from 1993, so I'm somewhat stuck with the terminology. From neilc at norwich.edu Wed Aug 7 08:32:52 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 7 Aug 2013 12:32:52 GMT Subject: Enum vs OrderedEnum References: <7wbo5ab6y0.fsf@benfinney.id.au> Message-ID: On 2013-08-07, Ian Kelly wrote: > On Tue, Aug 6, 2013 at 7:55 PM, Ben Finney wrote: >> Ian Kelly writes: >> Terrain that is ?radiated? would be terrain that has some kind of spokes >> spreading out from its centre. I think you mean ?irradiated?. >> >> Hope the game goes well :-) > > It's actually a reimplementation of a game from 1993, so I'm > somewhat stuck with the terminology. I haven't played MOO1 for at least a month. :) -- Neil Cerutti From krismesenbrink at gmail.com Tue Aug 6 23:38:24 2013 From: krismesenbrink at gmail.com (krismesenbrink at gmail.com) Date: Tue, 6 Aug 2013 20:38:24 -0700 (PDT) Subject: new to While statements Message-ID: <69a10630-98dc-4a31-89ef-2770b3c1fd70@googlegroups.com> import random def room (): hp = 10 while hp != 0: random_Number = random.randint(1, 2) #asking if you want to roll/play des = input("Would you like to roll the die?") if des == ("y"): print ("Rolling the die...") print ("You rolled a...") print (random_Number) #a "monster" appers if you roll a 1"" if random_Number == 1: monster_hp = 10 print ("Oh no a Monsster!") print ("would you like to attack?") answer = input("y or n?") if answer == "y": #if you choose to battle this is what happens while monster_hp >=0: print ("you attack") damage_done = random.randint(0,5) print ("You do ",damage_done,"damage") monster_hp = monster_hp - damage_done print ("the monster takes a hit, it has ", monster_hp, "left") elif answer == ("n"): print ("you run away") else: print ("You and the monster just stare at one another") else: print ("You find nothing") # if you decisde to not play it will kill you elif des == ("no"): hp = 0 print ("Maybe next time!") else: print ("please enter yes or no") room() this is the code i'm making. as the subject says im new to while statements. i am having problems with the monster battle part, it takes health away from the "monster" but as soon as it gets to 0 or less i'd like the code to start from the top and ask you to roll the die again. any help on this would be greatly appreciative From mikelhamer at gmail.com Wed Aug 7 00:09:50 2013 From: mikelhamer at gmail.com (snakeinmyboot) Date: Tue, 6 Aug 2013 21:09:50 -0700 (PDT) Subject: new to While statements In-Reply-To: <69a10630-98dc-4a31-89ef-2770b3c1fd70@googlegroups.com> References: <69a10630-98dc-4a31-89ef-2770b3c1fd70@googlegroups.com> Message-ID: Hey there, cool idea you've got going on here! As far as I can tell though...what you want to happen, is indeed actually happening. Did you mean something else? Everytime I run the script and defeat a monster, it asks me if I want to roll the dice again. From krismesenbrink at gmail.com Wed Aug 7 00:13:01 2013 From: krismesenbrink at gmail.com (krismesenbrink at gmail.com) Date: Tue, 6 Aug 2013 21:13:01 -0700 (PDT) Subject: new to While statements In-Reply-To: References: <69a10630-98dc-4a31-89ef-2770b3c1fd70@googlegroups.com> Message-ID: <8b57b60b-074a-4060-b53e-174278b7d9b8@googlegroups.com> and it seems you are right about that, i don't know what was wrong with my IDE before, i closed it and opened it up again,seemed to fix the problem. thanks for taking the time to look at it anyway! From mikelhamer at gmail.com Wed Aug 7 00:17:32 2013 From: mikelhamer at gmail.com (snakeinmyboot) Date: Tue, 6 Aug 2013 21:17:32 -0700 (PDT) Subject: new to While statements In-Reply-To: <69a10630-98dc-4a31-89ef-2770b3c1fd70@googlegroups.com> References: <69a10630-98dc-4a31-89ef-2770b3c1fd70@googlegroups.com> Message-ID: yea no problem. heres a little tip though so you atleast get something out of the post. monster_hp = monster_hp - damage_done can be simplified by writing monster_hp -= damage_done the -= means equal to whatever is on the left, minus whatevers on the right. this can be done with addition, multiplication, division, etc etc etc. From joshua at landau.ws Wed Aug 7 00:37:39 2013 From: joshua at landau.ws (Joshua Landau) Date: Wed, 7 Aug 2013 05:37:39 +0100 Subject: new to While statements In-Reply-To: <69a10630-98dc-4a31-89ef-2770b3c1fd70@googlegroups.com> References: <69a10630-98dc-4a31-89ef-2770b3c1fd70@googlegroups.com> Message-ID: On 7 August 2013 04:38, wrote: > import random > > > > def room (): No need for the space after "room". > hp = 10 > while hp != 0: "while hp:" would be idiomatic, but you probably want "while hp > 0" if you allow negatives. > random_Number = random.randint(1, 2) You meant "random_number", I'm sure ;). > #asking if you want to roll/play Don't comment lines unless the comment says more than the line. > des = input("Would you like to roll the die?") > > if des == ("y"): You don't need the brackets. Try to avoid redundant brackets where they don't clarify anything. Additionally, you should probably be more lenient and consistent with answers. Maybe something like: if des.casefold() in ("y", "yes"): and its counterpart if des.casefold() in ("n", "no"): > print ("Rolling the die...") > print ("You rolled a...") > print (random_Number) You should avoid spaces between the function and the brackets. > #a "monster" appers if you roll a 1"" > if random_Number == 1: > monster_hp = 10 > print ("Oh no a Monsster!") > print ("would you like to attack?") > answer = input("y or n?") > if answer == "y": > #if you choose to battle this is what happens > while monster_hp >=0: Think carefully here -- do you want to have a round when monster_hp is greater *or equal* to 0? Maybe you would rather only if it's alive (hp > 0)? > print ("you attack") > damage_done = random.randint(0,5) > print ("You do ",damage_done,"damage") > monster_hp = monster_hp - damage_done monster_hp -= damage_done > print ("the monster takes a hit, it has ", monster_hp, > "left") > > elif answer == ("n"): > print ("you run away") > > else: > print ("You and the monster just stare at one another") > else: > print ("You find nothing") > # if you decisde to not play it will kill you > elif des == ("no"): > hp = 0 Gah! You just kill him off? Are you sure you don't want to use a "break" or "return" to quit the loop or function? > print ("Maybe next time!") > else: > print ("please enter yes or no") > > room() As a whole, +1 for the good naming, decent attempt at spacing and a mostly-sane layout. > this is the code i'm making. as the subject says im new to while statements. i am having problems with the monster battle part, it takes health away from the "monster" but as soon as it gets to 0 or less i'd like the code to start from the top and ask you to roll the die again. any help on this would be greatly appreciative PS: I'd use a state machine for times like this, eg. import random def prompt(action, retort_invalid=None): while "asking for reponse": response = input("Would you like to {}?".format(action)).casefold() if response in ["y", "yes"]: return True elif response in ["n", "no"]: return False else: if retort_invalid is not None: print(retort_invalid) def room(): state = "wandering" while "adventuring": if state == "idle": if prompt("wander around") print("Rolling the die...") roll = random.randint(1, 2) print("You rolled a {}.".format(roll)) if roll == 1: monster_hp = 10 print("Oh no! A Monster!") state = "facing monster" else: print("You find nothing") else: print("Maybe next time!") return elif state == "facing monster": will_attack = if prompt("attack", "You and the monster just stare at one another"): state = "attacking" else: print("you run away") state = "idle" elif state == "attacking": damage_done = random.randint(0, 5) monster_hp -= damage_done print("You attack to do", damage_done, "damage") print("The monster takes a hit, it has", monster_hp, "hp left") if monster_hp <= 0: state = "idle" room() The advantage of this is everything sits at the same level so you know whether you've covered all options and permutations of options. It's only longer because I made the "prompt" function which is more rigorous than the current method. From vito.detullio at gmail.com Wed Aug 7 01:20:28 2013 From: vito.detullio at gmail.com (Vito De Tullio) Date: Wed, 07 Aug 2013 07:20:28 +0200 Subject: new to While statements References: <69a10630-98dc-4a31-89ef-2770b3c1fd70@googlegroups.com> Message-ID: Joshua Landau wrote: > while "asking for reponse": > while "adventuring": that's a funny way to say `while True:`... -- By ZeD From dan at tombstonezero.net Wed Aug 7 01:35:01 2013 From: dan at tombstonezero.net (Dan Sommers) Date: Wed, 07 Aug 2013 05:35:01 GMT Subject: new to While statements References: <69a10630-98dc-4a31-89ef-2770b3c1fd70@googlegroups.com> < CAN1F8qVtj3coXU_Q-VeJfvSmx3=C6q4QoJx5nOq0AUy_2Hb4QQ@mail.gmail.com> < mailman.296.1375852843.1251.python-list@python.org> Message-ID: <90lMt.30102$907.25512@fx17.iad> On Wed, 07 Aug 2013 07:20:28 +0200, Vito De Tullio wrote: > Joshua Landau wrote: > > >> while "asking for reponse": > >> while "adventuring": > > that's a funny way to say `while True:`... Funny, perhaps, the first time you see it, but way more informative than the other way to the next one who comes along and reads it. -- Dan From mikelhamer at gmail.com Wed Aug 7 01:42:42 2013 From: mikelhamer at gmail.com (snakeinmyboot) Date: Tue, 6 Aug 2013 22:42:42 -0700 (PDT) Subject: new to While statements In-Reply-To: <90lMt.30102$907.25512@fx17.iad> References: <69a10630-98dc-4a31-89ef-2770b3c1fd70@googlegroups.com> <90lMt.30102$907.25512@fx17.iad> Message-ID: <5159c8f0-9053-425d-91d0-798cb429303e@googlegroups.com> I wish I understood half of what you posted Dan. Time to hit the books From dan at tombstonezero.net Wed Aug 7 01:52:11 2013 From: dan at tombstonezero.net (Dan Sommers) Date: Wed, 07 Aug 2013 05:52:11 GMT Subject: new to While statements References: <69a10630-98dc-4a31-89ef-2770b3c1fd70@googlegroups.com> < CAN1F8qVtj3coXU_Q-VeJfvSmx3=C6q4QoJx5nOq0AUy_2Hb4QQ@mail.gmail.com> < mailman.296.1375852843.1251.python-list@python.org> <90lMt.30102$907.25512@ fx17.iad> <5159c8f0-9053-425d-91d0-798cb429303e@googlegroups.com> Message-ID: On Tue, 06 Aug 2013 22:42:42 -0700, snakeinmyboot wrote: > I wish I understood half of what you posted Dan. Time to hit the books So think about the "while" statement: it takes an expression (the part before the colon) and a suite (the part after the colon and before the next statement at the same indent level as the while statement), evaluates the truthiness of that expression, and executes its suite if the expression is true. So "while True" evaluates True, discovers that it's true, and executes its suite. So "while 'some string'" evaluates 'some string,' discovers that it's true, and executes its suite. The language sees both literals the same way, but one of them is much more useful to a human reader. Statements like "while True," "while x < 0," and "while object.method() != False" are more conventional, but really no different from "while y" and "while 'a description of what this loop does.'" From krismesenbrink at gmail.com Wed Aug 7 02:26:15 2013 From: krismesenbrink at gmail.com (krismesenbrink at gmail.com) Date: Tue, 6 Aug 2013 23:26:15 -0700 (PDT) Subject: new to While statements In-Reply-To: References: <69a10630-98dc-4a31-89ef-2770b3c1fd70@googlegroups.com> <90lMt.30102$907.25512@> <5159c8f0-9053-425d-91d0-798cb429303e@googlegroups.com> Message-ID: <182eaf32-4148-44fd-a3d2-39f283cc4c23@googlegroups.com> wow everyone thanks for the feed back! i'll have to rewrite this with everything you guys taught me. this makes ALOT more sense. :D From vito.detullio at gmail.com Wed Aug 7 14:05:13 2013 From: vito.detullio at gmail.com (Vito De Tullio) Date: Wed, 07 Aug 2013 20:05:13 +0200 Subject: new to While statements References: <69a10630-98dc-4a31-89ef-2770b3c1fd70@googlegroups.com> <90lMt.30102$907.25512@fx17.iad> Message-ID: Dan Sommers wrote: >>> while "asking for reponse": >> >>> while "adventuring": >> >> that's a funny way to say `while True:`... > > Funny, perhaps, the first time you see it, but way more informative than > the other way to the next one who comes along and reads it. While I understand that it's syntactically and semantically correct, my nose still doesn't like it. It's not that's just not common... I just think it's a mishmash... it's not a "rule" thing, more a "feeling wrong" one. Maybe it's better if I try to explain in another way... My first instinct about it it's to think why the author choose this way to talk to the reader. This message it's clearly meant to be read by another programmer, not by the user. But in my mind it should be a comment. In my mind code explain "what" is happening, comment say "why". (Sometimes talking to my colleagues we say comment is a way to ask to forgiveness for a particular obscure code, as in "sorry, I needed to flip-this and flop-that for such-and-such reason") Following this reasoning, I will found more readable something like # asking for response while True: ... that while 'asking for response': ... because in the latter case the "why" and the "how" are mixed. It's like you're talking with the interpreter, but the message is for the programmer.. I hope I explained myself... -- By ZeD From davea at davea.name Wed Aug 7 21:31:09 2013 From: davea at davea.name (Dave Angel) Date: Thu, 8 Aug 2013 01:31:09 +0000 (UTC) Subject: new to While statements References: <69a10630-98dc-4a31-89ef-2770b3c1fd70@googlegroups.com> <90lMt.30102$907.25512@fx17.iad> Message-ID: Vito De Tullio wrote: > Dan Sommers wrote: > >>>> while "asking for reponse": >>> >>>> while "adventuring": >>> >>> that's a funny way to say `while True:`... >> >> Funny, perhaps, the first time you see it, but way more informative than >> the other way to the next one who comes along and reads it. > > While I understand that it's syntactically and semantically correct, my nose > still doesn't like it. > Neither does mine. There's no need for a trick here. "while True" reads better, and a comment (on the same line, preferably) can explain what the loop is for. -- DaveA From orgnut at yahoo.com Wed Aug 7 23:21:07 2013 From: orgnut at yahoo.com (Larry Hudson) Date: Wed, 07 Aug 2013 20:21:07 -0700 Subject: new to While statements In-Reply-To: <69a10630-98dc-4a31-89ef-2770b3c1fd70@googlegroups.com> References: <69a10630-98dc-4a31-89ef-2770b3c1fd70@googlegroups.com> Message-ID: On 08/06/2013 08:38 PM, krismesenbrink at gmail.com wrote: > import random > > > > def room (): > > hp = 10 > while hp != 0: > > random_Number = random.randint(1, 2) > > #asking if you want to roll/play > des = input("Would you like to roll the die?") > One very trivial comment... Add one or two spaces to the end of your prompt string, (I like to use two). No biggie, but it just looks nicer if the answer doesn't butt up directly against the end of the prompt. -=- Larry -=- From eschneider92 at comcast.net Wed Aug 7 04:17:21 2013 From: eschneider92 at comcast.net (eschneider92 at comcast.net) Date: Wed, 7 Aug 2013 01:17:21 -0700 (PDT) Subject: beginner question (True False help) Message-ID: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> I'm trying to create an option for the program to repeat if the user types 'y' or 'yes', using true and false values, or otherwise end the program. If anyone could explain to me how to get this code working, I'd appreciate it. letters='abcdefghijklmn' batman=True def thingy(): print('type letter from a to n') typedletter=input() if typedletter in letters: print('yes') else: print('no') def repeat(): print('go again?') goagain=input() if goagain in ('y', 'yes'): print('ok') else: print('goodbye') batman=False while batman==True: thingy() repeat() print('this is the end') From joshua at landau.ws Wed Aug 7 04:42:43 2013 From: joshua at landau.ws (Joshua Landau) Date: Wed, 7 Aug 2013 09:42:43 +0100 Subject: beginner question (True False help) In-Reply-To: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> References: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> Message-ID: On 7 August 2013 09:17, wrote: > I'm trying to create an option for the program to repeat if the user types 'y' or 'yes', using true and false values, or otherwise end the program. If anyone could explain to me how to get this code working, I'd appreciate it. Always tell people what in particular you don't understand (*ducks*) because it wasn't obvious what part of the problem you were unable to fulfil. > letters='abcdefghijklmn' > batman=True > def thingy(): > print('type letter from a to n') > typedletter=input() > if typedletter in letters: > print('yes') > else: > print('no') > def repeat(): > print('go again?') > goagain=input() > if goagain in ('y', 'yes'): > print('ok') > else: > print('goodbye') > batman=False This doesn't do what you want it to. x = "old thing" def change_x(): x = "new thing" change_x() print(x) # Not changed! The solution is to put "global x" at the start of the function. > while batman==True: > thingy() > repeat() > print('this is the end') Note that this isn't actually a good way to do it. Imagine you had several hundred function -- would you really want to have an equivalent number of names floating around that you have to look after? The solution is to make the functions simply return values: x = "old thing" def return_thing(): x = "new thing" return "new thing" # You can do this in one line x = return_thing() # Get the value from the function and change x with it Does this make sense? From eschneider92 at comcast.net Wed Aug 7 16:59:50 2013 From: eschneider92 at comcast.net (eschneider92 at comcast.net) Date: Wed, 7 Aug 2013 13:59:50 -0700 (PDT) Subject: beginner question (True False help) In-Reply-To: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> References: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> Message-ID: <7c627d74-a5c5-4356-9a04-9608de0006ba@googlegroups.com> What I wanted to happen is when the user typed something other than 'y' or 'yes' after being asked 'go again?', the batman==False line would cause the program to stop asking anything and say 'this is the end'. Instead, what is happening is that the program just keeps going. I figured that after defining the function (thingy(), repeat()), that the while statement would repeat until the 'go again' user input was something other than 'y' or 'yes', and the batman==False part of the repeat() function would cause the 'while batman==True' part to become False and end. You probably answered my question and I'm too dumb to see it, but that's a slight elaboration on my problem. From davea at davea.name Wed Aug 7 21:18:50 2013 From: davea at davea.name (Dave Angel) Date: Thu, 8 Aug 2013 01:18:50 +0000 (UTC) Subject: beginner question (True False help) References: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> <7c627d74-a5c5-4356-9a04-9608de0006ba@googlegroups.com> Message-ID: eschneider92 at comcast.net wrote: > What I wanted to happen is when the user typed something other than 'y' or 'yes' after being asked 'go again?', the batman==False line would cause the program to stop asking anything and say 'this is the end'. Instead, what is happening is that the program just keeps going. I figured that after defining the function (thingy(), repeat()), that the while statement would repeat until the 'go again' user input was something other than 'y' or 'yes', and the batman==False part of the repeat() function would cause the 'while batman==True' part to become False and end. You probably answered my question and I'm too dumb to see it, but that's a slight elaboration on my problem. When you assign a variable inside a function, it has no effect on a global variable with similar name. In order to make it change the global, you'd have needed the global declaration. Try this: var = 42 def myfunc(): var = 90 print "before:", var myfunc() print "after:", var Now, change the function, by adding a declaration: def myfunc(): global var var = 90 and the result will change. -- DaveA From orgnut at yahoo.com Wed Aug 7 22:49:55 2013 From: orgnut at yahoo.com (Larry Hudson) Date: Wed, 07 Aug 2013 19:49:55 -0700 Subject: beginner question (True False help) In-Reply-To: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> References: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> Message-ID: <_4udneujNNfJmp7PnZ2dnUVZ_gOdnZ2d@giganews.com> On 08/07/2013 01:17 AM, eschneider92 at comcast.net wrote: > I'm trying to create an option for the program to repeat if the user types 'y' or 'yes', using true and false values, or otherwise end the program. If anyone could explain to me how to get this code working, I'd appreciate it. > > letters='abcdefghijklmn' > batman=True > def thingy(): > print('type letter from a to n') > typedletter=input() > if typedletter in letters: > print('yes') > else: > print('no') > def repeat(): > print('go again?') > goagain=input() > if goagain in ('y', 'yes'): > print('ok') > else: > print('goodbye') > batman=False > while batman==True: > thingy() > repeat() > print('this is the end') > You've already received answers to this, primarily pointing out that batman needs to be declared as global in your repeat() function. Global variables can be read from inside a function without declaring them as such, but if you need to change them, they MUST be declared as globals, otherwise it will merely create an independant local variable with the same name. A second possibility is to do away with batman in the repeat() function, and instead return True in the 'yes' clause and False in the else clause. Then in your while loop, change the repeat() line to: batman = repeat() A third version (which I would prefer) is to do away with batman altogether (maybe the Penguin got 'im??) ;-) Use the True/False version of repeat() and change the while loop to: while True: thingy() if not repeat(): break And finally unindent your final print() line. The way you have it will print 'The end' every time in your loop. Not what you want, I'm sure. -=- Larry -=- From wxjmfauth at gmail.com Thu Aug 8 02:20:09 2013 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Wed, 7 Aug 2013 23:20:09 -0700 (PDT) Subject: beginner question (True False help) In-Reply-To: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> References: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> Message-ID: <72cd8537-7494-48d6-a58a-a50a319d8e2a@googlegroups.com> Le mercredi 7 ao?t 2013 10:17:21 UTC+2, eschne... at comcast.net a ?crit?: > I'm trying to create an option for the program to repeat if the user types 'y' or 'yes', using true and false values, or otherwise end the program. If anyone could explain to me how to get this code working, I'd appreciate it. > > > > letters='abcdefghijklmn' > > batman=True > > def thingy(): > > print('type letter from a to n') > > typedletter=input() > > if typedletter in letters: > > print('yes') > > else: > > print('no') > > def repeat(): > > print('go again?') > > goagain=input() > > if goagain in ('y', 'yes'): > > print('ok') > > else: > > print('goodbye') > > batman=False > > while batman==True: > > thingy() > > repeat() > > print('this is the end') ----------- Your loop is not very well organized. It should be at the same time the "loop" and the "condition tester". Compare your code with this and note the missing and unnecessary "batman": >>> def z(): ... letters = 'abc' ... c = input('letter: ') ... while c in letters: ... print('do stuff') ... c = input('letter: ') ... print('end, fin, Schluss') ... >>> z() letter: a do stuff letter: b do stuff letter: b do stuff letter: c do stuff letter: n end, fin, Schluss >>> >>> z() letter: q end, fin, Schluss Variant It is common to use a infinite loop and to break it in order to end the job. >>> def z2(): ... letters = 'abc' ... while True: ... c = input('letter: ') ... if c not in letters: ... print('end, fin, Schluss') ... break ... else: ... print('do stuff') ... >>> z2() letter: a do stuff letter: b do stuff letter: a do stuff letter: q end, fin, Schluss jmf From rosuav at gmail.com Thu Aug 8 07:41:50 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 8 Aug 2013 12:41:50 +0100 Subject: beginner question (True False help) In-Reply-To: <72cd8537-7494-48d6-a58a-a50a319d8e2a@googlegroups.com> References: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> <72cd8537-7494-48d6-a58a-a50a319d8e2a@googlegroups.com> Message-ID: On Thu, Aug 8, 2013 at 7:20 AM, wrote: >>>> def z2(): > ... letters = 'abc' > ... while True: > ... c = input('letter: ') > ... if c not in letters: > ... print('end, fin, Schluss') > ... break > ... else: > ... print('do stuff') Minor quibble: I don't like having a hard exit followed by an "else". If the "if" branch will unconditionally quit the loop (with a break, here, but could also be a return, a thrown exception, etc etc), I would prefer to see the "else" removed and its code unindented one level. Maybe this is just personal preference, though, learned from assembly language programming where a "block if" looks something like this: ; if x == y: CMP x,y JNZ .else ; Code for "x == y" JMP .endif .else: ; Code for "else" .endif Putting an unconditional departure in the "x == y" branch makes the "JMP .endif" redundant. ChrisA From tjreedy at udel.edu Thu Aug 8 16:29:00 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 08 Aug 2013 16:29:00 -0400 Subject: beginner question (True False help) In-Reply-To: References: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> <72cd8537-7494-48d6-a58a-a50a319d8e2a@googlegroups.com> Message-ID: On 8/8/2013 7:41 AM, Chris Angelico wrote: > On Thu, Aug 8, 2013 at 7:20 AM, wrote: >>>>> def z2(): >> ... letters = 'abc' >> ... while True: >> ... c = input('letter: ') >> ... if c not in letters: >> ... print('end, fin, Schluss') >> ... break >> ... else: >> ... print('do stuff') > > > Minor quibble: I don't like having a hard exit followed by an "else". Whereas I tend to prefer to have the two alternatives cleanly marked as alternatives by both being indented the same. Many alternatives are not so trivial as the above. I remember reading one snippet in the CPython codebase where the 'else' was omitted and the if clause subdivided into about three paths. It took at least a minute to determine that all paths terminated in such a way that there really was an inplied else. How much easier it would have been to read the code if the author had explicitly types the 'else'. > If the "if" branch will unconditionally quit the loop (with a break, > here, but could also be a return, a thrown exception, etc etc), I > would prefer to see the "else" removed and its code unindented one > level. Maybe this is just personal preference, though, learned from > assembly language programming where a "block if" looks something like > this: > > ; if x == y: > CMP x,y > JNZ .else > ; Code for "x == y" > JMP .endif > .else: > ; Code for "else" > .endif > > Putting an unconditional departure in the "x == y" branch makes the > "JMP .endif" redundant. Python is not assembly ;-). 3.3 effectively ignores the extraneous 'else:'. Either way, if the condition is false, control jumps to the second print. For what little it matters, the bytecode is the same length. def f(): while True: if a: b = 1 break else: b = 2 >>> dis(f) 2 0 SETUP_LOOP 25 (to 28) 3 >> 3 LOAD_GLOBAL 0 (a) 6 POP_JUMP_IF_FALSE 19 4 9 LOAD_CONST 1 (1) 12 STORE_FAST 0 (b) 5 15 BREAK_LOOP 16 JUMP_ABSOLUTE 3 7 >> 19 LOAD_CONST 2 (2) 22 STORE_FAST 0 (b) 25 JUMP_ABSOLUTE 3 >> 28 LOAD_CONST 0 (None) 31 RETURN_VALUE def f(): while True: if a: b = 1 break b = 2 >>> dis(f) 2 0 SETUP_LOOP 25 (to 28) 3 >> 3 LOAD_GLOBAL 0 (a) 6 POP_JUMP_IF_FALSE 19 4 9 LOAD_CONST 1 (1) 12 STORE_FAST 0 (b) 5 15 BREAK_LOOP 16 JUMP_FORWARD 0 (to 19) 6 >> 19 LOAD_CONST 2 (2) 22 STORE_FAST 0 (b) 25 JUMP_ABSOLUTE 3 >> 28 LOAD_CONST 0 (None) 31 RETURN_VALUE -- Terry Jan Reedy From wxjmfauth at gmail.com Fri Aug 9 04:05:33 2013 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Fri, 9 Aug 2013 01:05:33 -0700 (PDT) Subject: beginner question (True False help) In-Reply-To: References: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> <72cd8537-7494-48d6-a58a-a50a319d8e2a@googlegroups.com> Message-ID: <1db0eff5-aa0c-40f0-8478-fddf2b5224a6@googlegroups.com> Le jeudi 8 ao?t 2013 22:29:00 UTC+2, Terry Reedy a ?crit?: > On 8/8/2013 7:41 AM, Chris Angelico wrote: > > > On Thu, Aug 8, 2013 at 7:20 AM, wrote: > > >>>>> def z2(): > > >> ... letters = 'abc' > > >> ... while True: > > >> ... c = input('letter: ') > > >> ... if c not in letters: > > >> ... print('end, fin, Schluss') > > >> ... break > > >> ... else: > > >> ... print('do stuff') > > > > > > > > > Minor quibble: I don't like having a hard exit followed by an "else". > > > > Whereas I tend to prefer to have the two alternatives cleanly marked as > > alternatives by both being indented the same. > > > > Many alternatives are not so trivial as the above. I remember reading > > one snippet in the CPython codebase where the 'else' was omitted and the > > if clause subdivided into about three paths. It took at least a minute > > to determine that all paths terminated in such a way that there really > > was an inplied else. How much easier it would have been to read the code > > if the author had explicitly types the 'else'. > > > > > If the "if" branch will unconditionally quit the loop (with a break, > > > here, but could also be a return, a thrown exception, etc etc), I > > > would prefer to see the "else" removed and its code unindented one > > > level. Maybe this is just personal preference, though, learned from > > > assembly language programming where a "block if" looks something like > > > this: > > > > > > ; if x == y: > > > CMP x,y > > > JNZ .else > > > ; Code for "x == y" > > > JMP .endif > > > .else: > > > ; Code for "else" > > > .endif > > > > > > Putting an unconditional departure in the "x == y" branch makes the > > > "JMP .endif" redundant. > > > > Python is not assembly ;-). 3.3 effectively ignores the extraneous > > 'else:'. Either way, if the condition is false, control jumps to the > > second print. For what little it matters, the bytecode is the same length. > > > > def f(): > > while True: > > if a: > > b = 1 > > break > > else: > > b = 2 > > > > >>> dis(f) > > 2 0 SETUP_LOOP 25 (to 28) > > > > 3 >> 3 LOAD_GLOBAL 0 (a) > > 6 POP_JUMP_IF_FALSE 19 > > > > 4 9 LOAD_CONST 1 (1) > > 12 STORE_FAST 0 (b) > > > > 5 15 BREAK_LOOP > > 16 JUMP_ABSOLUTE 3 > > > > 7 >> 19 LOAD_CONST 2 (2) > > 22 STORE_FAST 0 (b) > > 25 JUMP_ABSOLUTE 3 > > >> 28 LOAD_CONST 0 (None) > > 31 RETURN_VALUE > > > > def f(): > > while True: > > if a: > > b = 1 > > break > > b = 2 > > > > >>> dis(f) > > 2 0 SETUP_LOOP 25 (to 28) > > > > 3 >> 3 LOAD_GLOBAL 0 (a) > > 6 POP_JUMP_IF_FALSE 19 > > > > 4 9 LOAD_CONST 1 (1) > > 12 STORE_FAST 0 (b) > > > > 5 15 BREAK_LOOP > > 16 JUMP_FORWARD 0 (to 19) > > > > 6 >> 19 LOAD_CONST 2 (2) > > 22 STORE_FAST 0 (b) > > 25 JUMP_ABSOLUTE 3 > > >> 28 LOAD_CONST 0 (None) > > 31 RETURN_VALUE > > > > -- > > Terry Jan Reedy ----- The problem of this guy is not at this level. His problem is more simply, he most probably does not understand how to build a correct, proper loop. "What I wanted to happen is when the user typed something ... ... would cause the program to stop ... " jmf From eschneider92 at comcast.net Fri Aug 9 18:27:50 2013 From: eschneider92 at comcast.net (eschneider92 at comcast.net) Date: Fri, 9 Aug 2013 15:27:50 -0700 (PDT) Subject: beginner question (True False help) In-Reply-To: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> References: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> Message-ID: This is what I ended up with btw. Does this insult anyone's more well attuned Python sensibilities? letters='abcdefghijkl' def repeat(): print('wanna go again?') batman=input() if batman in ('y','yes'): main() else: return def main(): print('guess a letter') batman=input() if batman in letters: print('ok that letter was in letters') repeat() else: print('asdasdasd') repeat() main() print('how ya doin') From joshua at landau.ws Fri Aug 9 19:05:17 2013 From: joshua at landau.ws (Joshua Landau) Date: Sat, 10 Aug 2013 00:05:17 +0100 Subject: beginner question (True False help) In-Reply-To: References: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> Message-ID: On 9 August 2013 23:27, wrote: > This is what I ended up with btw. Does this insult anyone's more well attuned Python sensibilities? ... Yes. You didn't listen to any of the advice we've been giving you. You've had *much* better answers given than this. Start from the top. We need letters, so define that: letters = "abcdefghijkl" We then want to loop, possibly forever. A good choice is a "while" loop. # True is always True, so will loop forever while True: We then want to ask for a letter. We want to use "input". Write "help(input)" in the Python Shell and you get >>> help(input) Help on built-in function input in module builtins: input(...) input([prompt]) -> string Read a string from standard input. The trailing newline is stripped. If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError. On Unix, GNU readline is used if enabled. The prompt string, if given, is printed without a trailing newline before reading. So our line should be: letter = input("Type a letter from 'a' to 'n' in the alphabet: ") Then we want to test if it's on of our letters: if letter in letters: And if so we want to say something positive: print("That's right.") If not we want to say something negative: else: print("That's wrong.") And then we want to ask if we should go again: go_again = input("Do you want to do this again? ") If the response is "y" or "yes", we want to continue looping. The while loop will do that automatically, so we can do nothing in this circumstance. If the response in *not* "y" or "yes", we want to stop: if go_again not in ("y", "yes"): break That's it. No need to complicate things. Just take it one step at a time. From eschneider92 at comcast.net Fri Aug 9 18:28:47 2013 From: eschneider92 at comcast.net (eschneider92 at comcast.net) Date: Fri, 9 Aug 2013 15:28:47 -0700 (PDT) Subject: beginner question (True False help) In-Reply-To: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> References: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> Message-ID: This is what I ended up with btw. Does this insult anyone's more well-attuned Pythonic sensibilities? letters='abcdefghijkl' def repeat(): print('wanna go again?') batman=input() if batman in ('y','yes'): main() else: return def main(): print('guess a letter') batman=input() if batman in letters: print('ok that letter was in letters') repeat() else: print('asdasdasd') repeat() main() print('how ya doin') From eschneider92 at comcast.net Fri Aug 9 19:14:26 2013 From: eschneider92 at comcast.net (eschneider92 at comcast.net) Date: Fri, 9 Aug 2013 16:14:26 -0700 (PDT) Subject: beginner question (True False help) In-Reply-To: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> References: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> Message-ID: <7fa57433-6145-48ce-92a4-c5ba5b441a40@googlegroups.com> I don't understand any of the advice any of you have given. From joshua at landau.ws Fri Aug 9 19:30:58 2013 From: joshua at landau.ws (Joshua Landau) Date: Sat, 10 Aug 2013 00:30:58 +0100 Subject: beginner question (True False help) In-Reply-To: <7fa57433-6145-48ce-92a4-c5ba5b441a40@googlegroups.com> References: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> <7fa57433-6145-48ce-92a4-c5ba5b441a40@googlegroups.com> Message-ID: On 10 August 2013 00:14, wrote: > I don't understand any of the advice any of you have given. What about it don't you understand? Pick a sentence you don't understand and throw it back at us. If you understand all the sentences but not how they come together, say so. If there's a leap that you don't understand, say that you don't get it. We've tried rephrasing things a few ways but without any interaction we can't really help. --- You have said "I figured that ... the batman==False part of the repeat() function would cause the 'while batman==True' part to become False and end." We have said this is untrue. The "batman = False" inside the function does not affect "batman" outside of the function. You need to put "global batman" in the function for it to change "batman" on a global scope. You've not once explained what part of this explanation confuses you. From eschneider92 at comcast.net Fri Aug 9 19:24:36 2013 From: eschneider92 at comcast.net (eschneider92 at comcast.net) Date: Fri, 9 Aug 2013 16:24:36 -0700 (PDT) Subject: beginner question (True False help) In-Reply-To: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> References: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> Message-ID: <3a353a32-df97-41f7-81fa-a4323bd83d01@googlegroups.com> Thanks, though me not utilizing any of the other advice wasn't from lack of trying; I couldn't understand any of it. I get it now that I have a corrrect example code in front of me. From eschneider92 at comcast.net Fri Aug 9 19:34:48 2013 From: eschneider92 at comcast.net (eschneider92 at comcast.net) Date: Fri, 9 Aug 2013 16:34:48 -0700 (PDT) Subject: beginner question (True False help) In-Reply-To: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> References: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> Message-ID: <35939249-9d6f-46ac-b0b6-6ab8152cdae9@googlegroups.com> What does global mean? From joshua at landau.ws Fri Aug 9 21:22:01 2013 From: joshua at landau.ws (Joshua Landau) Date: Sat, 10 Aug 2013 02:22:01 +0100 Subject: beginner question (True False help) In-Reply-To: <35939249-9d6f-46ac-b0b6-6ab8152cdae9@googlegroups.com> References: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> <35939249-9d6f-46ac-b0b6-6ab8152cdae9@googlegroups.com> Message-ID: On 10 August 2013 00:34, wrote: > What does global mean? Python has "scopes" for its variables. Most programming languages do. A "scope" is a restriction on where variables exist -- they exist only within the scope. This can be seen in this example: def function(): # A new "scope" is made when you enter a function variable = 100 function() print(variable) # Error, as variable doesn't exist outside of "function"'s scope There are lots of different "scopes" in code. Every function has one, and there are some more too. One of the scopes is the "global" scope. This is the scope *outside* of all the functions and other scopes. Everything in the file is within this sope: # Make in global scope variable = 100 def function(): # Works because we're inside the global scope print(variable) # Prints "100" function() So "a = b" inside the function applies to the function's scope, but when accessing variables (such as "print(variable)") it will look in all of the outer scopes too. If you want to write "a = b" inside the function and change the global scope, you need to say that "a" refers to the "a" in the global scope. You do that like this: def function(): # "variable" is in the global scope, not the functions' global variable variable = 100 function() # Prints "100" print(variable) Does that help you understand what "global" means? From steve+comp.lang.python at pearwood.info Fri Aug 9 21:40:35 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 Aug 2013 01:40:35 GMT Subject: beginner question (True False help) References: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> <35939249-9d6f-46ac-b0b6-6ab8152cdae9@googlegroups.com> Message-ID: <52059a13$0$30000$c3e8da3$5496439d@news.astraweb.com> On Fri, 09 Aug 2013 16:34:48 -0700, eschneider92 wrote: > What does global mean? Hi eschneider92, A few bits of advice: - You may like to actually sign your emails with a real name, or at least an alias that you want to be called, otherwise we'll just call you by your email address, and apart from sounding silly, many people don't like that. - You might also find that the tutor mailing list is a better match for your status as a complete beginner to Python. You can subscribe to it here: http://mail.python.org/mailman/listinfo/tutor - If you do, please take my advice and use individual emails, not daily digests. It is MUCH easier to carry on a back-and-forth conversation with individual emails. - Please try to quote enough of the message you are replying to to establish context, but without filling the email with page after page of irrelevant history. (Notice the line at the top of this message, starting with ">"? That's what you previously wrote.) If you need help configuring your email program to quote the previous message, just ask. -- Steven From eschneider92 at comcast.net Fri Aug 9 19:40:50 2013 From: eschneider92 at comcast.net (eschneider92 at comcast.net) Date: Fri, 9 Aug 2013 16:40:50 -0700 (PDT) Subject: beginner question (True False help) In-Reply-To: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> References: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> Message-ID: (I forgot to post this with my last post.) Also, I don't understand any part of the following example, so there's no specific line that's confusing me. Thanks for the help btw. var = 42 def myfunc(): var = 90 print "before:", var myfunc() print "after:", var def myfunc(): global var var = 90 From python at mrabarnett.plus.com Fri Aug 9 20:39:47 2013 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 10 Aug 2013 01:39:47 +0100 Subject: beginner question (True False help) In-Reply-To: References: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> Message-ID: <52058BD3.1090806@mrabarnett.plus.com> On 10/08/2013 00:40, eschneider92 at comcast.net wrote: > (I forgot to post this with my last post.) > Also, I don't understand any part of the following example, so there's no specific line that's confusing me. Thanks for the help btw. > You don't understand _any_ of it? > var = 42 Here you're assigning to 'var'. You're not in a function, so 'var' is a global variable. > def myfunc(): > var = 90 Here you're assigning to 'var'. If you assign to a variable anywhere in a function, and you don't say that that variable is global, then it's treated as being local to that function, and completely unrelated to any other variable outside that function. > > print "before:", var > myfunc() > print "after:", var > > def myfunc(): > global var > var = 90 > Here you're assigning to 'var', but this time you've declared that it's global, so you're assigning to the global variable called 'var'. From eschneider92 at comcast.net Fri Aug 9 19:43:08 2013 From: eschneider92 at comcast.net (eschneider92 at comcast.net) Date: Fri, 9 Aug 2013 16:43:08 -0700 (PDT) Subject: beginner question (True False help) In-Reply-To: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> References: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> Message-ID: <67c27dd9-f33b-4445-9df4-433b10850612@googlegroups.com> (I forgot to post this with my last post.) Also, I don't understand any part of the following example, so there's no specific line that's confusing me. Thanks for the help btw. var = 42 def myfunc(): var = 90 print "before:", var myfunc() print "after:", var def myfunc(): global var var = 90 From eschneider92 at comcast.net Fri Aug 9 21:08:34 2013 From: eschneider92 at comcast.net (eschneider92 at comcast.net) Date: Fri, 9 Aug 2013 18:08:34 -0700 (PDT) Subject: beginner question (True False help) In-Reply-To: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> References: <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> Message-ID: <46446a7f-f5e4-411e-9eb5-acd619dcad7a@googlegroups.com> I'm sorry, but I still don't understand how it applies to my problem. Thanks for everyone's patience. From liuerfire at gmail.com Wed Aug 7 04:50:48 2013 From: liuerfire at gmail.com (liuerfire Wang) Date: Wed, 7 Aug 2013 16:50:48 +0800 Subject: make elements of a list twice or more. Message-ID: Sorry for the title which didn't make clear. Here is a list x = [b, a, c] (a, b, c are elements of x. Each of them are different type). Now I wanna generate a new list as [b, b, a, a, c, c]. I know we can do like that: tmp = [] for i in x: tmp.append(i) tmp.append(i) However, I wander is there a more beautiful way to do it, like [i for i in x]? Thanks. -- Best regards. /********************************** google+: +liuerfire twitter: @liuerfire ??????????????~^_^~ ***********************************/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From liuerfire at gmail.com Wed Aug 7 04:56:03 2013 From: liuerfire at gmail.com (liuerfire Wang) Date: Wed, 7 Aug 2013 16:56:03 +0800 Subject: make elements of a list twice or more. In-Reply-To: References: Message-ID: I got it! It can do like [i for i in x for y in range(2)] On Wed, Aug 7, 2013 at 4:50 PM, liuerfire Wang wrote: > Sorry for the title which didn't make clear. > > Here is a list x = [b, a, c] (a, b, c are elements of x. Each of them are > different type). Now I wanna generate a new list as [b, b, a, a, c, c]. > > I know we can do like that: > > tmp = [] > for i in x: > tmp.append(i) > tmp.append(i) > > However, I wander is there a more beautiful way to do it, like [i for i in > x]? > > Thanks. > > > -- > Best regards. > /********************************** > google+: +liuerfire twitter: @liuerfire > ??????????????~^_^~ > ***********************************/ > > -- Best regards. /********************************** google+: +liuerfire twitter: @liuerfire ??????????????~^_^~ ***********************************/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Wed Aug 7 12:59:55 2013 From: __peter__ at web.de (Peter Otten) Date: Wed, 07 Aug 2013 18:59:55 +0200 Subject: make elements of a list twice or more. References: Message-ID: liuerfire Wang wrote: > Here is a list x = [b, a, c] (a, b, c are elements of x. Each of them are > different type). Now I wanna generate a new list as [b, b, a, a, c, c]. > > I know we can do like that: > > tmp = [] > for i in x: > tmp.append(i) > tmp.append(i) > > However, I wander is there a more beautiful way to do it, like [i for i in > x]? Using itertools: >>> items [b, a, c] >>> from itertools import chain, tee, repeat >>> list(chain.from_iterable(zip(*tee(items)))) [b, b, a, a, c, c] Also using itertools: >>> list(chain.from_iterable(repeat(item, 2) for item in items)) [b, b, a, a, c, c] For lists only, should be fast: >>> result = 2*len(items)*[None] >>> result[::2] = result[1::2] = items >>> result [b, b, a, a, c, c] But I would call none of these beautiful... From joshua at landau.ws Wed Aug 7 18:18:18 2013 From: joshua at landau.ws (Joshua Landau) Date: Wed, 7 Aug 2013 23:18:18 +0100 Subject: make elements of a list twice or more. In-Reply-To: References: Message-ID: On 7 August 2013 17:59, Peter Otten <__peter__ at web.de> wrote: > liuerfire Wang wrote: > >> Here is a list x = [b, a, c] (a, b, c are elements of x. Each of them are >> different type). Now I wanna generate a new list as [b, b, a, a, c, c]. >> >> I know we can do like that: >> >> tmp = [] >> for i in x: >> tmp.append(i) >> tmp.append(i) >> >> However, I wander is there a more beautiful way to do it, like [i for i in >> x]? > > Using itertools: > >>>> items > [b, a, c] >>>> from itertools import chain, tee, repeat > >>>> list(chain.from_iterable(zip(*tee(items)))) > [b, b, a, a, c, c] > > Also using itertools: > >>>> list(chain.from_iterable(repeat(item, 2) for item in items)) > [b, b, a, a, c, c] list(chain.from_iterable([item, item] for item in items)) ? I'm actually posting to point out http://www.python.org/dev/peps/pep-0448/ would let you write: [*(item, item) for item in items] which I think is totz rad and beats out OP's [item for item in items for _ in range(2)] in readability, succinctness and obviousness. PS: For jokes, you can also painfully do: list((yield item) or item for item in items) > For lists only, should be fast: > >>>> result = 2*len(items)*[None] >>>> result[::2] = result[1::2] = items >>>> result > [b, b, a, a, c, c] > > But I would call none of these beautiful... Au contraire, that is marvelous (I'd still avoid it, though). From liuerfire at gmail.com Wed Aug 7 22:25:45 2013 From: liuerfire at gmail.com (liuerfire Wang) Date: Thu, 8 Aug 2013 10:25:45 +0800 Subject: make elements of a list twice or more. In-Reply-To: References: Message-ID: On Thu, Aug 8, 2013 at 6:18 AM, Joshua Landau wrote: > > I'm actually posting to point out > http://www.python.org/dev/peps/pep-0448/ would let you write: > > [*(item, item) for item in items] It seems like that it can be only used in python 3.4? I just use python 2.7 because of work needs. > > > For lists only, should be fast: > > > >>>> result = 2*len(items)*[None] > >>>> result[::2] = result[1::2] = items > >>>> result > > [b, b, a, a, c, c] Yeah, this is amazing and very fast. I just make a test: import timeit from itertools import chain, tee, repeat x = [1, 2, 3, 4, 5, 6, 7, 8] def test1(): [i for i in x for y in range(2)] def test2(): tmp = [] for i in x: tmp.append(i) tmp.append(i) def test3(): list(chain.from_iterable(zip(*tee(x)))) def test4(): result = 2 * len(x) * [None] result[::2] = result[1::2] = x if __name__ == '__main__': t1 = timeit.Timer("test1()", "from __main__ import test1") t2 = timeit.Timer("test2()", "from __main__ import test2") t3 = timeit.Timer("test3()", "from __main__ import test3") t4 = timeit.Timer("test4()", "from __main__ import test4") print t1.timeit(1000000) print t2.timeit(1000000) print t3.timeit(1000000) print t4.timeit(1000000) And the result is: 4.56177520752 2.85114097595 7.61084198952 1.29519414902 On Thu, Aug 8, 2013 at 6:18 AM, Joshua Landau wrote: > On 7 August 2013 17:59, Peter Otten <__peter__ at web.de> wrote: > > liuerfire Wang wrote: > > > >> Here is a list x = [b, a, c] (a, b, c are elements of x. Each of them > are > >> different type). Now I wanna generate a new list as [b, b, a, a, c, c]. > >> > >> I know we can do like that: > >> > >> tmp = [] > >> for i in x: > >> tmp.append(i) > >> tmp.append(i) > >> > >> However, I wander is there a more beautiful way to do it, like [i for i > in > >> x]? > > > > Using itertools: > > > >>>> items > > [b, a, c] > >>>> from itertools import chain, tee, repeat > > > >>>> list(chain.from_iterable(zip(*tee(items)))) > > [b, b, a, a, c, c] > > > > Also using itertools: > > > >>>> list(chain.from_iterable(repeat(item, 2) for item in items)) > > [b, b, a, a, c, c] > > list(chain.from_iterable([item, item] for item in items)) > ? > > > I'm actually posting to point out > http://www.python.org/dev/peps/pep-0448/ would let you write: > > [*(item, item) for item in items] > > which I think is totz rad and beats out OP's > > [item for item in items for _ in range(2)] > > in readability, succinctness and obviousness. > > > PS: For jokes, you can also painfully do: > > list((yield item) or item for item in items) > > > > For lists only, should be fast: > > > >>>> result = 2*len(items)*[None] > >>>> result[::2] = result[1::2] = items > >>>> result > > [b, b, a, a, c, c] > > > > But I would call none of these beautiful... > > Au contraire, that is marvelous (I'd still avoid it, though). > -- > http://mail.python.org/mailman/listinfo/python-list > -- Best regards. /********************************** google+: +liuerfire twitter: @liuerfire ??????????????~^_^~ ***********************************/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From wuwei23 at gmail.com Mon Aug 19 23:42:27 2013 From: wuwei23 at gmail.com (alex23) Date: Tue, 20 Aug 2013 13:42:27 +1000 Subject: make elements of a list twice or more. In-Reply-To: References: Message-ID: On 7/08/2013 6:50 PM, liuerfire Wang wrote: > Here is a list x = [b, a, c] (a, b, c are elements of x. Each of them > are different type). Now I wanna generate a new list as [b, b, a, a, c, c]. from itertools import chain new_list = list(chain.from_iterable(zip(x,x))) From toby at tobiah.org Wed Aug 21 16:01:20 2013 From: toby at tobiah.org (Tobiah) Date: Wed, 21 Aug 2013 13:01:20 -0700 Subject: make elements of a list twice or more. In-Reply-To: References: Message-ID: <52151C90.9080602@tobiah.org> On 08/07/2013 01:50 AM, liuerfire Wang wrote: > Sorry for the title which didn't make clear. > > Here is a list x = [b, a, c] (a, b, c are elements of x. Each of them are different type). Now I wanna generate a new list as [b, > b, a, a, c, c]. If you don't care about the order, you can do: x = x + x From redstone-cold at 163.com Wed Aug 7 05:12:29 2013 From: redstone-cold at 163.com (iMath) Date: Wed, 7 Aug 2013 02:12:29 -0700 (PDT) Subject: Resuming the HTTP Download of a File and HTTP compression Message-ID: <6d1ea879-a2a1-49e6-b3e8-de10dfce6b31@googlegroups.com> the file I want to download is 1000000 bytes uncompressed, the downloads was interrupted when 5000 bytes compressed data was transmitted .If I want to Resuming the HTTP Download ,I wonder what value of the HTTP Range header should be ,?bytes=5000-? or ?bytes= -?? why ? From redstone-cold at 163.com Wed Aug 7 05:14:31 2013 From: redstone-cold at 163.com (iMath) Date: Wed, 7 Aug 2013 02:14:31 -0700 (PDT) Subject: Resuming the HTTP Download of a File and HTTP compression Message-ID: the file I want to download is 1000000 bytes uncompressed, the downloads was interrupted when 5000 bytes compressed data was transmitted .If I want to Resuming the HTTP Download ,I wonder what value of the HTTP Range header should be ,?bytes=5000-? or ?bytes= -?? why ? From balderman at gmail.com Wed Aug 7 09:39:24 2013 From: balderman at gmail.com (balderman at gmail.com) Date: Wed, 7 Aug 2013 06:39:24 -0700 (PDT) Subject: Mock pathc question Message-ID: <004ef25c-c5c8-42cd-8a4d-6e36a18a34b0@googlegroups.com> Hi I would like to mock patch the attribute 'calc' in the 'Client' class (See code below). I have 2 unit tests: 1) test1 - that patch an existing instance of 'Client' - it works fine. 1) test2 - that tries to patch the 'Client' class. My expectation is that after the patching, every instance of 'Client' will be created with 'MockClient'. However this is not the case.. Can you please advice? Thanks Avishay code below: ------------------------------------- import mock import sys import unittest SEVEN = 7 class Calc: def __init__(self): print self.__class__ def add(self,a,b): return a + b class MockCalc: def __init__(self): print self.__class__ def add(self,a,b): return SEVEN class Client: def __init__(self): self.calc = Calc() def add(self,a,b): return self.calc.add(a,b) class TestIt(unittest.TestCase): def setUp(self): pass def test2(self): '''Mocking the Calc and replace it with MockCalc.''' print " \ntest2 " my_mock = mock.patch('mock_play.Calc',create=True, new=MockCalc) my_mock.start() # client should be created with 'MockCalc' client = Client() # result should be 7 print str(client.add(1,34)) my_mock.stop() # result should be 35 again print str(client.add(1,34)) def test1(self): '''Mocking the client instance.''' print " test1 " client = Client() my_mock = mock.patch.object(client, 'calc', new_callable=MockCalc) # result should be 35 print str(client.add(1,34)) # now i want to switch to the MockCalc my_mock.start() # result should be 7 print str(client.add(1,34)) my_mock.stop() # result should be 35 again print str(client.add(1,34)) if __name__ == "__main__": unittest.main() From jeanmichel at sequans.com Wed Aug 7 14:01:54 2013 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 7 Aug 2013 20:01:54 +0200 (CEST) Subject: Mock pathc question In-Reply-To: <004ef25c-c5c8-42cd-8a4d-6e36a18a34b0@googlegroups.com> Message-ID: <1126236036.30435698.1375898514852.JavaMail.root@sequans.com> ----- Mail original ----- > Hi > I would like to mock patch the attribute 'calc' in the 'Client' class > (See code below). > I have 2 unit tests: > 1) test1 - that patch an existing instance of 'Client' - it works > fine. > 1) test2 - that tries to patch the 'Client' class. My expectation is > that after the patching, every instance of 'Client' will be created > with 'MockClient'. However this is not the case.. > > Can you please advice? > > Thanks > > Avishay One way to do this is to decorate the test2 method, http://www.voidspace.org.uk/python/mock/patch.html. This way you get rid of all the start/stop boiler-plate, the scope of your patch is the scope of the method. *code not tested* class TestIt(unittest.TestCase): def setUp(self): pass @mock.patch(Calc, MockCalc) def test2(self): client = Client() # result should be 7 print str(client.add(1,34)) def test3(self): client = Client() # result should be 35 again print str(client.add(1,34)) By the way, what is 'mock_play' in your original post, could be the reason why you things did go wrong. 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 balderman at gmail.com Thu Aug 8 00:51:50 2013 From: balderman at gmail.com (Avishay Balderman) Date: Thu, 8 Aug 2013 07:51:50 +0300 Subject: Mock pathc question In-Reply-To: <1126236036.30435698.1375898514852.JavaMail.root@sequans.com> References: <1126236036.30435698.1375898514852.JavaMail.root@sequans.com> Message-ID: <-8538360675205673045@unknownmsgid> Hi 1) I prefer to use start/stop and not the decorator . 2) mock_play is the name of the module where the code belongs Thanks Avishay Sent from my iPhone On 7 ???? 2013, at 21:01, Jean-Michel Pichavant wrote: ----- Mail original ----- Hi I would like to mock patch the attribute 'calc' in the 'Client' class (See code below). I have 2 unit tests: 1) test1 - that patch an existing instance of 'Client' - it works fine. 1) test2 - that tries to patch the 'Client' class. My expectation is that after the patching, every instance of 'Client' will be created with 'MockClient'. However this is not the case.. Can you please advice? Thanks Avishay One way to do this is to decorate the test2 method, http://www.voidspace.org.uk/python/mock/patch.html. This way you get rid of all the start/stop boiler-plate, the scope of your patch is the scope of the method. *code not tested* class TestIt(unittest.TestCase): def setUp(self): pass @mock.patch(Calc, MockCalc) def test2(self): client = Client() # result should be 7 print str(client.add(1,34)) def test3(self): client = Client() # result should be 35 again print str(client.add(1,34)) By the way, what is 'mock_play' in your original post, could be the reason why you things did go wrong. 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. -------------- next part -------------- An HTML attachment was scrubbed... URL: From jeanmichel at sequans.com Thu Aug 8 06:04:38 2013 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Thu, 8 Aug 2013 12:04:38 +0200 (CEST) Subject: Mock pathc question In-Reply-To: <-8538360675205673045@unknownmsgid> Message-ID: <542969021.30699835.1375956278610.JavaMail.root@sequans.com> ----- Mail original ----- > Hi > 1) I prefer to use start/stop and not the decorator . > 2) mock_play is the name of the module where the code belongs > Thanks > Avishay > Sent from my iPhone You should not neeed to refer to the Calc class using mock_play since it is defined in the very same file (module). Possibly mock_play.Calc and Calc are 2 different classes, hence you're not patching the good one. Note how you patch "mock_play.Calc" and then instanciate using "self.calc = Calc()" right before my_mock = mock.patch('mock_play.Calc',create=True, new=MockCalc) add print mock_play.Calc print Calc and verify that they are both the very same object. Cheers, JM PS : Please do not top post in this list -- 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 balderman at gmail.com Thu Aug 8 12:15:31 2013 From: balderman at gmail.com (balderman at gmail.com) Date: Thu, 8 Aug 2013 09:15:31 -0700 (PDT) Subject: Mock pathc question In-Reply-To: References: <-8538360675205673045@unknownmsgid> Message-ID: On Thursday, August 8, 2013 12:04:38 PM UTC+2, Jean-Michel Pichavant wrote: > ----- Mail original ----- > > > Hi > > 1) I prefer to use start/stop and not the decorator . > > 2) mock_play is the name of the module where the code belongs > > > Thanks > > > Avishay > > Sent from my iPhone > > > You should not neeed to refer to the Calc class using mock_play since it is defined in the very same file (module). > Possibly mock_play.Calc and Calc are 2 different classes, hence you're not patching the good one. > > Note how you patch "mock_play.Calc" and then instanciate using "self.calc = Calc()" > > right before > my_mock = mock.patch('mock_play.Calc',create=True, new=MockCalc) > > add > print mock_play.Calc > print Calc > > > and verify that they are both the very same object. > > Cheers, > > JM > > > PS : Please do not top post in this list > > > -- 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. Hi Here is a working solution: http://stackoverflow.com/questions/18121084/mock-patch-does-not-work-properly thanks From mclefavor at gmail.com Wed Aug 7 15:38:48 2013 From: mclefavor at gmail.com (Matthew Lefavor) Date: Wed, 7 Aug 2013 15:38:48 -0400 Subject: Is a Metaclass the appropriate way to solve this problem? Message-ID: All: Like most people, I find the whole metaclass topic pretty obscure, and I have avoided trying to use one for a while. I am also aware of Tim Peter's famous advice that if you have to ask whether you need a metaclass, then you almost certainly don't. But in this case I know I am solving a problem similar to problems that other high-profile Python modules (e.g., Django's Model class) have solved using metaclasses. My company is using a database whose interface is defined in a series of JSON objects. I am writing some interface code to the database and I am trying to hide the detail of the JSON implementation from the user. I want the user to be able to define a class representing a database entry for any arbitrary table, whose attributes represent database entities, like fields or tags, with syntax something like the following: class DataSet: data_set_id = DatabaseKeyField(int) description = DatabaseField(str) creation_date = DatabaseField(datetime.date) creation_timestamp = DatabaseField(datetime.datetime) def __init__(self, ds_id, description, timestamp): self.data_set_id = ds_id self.description = description self.creation_timestamp = timestamp self.creation_date = timestamp.date I know that to create the DatabaseField objects I should be using a descriptor. But I also want the DataSet to automatically gain methods that will convert it into the expected JSON syntax (e.g., a __specifier__ method that will create a JSON object with only "key" fields, and an __object__ method that will create a JSON object with all the fields and other bells and whistles.) My ultimate question then: How do I go about adding those kinds of methods (which iterate through all the attributes which are Database* descriptors)? I know that I could eschew metaclasses altogether and use a common super-class, but this doesn't feel like the right situation for inheritance to me. Is a metaclass going to be the cleanest and easiest-to-understand way to solve this problem? Thank you, all! -MCL -------------- next part -------------- An HTML attachment was scrubbed... URL: From ian.g.kelly at gmail.com Wed Aug 7 16:28:14 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 7 Aug 2013 14:28:14 -0600 Subject: Is a Metaclass the appropriate way to solve this problem? In-Reply-To: References: Message-ID: On Wed, Aug 7, 2013 at 1:38 PM, Matthew Lefavor wrote: > I know that to create the DatabaseField objects I should be using a > descriptor. But I also want the DataSet to automatically gain methods that > will convert it into the expected JSON syntax (e.g., a __specifier__ method > that will create a JSON object with only "key" fields, and an __object__ > method that will create a JSON object with all the fields and other bells > and whistles.) > > My ultimate question then: How do I go about adding those kinds of methods > (which iterate through all the attributes which are Database* descriptors)? > I know that I could eschew metaclasses altogether and use a common > super-class, but this doesn't feel like the right situation for inheritance > to me. Is a metaclass going to be the cleanest and easiest-to-understand way > to solve this problem? You could use a class decorator. It's not as flexible as a metaclass, but from the sound of it you don't need that kind of flexibility; you just need to modify the class a bit after it's already been created. From bv8bv8bv8 at gmail.com Wed Aug 7 17:28:08 2013 From: bv8bv8bv8 at gmail.com (BV BV) Date: Wed, 7 Aug 2013 14:28:08 -0700 (PDT) Subject: Why Muslim Women Wear the Veil ??????????????? Message-ID: <29508ef0-9744-4740-98a8-530e5ca2b415@googlegroups.com> Why Muslim Women Wear the Veil In recent years, a small piece of cloth has managed to cause quite a stir. The scarf or hijab that Muslim women wear on their heads is making headlines around the world. Hijab is banned in French public schools and other European countries have adopted, or are drafting similar legislation. In Australia, a radio presenter triggered both debate and outrage when he called for the face veil (niqab) to be banned from banks and post offices. Even predominantly Muslim countries such as Turkey and Tunisia ban the hijab in certain government buildings. When a small piece of fabric causes such controversy and conflict, wouldn?t it be easier to remove it? Why then, under such circumstances, do Muslim women wear scarves? There are a myriad of reasons why, but the easy, one sentence answer is, because they believe God has made it an obligation for believing women. In the Quran God tells the believing men and women to lower their gaze and to dress modestly. He (God) specifically addresses women when He asks them not to show off their adornment, except that which is apparent, and draw their veils over their bodies. (Quran 24:30-31) These verses of Quran are known as the verses of hijab and it is the consensus of Islamic scholars that they make the wearing of hijab mandatory. Some countries, such as Saudi Arabia and Qatar do enforce a dress code. Women there are expected to cover their hair and wear some sort of loose fitting, full-length garment over their clothes. However, for the majority of Muslim women around the world, to cover, or not to cover, is a freely made choice. God requires Muslim women to dress modestly and to wear the hijab in public and in the presence of men who are not close relatives. Although the English word scarf and the Arabic term hijab have become interchangeable, it is worth noting that hijab is more than just a scarf. It is a term that covers a variety of clothing including scarves, but also a variety of different dress styles from around the world. Many have cultural connotations such as the Pakistani shalwar khamis or the Afghani burqa, but whenever a Muslim woman covers ?her adornment?, she is said to be wearing hijab. The literal meaning of hijab is to veil, to cover, or to screen. Islam is known as a religion concerned with community cohesion and moral boundaries, and therefore hijab is a way of ensuring that the moral boundaries between unrelated men and women are respected. In this sense, the term hijab encompasses more than a scarf and more then a dress code. It is a term that denotes modest dressing and modest behaviour. For instance, if a Muslim woman was wearing a scarf but at the same time using bad language, she would not be fulfilling the requirements of hijab. The majority of Muslim women wear hijab, to obey God, and to be known as respectable women. (Quran 33:59) However, in the last 30 years hijab has emerged as a sign of Islamic consciousness. Many women see wearing the hijab as indicative of their desire to be part of an Islamic revival, especially in countries where the practice of Islam is discouraged or even forbidden. While those who seek to ban hijab refer to it as a symbol of gender based repression, the women who choose to don a scarf, or to wear hijab, in the broadest sense of the word, do so by making personal decisions and independent choices. They view it as a right and not a burden. Nor do these women regard hijab as a sign of oppression. Women who wear hijab often describe themselves as being ?set free? from society?s unrealistic fashion culture. Hijab frees women from being thought of as sexual objects of desire or from being valued for their looks, or body shape rather then their minds and intellect. No longer slaves to consumerism, hijab liberates women from the need to conform to unrealistic stereotypes and images dictated by the media. Women wearing hijab have expressed that dressing modestly and covering their hair, minimises sexual harassment in the workplace. The aura of privacy created by hijab is indicative of the great value Islam places upon women. It is true that in some families and in some cultures women are forced to wear hijab but this is not the norm. The Quran clearly states that there is no compulsion in religion (2:256). Women who choose to wear hijab do not make the decision lightly. In fact many women testify that they faced great animosity from their Muslim or non-Muslim families when they decided to cover. Across the globe there are numerous instances of women having to defend their right to wear the hijab. Hijab can be a symbol of piety and it can be a sign of great inner strength and fortitude. A woman wearing hijab becomes a very visible sign of Islam. While Muslim men can blend easily into any society, Muslim woman are often put on the line, and forced to defend not only their decision to cover, but also their religion. Nevertheless, women who wear hijab insist that the advantages far outweigh any disadvantage conjured up by media bias or general ignorance. http://www.islamreligion.com/articles/2770/ thank you From adam.preble at gmail.com Thu Aug 8 02:32:44 2013 From: adam.preble at gmail.com (adam.preble at gmail.com) Date: Wed, 7 Aug 2013 23:32:44 -0700 (PDT) Subject: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" Message-ID: <215331fa-379f-4251-b722-44555349fbb5@googlegroups.com> We were coming into Python's unittest module from backgrounds in nunit, where they use a decorate to identify tests. So I was hoping to avoid the convention of prepending "test" to the TestClass methods that are to be actually run. I'm sure this comes up all the time, but I mean not to have to do: class Test(unittest.TestCase): def testBlablabla(self): self.assertEqual(True, True) But instead: class Test(unittest.TestCase): @test def Blablabla(self): self.assertEqual(True, True) This is admittedly a petty thing. I have just about given up trying to actually deploy a decorator, but I haven't necessarily given up on trying to do it for the sake of knowing if it's possible. Superficially, you'd think changing a function's __name__ should do the trick, but it looks like test discovery happens without looking at the transformed function. I tried a decorator like this: def prepend_test(func): print "running prepend_test" func.__name__ = "test" + func.__name__ def decorator(*args, **kwargs): return func(args, kwargs) return decorator When running unit tests, I'll see "running prepend_test" show up, but a dir on the class being tested doesn't show a renamed function. I assume it only works with instances. Are there any other tricks I could consider? From tjreedy at udel.edu Thu Aug 8 04:04:30 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 08 Aug 2013 04:04:30 -0400 Subject: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" In-Reply-To: <215331fa-379f-4251-b722-44555349fbb5@googlegroups.com> References: <215331fa-379f-4251-b722-44555349fbb5@googlegroups.com> Message-ID: On 8/8/2013 2:32 AM, adam.preble at gmail.com wrote: > We were coming into Python's unittest module from backgrounds in nunit, where they use a decorate to identify tests. So I was hoping to avoid the convention of prepending "test" to the TestClass methods that are to be actually run. I'm sure this comes up all the time, but I mean not to have to do: > > class Test(unittest.TestCase): > def testBlablabla(self): > self.assertEqual(True, True) > > But instead: > class Test(unittest.TestCase): > @test > def Blablabla(self): > self.assertEqual(True, True) I cannot help but note that this is *more* typing. But anyhow, something like this might work. def test(f): f.__class__.__dict__['test_'+f.__name__] might work. Or maybe for the body just setattr(f.__class__, 'test_'+f.__name__) > Superficially, you'd think changing a function's __name__ should do the trick, but it looks like test discovery happens without looking at the transformed function. I am guessing that unittest discovery for each class is something like if isinstance (cls, unittest.TestCase): for name, f in cls.__dict__.items(): if name.startswith('test'): yield f You were thinking it would be ... for f in cls.__dict__.values(): if f.__name__.startwith('test'): yield f Not ridiculous, but you seem to have disproven it. I believe you can take 'name' in the docs to be bound or namespace name rather than definition or attribute name. -- Terry Jan Reedy From __peter__ at web.de Thu Aug 8 04:32:57 2013 From: __peter__ at web.de (Peter Otten) Date: Thu, 08 Aug 2013 10:32:57 +0200 Subject: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" References: <215331fa-379f-4251-b722-44555349fbb5@googlegroups.com> Message-ID: adam.preble at gmail.com wrote: > We were coming into Python's unittest module from backgrounds in nunit, > where they use a decorate to identify tests. So I was hoping to avoid the > convention of prepending "test" to the TestClass methods that are to be > actually run. I'm sure this comes up all the time, but I mean not to have > to do: > > class Test(unittest.TestCase): > def testBlablabla(self): > self.assertEqual(True, True) > > But instead: > class Test(unittest.TestCase): > @test > def Blablabla(self): > self.assertEqual(True, True) > > This is admittedly a petty thing. I have just about given up trying to > actually deploy a decorator, but I haven't necessarily given up on trying > to do it for the sake of knowing if it's possible. > > Superficially, you'd think changing a function's __name__ should do the > trick, but it looks like test discovery happens without looking at the > transformed function. I tried a decorator like this: > > def prepend_test(func): > print "running prepend_test" > func.__name__ = "test" + func.__name__ > > def decorator(*args, **kwargs): > return func(args, kwargs) > > return decorator > > When running unit tests, I'll see "running prepend_test" show up, but a > dir on the class being tested doesn't show a renamed function. I assume > it only works with instances. Are there any other tricks I could > consider? I think you are misunderstanding what a decorator does. You can think of def f(...): ... as syntactic sugar for an assignment f = make_function(...) A decorator intercepts that f = decorator(make_function(...)) and therefore can modify or replace the function object, but has no influence on the name binding. For unittest to allow methods bound to a name not starting with "test" you have to write a custom test loader. import functools import unittest.loader import unittest def test(method): method.unittest_method = True return method class MyLoader(unittest.TestLoader): def getTestCaseNames(self, testCaseClass): def isTestMethod(attrname, testCaseClass=testCaseClass, prefix=self.testMethodPrefix): attr = getattr(testCaseClass, attrname) if getattr(attr, "unittest_method", False): return True return attrname.startswith(prefix) and callable(attr) testFnNames = list(filter(isTestMethod, dir(testCaseClass))) if self.sortTestMethodsUsing: testFnNames.sort(key=functools.cmp_to_key(self.sortTestMethodsUsing)) return testFnNames class A(unittest.TestCase): def test_one(self): pass @test def two(self): pass if __name__ == "__main__": unittest.main(testLoader=MyLoader()) Alternatively you can write a metaclass that *can* intercept the name binding process: $ cat mytestcase.py import unittest __UNITTEST = True PREFIX = "test_" class Type(type): def __new__(class_, name, bases, classdict): newclassdict = {} for name, attr in classdict.items(): if getattr(attr, "test", False): assert not name.startswith(PREFIX) name = PREFIX + name assert name not in newclassdict newclassdict[name] = attr return type.__new__(class_, name, bases, newclassdict) class MyTestCase(unittest.TestCase, metaclass=Type): pass def test(method): method.test = True return method $ cat mytestcase_demo.py import unittest from mytestcase import MyTestCase, test class T(MyTestCase): def test_one(self): pass @test def two(self): pass if __name__ == "__main__": unittest.main() $ python3 mytestcase_demo.py -v test_one (__main__.test_two) ... ok test_two (__main__.test_two) ... ok ---------------------------------------------------------------------- Ran 2 tests in 0.000s OK From __peter__ at web.de Thu Aug 8 04:50:47 2013 From: __peter__ at web.de (Peter Otten) Date: Thu, 08 Aug 2013 10:50:47 +0200 Subject: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" References: <215331fa-379f-4251-b722-44555349fbb5@googlegroups.com> Message-ID: Peter Otten wrote: > $ python3 mytestcase_demo.py -v > test_one (__main__.test_two) ... ok > test_two (__main__.test_two) ... ok > > ---------------------------------------------------------------------- > Ran 2 tests in 0.000s Oops, that's an odd class name. Fixing the name clash in Types.__new__() is left as an exercise... From adam.preble at gmail.com Thu Aug 8 12:17:54 2013 From: adam.preble at gmail.com (adam.preble at gmail.com) Date: Thu, 8 Aug 2013 09:17:54 -0700 (PDT) Subject: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" In-Reply-To: References: <215331fa-379f-4251-b722-44555349fbb5@googlegroups.com> Message-ID: On Thursday, August 8, 2013 3:50:47 AM UTC-5, Peter Otten wrote: > Peter Otten wrote: > Oops, that's an odd class name. Fixing the name clash in Types.__new__() is > > left as an exercise... I will do some experiments with a custom test loader since I wasn't aware of that as a viable alternative. I am grateful for the responses. From ned at nedbatchelder.com Thu Aug 8 13:14:37 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Thu, 08 Aug 2013 13:14:37 -0400 Subject: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" In-Reply-To: References: <215331fa-379f-4251-b722-44555349fbb5@googlegroups.com> Message-ID: <5203D1FD.2070306@nedbatchelder.com> On 8/8/13 12:17 PM, adam.preble at gmail.com wrote: > On Thursday, August 8, 2013 3:50:47 AM UTC-5, Peter Otten wrote: >> Peter Otten wrote: >> Oops, that's an odd class name. Fixing the name clash in Types.__new__() is >> >> left as an exercise... > I will do some experiments with a custom test loader since I wasn't aware of that as a viable alternative. I am grateful for the responses. If you can use another test runner, they often have more flexible and powerful ways to do everything. nosetests will let you use a __test__ attribute, for example, to mark tests. Your decorator could simply assign that attribute on the test methods. You'd still write your tests using the unittest base classes, but run them with nose. --Ned. From adam.preble at gmail.com Fri Aug 9 01:07:54 2013 From: adam.preble at gmail.com (adam.preble at gmail.com) Date: Thu, 8 Aug 2013 22:07:54 -0700 (PDT) Subject: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" In-Reply-To: References: <215331fa-379f-4251-b722-44555349fbb5@googlegroups.com> Message-ID: <3b1f6c61-da0b-42bd-acf5-95962f6d33d9@googlegroups.com> On Thursday, August 8, 2013 3:50:47 AM UTC-5, Peter Otten wrote: > Peter Otten wrote: > Oops, that's an odd class name. Fixing the name clash in Types.__new__() is > > left as an exercise... Interesting, I got __main__.T, even though I pretty much just tried your code wholesale. For what it's worth, I'm using Python 2.7. I'm glad to see that code since I learned a lot of tricks from it. From __peter__ at web.de Fri Aug 9 02:31:43 2013 From: __peter__ at web.de (Peter Otten) Date: Fri, 09 Aug 2013 08:31:43 +0200 Subject: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" References: <215331fa-379f-4251-b722-44555349fbb5@googlegroups.com> <3b1f6c61-da0b-42bd-acf5-95962f6d33d9@googlegroups.com> Message-ID: adam.preble at gmail.com wrote: > On Thursday, August 8, 2013 3:50:47 AM UTC-5, Peter Otten wrote: >> Peter Otten wrote: >> Oops, that's an odd class name. Fixing the name clash in Types.__new__() >> is >> >> left as an exercise... > > Interesting, I got __main__.T, even though I pretty much just tried your > code wholesale. I see I have to fix it myself then... > For what it's worth, I'm using Python 2.7. I'm glad to > see that code since I learned a lot of tricks from it. [My buggy code] > class Type(type): > def __new__(class_, name, bases, classdict): Here 'name' is the class name > newclassdict = {} > for name, attr in classdict.items(): > if getattr(attr, "test", False): > assert not name.startswith(PREFIX) > name = PREFIX + name > assert name not in newclassdict > newclassdict[name] = attr Here 'name' is the the last key of classdict which is passed to type.__new__ instead of the actual class name. > return type.__new__(class_, name, bases, newclassdict) [Fixed version] class Type(type): def __new__(class_, classname, bases, classdict): newclassdict = {} for name, attr in classdict.items(): if getattr(attr, "test", False): assert not name.startswith(PREFIX) name = PREFIX + name assert name not in newclassdict newclassdict[name] = attr return type.__new__(class_, classname, bases, newclassdict) From adam.preble at gmail.com Mon Aug 12 00:25:10 2013 From: adam.preble at gmail.com (adam.preble at gmail.com) Date: Sun, 11 Aug 2013 21:25:10 -0700 (PDT) Subject: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" In-Reply-To: References: <215331fa-379f-4251-b722-44555349fbb5@googlegroups.com> <3b1f6c61-da0b-42bd-acf5-95962f6d33d9@googlegroups.com> Message-ID: <77d3df08-a287-4cb8-8230-ec59ea6219b2@googlegroups.com> On Friday, August 9, 2013 1:31:43 AM UTC-5, Peter Otten wrote: > I see I have to fix it myself then... Sorry man, I think in my excitement of seeing the first of your examples to work, that I missed the second example, only seeing your comments about it at the end of the post. I didn't expect such a good response. From adam.preble at gmail.com Thu Aug 8 12:20:55 2013 From: adam.preble at gmail.com (adam.preble at gmail.com) Date: Thu, 8 Aug 2013 09:20:55 -0700 (PDT) Subject: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" In-Reply-To: References: <215331fa-379f-4251-b722-44555349fbb5@googlegroups.com> Message-ID: <292722f2-56a7-4a9d-a3ad-e65b44cac834@googlegroups.com> On Thursday, August 8, 2013 3:04:30 AM UTC-5, Terry Reedy wrote: > I cannot help but note that this is *more* typing. But anyhow, something It wasn't so much about the typing so much as having "test" in front of everything. It's a problem particular to me since I'm writing code that, well, runs experiments. So the word "test" is already all over the place. I would even prefer if I could do away with assuming everything starting with "test" is a unittest, but I didn't think I could; it looks like Peter Otten got me in the right direction. > like this might work. > > def test(f): > > f.__class__.__dict__['test_'+f.__name__] > > > > might work. Or maybe for the body just > > setattr(f.__class__, 'test_'+f.__name__) > Just for giggles I can mess around with those exact lines, but I did get spanked trying to do something similar. I couldn't reference __class__ for some reason (Python 2.7 problem?). From tjreedy at udel.edu Thu Aug 8 15:28:40 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 08 Aug 2013 15:28:40 -0400 Subject: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" In-Reply-To: <292722f2-56a7-4a9d-a3ad-e65b44cac834@googlegroups.com> References: <215331fa-379f-4251-b722-44555349fbb5@googlegroups.com> <292722f2-56a7-4a9d-a3ad-e65b44cac834@googlegroups.com> Message-ID: On 8/8/2013 12:20 PM, adam.preble at gmail.com wrote: > On Thursday, August 8, 2013 3:04:30 AM UTC-5, Terry Reedy wrote: >> def test(f): >> >> f.__class__.__dict__['test_'+f.__name__] Sorry, f.__class__ is 'function', not the enclosing class. A decorator for a method could not get the enclosing class name until 3.3, when it would be part of f.__qualname__. Use one of the other suggestions. > Just for giggles I can mess around with those exact lines, but I did get spanked trying to do something similar. I couldn't reference __class__ for some reason (Python 2.7 problem?). In 2.x, old-style classes and instances thereof do not have .__class__. All other objects do, as far as I know. -- Terry Jan Reedy From krismesenbrink at gmail.com Thu Aug 8 03:19:58 2013 From: krismesenbrink at gmail.com (krismesenbrink at gmail.com) Date: Thu, 8 Aug 2013 00:19:58 -0700 (PDT) Subject: Issues with if and elif statements in 3.3 Message-ID: <02128546-7a5d-4fe8-a472-47cfd4f76342@googlegroups.com> def town(): print ("You stand in the middle of Coffeington while you descide what" " to do next, you have herd rumor of the Coffeington Caves that run" "under the city, would you like to check them out?") answer = input() if answer == ("yes") or ("Yes") or ("y"): print("You set out for the Coffeington Caves") elif answer == ("no") or ("No") or ("n"): print("Oh...well im sure you can find something else to do") else: print("You just stand there") town() i don't know why the "elif" or "else" part of the "if statment" wont trigger. what ends up happening is that regardless of what answer you put in input it will always print out "you set out for the Coffeington Caves". whats supposed to happen is if you say "no" it should just end? i think anway. From davea at davea.name Thu Aug 8 06:34:57 2013 From: davea at davea.name (Dave Angel) Date: Thu, 8 Aug 2013 10:34:57 +0000 (UTC) Subject: Issues with if and elif statements in 3.3 References: <02128546-7a5d-4fe8-a472-47cfd4f76342@googlegroups.com> Message-ID: krismesenbrink at gmail.com wrote: > def town(): > print ("You stand in the middle of Coffeington while you descide what" > " to do next, you have herd rumor of the Coffeington Caves that run" > "under the city, would you like to check them out?") > answer = input() > if answer == ("yes") or ("Yes") or ("y"): This doesn't do what you think it does. First it compares answer to "yes". Then it takes the result of that and OR's it with "Yes". Then it takes the result of that and OR's it with "y". Finally it takes the bool of the result and decides whether to execute the if-body. Since those OR's will always be true, it always executes the if-body, and never the elif or else body. Fix the expression to what you presumably meant: if answer == "yes" or answer == "Yes" or answer == "y": Or less typing: if answer in ("yes", "Yes", "y"): > print("You set out for the Coffeington Caves") > elif answer == ("no") or ("No") or ("n"): > print("Oh...well im sure you can find something else to do") > else: > print("You just stand there") > town() > > > > i don't know why the "elif" or "else" part of the "if statment" wont trigger. what ends up happening is that regardless of what answer you put in input it will always print out "you set out for the Coffeington Caves". whats supposed to happen is if you say "no" it should just end? i think anway. -- DaveA From krismesenbrink at gmail.com Thu Aug 8 18:20:01 2013 From: krismesenbrink at gmail.com (Kris Mesenbrink) Date: Thu, 8 Aug 2013 15:20:01 -0700 (PDT) Subject: Issues with if and elif statements in 3.3 In-Reply-To: References: <02128546-7a5d-4fe8-a472-47cfd4f76342@googlegroups.com> Message-ID: <5674a7c0-d046-43a5-a998-29ada6c08412@googlegroups.com> WOW as if it was something as easy as that,i had been looking for awhile on what i was doing wrong. as it seems i just don't know my way around if statements at all, thank a bunch for this. makes everything else i have been code work thanks again From sagar.varule at gmail.com Thu Aug 8 03:20:25 2013 From: sagar.varule at gmail.com (sagar varule) Date: Thu, 8 Aug 2013 00:20:25 -0700 (PDT) Subject: Paramiko Help. Execute command to Interactive Shell which is opened by SSHClient() Message-ID: Hi All, Im using Paramiko for my SSH automation. Im using method that is shown in demo_simple.py example which comes with Paramiko. Below is code from demo_simple.py. As you can make out, below code opens SSH connection and opens Interactie Shell, and then wait for the command from user. I want to submit the command to this Interactive Shell using code. try: client = paramiko.SSHClient() client.load_system_host_keys() client.set_missing_host_key_policy(paramiko.WarningPolicy()) print '*** Connecting...' client.connect(hostname, port, username, password) chan = client.invoke_shell() print repr(client.get_transport()) print '*** Here we go!' print interactive.interactive_shell(chan) chan.close() client.close() Well Another approach I tried is instead of opening interactive_shell, directly issue command using; stdin, stdout, stderr = client.exec_command(bv_cmd) for line in stderr.readlines(): print line for line in stdout.readlines(): print line But problem here is client.exec_command(bv_cmd) waits for command to execute completely and then it returns to stdout,stderr. And I want to see the ouput from the command during its execution. Because my command takes long time for execution. Big Picture in My Mind: Big Picture I that want to achieve is, Opening different SSH connection to different host, and it will issue commands to all host, wait for execution. All execution should happen parallel.(just wanted to share my thought, and wanted to hear opinions from you all. Is this possible??). I am not big programmer, just 2 years experience with asp.net C# 2.0 so i would appreciate if discussion happens in simple english. From sagar.varule at gmail.com Sun Aug 11 01:18:49 2013 From: sagar.varule at gmail.com (sagar varule) Date: Sat, 10 Aug 2013 22:18:49 -0700 (PDT) Subject: Paramiko Help. Execute command to Interactive Shell which is opened by SSHClient() In-Reply-To: References: Message-ID: <3e9da33f-f37f-4df2-9c97-c4f2010f0471@googlegroups.com> On Thursday, August 8, 2013 12:50:25 PM UTC+5:30, sagar varule wrote: > Hi All, > > > > Im using Paramiko for my SSH automation. Im using method that is shown in demo_simple.py example which comes with Paramiko. Below is code from demo_simple.py. > > > > As you can make out, below code opens SSH connection and opens Interactie Shell, and then wait for the command from user. > > I want to submit the command to this Interactive Shell using code. > > > > try: > > client = paramiko.SSHClient() > > client.load_system_host_keys() > > client.set_missing_host_key_policy(paramiko.WarningPolicy()) > > print '*** Connecting...' > > client.connect(hostname, port, username, password) > > chan = client.invoke_shell() > > print repr(client.get_transport()) > > print '*** Here we go!' > > print > > interactive.interactive_shell(chan) > > chan.close() > > client.close() > > > > Well Another approach I tried is instead of opening interactive_shell, directly issue command using; > > > > stdin, stdout, stderr = client.exec_command(bv_cmd) > > for line in stderr.readlines(): > > print line > > for line in stdout.readlines(): > > print line > > But problem here is client.exec_command(bv_cmd) waits for command to execute completely and then it returns to stdout,stderr. And I want to see the ouput from the command during its execution. Because my command takes long time for execution. > > > > Big Picture in My Mind: Big Picture I that want to achieve is, Opening different SSH connection to different host, and it will issue commands to all host, wait for execution. All execution should happen parallel.(just wanted to share my thought, and wanted to hear opinions from you all. Is this possible??). I am not big programmer, just 2 years experience with asp.net C# 2.0 so i would appreciate if discussion happens in simple english. Can any one comment on this.. From joshua at landau.ws Sun Aug 11 01:58:31 2013 From: joshua at landau.ws (Joshua Landau) Date: Sun, 11 Aug 2013 06:58:31 +0100 Subject: Paramiko Help. Execute command to Interactive Shell which is opened by SSHClient() In-Reply-To: <3e9da33f-f37f-4df2-9c97-c4f2010f0471@googlegroups.com> References: <3e9da33f-f37f-4df2-9c97-c4f2010f0471@googlegroups.com> Message-ID: On 11 August 2013 06:18, sagar varule wrote: > Can any one comment on this.. If you don't get replies here it's probably because no-one knows Paramiko. I suggest posting elsewhere to see if there are any Paramiko users in other places willing to help. There might be a Paramiko mailing list. You also didn't say what didn't work with the first block of code. Also, what is "interactive"? From sagar.varule at gmail.com Sun Aug 11 03:02:44 2013 From: sagar.varule at gmail.com (sagar varule) Date: Sun, 11 Aug 2013 00:02:44 -0700 (PDT) Subject: Paramiko Help. Execute command to Interactive Shell which is opened by SSHClient() In-Reply-To: References: <3e9da33f-f37f-4df2-9c97-c4f2010f0471@googlegroups.com> Message-ID: <65f9e894-2528-46da-875f-2bebfaa182c1@googlegroups.com> On Sunday, August 11, 2013 11:28:31 AM UTC+5:30, Joshua Landau wrote: > On 11 August 2013 06:18, sagar varule wrote: > > > Can any one comment on this.. > > > > If you don't get replies here it's probably because no-one knows > > Paramiko. I suggest posting elsewhere to see if there are any Paramiko > > users in other places willing to help. There might be a Paramiko > > mailing list. > > > > You also didn't say what didn't work with the first block of code. > > Also, what is "interactive"? Submitting Command to Interactive Shell through code did not work. From joshua at landau.ws Sun Aug 11 03:10:45 2013 From: joshua at landau.ws (Joshua Landau) Date: Sun, 11 Aug 2013 08:10:45 +0100 Subject: Paramiko Help. Execute command to Interactive Shell which is opened by SSHClient() In-Reply-To: <65f9e894-2528-46da-875f-2bebfaa182c1@googlegroups.com> References: <3e9da33f-f37f-4df2-9c97-c4f2010f0471@googlegroups.com> <65f9e894-2528-46da-875f-2bebfaa182c1@googlegroups.com> Message-ID: On 11 August 2013 08:02, sagar varule wrote: > On Sunday, August 11, 2013 11:28:31 AM UTC+5:30, Joshua Landau wrote: >> You also didn't say what didn't work with the first block of code. > > Submitting Command to Interactive Shell through code did not work. In what way didn't it work? What's the problem? Also, what is "Interactive Shell"? From rosuav at gmail.com Sun Aug 11 04:57:14 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 11 Aug 2013 09:57:14 +0100 Subject: Paramiko Help. Execute command to Interactive Shell which is opened by SSHClient() In-Reply-To: References: Message-ID: On Thu, Aug 8, 2013 at 8:20 AM, sagar varule wrote: > stdin, stdout, stderr = client.exec_command(bv_cmd) > for line in stderr.readlines(): > print line > for line in stdout.readlines(): > print line > But problem here is client.exec_command(bv_cmd) waits for command to execute completely and then it returns to stdout,stderr. And I want to see the ouput from the command during its execution. Because my command takes long time for execution. Are you certain that exec_command is what's waiting, and not readlines()? ChrisA From joshua at landau.ws Sun Aug 11 06:10:42 2013 From: joshua at landau.ws (Joshua Landau) Date: Sun, 11 Aug 2013 11:10:42 +0100 Subject: Paramiko Help. Execute command to Interactive Shell which is opened by SSHClient() In-Reply-To: References: Message-ID: On 11 August 2013 09:57, Chris Angelico wrote: > On Thu, Aug 8, 2013 at 8:20 AM, sagar varule wrote: >> stdin, stdout, stderr = client.exec_command(bv_cmd) >> for line in stderr.readlines(): >> print line >> for line in stdout.readlines(): >> print line >> But problem here is client.exec_command(bv_cmd) waits for command to execute completely and then it returns to stdout,stderr. And I want to see the ouput from the command during its execution. Because my command takes long time for execution. > > Are you certain that exec_command is what's waiting, and not readlines()? That's a very good catch; Sagar, you might want to look at: http://stackoverflow.com/a/4896288/1763356 for a good solution if this is the case. There might be more builtin ways (maybe select.select) but I'm betting that link's the most rigorous. From wxjmfauth at gmail.com Thu Aug 8 03:52:36 2013 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Thu, 8 Aug 2013 00:52:36 -0700 (PDT) Subject: Thinking Unicode Message-ID: <38f5397f-cb11-44f1-8adc-c043035d325c@googlegroups.com> I wrote many times on this list, the ascii (generic name for "byte string") world and the unicode world are two incompatible worlds. There are bridges, basically there are incompatible, they requires to think differently. There is an interesting case on the dev list: http://mail.python.org/pipermail/python-dev/2013-July/127420.html There is nothing wrong in polishing the documentation, but interestingly the discussion turned out about the usage of "--" and "---" instead of real en-dashes and em-dashes, understand use ascii and not unicode. It has been argued TeX uses "--" and "---". True for the pre-unicode engines. It's no more the case. Steven proposed the usage of \N{EM DASH}, ... Good point, a real step towards unicode, but why using ascii when one can use directly "?", "?"? Is it not the purpose to use unicode in an utf-8 file, many recommand? If utf-8 is (and has been created to be) compatible with ascii, it seems today the usage is to make utf-8 compatible with ascii! The .rst files have been touched and in my last check, 1-2 days ago, the --------- has been replaced by -------------. No trace of real en-dashes, em-dashes in diff's. What happen if confusion is reappearing? Simple, reopen a discussion and continue to not solve problems. ----- Somebody wrote: "... (and nobody really wants to type three hyphens except for a handful of typographical nuts)..." Completely "out of phase". Beyond that comment (or kind of comment), (I'm "spying" the misc. lists since years), not a suprise that Python and Unicode never work. jmf PS >>> '??'.encode('cp1252') b'\x96\x97' >>> '??'.encode('mac-roman') b'\xd0\xd1' >>> '??'.encode('latin-1') Traceback (most recent call last): File "", line 1, in UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-1: ordinal not in range(256) From zhangjiaqiang at gmail.com Thu Aug 8 05:03:28 2013 From: zhangjiaqiang at gmail.com (Zhang JiaQiang) Date: Thu, 8 Aug 2013 02:03:28 -0700 (PDT) Subject: To make simpleXMLRPC support multi-thread Message-ID: I try to use simpleXMLRPC and support request multithreads ?used ThreadingMixIn. Why what I get from the console still blocking ? launch the two clients at the same time?one finish, then begin the other, between them there is 15 sec. Here is the console output on server windows: Use Cc to exit DEBUG:root:111111 request dir the directory(/) DEBUG:root:block 111111 ... nio102 - - [08/Aug/2013 15:35:17] "POST /RPC2 HTTP/1.0" 200 - DEBUG:root:222222 request dir the directory(/) DEBUG:root:block 222222 ... nio102 - - [08/Aug/2013 15:35:32] "POST /RPC2 HTTP/1.0" 200 - The following are the codes: ###### client 1111111 ########## import xmlrpclib proxy = xmlrpclib.ServerProxy('http://xx.xx.xx.xx:9000') print proxy.dir_contents('/', '111111') ###### client 2222222 ########### import xmlrpclib proxy = xmlrpclib.ServerProxy('http://xx.xx.xx.xx:9000') print proxy.dir_contents('/', '222222') ###### server ################### from SimpleXMLRPCServer import SimpleXMLRPCServer from SocketServer import ThreadingMixIn import logging import os import time logging.basicConfig(level=logging.DEBUG) def list_contents(dir_name, client): logging.debug('%s request list the directory(%s)', client, dir_name) logging.debug('block %s request for 15 sec...' % client) time.sleep(15) return os.listdir(dir_name) class ListDirRPCServer(ThreadingMixIn, SimpleXMLRPCServer): def __init__(self, ip, port): self.server = SimpleXMLRPCServer((ip, port), logRequests=True) self.server.register_function(list_contents) def active_server(self): try: print "Use Cc to exit" self.server.serve_forever() except KeyboardInterrupt: print "exiting" if __name__ == '__main__': ip = 'xx.xx.xx.xx' port = 9000 list_rpc = ListDirRPCServer(ip, port) list_rpc.active_server() From dieter at handshake.de Fri Aug 9 02:34:27 2013 From: dieter at handshake.de (dieter) Date: Fri, 09 Aug 2013 08:34:27 +0200 Subject: To make simpleXMLRPC support multi-thread References: Message-ID: <87li4b1ifg.fsf@handshake.de> Zhang JiaQiang writes: > I try to use simpleXMLRPC and support request multithreads ?used ThreadingMixIn. > > Why what I get from the console still blocking ? I am not sure - but, you fail to call the inherited "__init__" method in your deriving class. This might things mess up. > ... > class ListDirRPCServer(ThreadingMixIn, SimpleXMLRPCServer): > def __init__(self, ip, port): > self.server = SimpleXMLRPCServer((ip, port), logRequests=True) > self.server.register_function(list_contents) Usually, in a derived class's "__init__" method, you should call the inherited "__init__" method. From neatuovi at gmail.com Thu Aug 8 05:45:00 2013 From: neatuovi at gmail.com (Neatu Ovidiu Gabriel) Date: Thu, 8 Aug 2013 02:45:00 -0700 (PDT) Subject: Suggestion: PEP for popping slices from lists Message-ID: The list.pop(index) returns the element represented by the index and also reduces the list by removing that element. So it a short one liner for doing both things. But when it comes for popping a slice of the list there is nothing similar for doing in that simple way. If you want to remove a slice and also reduce the list you will have something like this: a_list, a_slice = a_list[:size], a_list[size:] or even worser if you try to do the same for something in the middle. My proposal is the extension of list.pop for accepting a way for popping slices. When doing this: a_list.pop(i,j) pop will return the slice [i,j] and remove it from the list. For popping from an index to the end: a_list.pop(i, len(a_list)) Or even emptying the whole list: a_list.pop(0, len(a_list)) So this is it :) From __peter__ at web.de Thu Aug 8 06:07:16 2013 From: __peter__ at web.de (Peter Otten) Date: Thu, 08 Aug 2013 12:07:16 +0200 Subject: Suggestion: PEP for popping slices from lists References: Message-ID: Neatu Ovidiu Gabriel wrote: > The list.pop(index) returns the element represented by the index and also > reduces the list by removing that element. So it a short one liner for > doing both things. But when it comes for popping a slice of the list there > is nothing similar for doing in that simple way. > > If you want to remove a slice and also reduce the list you will have > something like this: > > a_list, a_slice = a_list[:size], a_list[size:] > > or even worser if you try to do the same for something in the middle. > > My proposal is the extension of list.pop for accepting a way for popping > slices. > > When doing this: > > a_list.pop(i,j) > > pop will return the slice [i,j] and remove it from the list. > > For popping from an index to the end: > > a_list.pop(i, len(a_list)) > > Or even emptying the whole list: > > a_list.pop(0, len(a_list)) > > > So this is it :) You'd use 'del' to remove a slice from a list. So: >>> def pop_slice(items, *indices): ... x = slice(*indices) ... result = items[x] ... del items[x] ... return result ... >>> items = range(10) >>> pop_slice(items, 3) [0, 1, 2] >>> items [3, 4, 5, 6, 7, 8, 9] >>> pop_slice(items, 3, 4) [6] >>> items [3, 4, 5, 7, 8, 9] >>> pop_slice(items, None, None, 2) [3, 5, 8] >>> items [4, 7, 9] But what's your use case? Does it occur often enough that you cannot afford a two-liner like result = items[start:stop] del items[start:stop] ? From neatuovi at gmail.com Thu Aug 8 06:38:18 2013 From: neatuovi at gmail.com (Neatu Ovidiu) Date: Thu, 8 Aug 2013 03:38:18 -0700 (PDT) Subject: Suggestion: PEP for popping slices from lists In-Reply-To: References: Message-ID: <531d89a8-61e1-4117-b4cc-f02b3be30bb9@googlegroups.com> On Thursday, August 8, 2013 1:07:16 PM UTC+3, Peter Otten wrote: > Neatu Ovidiu Gabriel wrote: > > > > > The list.pop(index) returns the element represented by the index and also > > > reduces the list by removing that element. So it a short one liner for > > > doing both things. But when it comes for popping a slice of the list there > > > is nothing similar for doing in that simple way. > > > > > > If you want to remove a slice and also reduce the list you will have > > > something like this: > > > > > > a_list, a_slice = a_list[:size], a_list[size:] > > > > > > or even worser if you try to do the same for something in the middle. > > > > > > My proposal is the extension of list.pop for accepting a way for popping > > > slices. > > > > > > When doing this: > > > > > > a_list.pop(i,j) > > > > > > pop will return the slice [i,j] and remove it from the list. > > > > > > For popping from an index to the end: > > > > > > a_list.pop(i, len(a_list)) > > > > > > Or even emptying the whole list: > > > > > > a_list.pop(0, len(a_list)) > > > > > > > > > So this is it :) > > > > You'd use 'del' to remove a slice from a list. So: > > > > >>> def pop_slice(items, *indices): > > ... x = slice(*indices) > > ... result = items[x] > > ... del items[x] > > ... return result > > ... > > >>> items = range(10) > > >>> pop_slice(items, 3) > > [0, 1, 2] > > >>> items > > [3, 4, 5, 6, 7, 8, 9] > > >>> pop_slice(items, 3, 4) > > [6] > > >>> items > > [3, 4, 5, 7, 8, 9] > > >>> pop_slice(items, None, None, 2) > > [3, 5, 8] > > >>> items > > [4, 7, 9] > > > > But what's your use case? > > Does it occur often enough that you cannot afford a two-liner like > > > > result = items[start:stop] > > del items[start:stop] > > > > ? > But what's your use case? > > Does it occur often enough that you cannot afford a two-liner like I think uses cases are plenty. And how can I figure out how often it occurs? I don't have a hint, it remains an open question. The issues I see so far with my proposal are: - what you said, how often it is used, if it deserves a place - maybe it should be a separate function like pop_slice to don't alter in anyway the behavior of good old pop - it will rise some backwards compatibility issues I just find it's a more pythonic way to deal with this situation. One short line instead of two or one long line is better. This shortness of typing is the core feature of Python so I think my proposal it's leap towards simplicity. From nicholas.cole at gmail.com Thu Aug 8 07:12:53 2013 From: nicholas.cole at gmail.com (Nicholas Cole) Date: Thu, 8 Aug 2013 12:12:53 +0100 Subject: Suggestion: PEP for popping slices from lists In-Reply-To: <531d89a8-61e1-4117-b4cc-f02b3be30bb9@googlegroups.com> References: <531d89a8-61e1-4117-b4cc-f02b3be30bb9@googlegroups.com> Message-ID: On Thu, Aug 8, 2013 at 11:38 AM, Neatu Ovidiu wrote: > > > > But what's your use case? > > > > Does it occur often enough that you cannot afford a two-liner like > I think uses cases are plenty. > > The possible cases I can think of would be better served with list comprehensions (what you seem to want is to create lists based on other lists) - but maybe I'm missing something. Could we have one example? N. -------------- next part -------------- An HTML attachment was scrubbed... URL: From neatuovi at gmail.com Thu Aug 8 07:40:19 2013 From: neatuovi at gmail.com (Neatu Ovidiu) Date: Thu, 8 Aug 2013 04:40:19 -0700 (PDT) Subject: Suggestion: PEP for popping slices from lists In-Reply-To: References: <531d89a8-61e1-4117-b4cc-f02b3be30bb9@googlegroups.com> Message-ID: On Thursday, August 8, 2013 2:12:53 PM UTC+3, Nicholas wrote: > On Thu, Aug 8, 2013 at 11:38 AM, Neatu Ovidiu wrote: > > > > > > > > > But what's your use case? > > > > > > Does it occur often enough that you cannot afford a two-liner like > > I think uses cases are plenty. > > > > > The possible cases I can think of would be better served with list comprehensions (what you seem to want is to create lists based on other lists) - but maybe I'm missing something. ?Could we have one example? > > > > N. This can be useful for doing all kinds of basic stuff. For example if you wanted to take 4 items of a list at at a time, do something with them and then update the list. jobs = ['job1', 'job2', 'job3', 'job5', 'job6', 'job7', 'job8', 'job9', 'job10'] while jobs: print jobs.pop_slice(0,4) From skip at pobox.com Thu Aug 8 10:20:58 2013 From: skip at pobox.com (Skip Montanaro) Date: Thu, 8 Aug 2013 09:20:58 -0500 Subject: Suggestion: PEP for popping slices from lists In-Reply-To: References: <531d89a8-61e1-4117-b4cc-f02b3be30bb9@googlegroups.com> Message-ID: On Thu, Aug 8, 2013 at 6:40 AM, Neatu Ovidiu wrote: > This can be useful for doing all kinds of basic stuff. For example if you wanted to take 4 items of a list at at a time, do something with them and then update the list. > > jobs = ['job1', 'job2', 'job3', 'job5', 'job6', 'job7', 'job8', 'job9', 'job10'] > while jobs: > print jobs.pop_slice(0,4) My initial reaction to this is that you want to infer structure where none exists, so why not make the structure explicit? In any case, couldn't you subclass the list type and add a pop_slice method to do what you want? I'm not an iterator maven, but this also seems like something you could mix up from something in the itertools module. Skip From neatuovi at gmail.com Thu Aug 8 07:44:05 2013 From: neatuovi at gmail.com (Neatu Ovidiu) Date: Thu, 8 Aug 2013 04:44:05 -0700 (PDT) Subject: Suggestion: PEP for popping slices from lists In-Reply-To: References: <531d89a8-61e1-4117-b4cc-f02b3be30bb9@googlegroups.com> Message-ID: <9bd6192b-2c71-4662-808a-fd7e74dedeb8@googlegroups.com> On Thursday, August 8, 2013 2:12:53 PM UTC+3, Nicholas wrote: > On Thu, Aug 8, 2013 at 11:38 AM, Neatu Ovidiu wrote: > > > > > > > > > But what's your use case? > > > > > > Does it occur often enough that you cannot afford a two-liner like > > I think uses cases are plenty. > > > > > The possible cases I can think of would be better served with list comprehensions (what you seem to want is to create lists based on other lists) - but maybe I'm missing something. ?Could we have one example? > > > > N. This can be useful for doing all kinds of basic stuff. For example if you wanted to take 4 items of a list at at a time, do something with them and then update the list. jobs = ['job1', 'job2', 'job3', 'job4', 'job5', 'job6', 'job7', 'job8', 'job9', 'job10'] while jobs: print(jobs.pop_slice(0,4)) should output 'job1', 'job2', 'job3', 'job4' 'job5', 'job6', 'job7', 'job8' 'job9', 'job10' From neatuovi at gmail.com Thu Aug 8 07:50:27 2013 From: neatuovi at gmail.com (Neatu Ovidiu) Date: Thu, 8 Aug 2013 04:50:27 -0700 (PDT) Subject: Suggestion: PEP for popping slices from lists In-Reply-To: <9bd6192b-2c71-4662-808a-fd7e74dedeb8@googlegroups.com> References: <531d89a8-61e1-4117-b4cc-f02b3be30bb9@googlegroups.com> <9bd6192b-2c71-4662-808a-fd7e74dedeb8@googlegroups.com> Message-ID: <62769807-dff7-40a0-a060-d1e0b7c0685f@googlegroups.com> On Thursday, August 8, 2013 2:44:05 PM UTC+3, Neatu Ovidiu wrote: > On Thursday, August 8, 2013 2:12:53 PM UTC+3, Nicholas wrote: > > > On Thu, Aug 8, 2013 at 11:38 AM, Neatu Ovidiu wrote: > > > > > > > > > > > > > > > > > > > > > > > > > But what's your use case? > > > > > > > > > > > > > > Does it occur often enough that you cannot afford a two-liner like > > > > > > I think uses cases are plenty. > > > > > > > > > > > > > > > The possible cases I can think of would be better served with list comprehensions (what you seem to want is to create lists based on other lists) - but maybe I'm missing something. ?Could we have one example? > > > > > > > > > > > > N. > > > > This can be useful for doing all kinds of basic stuff. For example if you wanted to take 4 items of a list at at a time, do something with them and then update the list. > > > > jobs = ['job1', 'job2', 'job3', 'job4', 'job5', 'job6', 'job7', 'job8', 'job9', 'job10'] > > while jobs: > > print(jobs.pop_slice(0,4)) > > > > should output > > > > 'job1', 'job2', 'job3', 'job4' > > 'job5', 'job6', 'job7', 'job8' > > 'job9', 'job10' The idea "popped" in my mind while thinking about this question. http://stackoverflow.com/questions/18121416/right-split-a-string-into-groups-of-3/18122084 I founded the list comprehensions solutions kind of cumbersome and thought that there should be a simple way to do this kind of stuff. From nicholas.cole at gmail.com Thu Aug 8 09:08:13 2013 From: nicholas.cole at gmail.com (Nicholas Cole) Date: Thu, 8 Aug 2013 14:08:13 +0100 Subject: Suggestion: PEP for popping slices from lists In-Reply-To: <62769807-dff7-40a0-a060-d1e0b7c0685f@googlegroups.com> References: <531d89a8-61e1-4117-b4cc-f02b3be30bb9@googlegroups.com> <9bd6192b-2c71-4662-808a-fd7e74dedeb8@googlegroups.com> <62769807-dff7-40a0-a060-d1e0b7c0685f@googlegroups.com> Message-ID: On Thu, Aug 8, 2013 at 12:50 PM, Neatu Ovidiu wrote: > On Thursday, August 8, 2013 2:44:05 PM UTC+3, Neatu Ovidiu wrote: > > On Thursday, August 8, 2013 2:12:53 PM UTC+3, Nicholas wrote: > > > > > On Thu, Aug 8, 2013 at 11:38 AM, Neatu Ovidiu > wrote: > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > But what's your use case? > > > > > > > > > > > > > > > > > > > > > > Does it occur often enough that you cannot afford a two-liner like > > > > > > > > > > I think uses cases are plenty. > > > > > > > > > > > > > > > > > > > > > > > > > The possible cases I can think of would be better served with list > comprehensions (what you seem to want is to create lists based on other > lists) - but maybe I'm missing something. Could we have one example? > > > > > > > > > > > > > > > > > > > > N. > > > > > > > > This can be useful for doing all kinds of basic stuff. For example if > you wanted to take 4 items of a list at at a time, do something with them > and then update the list. > > > > > > > > jobs = ['job1', 'job2', 'job3', 'job4', 'job5', 'job6', 'job7', 'job8', > 'job9', 'job10'] > > > > while jobs: > > > > print(jobs.pop_slice(0,4)) > > > > > > > > should output > > > > > > > > 'job1', 'job2', 'job3', 'job4' > > > > 'job5', 'job6', 'job7', 'job8' > > > > 'job9', 'job10' > > The idea "popped" in my mind while thinking about this question. > > http://stackoverflow.com/questions/18121416/right-split-a-string-into-groups-of-3/18122084 > I founded the list comprehensions solutions kind of cumbersome and thought > that there should be a simple way to do this kind of stuff. > -- > http://mail.python.org/mailman/listinfo/python-list > Still seems a bit like a solution looking for a problem to me. Why would you want to take four items at a time for a job from an arbitrary part of a list? I agree splitting a string into groups of three looks a bit cumbersome in the example you've given, but a generator could be written quite easily, and would almost certainly be quicker than trying to alter the list in place. Best wishes, N. -------------- next part -------------- An HTML attachment was scrubbed... URL: From neatuovi at gmail.com Thu Aug 8 09:32:36 2013 From: neatuovi at gmail.com (Neatu Ovidiu) Date: Thu, 8 Aug 2013 06:32:36 -0700 (PDT) Subject: Suggestion: PEP for popping slices from lists In-Reply-To: References: <531d89a8-61e1-4117-b4cc-f02b3be30bb9@googlegroups.com> <9bd6192b-2c71-4662-808a-fd7e74dedeb8@googlegroups.com> <62769807-dff7-40a0-a060-d1e0b7c0685f@googlegroups.com> Message-ID: <7c8c0342-f533-4e2e-9df4-71b2ccaf1929@googlegroups.com> On Thursday, August 8, 2013 4:08:13 PM UTC+3, Nicholas wrote: > On Thu, Aug 8, 2013 at 12:50 PM, Neatu Ovidiu wrote: > > > > > On Thursday, August 8, 2013 2:44:05 PM UTC+3, Neatu Ovidiu wrote: > > > On Thursday, August 8, 2013 2:12:53 PM UTC+3, Nicholas wrote: > > > > > > > On Thu, Aug 8, 2013 at 11:38 AM, Neatu Ovidiu wrote: > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > But what's your use case? > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > Does it occur often enough that you cannot afford a two-liner like > > > > > > > > > > > > > > I think uses cases are plenty. > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > The possible cases I can think of would be better served with list comprehensions (what you seem to want is to create lists based on other lists) - but maybe I'm missing something. ?Could we have one example? > > > > > > > > > > > > > > > > > > > > > > > > > > > > > N. > > > > > > > > > > > > This can be useful for doing all kinds of basic stuff. For example if you wanted to take 4 items of a list at at a time, do something with them and then update the list. > > > > > > > > > > > > jobs = ['job1', 'job2', 'job3', 'job4', 'job5', 'job6', 'job7', 'job8', 'job9', 'job10'] > > > > > > while jobs: > > > > > > ? ? print(jobs.pop_slice(0,4)) > > > > > > > > > > > > should output > > > > > > > > > > > > 'job1', 'job2', 'job3', 'job4' > > > > > > 'job5', 'job6', 'job7', 'job8' > > > > > > 'job9', 'job10' > > > > The idea "popped" in my mind while thinking about this question. > > http://stackoverflow.com/questions/18121416/right-split-a-string-into-groups-of-3/18122084 > > I founded the list comprehensions solutions kind of cumbersome and thought that there should be a simple way to do this kind of stuff. > > -- > > http://mail.python.org/mailman/listinfo/python-list > > > > > > Still seems a bit like a solution looking for a problem to me. > > > > Why would you want to take four items at a time for a job from an arbitrary part of a list? ?I agree splitting a string into groups of three looks a bit cumbersome in the example you've given, but a generator could be written quite easily, and would almost certainly be quicker than trying to alter the list in place. > > > > Best wishes, > > > N. You are perfectly right. But I looked at it more like an improvement in the style of writing solutions and also a natural option because slices are highly present all over in python. From nicholas.cole at gmail.com Thu Aug 8 10:03:19 2013 From: nicholas.cole at gmail.com (Nicholas Cole) Date: Thu, 8 Aug 2013 15:03:19 +0100 Subject: Suggestion: PEP for popping slices from lists In-Reply-To: <7c8c0342-f533-4e2e-9df4-71b2ccaf1929@googlegroups.com> References: <531d89a8-61e1-4117-b4cc-f02b3be30bb9@googlegroups.com> <9bd6192b-2c71-4662-808a-fd7e74dedeb8@googlegroups.com> <62769807-dff7-40a0-a060-d1e0b7c0685f@googlegroups.com> <7c8c0342-f533-4e2e-9df4-71b2ccaf1929@googlegroups.com> Message-ID: On Thu, Aug 8, 2013 at 2:32 PM, Neatu Ovidiu wrote: > On Thursday, August 8, 2013 4:08:13 PM UTC+3, Nicholas wrote: > > On Thu, Aug 8, 2013 at 12:50 PM, Neatu Ovidiu wrote: > > > > > > > > > > On Thursday, August 8, 2013 2:44:05 PM UTC+3, Neatu Ovidiu wrote: > > > > > On Thursday, August 8, 2013 2:12:53 PM UTC+3, Nicholas wrote: > > > > > > > > > > > On Thu, Aug 8, 2013 at 11:38 AM, Neatu Ovidiu > wrote: > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > But what's your use case? > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > Does it occur often enough that you cannot afford a two-liner like > > > > > > > > > > > > > > > > > > > > > > I think uses cases are plenty. > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > The possible cases I can think of would be better served with list > comprehensions (what you seem to want is to create lists based on other > lists) - but maybe I'm missing something. Could we have one example? > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > N. > > > > > > > > > > > > > > > > > > > > This can be useful for doing all kinds of basic stuff. For example if > you wanted to take 4 items of a list at at a time, do something with them > and then update the list. > > > > > > > > > > > > > > > > > > > > jobs = ['job1', 'job2', 'job3', 'job4', 'job5', 'job6', 'job7', > 'job8', 'job9', 'job10'] > > > > > > > > > > while jobs: > > > > > > > > > > print(jobs.pop_slice(0,4)) > > > > > > > > > > > > > > > > > > > > should output > > > > > > > > > > > > > > > > > > > > 'job1', 'job2', 'job3', 'job4' > > > > > > > > > > 'job5', 'job6', 'job7', 'job8' > > > > > > > > > > 'job9', 'job10' > > > > > > > > The idea "popped" in my mind while thinking about this question. > > > > > http://stackoverflow.com/questions/18121416/right-split-a-string-into-groups-of-3/18122084 > > > > I founded the list comprehensions solutions kind of cumbersome and > thought that there should be a simple way to do this kind of stuff. > > > > -- > > > > http://mail.python.org/mailman/listinfo/python-list > > > > > > > > > > > > Still seems a bit like a solution looking for a problem to me. > > > > > > > > Why would you want to take four items at a time for a job from an > arbitrary part of a list? I agree splitting a string into groups of three > looks a bit cumbersome in the example you've given, but a generator could > be written quite easily, and would almost certainly be quicker than trying > to alter the list in place. > > > > > > > > Best wishes, > > > > > > N. > > You are perfectly right. But I looked at it more like an improvement in > the style of writing solutions and also a natural option because slices are > highly present all over in python. > I wasn't knocking it. I was just trying to think it through. -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Thu Aug 8 16:03:27 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 08 Aug 2013 16:03:27 -0400 Subject: Suggestion: PEP for popping slices from lists In-Reply-To: <9bd6192b-2c71-4662-808a-fd7e74dedeb8@googlegroups.com> References: <531d89a8-61e1-4117-b4cc-f02b3be30bb9@googlegroups.com> <9bd6192b-2c71-4662-808a-fd7e74dedeb8@googlegroups.com> Message-ID: On 8/8/2013 7:44 AM, Neatu Ovidiu wrote: Objection 1. People usually want to chunk sequences, not lists specifically. We now try to add new features that work with iterators and iterables generally, not just lists. > This can be useful for doing all kinds of basic stuff. For example if you wanted to take 4 items of a list at at a time, do something with them and then update the list. > > jobs = ['job1', 'job2', 'job3', 'job4', 'job5', 'job6', 'job7', 'job8', 'job9', 'job10'] > while jobs: > print(jobs.pop_slice(0,4)) > > should output > > 'job1', 'job2', 'job3', 'job4' > 'job5', 'job6', 'job7', 'job8' > 'job9', 'job10' Objection 2. Usually when one wants to do this sort of thing, one wants the list either be intact or empty at the end. Emptying it chunk by chunk is worse than useless because it turns an O(n) process into an O(n*n) process. The same is true of destructively iterating through a list with .pop(0). When I proposed the addition of .pop(), I meant it as the inverses of .append and did not include the (optional) index parameter. It is seldom used and usually only once, to remove a single leading item. The addition of iterators, which occurred *after* the addition of .pop, replaced some uses of .pop(0). For instance first = mylist.pop(0) # O(N) operation for item in mylist: process(first, item) del mylist can, since 2.2, be written as it = iter(mylist) first = next(it) # O(1) operation for item in it: process(first, item) del mylist If .pop were being added today, I would argue against including the index parameter. -- Terry Jan Reedy From joshua at landau.ws Thu Aug 8 17:32:27 2013 From: joshua at landau.ws (Joshua Landau) Date: Thu, 8 Aug 2013 22:32:27 +0100 Subject: Suggestion: PEP for popping slices from lists In-Reply-To: References: <531d89a8-61e1-4117-b4cc-f02b3be30bb9@googlegroups.com> <9bd6192b-2c71-4662-808a-fd7e74dedeb8@googlegroups.com> Message-ID: On 8 August 2013 21:03, Terry Reedy wrote: > If .pop were being added today, I would argue against including the index > parameter. GASP! That's no fair! 1) When using pop you normally want to keep the mutability available, so iter(mylist) is a no-go. 2) When using the index, it's often somewhere in the middle that you're popping from 3) There's always deque for deques That said you can always use blist which has O(log n) time complexity for these pops. It's a hassle it's not stdlib :/. From python.list at tim.thechases.com Thu Aug 8 17:50:25 2013 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 8 Aug 2013 16:50:25 -0500 Subject: Suggestion: PEP for popping slices from lists In-Reply-To: References: <531d89a8-61e1-4117-b4cc-f02b3be30bb9@googlegroups.com> <9bd6192b-2c71-4662-808a-fd7e74dedeb8@googlegroups.com> Message-ID: <20130808165025.307db702@bigbox.christie.dr> On 2013-08-08 22:32, Joshua Landau wrote: > On 8 August 2013 21:03, Terry Reedy wrote: > > If .pop were being added today, I would argue against including > > the index parameter. > > 3) There's always deque for deques Unless you have pre-2.4 code, in which case I'm glad .pop() was included (but in this hypothetical world, we'd have a deque in pre-2.4 too :-D -tkc From tjreedy at udel.edu Thu Aug 8 18:10:52 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 08 Aug 2013 18:10:52 -0400 Subject: Suggestion: PEP for popping slices from lists In-Reply-To: References: <531d89a8-61e1-4117-b4cc-f02b3be30bb9@googlegroups.com> <9bd6192b-2c71-4662-808a-fd7e74dedeb8@googlegroups.com> Message-ID: On 8/8/2013 5:32 PM, Joshua Landau wrote: > On 8 August 2013 21:03, Terry Reedy wrote: >> If .pop were being added today, I would argue against including the index >> parameter. > > GASP! That's no fair! > > 1) When using pop you normally want to keep the mutability available, > so iter(mylist) is a no-go. > 2) When using the index, it's often somewhere in the middle that > you're popping from I have never done that and I do not believe I have ever seen that. It certainly is extremely rare in my experience. Removing an item *after* looking at it is something different. for i in range(len(mylist), -1, -1): if pred(mylist[i]): del mylist[i] -- Terry Jan Reedy From nospam at nospam.com Thu Aug 8 05:54:41 2013 From: nospam at nospam.com (Gilles) Date: Thu, 08 Aug 2013 11:54:41 +0200 Subject: [ActivePython] Add Qt/PyQt and wxWidgets/wxPython? Message-ID: Hello I upgraded to ActivePython 2.7.2.5, and would like to get started learning about Qt and wxWidgets in Python. I have a couple of question: 1. Are there obvious reasons to choose either QT/PyQt or wxWidgets/wxPython? I have the impression that Qt is a richer GUI than wxWidgets, but it could just be an impression. 2. Is there a no-brainer article that explains how to add Qt and wxWidgets to ActivePython community edition, since they're only included in the Business and Enterprise editions? Thank you. From gouzounakis at hotmail.com Thu Aug 8 07:01:29 2013 From: gouzounakis at hotmail.com (D. Xenakis) Date: Thu, 8 Aug 2013 04:01:29 -0700 (PDT) Subject: paramiko installation problem Message-ID: <4554cd28-e0a8-407d-93ca-06cc267fb938@googlegroups.com> Im having problems using paramiko after installation on my Win7 64bit system. I can see both paramiko and pycrypto being "there" installed via pip list: I have tried so many different ways but in the end im always getting the same error when trying to import paramiko: (i can import Crypto with no problems) Traceback (most recent call last): File "", line 1, in import paramiko File "C:\Python33\lib\site-packages\paramiko\__init__.py", line 64, in from transport import SecurityOptions, Transport ImportError: No module named 'transport' Please can you help? thx From gouzounakis at hotmail.com Thu Aug 8 14:04:39 2013 From: gouzounakis at hotmail.com (D. Xenakis) Date: Thu, 8 Aug 2013 11:04:39 -0700 (PDT) Subject: paramiko installation problem In-Reply-To: <4554cd28-e0a8-407d-93ca-06cc267fb938@googlegroups.com> References: <4554cd28-e0a8-407d-93ca-06cc267fb938@googlegroups.com> Message-ID: >>> import sys >>> print (sys.path) returns: ['C:\\Python33\\Lib\\idlelib', 'C:\\Python33\\lib\\site-packages\\pip-1.4-py3.3.egg', 'C:\\Windows\\system32\\python33.zip', 'C:\\Python33\\DLLs', 'C:\\Python33\\lib', 'C:\\Python33', 'C:\\Python33\\lib\\site-packages'] then if i type: >>> sys.path.append('C:\\Python33\\Lib\\site-packages\\paramiko') and then again.. >>> print (sys.path) returns: ['C:\\Python33\\Lib\\idlelib', 'C:\\Python33\\lib\\site-packages\\pip-1.4-py3.3.egg', 'C:\\Windows\\system32\\python33.zip', 'C:\\Python33\\DLLs', 'C:\\Python33\\lib', 'C:\\Python33', 'C:\\Python33\\lib\\site-packages', 'C:\\Python33\\Lib\\site-packages\\paramiko'] then if i type: >>> import paramiko i get this: Traceback (most recent call last): File "", line 1, in import paramiko File "C:\Python33\lib\site-packages\paramiko\__init__.py", line 64, in from transport import SecurityOptions, Transport File "C:\Python33\Lib\site-packages\paramiko\transport.py", line 296 except socket.error, e: ^ SyntaxError: invalid syntax ---------------------------------------------- when i close and reopen IDLE, then 'C:\\Python33\\Lib\\site-packages\\paramiko' is getting deleted. If my problem from the first post above has to do something with the path, then what exactly should i do? I would also like to ask here something extra.. :) in system enviroment variables my path contains the following things: C:\Python33\Lib\site-packages\PyQt5;C:\Python33\Scripts\;C:\Python33\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Python33\Lib\site-packages\paramiko;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files\NEST;C:\Program Files (x86)\Windows Live\Shared so what exactly is the difference between this path and the above.? A huge THANKS for the one who will find the courage to answer to this newbie ! From python at mrabarnett.plus.com Thu Aug 8 14:25:56 2013 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 08 Aug 2013 19:25:56 +0100 Subject: paramiko installation problem In-Reply-To: References: <4554cd28-e0a8-407d-93ca-06cc267fb938@googlegroups.com> Message-ID: <5203E2B4.7050000@mrabarnett.plus.com> On 08/08/2013 19:04, D. Xenakis wrote: >>>> import sys >>>> print (sys.path) > > returns: > > ['C:\\Python33\\Lib\\idlelib', 'C:\\Python33\\lib\\site-packages\\pip-1.4-py3.3.egg', 'C:\\Windows\\system32\\python33.zip', 'C:\\Python33\\DLLs', 'C:\\Python33\\lib', 'C:\\Python33', 'C:\\Python33\\lib\\site-packages'] > > then if i type: >>>> sys.path.append('C:\\Python33\\Lib\\site-packages\\paramiko') > > and then again.. >>>> print (sys.path) > > returns: > > ['C:\\Python33\\Lib\\idlelib', 'C:\\Python33\\lib\\site-packages\\pip-1.4-py3.3.egg', 'C:\\Windows\\system32\\python33.zip', 'C:\\Python33\\DLLs', 'C:\\Python33\\lib', 'C:\\Python33', 'C:\\Python33\\lib\\site-packages', 'C:\\Python33\\Lib\\site-packages\\paramiko'] > > then if i type: >>>> import paramiko > > i get this: > > Traceback (most recent call last): > File "", line 1, in > import paramiko > File "C:\Python33\lib\site-packages\paramiko\__init__.py", line 64, in > from transport import SecurityOptions, Transport > File "C:\Python33\Lib\site-packages\paramiko\transport.py", line 296 > except socket.error, e: > ^ > SyntaxError: invalid syntax > [snip] That syntax is supported in Python 2. In Python 3 it would be: except socket.error as e: I don't think that paramiko supports Python 3 (yet?). From gouzounakis at hotmail.com Thu Aug 8 15:25:48 2013 From: gouzounakis at hotmail.com (D. Xenakis) Date: Thu, 8 Aug 2013 12:25:48 -0700 (PDT) Subject: paramiko installation problem In-Reply-To: References: <4554cd28-e0a8-407d-93ca-06cc267fb938@googlegroups.com> Message-ID: Wow thats bad news. Any workaround? What im trying to succeed here is create one SSH tunnel, so that i can connect from a python script running on my pc, to a remote MySQL database running on my Host and id like to stick with Python 3.3 . Any thoughts? From kurt.alfred.mueller at gmail.com Thu Aug 8 10:23:46 2013 From: kurt.alfred.mueller at gmail.com (Kurt Mueller) Date: Thu, 08 Aug 2013 16:23:46 +0200 Subject: right adjusted strings containing umlauts Message-ID: <5203A9F2.1090004@gmail.com> I'd like to print strings right adjusted. ( Python 2.7.3, Linux 3.4.47-2.38-desktop ) from __future__ import print_function print( '>{0:>3}<'.format( 'a' ) ) > a< But if the string contains an Umlaut: print( '>{0:>3}<'.format( '?' ) ) > ?< Same with % notation: print( '>%3s<' % ( 'a' ) ) > a< print( '>%3s<' % ( '?' ) ) > ?< For a string with no Umlaut it uses 3 characters, but for an Umlaut it uses only 2 characters. I guess it has to to with unicode. How do I get it right? TIA -- Kurt Mueller From neilc at norwich.edu Thu Aug 8 10:40:27 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 8 Aug 2013 14:40:27 GMT Subject: right adjusted strings containing umlauts References: Message-ID: On 2013-08-08, Kurt Mueller wrote: > I'd like to print strings right adjusted. > ( Python 2.7.3, Linux 3.4.47-2.38-desktop ) > > from __future__ import print_function > print( '>{0:>3}<'.format( 'a' ) ) >> a< > > But if the string contains an Umlaut: > print( '>{0:>3}<'.format( '??' ) ) >> ??< > > Same with % notation: > print( '>%3s<' % ( 'a' ) ) >> a< > print( '>%3s<' % ( '??' ) ) >> ??< > > For a string with no Umlaut it uses 3 characters, but for an > Umlaut it uses only 2 characters. > > I guess it has to to with unicode. > How do I get it right? You guessed it! Use unicode strings instead of byte strings, e.g., u"...". -- Neil Cerutti From python at mrabarnett.plus.com Thu Aug 8 11:19:38 2013 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 08 Aug 2013 16:19:38 +0100 Subject: right adjusted strings containing umlauts In-Reply-To: References: Message-ID: <5203B70A.2050904@mrabarnett.plus.com> On 08/08/2013 15:40, Neil Cerutti wrote: > On 2013-08-08, Kurt Mueller wrote: >> I'd like to print strings right adjusted. >> ( Python 2.7.3, Linux 3.4.47-2.38-desktop ) >> >> from __future__ import print_function >> print( '>{0:>3}<'.format( 'a' ) ) >>> a< >> >> But if the string contains an Umlaut: >> print( '>{0:>3}<'.format( '??' ) ) >>> ??< >> >> Same with % notation: >> print( '>%3s<' % ( 'a' ) ) >>> a< >> print( '>%3s<' % ( '??' ) ) >>> ??< >> >> For a string with no Umlaut it uses 3 characters, but for an >> Umlaut it uses only 2 characters. >> >> I guess it has to to with unicode. >> How do I get it right? > > You guessed it! > > Use unicode strings instead of byte strings, e.g., u"...". > It also matters which actual codepoints you're using in the Unicode string. You could have u'?', which is one codepoint (u'\xE4' or u'\N{LATIN SMALL LETTER A WITH DIAERESIS}'), or u'a?', which two codepoints (u'a\u0308' or u'\N{LATIN SMALL LETTER A}\N{COMBINING DIAERESIS}'). From jfharden at gmail.com Thu Aug 8 10:43:11 2013 From: jfharden at gmail.com (jfharden at gmail.com) Date: Thu, 8 Aug 2013 07:43:11 -0700 (PDT) Subject: right adjusted strings containing umlauts In-Reply-To: References: Message-ID: <9781df99-f9c8-4217-aa67-7a714b7f2ebe@googlegroups.com> On Thursday, 8 August 2013 15:23:46 UTC+1, Kurt Mueller wrote: > I'd like to print strings right adjusted. > > print( '>{0:>3}<'.format( '?' ) ) > Make both strings unicode print( u'>{0:>3}<'.format( u'?' ) ) Why not use rjust for it though? u'?'.rjust(3) -- Jonathan From kurt.alfred.mueller at gmail.com Thu Aug 8 11:24:49 2013 From: kurt.alfred.mueller at gmail.com (Kurt Mueller) Date: Thu, 08 Aug 2013 17:24:49 +0200 Subject: right adjusted strings containing umlauts In-Reply-To: <9781df99-f9c8-4217-aa67-7a714b7f2ebe@googlegroups.com> References: <9781df99-f9c8-4217-aa67-7a714b7f2ebe@googlegroups.com> Message-ID: <5203B841.4060304@gmail.com> Am 08.08.2013 16:43, schrieb jfharden at gmail.com: > On Thursday, 8 August 2013 15:23:46 UTC+1, Kurt Mueller wrote: >> I'd like to print strings right adjusted. >> print( '>{0:>3}<'.format( '?' ) ) > > Make both strings unicode > print( u'>{0:>3}<'.format( u'?' ) ) > Why not use rjust for it though? > u'?'.rjust(3) In real life there is a list of strings in output_list from a command like: output_list = shlex.split( input_string, bool_cmnt, bool_posi, ) input_string is from a file, bool_* are either True or False repr( output_list ) ['\xc3\xb6', '\xc3\xbc', 'i', 's', 'f'] which should be printed right aligned. using: print( u'{0:>3} {1:>3} {2:>3} {3:>3} {4:>3}'.format( *output_list ) ) ( In real life, the alignement and the width is variable ) How do I prepare output_list the pythonic way to be unicode strings? What do I do, when input_strings/output_list has other codings like iso-8859-1? TIA -- Kurt Mueller From __peter__ at web.de Thu Aug 8 11:44:26 2013 From: __peter__ at web.de (Peter Otten) Date: Thu, 08 Aug 2013 17:44:26 +0200 Subject: right adjusted strings containing umlauts References: <9781df99-f9c8-4217-aa67-7a714b7f2ebe@googlegroups.com> <5203B841.4060304@gmail.com> Message-ID: Kurt Mueller wrote: > Am 08.08.2013 16:43, schrieb jfharden at gmail.com: >> On Thursday, 8 August 2013 15:23:46 UTC+1, Kurt Mueller wrote: >>> I'd like to print strings right adjusted. >>> print( '>{0:>3}<'.format( '?' ) ) >> >> Make both strings unicode >> print( u'>{0:>3}<'.format( u'?' ) ) >> Why not use rjust for it though? >> u'?'.rjust(3) > > In real life there is a list of strings in output_list from a command > like: output_list = shlex.split( input_string, bool_cmnt, bool_posi, ) > input_string is from a file, bool_* are either True or False > repr( output_list ) > ['\xc3\xb6', '\xc3\xbc', 'i', 's', 'f'] > which should be printed right aligned. > using: > print( u'{0:>3} {1:>3} {2:>3} {3:>3} {4:>3}'.format( *output_list ) ) > ( In real life, the alignement and the width is variable ) > > How do I prepare output_list the pythonic way to be unicode strings? > What do I do, when input_strings/output_list has other codings like > iso-8859-1? You have to know the actual encoding. With that information it's easy: >>> output_list ['\xc3\xb6', '\xc3\xbc', 'i', 's', 'f'] >>> encoding = "utf-8" >>> output_list = [s.decode(encoding) for s in output_list] >>> print output_list [u'\xf6', u'\xfc', u'i', u's', u'f'] Don't worry that there are still escape codes -- when you print the individual list items the caracters will show up as expected: >>> print ", ".join(output_list) ?, ?, i, s, f From kurt.alfred.mueller at gmail.com Thu Aug 8 12:16:40 2013 From: kurt.alfred.mueller at gmail.com (Kurt Mueller) Date: Thu, 08 Aug 2013 18:16:40 +0200 Subject: right adjusted strings containing umlauts In-Reply-To: References: <9781df99-f9c8-4217-aa67-7a714b7f2ebe@googlegroups.com> <5203B841.4060304@gmail.com> Message-ID: <5203C468.4020001@gmail.com> Am 08.08.2013 17:44, schrieb Peter Otten: > Kurt Mueller wrote: >> What do I do, when input_strings/output_list has other codings like >> iso-8859-1? > > You have to know the actual encoding. With that information it's easy: >>>> output_list > ['\xc3\xb6', '\xc3\xbc', 'i', 's', 'f'] >>>> encoding = "utf-8" >>>> output_list = [s.decode(encoding) for s in output_list] >>>> print output_list > [u'\xf6', u'\xfc', u'i', u's', u'f'] How do I get to know the actual encoding? I read from stdin. There can be different encondings. Usually utf8 but also iso-8859-1/latin9 are to be expected. But sys.stdin.encoding sais always 'None'. TIA -- Kurt Mueller From __peter__ at web.de Thu Aug 8 12:34:51 2013 From: __peter__ at web.de (Peter Otten) Date: Thu, 08 Aug 2013 18:34:51 +0200 Subject: right adjusted strings containing umlauts References: <9781df99-f9c8-4217-aa67-7a714b7f2ebe@googlegroups.com> <5203B841.4060304@gmail.com> <5203C468.4020001@gmail.com> Message-ID: Kurt Mueller wrote: > Am 08.08.2013 17:44, schrieb Peter Otten: >> Kurt Mueller wrote: >>> What do I do, when input_strings/output_list has other codings like >>> iso-8859-1? >> >> You have to know the actual encoding. With that information it's easy: >>>>> output_list >> ['\xc3\xb6', '\xc3\xbc', 'i', 's', 'f'] >>>>> encoding = "utf-8" >>>>> output_list = [s.decode(encoding) for s in output_list] >>>>> print output_list >> [u'\xf6', u'\xfc', u'i', u's', u'f'] > > How do I get to know the actual encoding? > I read from stdin. There can be different encondings. > Usually utf8 but also iso-8859-1/latin9 are to be expected. > But sys.stdin.encoding sais always 'None'. Even with $ cat funny_pic.jpg | ./mypythonscript.py you could "successfully" (i. e. no errors) decode stdin using iso-8859-1. So unfortunately you have to guess. A simple strategy is to try utf-8 and fall back to iso-8859-1 if that fails with a UnicodeDecodeError. There's also https://pypi.python.org/pypi/chardet From rosuav at gmail.com Thu Aug 8 12:37:36 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 8 Aug 2013 17:37:36 +0100 Subject: right adjusted strings containing umlauts In-Reply-To: <5203C468.4020001@gmail.com> References: <9781df99-f9c8-4217-aa67-7a714b7f2ebe@googlegroups.com> <5203B841.4060304@gmail.com> <5203C468.4020001@gmail.com> Message-ID: On Thu, Aug 8, 2013 at 5:16 PM, Kurt Mueller wrote: > Am 08.08.2013 17:44, schrieb Peter Otten: >> Kurt Mueller wrote: >>> What do I do, when input_strings/output_list has other codings like >>> iso-8859-1? >> >> You have to know the actual encoding. With that information it's easy: >>>>> output_list >> ['\xc3\xb6', '\xc3\xbc', 'i', 's', 'f'] >>>>> encoding = "utf-8" >>>>> output_list = [s.decode(encoding) for s in output_list] >>>>> print output_list >> [u'\xf6', u'\xfc', u'i', u's', u'f'] > > How do I get to know the actual encoding? > I read from stdin. There can be different encondings. > Usually utf8 but also iso-8859-1/latin9 are to be expected. > But sys.stdin.encoding sais always 'None'. If you can switch to Python 3, life becomes a LOT easier. The Python 3 input() function (which does the same job as raw_input() from Python 2) returns a Unicode string, meaning that it takes care of encodings for you. ChrisA From kurt.alfred.mueller at gmail.com Fri Aug 23 11:47:46 2013 From: kurt.alfred.mueller at gmail.com (Kurt Mueller) Date: Fri, 23 Aug 2013 17:47:46 +0200 Subject: right adjusted strings containing umlauts In-Reply-To: References: <9781df99-f9c8-4217-aa67-7a714b7f2ebe@googlegroups.com> <5203B841.4060304@gmail.com> <5203C468.4020001@gmail.com> Message-ID: <52178422.60704@gmail.com> Am 08.08.2013 18:37, schrieb Chris Angelico: > On Thu, Aug 8, 2013 at 5:16 PM, Kurt Mueller > wrote: >> Am 08.08.2013 17:44, schrieb Peter Otten: >>> Kurt Mueller wrote: >>>> What do I do, when input_strings/output_list has other codings like >>>> iso-8859-1? >>> You have to know the actual encoding. With that information it's easy: >>>>>> output_list >>> ['\xc3\xb6', '\xc3\xbc', 'i', 's', 'f'] >>>>>> encoding = "utf-8" >>>>>> output_list = [s.decode(encoding) for s in output_list] >>>>>> print output_list >>> [u'\xf6', u'\xfc', u'i', u's', u'f'] >> How do I get to know the actual encoding? >> I read from stdin. There can be different encondings. >> Usually utf8 but also iso-8859-1/latin9 are to be expected. >> But sys.stdin.encoding sais always 'None'. > > If you can switch to Python 3, life becomes a LOT easier. The Python 3 > input() function (which does the same job as raw_input() from Python > 2) returns a Unicode string, meaning that it takes care of encodings > for you. Because I cannot switch to Python 3 for now my life is not so easy:-) For some text manipulation tasks I need a template to split lines from stdin into a list of strings the way shlex.split() does it. The encoding of the input can vary. For further processing in Python I need the list of strings to be in unicode. Here is template.py: ############################################################################################################## #!/usr/bin/env python # vim: set fileencoding=utf-8 : # split lines from stdin into a list of unicode strings # Muk 2013-08-23 # Python 2.7.3 from __future__ import print_function import sys import shlex import chardet bool_cmnt = True # shlex: skip comments bool_posx = True # shlex: posix mode (strings in quotes) for inpt_line in sys.stdin: print( 'inpt_line=' + repr( inpt_line ) ) enco_type = chardet.detect( inpt_line )[ 'encoding' ] # {'encoding': 'EUC-JP', 'confidence': 0.99} print( 'enco_type=' + repr( enco_type ) ) try: strg_inpt = shlex.split( inpt_line, bool_cmnt, bool_posx, ) # shlex does not work on unicode except Exception, errr: # usually 'No closing quotation' print( "error='%s' on inpt_line='%s'" % ( errr, inpt_line.rstrip(), ), file=sys.stderr, ) continue print( 'strg_inpt=' + repr( strg_inpt ) ) # list of strings strg_unic = [ strg.decode( enco_type ) for strg in strg_inpt ] # decode the strings into unicode print( 'strg_unic=' + repr( strg_unic ) ) # list of unicode strings ############################################################################################################## $ cat | template.py Comments are welcome. TIA -- Kurt Mueller From kurt.alfred.mueller at gmail.com Wed Aug 28 04:01:41 2013 From: kurt.alfred.mueller at gmail.com (Kurt Mueller) Date: Wed, 28 Aug 2013 10:01:41 +0200 Subject: right adjusted strings containing umlauts In-Reply-To: References: <9781df99-f9c8-4217-aa67-7a714b7f2ebe@googlegroups.com> <5203B841.4060304@gmail.com> <5203C468.4020001@gmail.com> Message-ID: <521DAE65.4030706@gmail.com> Am 08.08.2013 18:37, schrieb Chris Angelico: > On Thu, Aug 8, 2013 at 5:16 PM, Kurt Mueller > wrote: >> Am 08.08.2013 17:44, schrieb Peter Otten: >>> Kurt Mueller wrote: >>>> What do I do, when input_strings/output_list has other codings like >>>> iso-8859-1? >>> You have to know the actual encoding. With that information it's easy: >>>>>> output_list >>> ['\xc3\xb6', '\xc3\xbc', 'i', 's', 'f'] >>>>>> encoding = "utf-8" >>>>>> output_list = [s.decode(encoding) for s in output_list] >>>>>> print output_list >>> [u'\xf6', u'\xfc', u'i', u's', u'f'] >> How do I get to know the actual encoding? >> I read from stdin. There can be different encondings. >> Usually utf8 but also iso-8859-1/latin9 are to be expected. >> But sys.stdin.encoding sais always 'None'. > > If you can switch to Python 3, life becomes a LOT easier. The Python 3 > input() function (which does the same job as raw_input() from Python > 2) returns a Unicode string, meaning that it takes care of encodings > for you. Because I cannot switch to Python 3 for now my life is not so easy:-) For some text manipulation tasks I need a template to split lines from stdin into a list of strings the way shlex.split() does it. The encoding of the input can vary. For further processing in Python I need the list of strings to be in unicode. Here is template.py: ############################################################################################################## #!/usr/bin/env python # vim: set fileencoding=utf-8 : # split lines from stdin into a list of unicode strings # Muk 2013-08-23 # Python 2.7.3 from __future__ import print_function import sys import shlex import chardet bool_cmnt = True # shlex: skip comments bool_posx = True # shlex: posix mode (strings in quotes) for inpt_line in sys.stdin: print( 'inpt_line=' + repr( inpt_line ) ) enco_type = chardet.detect( inpt_line )[ 'encoding' ] # {'encoding': 'EUC-JP', 'confidence': 0.99} print( 'enco_type=' + repr( enco_type ) ) try: strg_inpt = shlex.split( inpt_line, bool_cmnt, bool_posx, ) # shlex does not work on unicode except Exception, errr: # usually 'No closing quotation' print( "error='%s' on inpt_line='%s'" % ( errr, inpt_line.rstrip(), ), file=sys.stderr, ) continue print( 'strg_inpt=' + repr( strg_inpt ) ) # list of strings strg_unic = [ strg.decode( enco_type ) for strg in strg_inpt ] # decode the strings into unicode print( 'strg_unic=' + repr( strg_unic ) ) # list of unicode strings ############################################################################################################## $ cat | template.py Comments are welcome. TIA -- Kurt Mueller -- Kurt Mueller From davea at davea.name Wed Aug 28 06:23:12 2013 From: davea at davea.name (Dave Angel) Date: Wed, 28 Aug 2013 10:23:12 +0000 (UTC) Subject: right adjusted strings containing umlauts References: <9781df99-f9c8-4217-aa67-7a714b7f2ebe@googlegroups.com> <5203B841.4060304@gmail.com> <5203C468.4020001@gmail.com> <521DAE65.4030706@gmail.com> Message-ID: On 28/8/2013 04:01, Kurt Mueller wrote: > Because I cannot switch to Python 3 for now my life is not so easy:-) > > For some text manipulation tasks I need a template to split lines > from stdin into a list of strings the way shlex.split() does it. > The encoding of the input can vary. > For further processing in Python I need the list of strings to be in unicode. > According to: http://docs.python.org/2/library/shlex.html """Prior to Python 2.7.3, this module did not support Unicode input""" I take that to mean that if you upgrade to Python 2.7.3, 2.7.4, or 2.7.5, you'll have Unicode support. Presumably that would mean you could decode the string before calling shlex.split(). -- DaveA From kurt.alfred.mueller at gmail.com Wed Aug 28 07:17:51 2013 From: kurt.alfred.mueller at gmail.com (kurt.alfred.mueller at gmail.com) Date: Wed, 28 Aug 2013 04:17:51 -0700 (PDT) Subject: right adjusted strings containing umlauts In-Reply-To: References: <9781df99-f9c8-4217-aa67-7a714b7f2ebe@googlegroups.com> <5203B841.4060304@gmail.com> <5203C468.4020001@gmail.com> <521DAE65.4030706@gmail.com> Message-ID: <473e3274-9581-4699-a836-798ab2f54758@googlegroups.com> On Wednesday, August 28, 2013 12:23:12 PM UTC+2, Dave Angel wrote: > On 28/8/2013 04:01, Kurt Mueller wrote: > > Because I cannot switch to Python 3 for now my life is not so easy:-) > > For some text manipulation tasks I need a template to split lines > > from stdin into a list of strings the way shlex.split() does it. > > The encoding of the input can vary. > > For further processing in Python I need the list of strings to be in unicode. > According to: > http://docs.python.org/2/library/shlex.html > """Prior to Python 2.7.3, this module did not support Unicode > input""" > I take that to mean that if you upgrade to Python 2.7.3, 2.7.4, or > 2.7.5, you'll have Unicode support. I have Python 2.7.3 > Presumably that would mean you could decode the string before calling > shlex.split(). Yes, see new template.py: ############################################################### #!/usr/bin/env python # vim: set fileencoding=utf-8 : # split lines from stdin into a list of unicode strings # decode before shlex # Muk 2013-08-28 # Python 2.7.3 from __future__ import print_function import sys import shlex import chardet bool_cmnt = True # shlex: skip comments bool_posx = True # shlex: posix mode (strings in quotes) for inpt_line in sys.stdin: print( 'inpt_line=' + repr( inpt_line ) ) enco_type = chardet.detect( inpt_line )[ 'encoding' ] # {'encoding': 'EUC-JP', 'confidence': 0.99} print( 'enco_type=' + repr( enco_type ) ) strg_unic = inpt_line.decode( enco_type ) # decode the input line into unicode print( 'strg_unic=' + repr( strg_unic ) ) # unicode input line try: strg_inpt = shlex.split( strg_unic, bool_cmnt, bool_posx, ) # check if shlex works on unicode except Exception, errr: # usually 'No closing quotation' print( "error='%s' on inpt_line='%s'" % ( errr, inpt_line.rstrip(), ), file=sys.stderr, ) continue print( 'strg_inpt=' + repr( strg_inpt ) ) # list of strings ############################################################### $ python -V Python 2.7.3 $ echo -e "a b c d e\na ? u 1 2" | template.py inpt_line='a b c d e\n' enco_type='ascii' strg_unic=u'a b c d e\n' strg_inpt=['a', 'b', 'c', 'd', 'e'] inpt_line='a \xc3\x96 u 1 2\n' enco_type='utf-8' strg_unic=u'a \xd6 u 1 2\n' error=''ascii' codec can't encode character u'\xd6' in position 2: ordinal not in range(128)' on inpt_line='a ? u 1 2' $ echo -e "a b c d e\na ? u 1 2" | recode utf8..latin9 | ./split_shlex_unicode.py inpt_line='a b c d e\n' enco_type='ascii' strg_unic=u'a b c d e\n' strg_inpt=['a', 'b', 'c', 'd', 'e'] inpt_line='a \xd6 u 1 2\n' enco_type='windows-1252' strg_unic=u'a \xd6 u 1 2\n' error=''ascii' codec can't encode character u'\xd6' in position 2: ordinal not in range(128)' on inpt_line='a ? u 1 2' $ As can be seen, shlex does work only with unicode strings decoded from 'ascii' strings. (Python 2.7.3) -- Kurt M?ller From davea at davea.name Thu Aug 8 11:50:25 2013 From: davea at davea.name (Dave Angel) Date: Thu, 8 Aug 2013 15:50:25 +0000 (UTC) Subject: right adjusted strings containing umlauts References: <9781df99-f9c8-4217-aa67-7a714b7f2ebe@googlegroups.com> <5203B841.4060304@gmail.com> Message-ID: Kurt Mueller wrote: > Am 08.08.2013 16:43, schrieb jfharden at gmail.com: >> On Thursday, 8 August 2013 15:23:46 UTC+1, Kurt Mueller wrote: >>> I'd like to print strings right adjusted. >>> print( '>{0:>3}<'.format( '?' ) ) >> >> Make both strings unicode >> print( u'>{0:>3}<'.format( u'?' ) ) >> Why not use rjust for it though? >> u'?'.rjust(3) > > In real life there is a list of strings in output_list from a command like: > output_list = shlex.split( input_string, bool_cmnt, bool_posi, ) > input_string is from a file, bool_* are either True or False > repr( output_list ) > ['\xc3\xb6', '\xc3\xbc', 'i', 's', 'f'] > which should be printed right aligned. > using: > print( u'{0:>3} {1:>3} {2:>3} {3:>3} {4:>3}'.format( *output_list ) ) > ( In real life, the alignement and the width is variable ) > > How do I prepare output_list the pythonic way to be unicode strings? > What do I do, when input_strings/output_list has other codings like iso-8859-1? > In general, when reading from an outside device like a file, convert to unicode immediately, while you still know the encoding used in that particular file. Then after all processing, worry about alignment only when you're about to output the string. And at that point, you're subject to the quirks of the font as well as the quirks of the encoding of the terminal. As MRAB has pointed out, sometimes two code points are used to represent a single character which will end up taking a single column. Likewise sometimes a single code point will take more than one "column" to display. Ideograms are one example, but a font which is not fixed pitch is another. If you're going to a standard terminal, all you can do is get close. This is why there are special functions for gui's to help with alignment. -- DaveA From kurt.alfred.mueller at gmail.com Thu Aug 8 12:27:06 2013 From: kurt.alfred.mueller at gmail.com (Kurt Mueller) Date: Thu, 08 Aug 2013 18:27:06 +0200 Subject: right adjusted strings containing umlauts In-Reply-To: References: <9781df99-f9c8-4217-aa67-7a714b7f2ebe@googlegroups.com> <5203B841.4060304@gmail.com> Message-ID: <5203C6DA.6060108@gmail.com> Now I have this small example: ---------------------------------------------------------- #!/usr/bin/env python # vim: set fileencoding=utf-8 : from __future__ import print_function import sys, shlex print( repr( sys.stdin.encoding ) ) strg_form = u'{0:>3} {1:>3} {2:>3} {3:>3} {4:>3}' for inpt_line in sys.stdin: proc_line = shlex.split( inpt_line, False, True, ) encoding = "utf-8" proc_line = [ strg.decode( encoding ) for strg in proc_line ] print( strg_form.format( *proc_line ) ) ---------------------------------------------------------- $ echo -e "a b c d e\na ? u 1 2" | file - /dev/stdin: UTF-8 Unicode text $ echo -e "a b c d e\na ? u 1 2" | ./align_compact.py None a b c d e a ? u 1 2 $ echo -e "a b c d e\na ? u 1 2" | recode utf8..latin9 | file - /dev/stdin: ISO-8859 text $ echo -e "a b c d e\na ? u 1 2" | recode utf8..latin9 | ./align_compact.py None a b c d e Traceback (most recent call last): File "./align_compact.py", line 13, in proc_line = [ strg.decode( encoding ) for strg in proc_line ] File "/usr/lib64/python2.7/encodings/utf_8.py", line 16, in decode return codecs.utf_8_decode(input, errors, True) UnicodeDecodeError: 'utf8' codec can't decode byte 0xf6 in position 0: invalid start byte muk at mcp20:/sw/prog/scripts/text_manip> How do I handle this two inputs? TIA -- Kurt Mueller From davea at davea.name Thu Aug 8 13:47:11 2013 From: davea at davea.name (Dave Angel) Date: Thu, 8 Aug 2013 17:47:11 +0000 (UTC) Subject: right adjusted strings containing umlauts References: <9781df99-f9c8-4217-aa67-7a714b7f2ebe@googlegroups.com> <5203B841.4060304@gmail.com> <5203C6DA.6060108@gmail.com> Message-ID: Kurt Mueller wrote: > Now I have this small example: > ---------------------------------------------------------- > #!/usr/bin/env python > # vim: set fileencoding=utf-8 : > > from __future__ import print_function > import sys, shlex > > print( repr( sys.stdin.encoding ) ) > > strg_form = u'{0:>3} {1:>3} {2:>3} {3:>3} {4:>3}' > for inpt_line in sys.stdin: > proc_line = shlex.split( inpt_line, False, True, ) > encoding = "utf-8" > proc_line = [ strg.decode( encoding ) for strg in proc_line ] > print( strg_form.format( *proc_line ) ) > ---------------------------------------------------------- > > $ echo -e "a b c d e\na ? u 1 2" | file - > /dev/stdin: UTF-8 Unicode text > $ echo -e "a b c d e\na ? u 1 2" | ./align_compact.py > None > a b c d e > a ? u 1 2 > $ echo -e "a b c d e\na ? u 1 2" | recode utf8..latin9 | file - > /dev/stdin: ISO-8859 text > $ echo -e "a b c d e\na ? u 1 2" | recode utf8..latin9 | ./align_compact.py > None > a b c d e > Traceback (most recent call last): > File "./align_compact.py", line 13, in > proc_line = [ strg.decode( encoding ) for strg in proc_line ] > File "/usr/lib64/python2.7/encodings/utf_8.py", line 16, in decode > return codecs.utf_8_decode(input, errors, True) > UnicodeDecodeError: 'utf8' codec can't decode byte 0xf6 in position 0: invalid start byte > muk at mcp20:/sw/prog/scripts/text_manip> > > How do I handle this two inputs? > Once you're using pipes, you've given up any hope that the terminal will report a useful encoding, so I'm not surprised you're getting None for sys.stdin.encoding() So you can either do as others have suggested, and guess, or you can get the information explicitly, say from argv. In any case you'll need a different way to assign encoding = -- DaveA From wxjmfauth at gmail.com Fri Aug 9 04:30:55 2013 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Fri, 9 Aug 2013 01:30:55 -0700 (PDT) Subject: right adjusted strings containing umlauts In-Reply-To: References: <9781df99-f9c8-4217-aa67-7a714b7f2ebe@googlegroups.com> <5203B841.4060304@gmail.com> Message-ID: <9018bc25-e25e-47fb-b7ca-05c33a28b76c@googlegroups.com> Le jeudi 8 ao?t 2013 18:27:06 UTC+2, Kurt Mueller a ?crit?: > Now I have this small example: > > ---------------------------------------------------------- > > #!/usr/bin/env python > > # vim: set fileencoding=utf-8 : > > > > from __future__ import print_function > > import sys, shlex > > > > print( repr( sys.stdin.encoding ) ) > > > > strg_form = u'{0:>3} {1:>3} {2:>3} {3:>3} {4:>3}' > > for inpt_line in sys.stdin: > > proc_line = shlex.split( inpt_line, False, True, ) > > encoding = "utf-8" > > proc_line = [ strg.decode( encoding ) for strg in proc_line ] > > print( strg_form.format( *proc_line ) ) > > ---------------------------------------------------------- > > > > $ echo -e "a b c d e\na ? u 1 2" | file - > > /dev/stdin: UTF-8 Unicode text > > $ echo -e "a b c d e\na ? u 1 2" | ./align_compact.py > > None > > a b c d e > > a ? u 1 2 > > $ echo -e "a b c d e\na ? u 1 2" | recode utf8..latin9 | file - > > /dev/stdin: ISO-8859 text > > $ echo -e "a b c d e\na ? u 1 2" | recode utf8..latin9 | ./align_compact.py > > None > > a b c d e > > Traceback (most recent call last): > > File "./align_compact.py", line 13, in > > proc_line = [ strg.decode( encoding ) for strg in proc_line ] > > File "/usr/lib64/python2.7/encodings/utf_8.py", line 16, in decode > > return codecs.utf_8_decode(input, errors, True) > > UnicodeDecodeError: 'utf8' codec can't decode byte 0xf6 in position 0: invalid start byte > > muk at mcp20:/sw/prog/scripts/text_manip> > > > > How do I handle this two inputs? > > > > > > TIA > > -- > > Kurt Mueller -------- It's very easy. The error msg indicates, you cann't decode your series of bytes with the utf-8 codec, simply because your string is encoded in iso-8859-* (you did it explicitly!). Your problem is not Python, your problem is the coding of the characters. You should be aware about the coding of the strings you are manipulating (creating) and if necessary decode and/or encode correctly accordingly to what you wish, eg. a suitable coding for the display. That's on this level that Python (or any language) matters. The sys.std*.encoding is a different problem. iso-8859-* ? iso-8859-1 == latin-1 and latin9 == iso-8859-15. If one excepts "das grosse Eszett", both codings are able to handle German (it seems to be your case) and there are no problems when working directly with these codings. jmf From tjreedy at udel.edu Thu Aug 8 16:51:08 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 08 Aug 2013 16:51:08 -0400 Subject: right adjusted strings containing umlauts In-Reply-To: <5203B841.4060304@gmail.com> References: <9781df99-f9c8-4217-aa67-7a714b7f2ebe@googlegroups.com> <5203B841.4060304@gmail.com> Message-ID: On 8/8/2013 11:24 AM, Kurt Mueller wrote: > print( u'{0:>3} {1:>3} {2:>3} {3:>3} {4:>3}'.format( *output_list ) ) Using autonumbering feature, same as print( u'{:>3} {:>3} {:>3} {:>3} {:>3}'.format( *output_list ) ) print( (u' '.join([u'{:>3}']*5)).format(*output_list) ) print( (u' '.join([u'{:>3}']*len(output_list))).format(*output_list) ) -- Terry Jan Reedy From steve+comp.lang.python at pearwood.info Fri Aug 9 21:29:52 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 Aug 2013 01:29:52 GMT Subject: right adjusted strings containing umlauts References: <9781df99-f9c8-4217-aa67-7a714b7f2ebe@googlegroups.com> Message-ID: <52059790$0$30000$c3e8da3$5496439d@news.astraweb.com> On Thu, 08 Aug 2013 17:24:49 +0200, Kurt Mueller wrote: > What do I do, when input_strings/output_list has other codings like > iso-8859-1? When reading from a text file, honour some sort of encoding cookie at the top (or bottom) of the file, like Emacs and Vim use, or a BOM. If there is no encoding cookie, assume UTF-8. When reading from stdin, assume UTF-8. Otherwise, make it the caller's responsibility to specify the encoding if they wish to use something else. Pseudo-code: encoding = None if command line arguments include '--encoding': encoding = --encoding argument if encoding is None: if input file is stdin: encoding = 'utf-8' else: open file as binary if first 2-4 bytes look like a BOM: encoding = one of UTF-8 or UTF-16 or UTF-32 else: read first two lines if either looks like an encoding cookie: encoding = cookie # optionally check the end of the file as well close file if encoding is None: encoding = 'utf-8' read from file using encoding -- Steven From wachkama at gmail.com Thu Aug 8 15:05:57 2013 From: wachkama at gmail.com (wachkama at gmail.com) Date: Thu, 8 Aug 2013 12:05:57 -0700 (PDT) Subject: How Do I get my Python script to attach multiple files and send as a single email Message-ID: <28e2b5ac-6dbf-4f90-a0f0-fc5cc33fc2ef@googlegroups.com> I have a dilemma I cant figure out how to send multiple files as an attachment to my email using this script. I can only send a single file attachment . Help!!! Here is my script. All filename's are txt files. fo = with open(filename,'rb') fo1 = open(filename2,'rb') fo2= open(filename3, 'rb') fo3= open(filename4,'rb') fo4=open(filename5, "rb") filecontent = fo.read() filecontent1 = fo1.read() filecontent2 = fo2.read() filecontent3 = fo3.read() filecontent4= fo4.read() encodedcontent = base64.b64encode(filecontent) # base64 encodedcontent1 = base64.b64encode(filecontent1) # base64 encodedcontent2 = base64.b64encode(filecontent2) # base64 encodedcontent3 = base64.b64encode(filecontent3) # base64 encodedcontent4 = base64.b64encode(filecontent4) # base64 sender = 'me at mysite.com' reciever = 'me at mymail.com' marker = "AUNIQUEMARKER" body =""" Hi This is Sam, I have a some files containing some tickets for you. Good day :) """ # Define the main headers. part1 = """From: Master Tickets To: To Samuel Kamau Subject: Sending Attachement MIME-Version: 1.0 Content-Type: multipart/mixed; boundary=%s --%s """ % (marker, marker) # Define the message action part2 = """Content-Type: text/plain Content-Transfer-Encoding:8bit %s --%s """ % (body,marker) # Define the attachment section part3 = """Content-Type: multipart/mixed; name=\"%s\" Content-Transfer-Encoding:base64 Content-Disposition: attachment; filename=%s %s --%s-- """ %(filename, filename, encodedcontent, marker) message = part1 + part2 + part3 try: smtpObj = smtplib.SMTP('mx.usa.net') smtpObj.sendmail(sender, reciever, message,) print "Successfully sent email" except Exception: print "Error: unable to send email" From gary.herron at islandtraining.com Thu Aug 8 15:19:43 2013 From: gary.herron at islandtraining.com (Gary Herron) Date: Thu, 08 Aug 2013 12:19:43 -0700 Subject: How Do I get my Python script to attach multiple files and send as a single email In-Reply-To: <28e2b5ac-6dbf-4f90-a0f0-fc5cc33fc2ef@googlegroups.com> References: <28e2b5ac-6dbf-4f90-a0f0-fc5cc33fc2ef@googlegroups.com> Message-ID: <5203EF4F.7020603@islandtraining.com> On 08/08/2013 12:05 PM, wachkama at gmail.com wrote: > I have a dilemma I cant figure out how to send multiple files as an attachment to my email using this script. I can only send a single file attachment . Help!!! Here is my script. > All filename's are txt files. > > > There is a standard Python module named "email" which you should look at. It can build an email with all the parts, alternates, and attachments you want. Then you send the resulting message using your smtplib code. The email module is large and complex, but reasonably easy to learn (following the documentation examples). It's far, FAR, easier than rolling your message, especially when attachments are needed. Gary Herron From joel.goldstick at gmail.com Thu Aug 8 15:32:28 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Thu, 8 Aug 2013 15:32:28 -0400 Subject: How Do I get my Python script to attach multiple files and send as a single email In-Reply-To: <5203EF4F.7020603@islandtraining.com> References: <28e2b5ac-6dbf-4f90-a0f0-fc5cc33fc2ef@googlegroups.com> <5203EF4F.7020603@islandtraining.com> Message-ID: On Thu, Aug 8, 2013 at 3:19 PM, Gary Herron wrote: > On 08/08/2013 12:05 PM, wachkama at gmail.com wrote: >> >> I have a dilemma I cant figure out how to send multiple files as an >> attachment to my email using this script. I can only send a single file >> attachment . Help!!! Here is my script. >> All filename's are txt files. >> >> >> > > There is a standard Python module named "email" which you should look at. > It can build an email with all the parts, alternates, and attachments you > want. Then you send the resulting message using your smtplib code. The > email module is large and complex, but reasonably easy to learn (following > the documentation examples). It's far, FAR, easier than rolling your > message, especially when attachments are needed. > > Gary Herron > > -- > http://mail.python.org/mailman/listinfo/python-list Here is a good example: http://stackoverflow.com/questions/3362600/how-to-send-email-attachments-with-python I found it using the following google search: "python send email using smtp with multiple attachment" It was the second result. Google is your friend ;) -- Joel Goldstick http://joelgoldstick.com From ian.g.kelly at gmail.com Thu Aug 8 15:31:11 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 8 Aug 2013 13:31:11 -0600 Subject: How Do I get my Python script to attach multiple files and send as a single email In-Reply-To: <28e2b5ac-6dbf-4f90-a0f0-fc5cc33fc2ef@googlegroups.com> References: <28e2b5ac-6dbf-4f90-a0f0-fc5cc33fc2ef@googlegroups.com> Message-ID: On Thu, Aug 8, 2013 at 1:05 PM, wrote: > I have a dilemma I cant figure out how to send multiple files as an attachment to my email using this script. I can only send a single file attachment . Help!!! Here is my script. You just need to repeat part3 for each attachment. Also the content type in part3 should be the content type of the attachment, not multipart/mixed. You might also want to take a look at the email package in the standard library which will do a lot of this stuff for you. http://docs.python.org/3/library/email.html From gouzounakis at hotmail.com Thu Aug 8 15:30:33 2013 From: gouzounakis at hotmail.com (D. Xenakis) Date: Thu, 8 Aug 2013 12:30:33 -0700 (PDT) Subject: Python 3 and SSH Tunnel Message-ID: <092fdbf5-1db3-4ea5-82ee-4b02cec80dc6@googlegroups.com> HOWTO anyone? What im trying to succeed here is create one SSH tunnel, so that i can connect from a python script running on my pc, to a remote MySQL database running on my Host and id like to stick with Python 3.3 . I contacted my host and he informed me that this is the only way. I tried pycrypto + paramiko but from what i have noticed, paramiko is not Python 3.3 ready. Any thoughts? From skip at pobox.com Thu Aug 8 16:03:47 2013 From: skip at pobox.com (Skip Montanaro) Date: Thu, 8 Aug 2013 15:03:47 -0500 Subject: Python 3 and SSH Tunnel In-Reply-To: <092fdbf5-1db3-4ea5-82ee-4b02cec80dc6@googlegroups.com> References: <092fdbf5-1db3-4ea5-82ee-4b02cec80dc6@googlegroups.com> Message-ID: On Thu, Aug 8, 2013 at 2:30 PM, D. Xenakis wrote: > HOWTO anyone? > > What im trying to succeed here is create one SSH tunnel, so that i can connect from a python script running on my pc, to a remote MySQL database running on my Host and id like to stick with Python 3.3 . http://lmgtfy.com/?q=python3+ssh+tunnel First hit: http://zeromq.github.io/pyzmq/ssh.html which says, in part: "pexpect has no Python3 support at this time, so Python 3 users should get Thomas Kluyver?s pexpect-u fork." Also, search PyPI for "tunnel". There might well be something useful there. Skip From rosuav at gmail.com Thu Aug 8 18:54:44 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 8 Aug 2013 23:54:44 +0100 Subject: Python 3 and SSH Tunnel In-Reply-To: <092fdbf5-1db3-4ea5-82ee-4b02cec80dc6@googlegroups.com> References: <092fdbf5-1db3-4ea5-82ee-4b02cec80dc6@googlegroups.com> Message-ID: On Thu, Aug 8, 2013 at 8:30 PM, D. Xenakis wrote: > HOWTO anyone? > > What im trying to succeed here is create one SSH tunnel, so that i can connect from a python script running on my pc, to a remote MySQL database running on my Host and id like to stick with Python 3.3 . > > I contacted my host and he informed me that this is the only way. > > I tried pycrypto + paramiko but from what i have noticed, paramiko is not Python 3.3 ready. I'm not sure what exactly is going on here, but why not simply establish a tunnel using ssh(1) and then invoke your Python script separately? You simply point your script at a database on localhost, after establishing a tunnel from local 3306 to remote localhost:3306. No need to play with Python crypto. Alternatively, can you use PostgreSQL instead? :) ChrisA From gouzounakis at hotmail.com Thu Aug 8 20:46:30 2013 From: gouzounakis at hotmail.com (D. Xenakis) Date: Thu, 8 Aug 2013 17:46:30 -0700 (PDT) Subject: Python 3 and SSH Tunnel In-Reply-To: References: <092fdbf5-1db3-4ea5-82ee-4b02cec80dc6@googlegroups.com> Message-ID: <9c89bff2-bcf0-4e08-a714-da51e137faae@googlegroups.com> > > HOWTO anyone? > > > > > > What im trying to succeed here is create one SSH tunnel, so that i can connect from a python script running on my pc, to a remote MySQL database running on my Host and id like to stick with Python 3.3 . > > > > > > I contacted my host and he informed me that this is the only way. > > > > > > I tried pycrypto + paramiko but from what i have noticed, paramiko is not Python 3.3 ready. > > > > I'm not sure what exactly is going on here, but why not simply > > establish a tunnel using ssh(1) and then invoke your Python script > > separately? You simply point your script at a database on localhost, > > after establishing a tunnel from local 3306 to remote localhost:3306. > > No need to play with Python crypto. > > > > Alternatively, can you use PostgreSQL instead? :) > > > > ChrisA Yes you are right. I've played with putty to achieve this but to be honest i'd like something more efficient. Opening putty everytime and making all the connection settings etc, and then running the programm, is kinda messy. Id like this to be done in an automatic way from the program so that things roll easy. I thought maybe i should find a way how to call and run a batch file from inside my python program or a powershell command, but i do not know even if that could work for the ssh tunneling. any ideas? From rosuav at gmail.com Thu Aug 8 20:56:24 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 9 Aug 2013 01:56:24 +0100 Subject: Python 3 and SSH Tunnel In-Reply-To: <9c89bff2-bcf0-4e08-a714-da51e137faae@googlegroups.com> References: <092fdbf5-1db3-4ea5-82ee-4b02cec80dc6@googlegroups.com> <9c89bff2-bcf0-4e08-a714-da51e137faae@googlegroups.com> Message-ID: On Fri, Aug 9, 2013 at 1:46 AM, D. Xenakis wrote: > I've played with putty to achieve this but to be honest i'd like something more efficient. Opening putty everytime and making all the connection settings etc, and then running the programm, is kinda messy. Id like this to be done in an automatic way from the program so that things roll easy. > I thought maybe i should find a way how to call and run a batch file from inside my python program or a powershell command, but i do not know even if that could work for the ssh tunneling. You should at very least be able to save PuTTY's settings under some name. Once you've done that, check PuTTY's docs for a way to invoke it with particular saved settings. I'm pretty sure there's a way to do that. The program can then invoke that as a background process, then go on to do whatever you need. Be aware, though, that you'll need to set up passwordless access (with a keypair) if you're to fully automate the process. But you may have already done that. ChrisA From Bernd-Waterkamp at web.de Fri Aug 9 12:40:16 2013 From: Bernd-Waterkamp at web.de (Bernd Waterkamp) Date: Fri, 9 Aug 2013 18:40:16 +0200 Subject: Python 3 and SSH Tunnel References: <092fdbf5-1db3-4ea5-82ee-4b02cec80dc6@googlegroups.com> <9c89bff2-bcf0-4e08-a714-da51e137faae@googlegroups.com> Message-ID: <1int07otideyo$.1gow5olpdema0.dlg@40tude.net> D. Xenakis schrieb: > I've played with putty to achieve this but to be honest i'd like > something more efficient. Opening putty everytime and making all the > connection settings etc, and then running the programm, is kinda messy. > Id like this to be done in an automatic way from the program so that > things roll easy. I thought maybe i should find a way how to call and > run a batch file from inside my python program or a powershell command, > but i do not know even if that could work for the ssh tunneling. > > any ideas? Both popular frameworks for python SSH - twisted and paramiko - are still being ported to python3. If you need to run your code on Windows, take a look at plink, a command line tool for PuTTY: http://the.earth.li/~sgtatham/putty/0.60/htmldoc/Chapter7.html#plink You can wrap plink and your python script in a batch-file or call plink from inside your script using subprocess. From gouzounakis at hotmail.com Thu Aug 8 20:50:05 2013 From: gouzounakis at hotmail.com (D. Xenakis) Date: Thu, 8 Aug 2013 17:50:05 -0700 (PDT) Subject: Python 3 and SSH Tunnel In-Reply-To: References: <092fdbf5-1db3-4ea5-82ee-4b02cec80dc6@googlegroups.com> Message-ID: <9c004d9a-eecc-4eb3-8e61-657dc0db021d@googlegroups.com> > Alternatively, can you use PostgreSQL instead? :) Yes there is such an option to be honest. Would that be helpfull instead of MySQL? From rosuav at gmail.com Thu Aug 8 20:57:35 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 9 Aug 2013 01:57:35 +0100 Subject: Python 3 and SSH Tunnel In-Reply-To: <9c004d9a-eecc-4eb3-8e61-657dc0db021d@googlegroups.com> References: <092fdbf5-1db3-4ea5-82ee-4b02cec80dc6@googlegroups.com> <9c004d9a-eecc-4eb3-8e61-657dc0db021d@googlegroups.com> Message-ID: On Fri, Aug 9, 2013 at 1:50 AM, D. Xenakis wrote: >> Alternatively, can you use PostgreSQL instead? :) > > Yes there is such an option to be honest. > Would that be helpfull instead of MySQL? It would, mainly because it's simply a better database engine. Everything to do with tunneling is going to be the same, save that you use port 5432 instead of 3306. But check if you can configure remote access directly on PostgreSQL. ChrisA From gouzounakis at hotmail.com Sat Aug 10 06:39:49 2013 From: gouzounakis at hotmail.com (D. Xenakis) Date: Sat, 10 Aug 2013 03:39:49 -0700 (PDT) Subject: Python 3 and SSH Tunnel In-Reply-To: <092fdbf5-1db3-4ea5-82ee-4b02cec80dc6@googlegroups.com> References: <092fdbf5-1db3-4ea5-82ee-4b02cec80dc6@googlegroups.com> Message-ID: What about the security though? To be specific, i need to create an application (python 3.3 strictly) where users will save/load their settings online to a remote hosted database. I do not wish to change the database from listening to any other thing than localhost for security reasons, so i assume the best solution for me would be to make the program create some ssh tunnels before the saving/loading happens. But would this policy make my database (or the rest of the databases that im running on that machine) unsecure? Is there any workaround this? How would you do that online saving/loading? From veritatem.ignotam at gmail.com Sat Aug 10 14:29:39 2013 From: veritatem.ignotam at gmail.com (Veritatem Ignotam) Date: Sat, 10 Aug 2013 14:29:39 -0400 Subject: Python 3 and SSH Tunnel In-Reply-To: References: <092fdbf5-1db3-4ea5-82ee-4b02cec80dc6@googlegroups.com> Message-ID: <52068693.9060102@gmail.com> I think I missed an earlier thread of this and I'm not quite sure what your application is, but properly allocating user permissions on your databases should eliminate any security concerns there. Also, for the tunnel, whether you're using one account or multiple (one for each user), those accounts should be properly secured as well. Ignotus On 08/10/2013 06:39 AM, D. Xenakis wrote: > What about the security though? > > To be specific, i need to create an application (python 3.3 strictly) where users will save/load their settings online to a remote hosted database. I do not wish to change the database from listening to any other thing than localhost for security reasons, so i assume the best solution for me would be to make the program create some ssh tunnels before the saving/loading happens. > > But would this policy make my database (or the rest of the databases that im running on that machine) unsecure? Is there any workaround this? > > How would you do that online saving/loading? From Inna.Belakhova at melbourne.vic.gov.au Thu Aug 8 22:23:09 2013 From: Inna.Belakhova at melbourne.vic.gov.au (Inna Belakhova) Date: Fri, 9 Aug 2013 12:23:09 +1000 Subject: connection change Message-ID: Hi, I have a compiled code, which currently uses SQLITE connection (datastore). I was wondering if it is possible to change the connection string to use SQL Server database. I would like to export the datastore to SQL server database, as we are having issues with the sqlite limit. Thank you. Inna This email is intended solely for the named addressee. If you are not the addressee indicated please delete it immediately. -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Fri Aug 9 14:36:54 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Fri, 9 Aug 2013 14:36:54 -0400 Subject: connection change In-Reply-To: References: Message-ID: On Thu, Aug 8, 2013 at 10:23 PM, Inna Belakhova wrote: > Hi, > > > > I have a compiled code, which currently uses SQLITE connection (datastore). I don't understand 'compiled code'. It would be nice if you show the code, give the version of python you are using, and the operating system Have you tried to change your program to use mysql instead? If so, show the changes you made and what the results were. > I was wondering if it is possible to change the connection string to use SQL > Server database. yes it is possible > > > > I would like to export the datastore to SQL server database, as we are > having issues with the sqlite limit. > > > > Thank you. > > Inna > > > > This email is intended solely for the named addressee. > If you are not the addressee indicated please delete it immediately. > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Joel Goldstick http://joelgoldstick.com From joel.goldstick at gmail.com Sat Aug 10 01:02:08 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Sat, 10 Aug 2013 01:02:08 -0400 Subject: connection change In-Reply-To: <4sua09dji0fleg5cv9im7632hmas1pv893@4ax.com> References: <4sua09dji0fleg5cv9im7632hmas1pv893@4ax.com> Message-ID: On Fri, Aug 9, 2013 at 7:31 PM, Dennis Lee Bieber wrote: > On Fri, 9 Aug 2013 14:36:54 -0400, Joel Goldstick > declaimed the following: > > >> >>Have you tried to change your program to use mysql instead? If so, >>show the changes you made and what the results were. >> > > Pardon? "mssql" is not the same as "mysql" oops.. bad reading Dennis.. > -- > Wulfraed Dennis Lee Bieber AF6VN > wlfraed at ix.netcom.com HTTP://wlfraed.home.netcom.com/ > > -- > http://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com From Inna.Belakhova at melbourne.vic.gov.au Sun Aug 11 19:50:15 2013 From: Inna.Belakhova at melbourne.vic.gov.au (Inna Belakhova) Date: Mon, 12 Aug 2013 09:50:15 +1000 Subject: connection change In-Reply-To: References: <4sua09dji0fleg5cv9im7632hmas1pv893@4ax.com> Message-ID: Hi, I don't know much about Python code. Where is the connection made, eg config file - where can I find it? Our SQLITe database is currently 9GB and we have a table that contains 7GB of BLOB type. I think the table cannot handle any more insert of BLOB hence I want to change it to SQL database - which has no restriction on database size. Any suggestions? Cheers, Inna -----Original Message----- From: Python-list [mailto:python-list-bounces+inna.belakhova=melbourne.vic.gov.au at python.o rg] On Behalf Of Joel Goldstick Sent: Saturday, 10 August 2013 3:02 PM To: Dennis Lee Bieber Cc: python-list at python.org Subject: Re: connection change On Fri, Aug 9, 2013 at 7:31 PM, Dennis Lee Bieber wrote: > On Fri, 9 Aug 2013 14:36:54 -0400, Joel Goldstick > declaimed the following: > > >> >>Have you tried to change your program to use mysql instead? If so, >>show the changes you made and what the results were. >> > > Pardon? "mssql" is not the same as "mysql" oops.. bad reading Dennis.. > -- > Wulfraed Dennis Lee Bieber AF6VN > wlfraed at ix.netcom.com HTTP://wlfraed.home.netcom.com/ > > -- > http://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com -- http://mail.python.org/mailman/listinfo/python-list This email is intended solely for the named addressee. If you are not the addressee indicated please delete it immediately. From joel.goldstick at gmail.com Mon Aug 12 14:07:23 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 12 Aug 2013 14:07:23 -0400 Subject: connection change In-Reply-To: References: <4sua09dji0fleg5cv9im7632hmas1pv893@4ax.com> Message-ID: On Sun, Aug 11, 2013 at 7:50 PM, Inna Belakhova wrote: > Hi, > > I don't know much about Python code. Where is the connection made, eg > config file - where can I find it? > > Our SQLITe database is currently 9GB and we have a table that contains > 7GB of BLOB type. I think the table cannot handle any more insert of > BLOB hence I want to change it to SQL database - which has no > restriction on database size. > > Any suggestions? use google: "python mssql" > > Cheers, > Inna > -- Joel Goldstick http://joelgoldstick.com From ramercer at gmail.com Fri Aug 9 00:11:09 2013 From: ramercer at gmail.com (Adam Mercer) Date: Thu, 8 Aug 2013 23:11:09 -0500 Subject: Using sudo to write to a file as root from a script Message-ID: Hi I'm trying to write a script that writes some content to a file root through sudo, but it's not working at all. I am using: channel = 'stable' config_file = '/opt/ldg/etc/channel.conf' command = ['echo', '-n', channel, '|', 'sudo', 'tee', config_file] p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, _ = p.communicate() But it seems as if this isn't doing anything. I just want to write the contents of the variable channel to the file /opt/ldg/etc/channel.conf. But whatever I try just doesn't work. Can anyone offer any pointers? Cheers Adam From denismfmcmahon at gmail.com Fri Aug 9 00:36:21 2013 From: denismfmcmahon at gmail.com (Denis McMahon) Date: Fri, 9 Aug 2013 04:36:21 +0000 (UTC) Subject: Using sudo to write to a file as root from a script References: Message-ID: On Thu, 08 Aug 2013 23:11:09 -0500, Adam Mercer wrote: > Hi > > I'm trying to write a script that writes some content to a file root > through sudo, but it's not working at all. I am using: > > channel = 'stable' > config_file = '/opt/ldg/etc/channel.conf' > command = ['echo', '-n', channel, '|', 'sudo', 'tee', config_file] > p = subprocess.Popen(command, stdout=subprocess.PIPE, > stderr=subprocess.PIPE) > out, _ = p.communicate() > > But it seems as if this isn't doing anything. > > I just want to write the contents of the variable channel to the file > /opt/ldg/etc/channel.conf. But whatever I try just doesn't work. Can > anyone offer any pointers? Do you find anything with: $ grep sudo /var/log/auth.log (you may need to specify a different log) Is the process that's trying to use the sudo command allowed to do so without a password? man sudoers Note - after editing /etc/sudoers you must set the permissions back to 440 -- Denis McMahon, denismfmcmahon at gmail.com From nobody at nowhere.com Fri Aug 9 16:12:20 2013 From: nobody at nowhere.com (Nobody) Date: Fri, 09 Aug 2013 21:12:20 +0100 Subject: Using sudo to write to a file as root from a script References: Message-ID: On Thu, 08 Aug 2013 23:11:09 -0500, Adam Mercer wrote: > I'm trying to write a script that writes some content to a file root > through sudo, but it's not working at all. I am using: > command = ['echo', '-n', channel, '|', 'sudo', 'tee', config_file] You can't create a pipeline like this. All of the list elements after the first will be passed as arguments to "echo". Try: command = ['sudo', 'tee', config_file] p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, _ = p.communicate('channel') From nobody at nowhere.com Fri Aug 9 16:16:14 2013 From: nobody at nowhere.com (Nobody) Date: Fri, 09 Aug 2013 21:16:14 +0100 Subject: Using sudo to write to a file as root from a script References: Message-ID: On Fri, 09 Aug 2013 21:12:20 +0100, Nobody wrote: > Try: > > command = ['sudo', 'tee', config_file] > p = subprocess.Popen(command, stdout=subprocess.PIPE, > stderr=subprocess.PIPE) > out, _ = p.communicate('channel') Oops; you also need stdin=subprocess.PIPE. From bouncingcats at gmail.com Fri Aug 9 00:47:56 2013 From: bouncingcats at gmail.com (David) Date: Fri, 9 Aug 2013 14:47:56 +1000 Subject: Using sudo to write to a file as root from a script In-Reply-To: References: Message-ID: On 9 August 2013 14:11, Adam Mercer wrote: > > I'm trying to write a script that writes some content to a file root > through sudo, but it's not working at all. I am using: [...] At a quick glance, I have a couple of suggestions. > command = ['echo', '-n', channel, '|', 'sudo', 'tee', config_file] sudo doesn't work like this. It doesn't read from standard input. You need to supply the command as an argument to sudo. Get the sudo syntax correct by learning to use it in a shell (eg terminal running bash ) before trying to use it from python code. Also, I think that passing the pipe character '|' as an argument to Popen is not the correct way to use pipes. So, if you figure out how to use sudo without '|' you will solve both these issues. From ishish at domhain.de Fri Aug 9 04:46:06 2013 From: ishish at domhain.de (ishish) Date: Fri, 09 Aug 2013 09:46:06 +0100 Subject: Using sudo to write to a file as root from a script In-Reply-To: References: Message-ID: <8bdbaa5d4566082fdb0ff03d477bdf0e@home.minuskel.de> Am 09.08.2013 05:47, schrieb David: > On 9 August 2013 14:11, Adam Mercer wrote: >> >> I'm trying to write a script that writes some content to a file root >> through sudo, but it's not working at all. I am using: > > [...] > > At a quick glance, I have a couple of suggestions. > >> command = ['echo', '-n', channel, '|', 'sudo', 'tee', config_file] > > sudo doesn't work like this. It doesn't read from standard input. You > need to supply the command as an argument to sudo. Get the sudo > syntax > correct by learning to use it in a shell (eg terminal running bash ) > before trying to use it from python code. > > Also, I think that passing the pipe character '|' as an argument to > Popen is not the correct way to use pipes. > > So, if you figure out how to use sudo without '|' you will solve both > these issues. Or try to change the permissions os.system("chmod 666 config_file) do what you want with the file and set the permissions back to their original state. From ramercer at gmail.com Fri Aug 9 09:21:22 2013 From: ramercer at gmail.com (Adam Mercer) Date: Fri, 9 Aug 2013 08:21:22 -0500 Subject: Using sudo to write to a file as root from a script In-Reply-To: References: Message-ID: On Thu, Aug 8, 2013 at 11:47 PM, David wrote: > At a quick glance, I have a couple of suggestions. > >> command = ['echo', '-n', channel, '|', 'sudo', 'tee', config_file] > > sudo doesn't work like this. It doesn't read from standard input. You > need to supply the command as an argument to sudo. Get the sudo syntax > correct by learning to use it in a shell (eg terminal running bash ) > before trying to use it from python code. The above does works in the terminal: [ram at cizin ~]$ ls -l /opt/ldg/etc/ total 20 -rw-rw-r-- 1 ram admin 5246 Jun 14 15:15 globus-user-env.csh -rw-rw-r-- 1 ram admin 3388 Jun 14 15:15 globus-user-env.sh -rw-rw-r-- 1 ram admin 91 Jun 24 11:33 ldg.conf -rw-r--r-- 1 ram admin 5 Jun 14 15:24 os.conf $ echo -n stable | sudo tee /opt/ldg/etc/channel.conf > /dev/null Password: $ ls -l /opt/ldg/etc/ total 24 -rw-r--r-- 1 root admin 6 Aug 9 07:59 channel.conf -rw-rw-r-- 1 ram admin 5246 Jun 14 15:15 globus-user-env.csh -rw-rw-r-- 1 ram admin 3388 Jun 14 15:15 globus-user-env.sh -rw-rw-r-- 1 ram admin 91 Jun 24 11:33 ldg.conf -rw-r--r-- 1 ram admin 5 Jun 14 15:24 os.conf $ Thats why I'm trying to use it. > Also, I think that passing the pipe character '|' as an argument to > Popen is not the correct way to use pipes. I believe subprocess uses pipes as follows import subprocess with open("/opt/ldg/etc/channel.conf","w") as out: subprocess.Popen(command, stdout=out) Then the file wouldn't be opened as root? And if the file already exists then I wouldn't be able to open it due to incorrect permissions. > So, if you figure out how to use sudo without '|' you will solve both > these issues. I'm open to suggestions as everything I can find involved pipes or redirections and neither seems to work with subprocess. I can use subprocess.call() as in: import subprocess import sys channel='stable' config_file='/opt/ldg/etc/channel.conf' command="echo -n %s | sudo tee %s > /dev/null" % (channel, config_file) try: retcode = subprocess.call(command, shell=True) if retcode < 0: sys.exit('Error: Failed to set channel.conf') except OSError as e: sys.exit('Error: Execution failed "%s"' % e) But I was under the impression that Popen was the preferred approach to running external processes? Cheers Adam From rosuav at gmail.com Fri Aug 9 09:29:39 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 9 Aug 2013 14:29:39 +0100 Subject: Using sudo to write to a file as root from a script In-Reply-To: References: Message-ID: On Fri, Aug 9, 2013 at 2:21 PM, Adam Mercer wrote: > command="echo -n %s | sudo tee %s > /dev/null" % (channel, config_file) > You shouldn't need to use 'echo' here. Just provide tee with the text on its standard input, and don't bother with the pipe at all. ChrisA From antoon.pardon at rece.vub.ac.be Fri Aug 9 09:42:07 2013 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Fri, 09 Aug 2013 15:42:07 +0200 Subject: Using sudo to write to a file as root from a script In-Reply-To: References: Message-ID: <5204F1AF.7020404@rece.vub.ac.be> Op 09-08-13 15:29, Chris Angelico schreef: > On Fri, Aug 9, 2013 at 2:21 PM, Adam Mercer wrote: >> command="echo -n %s | sudo tee %s > /dev/null" % (channel, config_file) >> > > You shouldn't need to use 'echo' here. Just provide tee with the text > on its standard input, and don't bother with the pipe at all. That is probably beside the point. I suspect Adam is just giving a minimal example to show the kind of thing he is trying to do. Nit picking the specific example instead of advising on the problem is likely to be less than helpful. -- Antoon Pardon From ramercer at gmail.com Fri Aug 9 09:50:59 2013 From: ramercer at gmail.com (Adam Mercer) Date: Fri, 9 Aug 2013 08:50:59 -0500 Subject: Using sudo to write to a file as root from a script In-Reply-To: <5204F1AF.7020404@rece.vub.ac.be> References: <5204F1AF.7020404@rece.vub.ac.be> Message-ID: On Fri, Aug 9, 2013 at 8:42 AM, Antoon Pardon wrote: > That is probably beside the point. I suspect Adam is just giving a > minimal example to show the kind of thing he is trying to do. > > Nit picking the specific example instead of advising on the problem > is likely to be less than helpful. It is a simplified example, but in this case the nitpicking was very helpful. Caused me to think about the problem differently, and therefore come up with a neater solution. Cheers Adam From rosuav at gmail.com Fri Aug 9 11:05:17 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 9 Aug 2013 16:05:17 +0100 Subject: Using sudo to write to a file as root from a script In-Reply-To: References: <5204F1AF.7020404@rece.vub.ac.be> Message-ID: On Fri, Aug 9, 2013 at 2:50 PM, Adam Mercer wrote: > On Fri, Aug 9, 2013 at 8:42 AM, Antoon Pardon > wrote: > >> That is probably beside the point. I suspect Adam is just giving a >> minimal example to show the kind of thing he is trying to do. >> >> Nit picking the specific example instead of advising on the problem >> is likely to be less than helpful. > > It is a simplified example, but in this case the nitpicking was very > helpful. Caused me to think about the problem differently, and > therefore come up with a neater solution. It wasn't nitpicking so much as suggesting a more Pythonic way to do things. It's entirely plausible that the response would have been "Actually, the real command is more complicated than 'echo' so I really do need a pipe", but it's *very* common in shell scripts to call on an external process to do something that in other languages is a builtin. For instance, how do you get the name of the parent of your current directory in bash? Something along the lines of: parent=$(dirname $(pwd$)$) which executes two external commands. In Python, it would be: os.path.dirname(os.getcwd()) which is two internal function calls. It's just one of the things to consider when porting code from one language to another - I wouldn't often use a list comprehension in bash, and I wouldn't often want a pipe between two external processes in Python. ChrisA From ramercer at gmail.com Fri Aug 9 09:42:26 2013 From: ramercer at gmail.com (Adam Mercer) Date: Fri, 9 Aug 2013 08:42:26 -0500 Subject: Using sudo to write to a file as root from a script In-Reply-To: References: Message-ID: On Fri, Aug 9, 2013 at 8:29 AM, Chris Angelico wrote: > You shouldn't need to use 'echo' here. Just provide tee with the text > on its standard input, and don't bother with the pipe at all. Thanks, that's much better! Cheers Adam From bouncingcats at gmail.com Fri Aug 9 11:56:28 2013 From: bouncingcats at gmail.com (David) Date: Sat, 10 Aug 2013 01:56:28 +1000 Subject: Using sudo to write to a file as root from a script In-Reply-To: References: Message-ID: On 9 August 2013 23:21, Adam Mercer wrote: > On Thu, Aug 8, 2013 at 11:47 PM, David wrote: > >> At a quick glance, I have a couple of suggestions. >> >>> command = ['echo', '-n', channel, '|', 'sudo', 'tee', config_file] >> >> sudo doesn't work like this. It doesn't read from standard input. You >> need to supply the command as an argument to sudo. Get the sudo syntax >> correct by learning to use it in a shell (eg terminal running bash ) >> before trying to use it from python code. > > The above does works in the terminal: Ah, sorry, I didn't pay close attention to what you are doing (with tee). >> So, if you figure out how to use sudo without '|' you will solve both >> these issues. At least I wasn't 100% wrong :) Anyway I'm glad some smarter people helped you. From antoon.pardon at rece.vub.ac.be Fri Aug 9 09:52:38 2013 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Fri, 09 Aug 2013 15:52:38 +0200 Subject: Using sudo to write to a file as root from a script In-Reply-To: References: Message-ID: <5204F426.2070708@rece.vub.ac.be> Op 09-08-13 06:11, Adam Mercer schreef: > Hi > > I'm trying to write a script that writes some content to a file root > through sudo, but it's not working at all. I am using: > > channel = 'stable' > config_file = '/opt/ldg/etc/channel.conf' > command = ['echo', '-n', channel, '|', 'sudo', 'tee', config_file] > p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) > out, _ = p.communicate() > > But it seems as if this isn't doing anything. > > I just want to write the contents of the variable channel to the file > /opt/ldg/etc/channel.conf. But whatever I try just doesn't work. Can > anyone offer any pointers? > > Cheers > > Adam subprocess.Popen by default doesn't use a shell. That could mean that the equivallent shell command you are trying to execute is: echo -n stable '|' sudo tee /opt/ldg/etc/channel.conf instead of echo -n stable | sudo tee /opt/ldg/etc/channel.conf which you probably want. You should also take care with the echo command. Echo is usually a shell builtin that can behave differently than the echo command. You should make sure you are using the one you actually want to use. -- Antoon Pardon From adam.preble at gmail.com Fri Aug 9 01:17:15 2013 From: adam.preble at gmail.com (adam.preble at gmail.com) Date: Thu, 8 Aug 2013 22:17:15 -0700 (PDT) Subject: Resolving import errors reported by PyLint in modules using Python.NET Message-ID: <2401ddc7-0fe0-4cd9-b7cf-f2dd8649c58f@googlegroups.com> PyLint can't figure out imports of .NET code being referenced in my Python scripts that use Python.NET. I can kind of see why; you have to evaluate some clr.AddReference calls for the imports to even succeed. I wonder if I have any recourse. Generally, to import a DLL you have to do a few things. I guess for an example I'll import a .NET string: ---- import clr # Python .NET common-language runtime module, the important part of it all clr.AddReference("System") from System import String # .NET System.String can = String("Spam") ---- PyLint is not amused: F: 4, 0: Unable to import 'System' (import-error) I wondered if there were any tricks to make it work. I don't want to just ignore import-error, either by explicitly telling pylint to ignore them, or be getting complacent in seeing them all the time. I am also kind of curious if PyLint will expose new problems if it's able to figure out more things after successfully passing the imports. I wouldn't really know. From benjamin.kaplan at case.edu Fri Aug 9 01:47:43 2013 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Thu, 8 Aug 2013 22:47:43 -0700 Subject: Resolving import errors reported by PyLint in modules using Python.NET In-Reply-To: <2401ddc7-0fe0-4cd9-b7cf-f2dd8649c58f@googlegroups.com> References: <2401ddc7-0fe0-4cd9-b7cf-f2dd8649c58f@googlegroups.com> Message-ID: On Thu, Aug 8, 2013 at 10:17 PM, wrote: > PyLint can't figure out imports of .NET code being referenced in my Python scripts that use Python.NET. I can kind of see why; you have to evaluate some clr.AddReference calls for the imports to even succeed. I wonder if I have any recourse. Generally, to import a DLL you have to do a few things. I guess for an example I'll import a .NET string: > > ---- > import clr # Python .NET common-language runtime module, the important part of it all > > clr.AddReference("System") > from System import String # .NET System.String > > can = String("Spam") > ---- > > PyLint is not amused: > F: 4, 0: Unable to import 'System' (import-error) > > I wondered if there were any tricks to make it work. I don't want to just ignore import-error, either by explicitly telling pylint to ignore them, or be getting complacent in seeing them all the time. I am also kind of curious if PyLint will expose new problems if it's able to figure out more things after successfully passing the imports. I wouldn't really know. Are you using Python.NET or IronPython? IronPython is reasonably well supported, and it looks like there's a patch you can use to get PyLint working on it (see http://mail.python.org/pipermail/ironpython-users/2012-June/016099.html ). Not sure what's going on with Python.NET From adam.preble at gmail.com Mon Aug 12 00:21:55 2013 From: adam.preble at gmail.com (adam.preble at gmail.com) Date: Sun, 11 Aug 2013 21:21:55 -0700 (PDT) Subject: Resolving import errors reported by PyLint in modules using Python.NET In-Reply-To: References: <2401ddc7-0fe0-4cd9-b7cf-f2dd8649c58f@googlegroups.com> Message-ID: <3170cecf-6169-4a20-baa9-c9a31b5b3da5@googlegroups.com> I thought I responded to this. Oh well On Friday, August 9, 2013 12:47:43 AM UTC-5, Benjamin Kaplan wrote: > Are you using Python.NET or IronPython? IronPython is reasonably well > > supported, and it looks like there's a patch you can use to get PyLint > > working on it (see > > http://mail.python.org/pipermail/ironpython-users/2012-June/016099.html > > ). Not sure what's going on with Python.NET Definitely Python.NET in this case. It looks like that issue is different than what I get. PyLint just gets import errors when trying to import the modules, which it just reports with everything else I've done wrong. From engg at cleantechsolution.in Fri Aug 9 02:53:51 2013 From: engg at cleantechsolution.in (engg at cleantechsolution.in) Date: Thu, 8 Aug 2013 23:53:51 -0700 (PDT) Subject: Data transfer from Python CGI to javascript Message-ID: I have written the following program #!c:\Python27\Python.exe import cgi, cgitb; import sys, serial cgitb.enable() ser = serial.Serial('COM27', 9600) myvar = ser.readline() print "Content-type:text/html\n\n" print """ Real Time Temperature

Real Time Temperature:

""" It is giving an output of 'NaN' in the output. If the python program is run alone it is giving the correct output. Can anyone please help. Is it also possible to separate the python and the javascript to two different files. If so how will I get/transfer the data in/from HTML . Thanks From python at mrabarnett.plus.com Fri Aug 9 11:13:59 2013 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 09 Aug 2013 16:13:59 +0100 Subject: Data transfer from Python CGI to javascript In-Reply-To: References: Message-ID: <52050737.8070209@mrabarnett.plus.com> On 09/08/2013 07:53, engg at cleantechsolution.in wrote: > I have written the following program > #!c:\Python27\Python.exe > import cgi, cgitb; > import sys, serial > cgitb.enable() > ser = serial.Serial('COM27', 9600) > myvar = ser.readline() > print "Content-type:text/html\n\n" > print """ > > > > Real Time Temperature > > > > >

Real Time Temperature:

>
> > """ > > It is giving an output of 'NaN' in the output. > If the python program is run alone it is giving the correct output. > Can anyone please help. > Is it also possible to separate the python and the javascript to two different files. If so how will I get/transfer the data in/from HTML . > Thanks > At a guess I'd say that it's because you have: setInterval("startTime();",1000); which will call 'startTime' with no arguments, but: function startTime(myvar) which means that it's expecting an argument. From luca.cerone at gmail.com Fri Aug 9 04:29:02 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Fri, 9 Aug 2013 01:29:02 -0700 (PDT) Subject: "Nested" virtual environments Message-ID: <18c0a8bb-9295-493b-b0aa-1593b7d05c2f@googlegroups.com> Dear all, is there a way to "nest" virtual environments? I work on several different projects that involve Python programming. For a lot of this projects I have to use the same packages (e.g. numpy, scipy, matplotlib and so on), while having to install packages that are specific for each project. For each of this project I created a virtual environment (using virtualenv --no-site-packages) and I had to reinstall the shared packages in each of them. I was wondering if there is a way to nest a virtual environment into another, so that I can create a "common" virtual environment that contains all the shared packages and then "specialize" the virtual environments installing the packages specific for each project. In a way this is not conceptually different to using virtualenv --system-site-packages, just instead of getting access to the system packages a virtual environment should be able to access the packages of an other one. Thanks a lot in advance for the help, Luca From marcelgmr at gmail.com Thu Aug 15 12:30:18 2013 From: marcelgmr at gmail.com (Marcel Rodrigues) Date: Thu, 15 Aug 2013 13:30:18 -0300 Subject: "Nested" virtual environments In-Reply-To: <18c0a8bb-9295-493b-b0aa-1593b7d05c2f@googlegroups.com> References: <18c0a8bb-9295-493b-b0aa-1593b7d05c2f@googlegroups.com> Message-ID: I don't know how to completely solve this problem, but here is something that can alleviate it considerably. If you have a recent version of pip, you can use wheels [1] to save built packages locally. First create a new virtualenv and install the common packages. Then put these packages in a wheel directory. Then, for any other virtualenv that need the common packages, you can easily install then from the wheel directory (this is fast even for numpy & friends, because nothing will be compiled again) [2]. # Create a new virtualenv virtualenv myenv source myenv/bin/activate # Install the wheel package pip install wheel # Install your common packages pip install numpy scipy matplotlib # Create a requirements file pip freeze > /local/requirements.txt # Create wheel for the common packages pip wheel --wheel-dir=/local/wheels -r /local/requirements.txt Now you have all the built packages saved to /local/wheels, ready to install on any other environment. You can safely delete myenv. Test it with the following: # Create a virtualenv for a new project virtualenv myproj source myproj/bin/activate # Install common packages from wheel pip install --use-wheel --no-index --find-links=/local/wheels -r /local/requirements.txt [1] https://wheel.readthedocs.org [2] http://www.pip-installer.org/en/latest/cookbook.html#building-and-installing-wheels 2013/8/9 Luca Cerone > Dear all, is there a way to "nest" virtual environments? > > I work on several different projects that involve Python programming. > > For a lot of this projects I have to use the same packages (e.g. numpy, > scipy, matplotlib and so on), while having to install packages that are > specific > for each project. > > For each of this project I created a virtual environment (using virtualenv > --no-site-packages) and I had to reinstall the shared packages in each of > them. > > I was wondering if there is a way to nest a virtual environment into > another, > so that I can create a "common" virtual environment that contains all the > shared packages and then "specialize" the virtual environments installing > the packages specific for each project. > > In a way this is not conceptually different to using virtualenv > --system-site-packages, just instead of getting access to the system > packages a virtual environment should be able to access the packages of an > other one. > > Thanks a lot in advance for the help, > Luca > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From luca.cerone at gmail.com Fri Aug 16 04:35:00 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Fri, 16 Aug 2013 01:35:00 -0700 (PDT) Subject: "Nested" virtual environments In-Reply-To: References: <18c0a8bb-9295-493b-b0aa-1593b7d05c2f@googlegroups.com> Message-ID: Thanks Marcel, I will give it a try during the weekend and let you know if it worked for me :) > > If you have a recent version of pip, you can use wheels [1] to save built packages locally. First create a new virtualenv and install the common packages. Then put these packages in a wheel directory. Then, for any other virtualenv that need the common packages, you can easily install then from the wheel directory (this is fast even for numpy & friends, because nothing will be compiled again) [2]. > > > > # Create a new virtualenv > > virtualenv myenv > > source myenv/bin/activate > # Install the wheel package > pip install wheel > # Install your common packages > > pip install numpy scipy matplotlib > # Create a requirements file > pip freeze > /local/requirements.txt > # Create wheel for the common packages > pip wheel --wheel-dir=/local/wheels -r /local/requirements.txt > > > Now you have all the built packages saved to /local/wheels, ready to install on any other environment. You can safely delete myenv. Test it with the following: > > # Create a virtualenv for a new project > > virtualenv myproj > source myproj/bin/activate > # Install common packages from wheel > pip install --use-wheel --no-index --find-links=/local/wheels -r /local/requirements.txt > > > > > > > > > > > > > > [1] https://wheel.readthedocs.org > [2] http://www.pip-installer.org/en/latest/cookbook.html#building-and-installing-wheels > > > > > > > 2013/8/9 Luca Cerone > > Dear all, is there a way to "nest" virtual environments? > > > > I work on several different projects that involve Python programming. > > > > For a lot of this projects I have to use the same packages (e.g. numpy, scipy, matplotlib and so on), while having to install packages that are specific > > for each project. > > > > For each of this project I created a virtual environment (using virtualenv --no-site-packages) and I had to reinstall the shared packages in each of them. > > > > I was wondering if there is a way to nest a virtual environment into another, > > so that I can create a "common" virtual environment ?that contains all the > > shared packages and then "specialize" the virtual environments installing the packages specific for each project. > > > > In a way this is not conceptually different to using virtualenv --system-site-packages, just instead of getting access to the system packages a virtual environment should be able to access the packages of an other one. > > > > > Thanks a lot in advance for the help, > > Luca > > -- > > http://mail.python.org/mailman/listinfo/python-list From engg at cleantechsolution.in Fri Aug 9 04:36:09 2013 From: engg at cleantechsolution.in (engg at cleantechsolution.in) Date: Fri, 9 Aug 2013 01:36:09 -0700 (PDT) Subject: passing Python data to a javascript function In-Reply-To: References: Message-ID: On Thursday, October 27, 2011 9:25:28 AM UTC+5:30, Chris Angelico wrote: > On Thu, Oct 27, 2011 at 2:51 PM, Bill Allen wrote: > > > Chris, > > > > > > Wow, that seems so simple now that I see it.? I was dancing around that all > > > day, but just not landing on it.?? Thanks so very much for the assist. > > >This code is giving an error 500 when run. Although the python code is independently running OK > > > --Bill > > > > > > Final code that works perfectly, passes the value from the Python script to > > > the javascript correctly: > > > > > > > > > > Congratulations! You've just written code that writes code. It takes a > > bit to get your head around it (especially when you start worrying > > about literal strings that might contain quotes, for instance), but > > it's really cool and seriously powerful stuff. Your server-side Python > > code can generate client-side Javascript code any way it likes... > > unlimited possibilities. > > > > ChrisA From devyncjohnson at gmail.com Fri Aug 9 15:30:37 2013 From: devyncjohnson at gmail.com (Devyn Collier Johnson) Date: Fri, 09 Aug 2013 15:30:37 -0400 Subject: Python3 Multiprocessing Message-ID: <5205435D.5090704@Gmail.com> Aloha! I need a command that will make threads created by "multiprocessing.Process()" wait for each other to complete. For instance, I want to do something like this: job1 = multiprocessing.Process(CMD1()) job2 = multiprocessing.Process(CMD2()) jobs1.start(); jobs2.start() PY_FUNC() The command "PY_FUNC()" depends on the end result of the actions of CMD1() and CMD2(). I need some kind of wait command for the two threads that will not let the script continue until job1 and job2 are complete. Is this possible in Python3? Mahalo, DevynCJohnson at Gmail.com From python at mrabarnett.plus.com Fri Aug 9 15:44:34 2013 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 09 Aug 2013 20:44:34 +0100 Subject: Python3 Multiprocessing In-Reply-To: <5205435D.5090704@Gmail.com> References: <5205435D.5090704@Gmail.com> Message-ID: <520546A2.50900@mrabarnett.plus.com> On 09/08/2013 20:30, Devyn Collier Johnson wrote: > Aloha! > > I need a command that will make threads created by > "multiprocessing.Process()" wait for each other to complete. For > instance, I want to do something like this: > > job1 = multiprocessing.Process(CMD1()) > job2 = multiprocessing.Process(CMD2()) > > jobs1.start(); jobs2.start() > > PY_FUNC() > > The command "PY_FUNC()" depends on the end result of the actions of > CMD1() and CMD2(). I need some kind of wait command for the two threads > that will not let the script continue until job1 and job2 are complete. > Is this possible in Python3? > Possibly you mean .join: jobs1.start() jobs2.start() jobs1.join() jobs2.join() From devyncjohnson at gmail.com Fri Aug 9 16:43:36 2013 From: devyncjohnson at gmail.com (Devyn Collier Johnson) Date: Fri, 09 Aug 2013 16:43:36 -0400 Subject: Python3 Multiprocessing In-Reply-To: <520546A2.50900@mrabarnett.plus.com> References: <5205435D.5090704@Gmail.com> <520546A2.50900@mrabarnett.plus.com> Message-ID: <52055478.2080704@Gmail.com> On 08/09/2013 03:44 PM, MRAB wrote: > On 09/08/2013 20:30, Devyn Collier Johnson wrote: >> Aloha! >> >> I need a command that will make threads created by >> "multiprocessing.Process()" wait for each other to complete. For >> instance, I want to do something like this: >> >> job1 = multiprocessing.Process(CMD1()) >> job2 = multiprocessing.Process(CMD2()) >> >> jobs1.start(); jobs2.start() >> >> PY_FUNC() >> >> The command "PY_FUNC()" depends on the end result of the actions of >> CMD1() and CMD2(). I need some kind of wait command for the two threads >> that will not let the script continue until job1 and job2 are complete. >> Is this possible in Python3? >> > Possibly you mean .join: > > jobs1.start() > jobs2.start() > > jobs1.join() > jobs2.join() > Thanks MRAB! That is easy. I always (incorrectly) thought the join() command got two threads and made them one. I did not know it made the script wait for the threads. Mahalo, DevynCJohnson at Gmail.com From random832 at fastmail.us Fri Aug 9 16:54:38 2013 From: random832 at fastmail.us (random832 at fastmail.us) Date: Fri, 09 Aug 2013 16:54:38 -0400 Subject: Python3 Multiprocessing In-Reply-To: <52055478.2080704@Gmail.com> References: <5205435D.5090704@Gmail.com> <520546A2.50900@mrabarnett.plus.com> <52055478.2080704@Gmail.com> Message-ID: <1376081678.11989.8065279.5BD23A9D@webmail.messagingengine.com> On Fri, Aug 9, 2013, at 16:43, Devyn Collier Johnson wrote: > Thanks MRAB! That is easy. I always (incorrectly) thought the join() > command got two threads and made them one. I did not know it made the > script wait for the threads. What you're missing is the fact that the main thread [i.e. the one running "the script", and that waits for the thread you call the method on] is, well, a thread. So, you start with two threads [the main thread and the jobs1 thread, for example], and end up with one [the main thread]. This is why it's called join. From ramit.prasad at jpmorgan.com.dmarc.invalid Fri Aug 9 16:46:17 2013 From: ramit.prasad at jpmorgan.com.dmarc.invalid (Prasad, Ramit) Date: Fri, 9 Aug 2013 20:46:17 +0000 Subject: Python3 Multiprocessing In-Reply-To: <52055478.2080704@Gmail.com> References: <5205435D.5090704@Gmail.com> <520546A2.50900@mrabarnett.plus.com> <52055478.2080704@Gmail.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF4741862BC92@SCACMX008.exchad.jpmchase.net> Devyn Collier Johnson > On 08/09/2013 03:44 PM, MRAB wrote: > > On 09/08/2013 20:30, Devyn Collier Johnson wrote: [snip] > >> > > jobs1.join() > > jobs2.join() > > > > Thanks MRAB! That is easy. I always (incorrectly) thought the join() > command got two threads and made them one. I did not know it made the > script wait for the threads. > It does join two threads, just not the threads you think! It "joins" a child thread with the parent thread and not two children thread. > Mahalo, > > DevynCJohnson at Gmail.com > -- ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From steve+comp.lang.python at pearwood.info Fri Aug 9 21:10:18 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 Aug 2013 01:10:18 GMT Subject: PEP 450 Adding a statistics module to Python Message-ID: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> I am seeking comments on PEP 450, Adding a statistics module to Python's standard library: http://www.python.org/dev/peps/pep-0450/ Please read the FAQs before asking anything :-) Also relevant: http://bugs.python.org/issue18606 -- Steven From skip at pobox.com Fri Aug 9 23:14:13 2013 From: skip at pobox.com (Skip Montanaro) Date: Fri, 9 Aug 2013 22:14:13 -0500 Subject: PEP 450 Adding a statistics module to Python In-Reply-To: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Aug 9, 2013 at 8:10 PM, Steven D'Aprano wrote: > I am seeking comments on PEP 450, Adding a statistics module to Python's > standard library: > > http://www.python.org/dev/peps/pep-0450/ > > Please read the FAQs before asking anything :-) Given that installing numpy or scipy is generally no more difficult that executing "pip install (scipy|numpy)" I'm not really feeling the need for a battery here... (Of course, I use this stuff at work from time-to-time, so maybe I'm more in the "nuclear reactor of batteries" camp anyway.) Skip From ben+python at benfinney.id.au Sat Aug 10 01:05:06 2013 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 10 Aug 2013 15:05:06 +1000 Subject: PEP 450 Adding a statistics module to Python References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: <7w7gfukuf1.fsf@benfinney.id.au> Skip Montanaro writes: > Given that installing numpy or scipy is generally no more difficult > that executing "pip install (scipy|numpy)" I'm not really feeling the > need for a battery here... NumPy and SciPy are not available for many Python users, including those using a Python implementation for which there is no Numpy support and those for whom large, dependency-heavy third-party packages are too much burden. See the Rationale of PEP 450 for more reasons why ?install NumPy? is not a feasible solution for many use cases, and why having ?statistics? as a pure-Python, standard-library package is desirable. -- \ ?Dad always thought laughter was the best medicine, which I | `\ guess is why several of us died of tuberculosis.? ?Jack Handey | _o__) | Ben Finney From stefan_ml at behnel.de Sat Aug 10 03:55:59 2013 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sat, 10 Aug 2013 09:55:59 +0200 Subject: PEP 450 Adding a statistics module to Python In-Reply-To: <7w7gfukuf1.fsf@benfinney.id.au> References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> <7w7gfukuf1.fsf@benfinney.id.au> Message-ID: Ben Finney, 10.08.2013 07:05: > Skip Montanaro writes: >> Given that installing numpy or scipy is generally no more difficult >> that executing "pip install (scipy|numpy)" I'm not really feeling the >> need for a battery here... > > See the Rationale of PEP 450 for more reasons why ?install NumPy? is not > a feasible solution for many use cases, and why having ?statistics? as a > pure-Python, standard-library package is desirable. The rationale suggests that the module is meant as a simple toolset for non-NumPy users. Are the APIs (class model, function names, etc.) similar enough to make it easy to switch, preferably in both directions? It would be good if a stdlib statistics module could be used as a SciPy fallback for the "simple" things, and if users of the stdlib module could easily switch their code to SciPy if they need more speed/features/whatever at some point, without having to relearn the name of each single function. I'm not asking for compatibility (doesn't sound reasonable without NumPy arrays), but I think that a similarity in terms of API naming (as far as it makes sense) should be clearly stated, e.g. in the Design Decisions section. Stefan From skip at pobox.com Sun Aug 11 07:50:36 2013 From: skip at pobox.com (Skip Montanaro) Date: Sun, 11 Aug 2013 06:50:36 -0500 Subject: PEP 450 Adding a statistics module to Python In-Reply-To: <7w7gfukuf1.fsf@benfinney.id.au> References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> <7w7gfukuf1.fsf@benfinney.id.au> Message-ID: > See the Rationale of PEP 450 for more reasons why ?install NumPy? is not > a feasible solution for many use cases, and why having ?statistics? as a > pure-Python, standard-library package is desirable. I read that before posting but am not sure I agree. I don't see the screaming need for this package. Why can't it continue to live on PyPI, where, once again, it is available as "pip install ..."? S From nicholas.cole at gmail.com Sun Aug 11 08:27:43 2013 From: nicholas.cole at gmail.com (Nicholas Cole) Date: Sun, 11 Aug 2013 13:27:43 +0100 Subject: PEP 450 Adding a statistics module to Python In-Reply-To: References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> <7w7gfukuf1.fsf@benfinney.id.au> Message-ID: On Sun, Aug 11, 2013 at 12:50 PM, Skip Montanaro wrote: > > See the Rationale of PEP 450 for more reasons why ?install NumPy? is not > > a feasible solution for many use cases, and why having ?statistics? as a > > pure-Python, standard-library package is desirable. > > I read that before posting but am not sure I agree. I don't see the > screaming need for this package. Why can't it continue to live on > PyPI, where, once again, it is available as "pip install ..."? Well, I *do* think this module would be a wonderful addition to the standard library. I've often used python to do analysis of data, nothing complicated enough to need NumPy, but certainly things where I've needed to find averages etc. I've rolled my own functions for these projects, and I'm sure they are fragile. Besides, it was just a pain to do them. PyPI is terrific. There are lots of excellent modules on there. It's a wonderful resource. But I think that the standard library is also a wonderful thing, and where there are clearly defined modules, that serve a general, well-defined function and where development does not need to be very rapid, I think they should go into the Standard Library. I'm aware that my opinion is just that of one user, but I read this PEP and I thought, "Thank Goodness! That looks great. About time too." N. -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Sun Aug 11 09:33:53 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 11 Aug 2013 13:33:53 GMT Subject: PEP 450 Adding a statistics module to Python References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> <7w7gfukuf1.fsf@benfinney.id.au> Message-ID: <520792c1$0$30000$c3e8da3$5496439d@news.astraweb.com> On Sun, 11 Aug 2013 06:50:36 -0500, Skip Montanaro wrote: >> See the Rationale of PEP 450 for more reasons why ?install NumPy? is >> not a feasible solution for many use cases, and why having ?statistics? >> as a pure-Python, standard-library package is desirable. > > I read that before posting but am not sure I agree. I don't see the > screaming need for this package. Why can't it continue to live on PyPI, > where, once again, it is available as "pip install ..."? The same could be said about any module, really. And indeed, some languages have that philosophy, they provide no libraries to speak of, if you want anything you have to either write it yourself or get it from somebody else. Not everyone has the luxury of being able, or allowed, to run "pip install" to get additional, non-standard packages. E.g. in corporate environments. But I've already said that in the PEP. -- Steven From roy at panix.com Sun Aug 11 10:02:21 2013 From: roy at panix.com (Roy Smith) Date: Sun, 11 Aug 2013 10:02:21 -0400 Subject: PEP 450 Adding a statistics module to Python References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> <7w7gfukuf1.fsf@benfinney.id.au> Message-ID: In article , Skip Montanaro wrote: > > See the Rationale of PEP 450 for more reasons why ???install NumPy??? is not > > a feasible solution for many use cases, and why having ???statistics??? as a > > pure-Python, standard-library package is desirable. > > I read that before posting but am not sure I agree. I don't see the > screaming need for this package. Why can't it continue to live on > PyPI, where, once again, it is available as "pip install ..."? My previous comments on this topic were along the lines of "installing numpy is a non-starter if all you need are simple mean/std-dev". You do, however, make a good point here. Running "pip install statistics" is a much lower barrier to entry than getting numpy going, especially if statistics is pure python and thus has no dependencies on compiler tool chains which may be missing. Still, I see two classes of function in PEP-450. Class 1 is the really basic stuff: * mean * std-dev Class 2 are the more complicated things like: * linear regression * median * mode * functions for calculating the probability of random variables from the normal, t, chi-squared, and F distributions * inference on the mean * anything that differentiates between population and sample I could see leaving class 2 stuff in an optional pure-python module to be installed by pip, but for (as the PEP phrases it), the simplest and most obvious statistical functions (into which I lump mean and std-dev), having them in the standard library would be a big win. From buzzard at invalid.invalid Sun Aug 11 11:44:25 2013 From: buzzard at invalid.invalid (duncan smith) Date: Sun, 11 Aug 2013 16:44:25 +0100 Subject: PEP 450 Adding a statistics module to Python In-Reply-To: References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> <7w7gfukuf1.fsf@benfinney.id.au> Message-ID: <5207b162$0$49501$862e30e2@ngroups.net> On 11/08/13 15:02, Roy Smith wrote: > In article , > Skip Montanaro wrote: > >>> See the Rationale of PEP 450 for more reasons why ???install NumPy??? is not >>> a feasible solution for many use cases, and why having ???statistics??? as a >>> pure-Python, standard-library package is desirable. >> >> I read that before posting but am not sure I agree. I don't see the >> screaming need for this package. Why can't it continue to live on >> PyPI, where, once again, it is available as "pip install ..."? > > My previous comments on this topic were along the lines of "installing > numpy is a non-starter if all you need are simple mean/std-dev". You > do, however, make a good point here. Running "pip install statistics" > is a much lower barrier to entry than getting numpy going, especially if > statistics is pure python and thus has no dependencies on compiler tool > chains which may be missing. > > Still, I see two classes of function in PEP-450. Class 1 is the really > basic stuff: > > * mean > * std-dev > > Class 2 are the more complicated things like: > > * linear regression > * median > * mode > * functions for calculating the probability of random variables > from the normal, t, chi-squared, and F distributions > * inference on the mean > * anything that differentiates between population and sample > > I could see leaving class 2 stuff in an optional pure-python module to > be installed by pip, but for (as the PEP phrases it), the simplest and > most obvious statistical functions (into which I lump mean and std-dev), > having them in the standard library would be a big win. > I would probably move other descriptive statistics (median, mode, correlation, ...) into Class 1. I roll my own statistical tests as I need them - simply to avoid having a dependency on R. But I generally do end up with a dependency on scipy because I need scipy.stats.distributions. So I guess a distinct library for probability distributions would be handy - but maybe it should not be in the standard library. Once we move on to statistical modelling (e.g. linear regression) I think the case for inclusion in the standard library becomes weaker still. Cheers. Duncan From jsf80238 at gmail.com Sat Aug 17 23:57:07 2013 From: jsf80238 at gmail.com (Jason Friedman) Date: Sat, 17 Aug 2013 21:57:07 -0600 Subject: PEP 450 Adding a statistics module to Python In-Reply-To: <7w7gfukuf1.fsf@benfinney.id.au> References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> <7w7gfukuf1.fsf@benfinney.id.au> Message-ID: > NumPy and SciPy are not available for many Python users, including those > using a Python implementation for which there is no Numpy support > and those for > whom large, dependency-heavy third-party packages are too much burden. > > See the Rationale of PEP 450 for more reasons why ?install NumPy? is not > a feasible solution for many use cases, and why having ?statistics? as a > pure-Python, standard-library package is desirable. +1 From roy at panix.com Sat Aug 10 07:50:23 2013 From: roy at panix.com (Roy Smith) Date: Sat, 10 Aug 2013 07:50:23 -0400 Subject: PEP 450 Adding a statistics module to Python References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article , Skip Montanaro wrote: > Given that installing numpy or scipy is generally no more difficult > that executing "pip install (scipy|numpy)" I'm not really feeling the > need for a battery here... I just tried installing numpy in a fresh virtualenv on an Ubuntu Precise box. I ran "pip install numpy". It took 1.5 minutes. It printed almost 1800 lines of build crap, including 383 warnings and 83 errors. For a newbie, that can be pretty intimidating. That's for the case where I've already installed numpy elsewhere on that box, so I already had the fortran compiler, and the rest of the build chain. For fun, I just spun up a new Ubuntu Precise instance in AWS. It came pre-installed with Python 2.7.3. I tried "pip install numpy", which told me that pip was not installed. At least it told me what I needed to do to get pip installed. Unfortunately, I didn't read the message carefully enough and typed "sudo apt-get install pip", which of course got me another error because the correct name of the package is python-pip. Doing "sudo apt-get install python-pip" finally got me to the point where I could start to install numpy. Of course, if I didn't have sudo privs on the box (most corporate environments), I never would have gotten that far. At this point, "sudo pip install numpy" got me a bunch of errors culminating in "RuntimeError: Broken toolchain: cannot link a simple C program", and no indication of how to get any further. At this point, most people would give up. I don't remember the full set of steps I needed to do the first time. Obviously, I would start with installing gcc, but I seem to remember there were additional steps needed to get fortran support. Having some simple statistics baked into the standard python package would be a big win. As shown above, installing numpy can be an insurmountable hurdle for people with insufficient sysadmin-fu. PEP-450 makes cogent arguments why rolling your own statistics routines is fraught with peril. Looking over our source tree, I see we've implemented std deviation in python at least twice. I'm sure they're both naive implementations of the sort PEP-450 warns about. And, yes, backporting to 2.7 would be a big win too. I know the goal is to get everybody onto 3.x, but my pip external dependency list includes 40 modules. It's going to be a long and complicated road to get to the point where I can move to 3.x, and I imagine most non-trivial projects are in a similar situation. From oscar.j.benjamin at gmail.com Sat Aug 10 08:23:56 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Sat, 10 Aug 2013 13:23:56 +0100 Subject: PEP 450 Adding a statistics module to Python In-Reply-To: References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 10 August 2013 12:50, Roy Smith wrote: > In article , > Skip Montanaro wrote: > >> Given that installing numpy or scipy is generally no more difficult >> that executing "pip install (scipy|numpy)" I'm not really feeling the >> need for a battery here... > > I just tried installing numpy in a fresh virtualenv on an Ubuntu Precise > box. I ran "pip install numpy". It took 1.5 minutes. It printed > almost 1800 lines of build crap, including 383 warnings and 83 errors. > For a newbie, that can be pretty intimidating. > > That's for the case where I've already installed numpy elsewhere on that > box, so I already had the fortran compiler, and the rest of the build > chain. For fun, I just spun up a new Ubuntu Precise instance in AWS. > It came pre-installed with Python 2.7.3. I tried "pip install numpy", > which told me that pip was not installed. > > At least it told me what I needed to do to get pip installed. > Unfortunately, I didn't read the message carefully enough and typed > "sudo apt-get install pip", which of course got me another error because > the correct name of the package is python-pip. Doing "sudo apt-get > install python-pip" finally got me to the point where I could start to > install numpy. > > Of course, if I didn't have sudo privs on the box (most corporate > environments), I never would have gotten that far. > > At this point, "sudo pip install numpy" got me a bunch of errors > culminating in "RuntimeError: Broken toolchain: cannot link a simple C > program", and no indication of how to get any further. You should use apt-get for numpy/scipy on Ubuntu. Although unfortunately IIRC this doesn't work as well as it should since Ubuntu doesn't install the appropriate BLAS/LAPACK libraries by default (leaving you with numpy's fallback libraries). On Windows you should use the MSI installer (or easy_install). Hopefully numpy/scipy will start distributing wheels soon and pip install numpy will actually work. Oscar From roy at panix.com Sat Aug 10 08:43:18 2013 From: roy at panix.com (Roy Smith) Date: Sat, 10 Aug 2013 08:43:18 -0400 Subject: PEP 450 Adding a statistics module to Python References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: Skip Montanaro wrote: > >> installing numpy or scipy is generally no more difficult > >> that executing "pip install (scipy|numpy)" I described the problems I had trying to follow that advice. In article , Oscar Benjamin wrote: > You should use apt-get for numpy/scipy on Ubuntu. Although > unfortunately IIRC this doesn't work as well as it should since Ubuntu > doesn't install the appropriate BLAS/LAPACK libraries by default > (leaving you with numpy's fallback libraries). That really kind of proves my point. It's *not* easy to install. Theres' a choice of methods, some of which work in some environments, some of which work in others. And even if apt-get is the preferred install method on Ubuntu, it's a method which is unavailable to people without root access (and may be undesirable if you rely on virtualenv to keep multiple projects cleanly separated). And, what happens if you don't have the right libraries? Do you end up with an install which is missing some functionality, or one where all the calls work, but they're slower, or numerically unstable, or what? All these questions go away if it's packaged with the standard library. I'm not sure where the line should be drawn between "basic stuff that should be included" and "advanced stuff that you need an add-on to get", but certainly mean and std-dev should be in the default distribution. From oscar.j.benjamin at gmail.com Sat Aug 10 09:17:13 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Sat, 10 Aug 2013 14:17:13 +0100 Subject: PEP 450 Adding a statistics module to Python In-Reply-To: References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 10 August 2013 13:43, Roy Smith wrote: > > In article , > Oscar Benjamin wrote: > >> You should use apt-get for numpy/scipy on Ubuntu. Although >> unfortunately IIRC this doesn't work as well as it should since Ubuntu >> doesn't install the appropriate BLAS/LAPACK libraries by default >> (leaving you with numpy's fallback libraries). > > That really kind of proves my point. It's *not* easy to install. > Theres' a choice of methods, some of which work in some environments, > some of which work in others. And even if apt-get is the preferred > install method on Ubuntu, it's a method which is unavailable to people > without root access (and may be undesirable if you rely on virtualenv to > keep multiple projects cleanly separated). > > And, what happens if you don't have the right libraries? Do you end up > with an install which is missing some functionality, or one where all > the calls work, but they're slower, or numerically unstable, or what? AFAIK not having separate BLAS/LAPACK libraries just means that certain operations are a lot slower. If there are differences in accuracy then they aren't significant enough that I've noticed. I think that the reason Ubuntu doesn't install them by default is because it's not sure which ones you want to use. Possibly the best free setup comes from using ATLAS but this is optimised in a CPU-specific way at build time. Ubuntu doesn't provide binaries for it as using generic x86 executables would defeat much of the point of the library (they do make it a lot easier by providing a source package though). Oscar From feliphil at gmx.net Tue Aug 13 14:14:55 2013 From: feliphil at gmx.net (Wolfgang Keller) Date: Tue, 13 Aug 2013 20:14:55 +0200 Subject: PEP 450 Adding a statistics module to Python References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: <20130813201455.9934f35166fb37cf012a4cb9@gmx.net> > I am seeking comments on PEP 450, Adding a statistics module to > Python's standard library: I don't think that you want to re-implement RPy. Sincerely, Wolfgang From oscar.j.benjamin at gmail.com Tue Aug 13 14:44:16 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 13 Aug 2013 19:44:16 +0100 Subject: PEP 450 Adding a statistics module to Python In-Reply-To: <20130813201455.9934f35166fb37cf012a4cb9@gmx.net> References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> <20130813201455.9934f35166fb37cf012a4cb9@gmx.net> Message-ID: On Aug 13, 2013 7:22 PM, "Wolfgang Keller" wrote: > > > I am seeking comments on PEP 450, Adding a statistics module to > > Python's standard library: > > I don't think that you want to re-implement RPy. You're right. He doesn't. Oscar -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Wed Aug 14 02:21:03 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 14 Aug 2013 06:21:03 GMT Subject: PEP 450 Adding a statistics module to Python References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> <20130813201455.9934f35166fb37cf012a4cb9@gmx.net> Message-ID: <520b21cf$0$29885$c3e8da3$5496439d@news.astraweb.com> On Tue, 13 Aug 2013 20:14:55 +0200, Wolfgang Keller wrote: >> I am seeking comments on PEP 450, Adding a statistics module to >> Python's standard library: > > I don't think that you want to re-implement RPy. I never suggested re-implementing RPy. When you read the PEP, you will see that this proposal is to have a Python implementation of statistics functions, not a thin wrapper around another language. -- Steven From cmpython at gmail.com Thu Aug 15 00:26:33 2013 From: cmpython at gmail.com (CM) Date: Wed, 14 Aug 2013 21:26:33 -0700 (PDT) Subject: PEP 450 Adding a statistics module to Python In-Reply-To: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: <43dae143-13a5-4a6a-a4fe-4842656571b6@googlegroups.com> On Friday, August 9, 2013 9:10:18 PM UTC-4, Steven D'Aprano wrote: > I am seeking comments on PEP 450, Adding a statistics module to Python's > standard library: I think it's a very good idea. Good PEP points, too. I hope it happens. From ramit.prasad at jpmorgan.com.dmarc.invalid Fri Aug 16 15:17:07 2013 From: ramit.prasad at jpmorgan.com.dmarc.invalid (Prasad, Ramit) Date: Fri, 16 Aug 2013 19:17:07 +0000 Subject: PEP 450 Adding a statistics module to Python In-Reply-To: <43dae143-13a5-4a6a-a4fe-4842656571b6@googlegroups.com> References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> <43dae143-13a5-4a6a-a4fe-4842656571b6@googlegroups.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF4741866B451@SCACMX008.exchad.jpmchase.net> CM wrote: > > On Friday, August 9, 2013 9:10:18 PM UTC-4, Steven D'Aprano wrote: > > I am seeking comments on PEP 450, Adding a statistics module to Python's > > standard library: > > I think it's a very good idea. Good PEP points, too. I hope it happens. > +1 especially for non-Cpython versions of Python. ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From taldcroft at cfa.harvard.edu Fri Aug 16 11:50:48 2013 From: taldcroft at cfa.harvard.edu (taldcroft at cfa.harvard.edu) Date: Fri, 16 Aug 2013 08:50:48 -0700 (PDT) Subject: PEP 450 Adding a statistics module to Python In-Reply-To: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: <0d60fd90-eb19-4702-acd5-dd7ba0eddeda@googlegroups.com> On Friday, August 9, 2013 9:10:18 PM UTC-4, Steven D'Aprano wrote: > I am seeking comments on PEP 450, Adding a statistics module to Python's > > standard library: > > > > http://www.python.org/dev/peps/pep-0450/ > > > > Please read the FAQs before asking anything :-) > I think this is a super idea. Python is showing up in high-school and colllege intro programming courses here in the U.S. Having a solid statistics module built in would work well in that context and make it even more natural as a complement to math courses. Beyond the educational aspect, having a built-in module to *correctly* handle the frequent light-weight use cases would be useful across many professional disciplines. I use NumPy on a daily basis and help scientists with installation problems frequently. I can emphatically state that NumPy is not easy to install for newbies. Open up a brand new Mac and look, no compilers! Even experienced users can have problems with gfortran vs. g77 etc. Anyone that has ever built BLAS/ATLAS from source will also tell you that SciPy is definitely not a simple "pip install" on many platforms (particularly if you don't have root). - Tom From chris.barker at noaa.gov Fri Aug 16 12:31:34 2013 From: chris.barker at noaa.gov (chris.barker at noaa.gov) Date: Fri, 16 Aug 2013 09:31:34 -0700 (PDT) Subject: PEP 450 Adding a statistics module to Python In-Reply-To: <0d60fd90-eb19-4702-acd5-dd7ba0eddeda@googlegroups.com> References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> <0d60fd90-eb19-4702-acd5-dd7ba0eddeda@googlegroups.com> Message-ID: > > I am seeking comments on PEP 450, Adding a statistics module to Python's The trick here is that numpy really is the "right" way to do this stuff. I like to say: "crunching numbers in python without numpy is like doing text processing without using the string object" What this is really an argument for is a numpy-lite in the standard library, which could be used to build these sorts of things on. But that's been rejected before... A few other comments: 1) the numpy folks have been VERY good at providing binaries for Windows and OS-X -- easy point and click installing. 2) I hope we're almost there with standardizing pip and binary wheels, at which point pip install will be painless. even before (2) -- pip install works fine anywhere the system is set up to build python extensions (granted, not a given on Windows and Mac, but pretty likely on Linux) -- the idea that running pip install wrote out a lot of text (but worked!) is somehow a barrier to entry is absurd -- anyone building their own stuff on Linux is used to that. (NOTE: you only need Fortran if you want highly optimized linear algebra stuff -- clearly this use-case is for folks that don't need that!) 3) The fact that the numpy functions have optional arguments is NOT a problem -- the simple calls work as expected -- no one needs to figure out the optional arguments that doesn't need them -- and if they do need them, they had better be there! All that being said -- if you do decide to do this, please use a PEP 3118 (enhanced buffer) supporting data type (probably array.array) -- compatibility with numpy and other packages for crunching numbers is very nice. If someone decides to build a stand-alone stats package -- building it on a ndarray-lite (PEP 3118 compatible) object would be a nice way to go. One other point -- for performance reason, is would be nice to have some compiled code in there -- this adds incentive to put it in the stdlib -- external packages that need compiling is what makes numpy unacceptable to some folks. -Chris From oscar.j.benjamin at gmail.com Fri Aug 16 13:15:52 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Fri, 16 Aug 2013 18:15:52 +0100 Subject: PEP 450 Adding a statistics module to Python In-Reply-To: References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> <0d60fd90-eb19-4702-acd5-dd7ba0eddeda@googlegroups.com> Message-ID: On 16 August 2013 17:31, wrote: >> > I am seeking comments on PEP 450, Adding a statistics module to Python's > > The trick here is that numpy really is the "right" way to do this stuff. Although it doesn't mention this in the PEP, a significant point that is worth bearing in mind is that numpy is only for CPython, not PyPy, IronPython, Jython etc. See here for a recent update on the status of NumPyPy: http://morepypy.blogspot.co.uk/2013_08_01_archive.html > I like to say: > "crunching numbers in python without numpy is like doing text processing without using the string object" It depends what kind of number crunching you're doing. Numpy gives efficient C-style number crunching but it doesn't really give efficient ways to take advantage of the areas where Python is better than C such as having efficient infinite range integers, and decimal and rational arithmetic in the standard library. You can use dtype=object to use all these things with numpy arrays but in my experience this is typically not faster than working with Python lists and is only really useful when you want numpy's multi-dimensional, view-type slicing. Here's an example where Steven's statistics module is more accurate: >>> numpy.mean([-1e60, 100, 100, 1e60]) 0.0 >>> statistics.mean([-1e60, 100, 100, 1e60]) 50.0 Okay so that's a toy example but it illustrates that Steven is aiming for ultra-high accuracy where numpy is primarily aimed at speed. He's also tried to ensure that it works properly with e.g. fractions: >>> from fractions import Fraction as F >>> data = [F('1/7'), F('3/7')] >>> numpy.mean(data) 0.2857142857142857 >>> statistics.mean(data) Fraction(2, 7) and decimals: >>> data = [D('0.1'), D('0.01'), D('0.001')] >>> numpy.mean(data) .... TypeError: unsupported operand type(s) for /: 'decimal.Decimal' and 'float' >>> statistics.mean(data) Decimal('0.037') > What this is really an argument for is a numpy-lite in the standard library, which could be used to build these sorts of things on. But that's been rejected before... If it's a numpy-lite then it's a numpy-ultra-lite. It really doesn't provide much of what numpy provides. I would describe it as a Pythonic implementation of elementary statistical computation rather than a numpy-lite. [snip] > > All that being said -- if you do decide to do this, please use a PEP 3118 (enhanced buffer) supporting data type (probably array.array) -- compatibility with numpy and other packages for crunching numbers is very nice. > > If someone decides to build a stand-alone stats package -- building it on a ndarray-lite (PEP 3118 compatible) object would be a nice way to go. Why? Yes I'd also like an ndarray-lite or rather an ultra-lite 1-dimensional version but why would it be useful for the statistics module over using standard Python containers? Note that numpy arrays do work with the reference implementation of the statistics module (they're just treated as iterables): >>> import numpy >>> import statistics >>> statistics.mean(numpy.array([1, 2, 3])) 2.0 >>> statistics.mean(numpy.array([[1, 2, 3], [4, 5, 6]])) array([ 2.5, 3.5, 4.5]) > One other point -- for performance reason, is would be nice to have some compiled code in there -- this adds incentive to put it in the stdlib -- external packages that need compiling is what makes numpy unacceptable to some folks. It might be good to have a C accelerator one day but actually I think the pure-Python-ness of it is a strong reason to have it since it provides accurate statistics functions to all Python implementations (unlike numpy) at no additional cost. Oscar From steve+comp.lang.python at pearwood.info Fri Aug 16 14:51:49 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 Aug 2013 18:51:49 GMT Subject: PEP 450 Adding a statistics module to Python References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> <0d60fd90-eb19-4702-acd5-dd7ba0eddeda@googlegroups.com> Message-ID: <520e74c5$0$30000$c3e8da3$5496439d@news.astraweb.com> On Fri, 16 Aug 2013 09:31:34 -0700, chris.barker wrote: >> > I am seeking comments on PEP 450, Adding a statistics module to >> > Python's > > The trick here is that numpy really is the "right" way to do this stuff. Numpy does not have a monopoly on the correct algorithms for statistics functions, and a big, heavyweight library like numpy is overkill for many lightweight statistics tasks. One shouldn't need to turn on a nuclear reactor just to put the light on in your fridge. > I like to say: > "crunching numbers in python without numpy is like doing text processing > without using the string object" Your analogy is backwards. String objects actually aren't optimal for heavy duty text processing, because they're immutable. If you're serious about crunching vast amounts of numbers, you'll use numpy. If you're serious about crunch vast amounts of text, say for a text editor or word processor, you *won't* use strings, you'll use some sort of mutable buffer, or ropes, or some other data type. But very unlikely to use strings. > What this is really an argument for is a numpy-lite in the standard > library, which could be used to build these sorts of things on. But > that's been rejected before... "Numpy-lite". Which parts of numpy? Who maintains it? The numpy release schedule is nothing like the standard library's release schedule, so which one has to change? Or does somebody fork numpy, giving two independent code bases? What about Jython, IronPython, and other Python implementations? Even PyPy doesn't support numpy yet, and Jython and IronPython probably never will, since they're not C-based. > A few other comments: > > 1) the numpy folks have been VERY good at providing binaries for Windows > and OS-X -- easy point and click installing. > > 2) I hope we're almost there with standardizing pip and binary wheels, > at which point pip install will be painless. Yeah, right, sure it will be. I've been waiting a decade for package management on Linux to become painless, and it still isn't. There's no reason to expect pip will be more painless than aptitude or yum. But even if it is, installation of software is not just a software problem to be solved by better technology. There is also the social problem that not everyone is permitted to arbitrarily install software. I'm not just talking about security policies on the machine, but security policies in real life. People can be sacked for installing software they don't have permission to install. Machines may be locked down, users may have to submit a request before software will be installed. That may involve a security audit, legal review of licencing, strategy for full roll-back, potentially even a complete code audit. (Imagine auditing all of numpy.) Or policy may simply say, *no software from unapproved vendors* full stop. Not everyone is privileged to be permitted to install whatever software they like, when they like. Here are two organisations that make software installation requests *easy*: http://www.uhd.edu/computing/acl/SoftwareInstallationRequest.html http://www.calstatela.edu/its/services/software/ instructsoftwarerequest.php/form2.php Pip install isn't going to fix that. There are many, many people in a situation where the Python std lib is approved, usually because it comes from a vendor with a support contract (say, RedHat, Ubuntu, or Suse), but getting third-party packages like numpy approved is next to impossible. "Just install numpy" is a solution for a privileged few. > even before (2) -- pip install works fine anywhere the system is set up > to build python extensions (granted, not a given on Windows and Mac, but > pretty likely on Linux) Oh, well that's okay then -- that's three, maybe four percent of the computing world taken care of! Problem solved! Not. > -- the idea that running pip install wrote out a > lot of text (but worked!) is somehow a barrier to entry is absurd -- > anyone building their own stuff on Linux is used to that. Do you realise that not all Python programmers are used to, or able to, "build their own stuff on Linux"? [...] > All that being said -- if you do decide to do this, please use a PEP > 3118 (enhanced buffer) supporting data type (probably array.array) -- > compatibility with numpy and other packages for crunching numbers is > very nice. py> import array py> data = array.array('f', range(1000)) py> import statistics py> statistics.mean(data) 499.5 py> statistics.stdev(data) 288.8194360957494 If the data type supports the sequence protocol, it should work with my module. If it fails to work, submit a bug report, and I will fix it. > If someone decides to build a stand-alone stats package -- building it > on a ndarray-lite (PEP 3118 compatible) object would be a nice way to > go. > > > One other point -- for performance reason, is would be nice to have some > compiled code in there -- this adds incentive to put it in the stdlib -- > external packages that need compiling is what makes numpy unacceptable > to some folks. Like the decimal module, it will probably remain pure-Python for a few releases, but I hope that in the future the statistics module will gain a C-accelerated version. (Or Java-accelerated for Jython, etc.) I expect that PyPy won't need one. But because it's not really aimed at number- crunching megabytes of data, speed is not the priority. -- Steven From chris.barker at noaa.gov Fri Aug 16 15:48:16 2013 From: chris.barker at noaa.gov (chris.barker at noaa.gov) Date: Fri, 16 Aug 2013 12:48:16 -0700 (PDT) Subject: PEP 450 Adding a statistics module to Python In-Reply-To: <520e74c5$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> <0d60fd90-eb19-4702-acd5-dd7ba0eddeda@googlegroups.com> <520e74c5$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Friday, August 16, 2013 11:51:49 AM UTC-7, Steven D'Aprano wrote: > > The trick here is that numpy really is the "right" way to do this stuff. > Numpy does not have a monopoly on the correct algorithms for statistics > functions, indeed not -- in fact, a number of them are quite lame, either because of chosen speed vs. accuracy trade offs, or just plain no-one-got-around-to-writing-the-code. I kind of mis-spoke: what I meant was: "a numpy ndarray-similar object is the "right"way to do this", not numpy itself. > and a big, heavyweight library like numpy is overkill for many > lightweight statistics tasks. One shouldn't need to turn on a nuclear > reactor just to put the light on in your fridge. sure -- but you are talking stdlib here -- where do we draw the line? a hard choice every time. > > "crunching numbers in python without numpy is like doing text processing > > without using the string object" > > Your analogy is backwards. String objects actually aren't optimal for > heavy duty text processing, because they're immutable. If you're serious > about crunching vast amounts of numbers, you'll use numpy. If you're > serious about crunch vast amounts of text, say for a text editor or word > processor, you *won't* use strings, you'll use some sort of mutable > buffer, or ropes, or some other data type. But very unlikely to use > strings. but you sure as heck won't use arbitrary pyton sequences of characters. which is what you are doing with this module. > > What this is really an argument for is a numpy-lite in the standard > > library, which could be used to build these sorts of things on. But > > that's been rejected before... > "Numpy-lite". Which parts of numpy? Who maintains it? The numpy release > schedule is nothing like the standard library's release schedule, so > which one has to change? Or does somebody fork numpy, giving two > independent code bases? yup -- that's why it's been rejected before -- but we did get PEP 3118 as a compromise, so one could build an nd-array-lite that was PEP 3118 compatible, and avoid many of the problems above. However, as much a problem is is to install a third-party compiled package, it's a hell of a lot less work than writing a bunch of new code, so it'll probably never get done. I myself am trying to write my new stuff to take PEP 3118 buffers, so I can get full high-performing numpy support, but not require users to have numpy -- it is a bit tricky, but can be done. If/when you get to the C-accelerated version, I suggest you consider it. > What about Jython, IronPython, and other Python implementations? Even > PyPy doesn't support numpy yet, and Jython and IronPython probably never > will, since they're not C-based. There is a numpy for IronPython, though I don't hink it got beyond the alpha stage. But your point is well taken -- but also a reason for an ndarray in the stdlib, then maybe other implementations would support it. > Yeah, right, sure it will be. I've been waiting a decade for package > management on Linux to become painless, and it still isn't. There's no > reason to expect pip will be more painless than aptitude or yum. Probably not, true -- but you needed to get Python from somewhere didn't you? You can't see it's easy to compile that on Windows! > There is also the social problem that not everyone is permitted to arbitrarily install software. I work for the Federal Government -- believe me, I know. There's Google App Engine, and things like that too, to support your point.... > complete code audit. (Imagine auditing all of numpy.) well, the more we add to Pyton's stdlib, the bigger an issue that will be for all Ptyon users -- antoher reason to be cautios. But at the end, I don't think there is a lot you can do with pyton without installing some third-party package? How many people do all their code development in IDLE? al their GUI's with tk? no image processing , writing their own web framework from scratch? The list goes on and on. I may have a few simple text processing scripts that don't use any third party packages, but nothing major. I teach Intro to Python, and while I could probably get away with only the stdlib for the intro class (but sure as heck not the web development class), I don't -- because there is a lot folks should know about do anything real in Python. So as much of a pain as it can be to use third-party packages, we can't put everything in the stdlib for that reason. > There are many, many people in a situation where the Python std lib is > approved, usually because it comes from a vendor with a support contract > (say, RedHat, Ubuntu, or Suse), but getting third-party packages like > numpy approved is next to impossible. don't all three of those ship numpy? I haven't used them in ages. > > to build python extensions (granted, not a given on Windows and Mac, but > > pretty likely on Linux) > > Oh, well that's okay then -- that's three, maybe four percent of the > computing world taken care of! Problem solved! hence the binaries.... really -- the "I can't install an unapproved package" is a show-stopper. "I can't built it" isn't. > > anyone building their own stuff on Linux is used to that. > > Do you realise that not all Python programmers are used to, or able to, > > "build their own stuff on Linux"? then why not "yum install numpy"? or whatever? > > All that being said -- if you do decide to do this, please use a PEP > > 3118 (enhanced buffer) supporting data type (probably array.array) -- > > compatibility with numpy and other packages for crunching numbers is > > very nice. > > py> import array > py> data = array.array('f', range(1000)) > py> import statistics > py> statistics.mean(data) > 499.5 I realized this after posting -- that is a nice feature, and could help a lot -- hurray for the buffer protocol! This makes room for compiled optimization down the road, and then you might be able to use your code with numpy arrays efficiently. > If the data type supports the sequence protocol, it should work with my > module. If it fails to work, submit a bug report, and I will fix it. fair enough. > Like the decimal module, it will probably remain pure-Python for a few > releases, but I hope that in the future the statistics module will gain a > C-accelerated version. (Or Java-accelerated for Jython, etc.) a perfectly reasonable development path. I expect > that PyPy won't need one. But because it's not really aimed at number- > crunching megabytes of data, speed is not the priority. I thought one of the key points of PyPy was performance? But anyway, maybe RPython and the JIT will take care of that. Anyway, this looks like a great project -- not so sure about putting it in the stdlib, and do hope you'll keep the number crunchers in mind, but great stuff none the less. -Chris From chris.barker at noaa.gov Fri Aug 16 15:00:18 2013 From: chris.barker at noaa.gov (chris.barker at noaa.gov) Date: Fri, 16 Aug 2013 12:00:18 -0700 (PDT) Subject: PEP 450 Adding a statistics module to Python In-Reply-To: References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> <0d60fd90-eb19-4702-acd5-dd7ba0eddeda@googlegroups.com> Message-ID: <55530414-1cf0-46fa-bdce-890d8679b292@googlegroups.com> On Friday, August 16, 2013 10:15:52 AM UTC-7, Oscar Benjamin wrote: > On 16 August 2013 17:31, wrote: > Although it doesn't mention this in the PEP, a significant point that > > is worth bearing in mind is that numpy is only for CPython, not PyPy, > > IronPython, Jython etc. See here for a recent update on the status of It does mention it, though I think not the additional implementations by name. And yes, the lack of numpy on the other implementation is a major limitation. > > "crunching numbers in python without numpy is like doing text processing without using the string object" > > It depends what kind of number crunching you're doing. As it depends on what kind of text processing your doing.....you could go a long way with a pure-python sequence of abstract characters library, but it would be painfully slow -- no one would even try. I guess there are more people working with, say, hundreds of numbers, than people trying to process an equally tiny amount of text...but this is a digression. My point about that is that you can only reasonably do string processing with python because python has the concept of a string, not just an arbitrary sequence of characters, and not just for speed's sake, but for the nice semantics. Anyone that has used an array-oriented language or library is likely to get addicted to the idea that an array of numbers as a first class concept is really, really helpful, for both performance and semantics. > Numpy gives efficient C-style number crunching which is the vastly most common case. Also, a properly designed algorithm may well need to know something about the internal storage/processing of the data type -- i.e. the best way to compute a given statistic for floating point may not be the same as for integers (or decimal, or...). Maybe you can get a good one that works for most, but.... > You can use dtype=object to use all these things with numpy arrays but in my > experience this is typically not faster than working with Python lists That's quite true. In fact, often slower. > and is only really useful when you want numpy's multi-dimensional, > view-type slicing. which is very useful indeed! > Here's an example where Steven's statistics module is more accurate: > >>> numpy.mean([-1e60, 100, 100, 1e60]) > > 0.0 > > >>> statistics.mean([-1e60, 100, 100, 1e60]) > > 50.0 the wonders of floating point arithmetic! -- but this looks like more of an argument for a better algorithm in numpy, than a reason to have something in the stdlib -- in fact, that's been discussed lately, there is talk of using compensated summation in the numpy sum() method -- not sure of the status. > Okay so that's a toy example but it illustrates that Steven is aiming > for ultra-high accuracy where numpy is primarily aimed at speed. well, yes, for the most part, numpy does trade speed for accuracy when it has too -- but that's not the case here, I think this is ta case of "no one took the time to write a better algorithm" He's also tried to ensure that it works properly with e.g. fractions: That is pretty cool, yes. > > What this is really an argument for is a numpy-lite in the standard library, which could be used to build these sorts of things on. But that's been rejected before... > > If it's a numpy-lite then it's a numpy-ultra-lite. It really doesn't > provide much of what numpy provides. I wasn't clear -- my point was that things like this should be build on a numpy-like array object (numpy-lite) -- so first adding such an object to the stdlib, then building this off it would be nice. But a key problem with that is where do you draw the line that defines numpy-lite? I"d say jsut the core storage object, but then someone wants to add statistics, and someone else wants to add polynomial, and then random numbers, then ... and pretty sure you've got numpy again! > > All that being said -- if you do decide to do this, please use a PEP 3118 (enhanced buffer) supporting data type (probably array.array) -- compatibility with numpy and other packages for crunching numbers is very nice. > > > If someone decides to build a stand-alone stats package -- building it on a ndarray-lite (PEP 3118 compatible) object would be a nice way to go. > > Why? Yes I'd also like an ndarray-lite or rather an ultra-lite > 1-dimensional version but why would it be useful for the statistics > module over using standard Python containers? Note that numpy arrays > do work with the reference implementation of the statistics module > (they're just treated as iterables): One of the really great things about numpy is that when you work with a LOT of numbers (which is not rare in this era of Big Data) it stores them efficiently, and you can push them around between different arrays, and other libraries without unpacking and copying data. That's what PEP 3118 is all about. It looks like there is some real care being put into these algorithms, so it would be nice if they could be efficiently used for large data sets and with numpy. > >>> import numpy > >>> import statistics > >>> statistics.mean(numpy.array([1, 2, 3])) you'll probably find that this is slower than a python list -- numpy has some overhead when used as a generic sequence. > > One other point -- for performance reason, is would be nice to have some compiled code in there -- this adds incentive to put it in the stdlib -- external packages that need compiling is what makes numpy unacceptable to some folks. > > It might be good to have a C accelerator one day but actually I think > the pure-Python-ness of it is a strong reason to have it since it > provides accurate statistics functions to all Python implementations > (unlike numpy) at no additional cost. Well, I'd rather not have a package that is great for education and toy problems, but not-so-good for the real ones... I guess my point is this: This is a way to make the standard python distribution better for some common computational tasks. But rather than think of it as "we need some stats functions in the python stdlib", perhaps we should be thinking: "out of the box python should be better for computation" -- in which case, I'd start with a decent array object. -Chris From oscar.j.benjamin at gmail.com Fri Aug 16 15:41:01 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Fri, 16 Aug 2013 20:41:01 +0100 Subject: PEP 450 Adding a statistics module to Python In-Reply-To: <55530414-1cf0-46fa-bdce-890d8679b292@googlegroups.com> References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> <0d60fd90-eb19-4702-acd5-dd7ba0eddeda@googlegroups.com> <55530414-1cf0-46fa-bdce-890d8679b292@googlegroups.com> Message-ID: On 16 August 2013 20:00, wrote: > > > One other point -- for performance reason, is would be nice to have some compiled code in there -- this adds incentive to put it in the stdlib -- external packages that need compiling is what makes numpy unacceptable to some folks. >> >> It might be good to have a C accelerator one day but actually I think >> the pure-Python-ness of it is a strong reason to have it since it >> provides accurate statistics functions to all Python implementations >> (unlike numpy) at no additional cost. > > Well, I'd rather not have a package that is great for education and toy problems, but not-so-good for the real ones... Again it depends what you mean by "real". From the other lists where we meet I'd guess that your problems are in the "needs a nuclear reactor" camp. I doubt that the stdlib will ever be sufficiently mathematically/computationally oriented to fully service either of our needs (and I don't mean that as a criticism). I persuaded the IT guys at my work that we needed the whole Enthought Python Distribution on all machines just because I didn't want to have to argue about individual packages. However in my real work, where I compute means and variances etc. I very often do work with very small datasets and I know a lot of others who work almost exclusively with them (think e.g. clinical data where N is often less than 100). > I guess my point is this: > > This is a way to make the standard python distribution better for some common computational tasks. But rather than think of it as "we need some stats functions in the python stdlib", perhaps we should be thinking: "out of the box python should be better for computation" -- in which case, I'd start with a decent array object. I think that, whether or not the statistics module gains a C accelerator, if a fast numerical array type comes along then I'd expect that the statistics module would use its methods as a fast path. And if it provides a speed boost without compromising boundedness or accuracy I'm sure that the array type would be used internally where appropriate (just as numpy converts collections to arrays before computation). Oscar From roy at panix.com Fri Aug 16 22:06:22 2013 From: roy at panix.com (Roy Smith) Date: Fri, 16 Aug 2013 22:06:22 -0400 Subject: PEP 450 Adding a statistics module to Python References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> <0d60fd90-eb19-4702-acd5-dd7ba0eddeda@googlegroups.com> Message-ID: In article <0d60fd90-eb19-4702-acd5-dd7ba0eddeda at googlegroups.com>, taldcroft at cfa.harvard.edu wrote: > Python is showing up in high-school and colllege intro programming > courses here in the U.S. Yup. For the past few years, I've been a judge in the NYC Science and Engineering Fair (http://collegenow.cuny.edu/sciencefair/). By far, the most common language I see CS projects done in, is Python. From josef.pktd at gmail.com Sat Aug 17 08:13:32 2013 From: josef.pktd at gmail.com (Josef Pktd) Date: Sat, 17 Aug 2013 05:13:32 -0700 (PDT) Subject: PEP 450 Adding a statistics module to Python In-Reply-To: References: <520592f9$0$30000$c3e8da3$5496439d@news.astraweb.com> <0d60fd90-eb19-4702-acd5-dd7ba0eddeda@googlegroups.com> Message-ID: <734ccb26-8ca5-4c3e-be46-e1c9470c0a90@googlegroups.com> I think the install issues in the pep are exaggerated, and are in my opinion not a sufficient reason to get something into the standard lib. google appengine includes numpy https://developers.google.com/appengine/docs/python/tools/libraries27 I'm on Windows, and installing numpy and scipy are just binary installers that install without problems. There are free binary distributions (for Windows and Ubuntu) that include all the main scientific applications. One-click installer on Windows http://code.google.com/p/pythonxy/wiki/Welcome http://code.google.com/p/winpython/ How many Linux distributions don't include numpy? (I have no idea.) For commercial support Enthought's and Continuum's distributions include all the main packages. I think having basic descriptive statistics is still useful in a basic python installation. Similarly, almost all the descriptive statistics moved from scipy.stats to numpy. However, what is the longterm scope of this supposed to be? I think working with pure python is interesting for educational purposes http://www.greenteapress.com/thinkstats/ but I don't think it will get very far for more extensive uses. Soon you will need some linear algebra (numpy.linalg and scipy.linalg) and special functions (scipy.special). You can reimplement them, but what's the point to duplicate them in the standard lib? For example: t test: which versions? one-sample, two-sample, paired and unpaired, with and without homogeneous variances, with 3 alternative hypothesis. If we have t test, shouldn't we also have ANOVA when we want to compare more than two samples? ... If the Python versions that are not using a C backend need a statistics package and partial numpy replacement, then I don't think it needs to be in the CPython lib. If think the "nuclear reactor" analogy is in my opinion misplaced. A python implementation of statistics is a bycycle, numpy is a car, and if you need some heavier lifting in statistics or machine learning, then the trucks are scipy, scikit-learn and statsmodels (and pandas for the data handling). And rpy for things that are not directly available in python. I'm one of the maintainers for scipy.stats and for statsmodels. We have a similar problem of deciding on the boundaries and scope of numpy, scipy.stats, pandas, patsy, statsmodels and scikit-learn. There is some overlap of functionality where the purpose or use cases are different, but in general we try to avoid too much duplication. https://pypi.python.org/pypi/statsmodels https://pypi.python.org/pypi/pandas https://pypi.python.org/pypi/patsy (R like formulas) https://pypi.python.org/pypi/scikit-learn Josef From gouzounakis at hotmail.com Sat Aug 10 07:32:42 2013 From: gouzounakis at hotmail.com (D. Xenakis) Date: Sat, 10 Aug 2013 04:32:42 -0700 (PDT) Subject: Python 3.3 + QtSql + ssh tunnel - Connection problem Message-ID: <60c57e73-9aa2-4eb7-896a-b1d7ea99c610@googlegroups.com> Im using python 3.3 on win7 64bit and trying to connect to a MySQL database on a remote server through a putty ssh tunnel. Running the script below im getting "Physical connection to the database did not activate!". What im i doing wrong?! I tried to find a working example but couldnt find one. thx in advance guys from PyQt5 import QtSql def main(): db = QtSql.QSqlDatabase.addDatabase("QMYSQL") db.setHostName("127.0.0.1") db.setPort(3306) db.setDatabaseName("username_databasename") db.setUserName(username) db.setPassword(userpassword) ok = db.open() if ok: print ("Physical connection to the database activated") else: print ("Physical connection to the database did not activate!") return create_table(db) db.close() main() From dorianmj at gmail.com Sat Aug 10 07:43:32 2013 From: dorianmj at gmail.com (Dorian MJ) Date: Sat, 10 Aug 2013 15:43:32 +0400 Subject: ftplib returns EOFError Message-ID: print "\t\tUploading file %s..." % newname try: self.ftp.storbinary("STOR %s" % newname, open(file)) except EOFError: # yep?? self.ftp.connect(self.ftpServ) # reconnecting self.ftp.login(ftpUser, ftpPass) self.ftp.storbinary("STOR %s" % newname, open(file)) print "\t\tFile %s uploaded" % newname -------------- next part -------------- An HTML attachment was scrubbed... URL: From gvanem at broadpark.no Sat Aug 10 09:09:31 2013 From: gvanem at broadpark.no (Gisle Vanem) Date: Sat, 10 Aug 2013 15:09:31 +0200 Subject: Calling Py_Main() and parsing the output from C Message-ID: <9FF35ECE151D4064A1110526DB6CCC8E@dev.null> Hello Python & C-experts. I'm trying to embed python27.dll in a C-program written in MingW-gcc 4.7.2. I've successfully done these initial steps: typedef int (*Py_Main_t) (int argc, char **argv); handle = LoadLibrary ("python27.dll"); py_main = (Py_Main_t) GetProcAddress (handle, "Py_Main"); argv[0] = who_am_I; /* the .exe of the embedding program. python*.dll doesn't seems to care what this is */ argv[1] = (char*) "-c"; argv[2] = PYTHON_CMD; /* see below */ argv[3] = NULL; rc = (*py_main) (3, argv); DEBUG: pyembed.c(76): Calling Py_Main(): argv[0] = "G:\vc_2010\VC\Projects\EnvTool\src\envtool.exe" argv[1] = "-c" argv[2] = "import sys;[sys.stdout.write('%s\n' % p) for (i,p) in enumerate(sys.path)]" argv[3] = NULL. Which produces the expected 'sys.path[]: g:\Programfiler\Python27\lib\site-packages\pyzmq-2.2.0.1-py2.7-win32.egg g:\Programfiler\Python27\lib\site-packages\nose-1.2.1-py2.7.egg ... But I'd like to grab the stdout from Py_Main() into a pipe, mmap-file or similar for the calling program to parse. Before I used the embedding solution, I simply spawned python.exe using my shell and popen(). Then parsed the output using fgets(). This work fine. But I'd like to try embedding now. Since avoiding the shell should be faster. No? How can I accomplish the grabbing of Py_Main() output simplest? Is creating a memory-mapped file in the calling program a good idea? Can Py_Main() print to that? If so, how? I'm on Win-XP SP3. --gv From i.am.songoku at gmail.com Sat Aug 10 11:33:36 2013 From: i.am.songoku at gmail.com (Krishnan Shankar) Date: Sat, 10 Aug 2013 21:03:36 +0530 Subject: Python Basic Doubt Message-ID: Hi Fellow Python Friends, I am new to Python and recently subscribed to the mailing list.I have a doubt regarding the basics of Python. Please help me in understanding the below concept. So doubt is on variables and their contained value. Why does in the below example from Interpreter exploration value of c take pre existing memory location. >>> a=10 >>> id(a) 21665504 >>> b=a >>> id(b) 21665504 >>> c=10 >>> id(c) 21665504 I am actually assigning new value to c. But from the value of id() all three variables take same location. With variables a and b it is ok. But why c taking the same location? Regards, Krishnan -------------- next part -------------- An HTML attachment was scrubbed... URL: From python.list at tim.thechases.com Sat Aug 10 12:40:40 2013 From: python.list at tim.thechases.com (Tim Chase) Date: Sat, 10 Aug 2013 11:40:40 -0500 Subject: Python Basic Doubt In-Reply-To: References: Message-ID: <20130810114040.6ac78fe8@bigbox.christie.dr> On 2013-08-10 21:03, Krishnan Shankar wrote: > >>> a=10 > >>> id(a) > 21665504 > >>> b=a > >>> id(b) > 21665504 > >>> c=10 > >>> id(c) > 21665504 > > I am actually assigning new value to c. But from the value of id() > all three variables take same location. With variables a and b it > is ok. But why c taking the same location? As an internal optimization, CPython caches small integer values >>> a = 256 >>> b = 256 >>> a is b True >>> a = 257 >>> b = 257 >>> a is b False Because it's an internal implementation detail, you shouldn't count on this behavior (Jython or Cython or IronPython may differ; or future versions of Python may cache a different range of numbers). Generally, if you are using the "is" operator to compare against anything other than None, you're doing it wrong. There are exceptions to this, but it takes knowing the particulars. -tkc From jarabal at gmail.com Sat Aug 10 14:00:58 2013 From: jarabal at gmail.com (Xavi) Date: Sat, 10 Aug 2013 20:00:58 +0200 Subject: Python Basic Doubt In-Reply-To: <20130810114040.6ac78fe8@bigbox.christie.dr> References: <20130810114040.6ac78fe8@bigbox.christie.dr> Message-ID: <52067FDA.2030908@gmail.com> Hello, El 10/08/2013 18:40, Tim Chase escribi?: > Generally, if you are using the "is" operator to compare against > anything other than None, you're doing it wrong. There are exceptions > to this, but it takes knowing the particulars. Now I have one doubt, I use 'is' to compare basic types in python 3, for example .- v = [] if type(v) is list: print('Is list...') Because I think it is more clear and faster than .- type(v) == [].__class__ ... or ... isinstance(v, list) Is this correct? Thanks. -- Xavi From tjreedy at udel.edu Sat Aug 10 15:46:13 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 10 Aug 2013 15:46:13 -0400 Subject: Python Basic Doubt In-Reply-To: <52067FDA.2030908@gmail.com> References: <20130810114040.6ac78fe8@bigbox.christie.dr> <52067FDA.2030908@gmail.com> Message-ID: On 8/10/2013 2:00 PM, Xavi wrote: > Hello, > > El 10/08/2013 18:40, Tim Chase escribi?: >> Generally, if you are using the "is" operator to compare against >> anything other than None, you're doing it wrong. There are exceptions >> to this, but it takes knowing the particulars. > > Now I have one doubt, I use 'is' to compare basic types in python 3, for > example .- > > v = [] > if type(v) is list: > print('Is list...') > > Because I think it is more clear and faster than .- > type(v) == [].__class__ ... or ... isinstance(v, list) > > Is this correct? It depends on the context. If one is writing a test for a function that is defined as returning a list, such as the builtin function *sorted*, then 'is list' would be correct. When one knows the type, as in your toy snippet, 'is list' is nonsensical. In a more typical situation, as when testing the argument to a function in the body of a function, then 'isinstance(arg, list)' is almost certainly more correct (but often still not best) as the function should usually accept at least any list subclass instance. def popslice(lis, start, stop=None, step=0): if not isinstance(lis, list): raise TypeError("Can only popslice a list") if stop is None: # here is where is *should* be used start, stop = 0, start ret = lis[start:stop:step] del lis[start:stop:step] return ret lis = list(range(10)) print(popslice(lis, 2, 9, 2), lis) >>> [2, 4, 6, 8] [0, 1, 3, 5, 7, 9] However, why exclude a mutable sequence that support slices but is not specifically a list? def popslice(seq, start, stop=None, step=0): if stop is None: # here is where is *should* be used start, stop = 0, start ret = seq[start:stop:step] del seq[start:stop:step] return ret Bad inputs will raise TypeErrors. TypeError: 'int' object is not subscriptable TypeError: 'tuple' object doesn't support item deletion It this is not good enough, wrap the body in try: ... except TypeError as e: raise TypeError("your custom message here") -- Terry Jan Reedy From rosuav at gmail.com Sat Aug 10 17:37:22 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 10 Aug 2013 22:37:22 +0100 Subject: Python Basic Doubt In-Reply-To: <52067FDA.2030908@gmail.com> References: <20130810114040.6ac78fe8@bigbox.christie.dr> <52067FDA.2030908@gmail.com> Message-ID: On Sat, Aug 10, 2013 at 7:00 PM, Xavi wrote: > Now I have one doubt, I use 'is' to compare basic types in python 3, for > example .- > > v = [] > if type(v) is list: > print('Is list...') > > Because I think it is more clear and faster than .- > type(v) == [].__class__ ... or ... isinstance(v, list) > > Is this correct? > Thanks. This really should be a separate thread, rather than a follow-up to the previous one, since it's quite unrelated. But anyway. The isinstance check is the better one, because it will also accept a subclass of list, which the others won't. ChrisA From gary.herron at islandtraining.com Sat Aug 10 17:48:23 2013 From: gary.herron at islandtraining.com (Gary Herron) Date: Sat, 10 Aug 2013 14:48:23 -0700 Subject: Python Basic Doubt In-Reply-To: <52067FDA.2030908@gmail.com> References: <20130810114040.6ac78fe8@bigbox.christie.dr> <52067FDA.2030908@gmail.com> Message-ID: <5206B527.6080700@islandtraining.com> On 08/10/2013 11:00 AM, Xavi wrote: > Hello, > > El 10/08/2013 18:40, Tim Chase escribi?: >> Generally, if you are using the "is" operator to compare against >> anything other than None, you're doing it wrong. There are exceptions >> to this, but it takes knowing the particulars. > > Now I have one doubt, I use 'is' to compare basic types in python 3, > for example .- > > v = [] > if type(v) is list: > print('Is list...') > > Because I think it is more clear and faster than .- > type(v) == [].__class__ ... or ... isinstance(v, list) > > Is this correct? > Thanks. No! Don't do that! If you want to compare values use the "==" operator. This is an oversimplification, but generally useful for all beginner (and most advanced) programmers: Don't use "is" for comparisons. Use "==". It 20 years of programming Python, I've *needed* to use "is" ... only once or twice. Beyond that, there is a small batch of comparisons where "is" is slightly more Pythonic, but not really necessary. And beyond that, there are several instances where the difference between "is" and "=="" are important. Mostly, using "is" is inappropriate and will get you into compassions that depend on implementation details. For instance don't use "is" until you understand this: q:~> python3 Python 3.3.1 (default, Apr 17 2013, 22:32:14) [GCC 4.7.3] on linux Type "help", "copyright", "credits" or "license" for more information. >>> 101 is 1+100 True >>> 1001 is 1+1000 False Gary Herron From rosuav at gmail.com Sat Aug 10 18:09:02 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 10 Aug 2013 23:09:02 +0100 Subject: Python Basic Doubt In-Reply-To: <5206B527.6080700@islandtraining.com> References: <20130810114040.6ac78fe8@bigbox.christie.dr> <52067FDA.2030908@gmail.com> <5206B527.6080700@islandtraining.com> Message-ID: On Sat, Aug 10, 2013 at 10:48 PM, Gary Herron wrote: > This is an oversimplification, but generally useful for all beginner (and > most advanced) programmers: > Don't use "is" for comparisons. Use "==". > It 20 years of programming Python, I've *needed* to use "is" ... only once > or twice. Hrm, I wouldn't make it that hard a rule. Both comparisons have their place. As has been mentioned earlier in this thread, checking if something is None is spelled "if something is None". Checking if it equals zero is spelled "if it == 0", which is a quite different check. The other common check that uses 'is' is with an argument default where absolutely anything could be passed: _notpassed = object() def frob(appendage, device=_notpassed): """Use some appendage to frob some device, or None to frob nothing. Omit device to frob whatever is currently held in that appendage""" if device is _notpassed: device = ... # whatever you need if device is not None: # frob the device But granted, equality comparisons are a LOT more common than identity comparisons. ChrisA From gary.herron at islandtraining.com Sat Aug 10 20:42:21 2013 From: gary.herron at islandtraining.com (Gary Herron) Date: Sat, 10 Aug 2013 17:42:21 -0700 Subject: Python Basic Doubt In-Reply-To: References: <20130810114040.6ac78fe8@bigbox.christie.dr> <52067FDA.2030908@gmail.com> <5206B527.6080700@islandtraining.com> Message-ID: <5206DDED.8030506@islandtraining.com> On 08/10/2013 03:09 PM, Chris Angelico wrote: > On Sat, Aug 10, 2013 at 10:48 PM, Gary Herron > wrote: >> This is an oversimplification, but generally useful for all beginner (and >> most advanced) programmers: >> Don't use "is" for comparisons. Use "==". >> It 20 years of programming Python, I've *needed* to use "is" ... only once >> or twice. > Hrm, I wouldn't make it that hard a rule. Both comparisons have their > place. As has been mentioned earlier in this thread, checking if > something is None is spelled "if something is None". Checking if it > equals zero is spelled "if it == 0", which is a quite different check. > The other common check that uses 'is' is with an argument default > where absolutely anything could be passed: > > _notpassed = object() > def frob(appendage, device=_notpassed): > """Use some appendage to frob some device, or None to frob nothing. > Omit device to frob whatever is currently held in that appendage""" > if device is _notpassed: > device = ... # whatever you need > if device is not None: > # frob the device > > But granted, equality comparisons are a LOT more common than identity > comparisons. > > ChrisA Everything you say is true, and even reasonable for those who know what's up. But for each of your examples, using "==" is equivalent to using "is". Each of if something == None if device == _not passed if device != None would all work as expected. In none of those cases is "is" actually needed. Given that, and the implementation dependent oddities, I still believe that it is *highly* misleading to teach a beginner about "is". Here's a challenge: What is the simplest non-contrived example where an "is" comparison is *required*. Where substitution of an "==" comparison would cause the program to fail or be significantly less efficient? (I'm not including the nearly immeasurably small timing difference between v==None and v is None.) Gary Herron From rosuav at gmail.com Sat Aug 10 21:00:51 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 11 Aug 2013 02:00:51 +0100 Subject: Python Basic Doubt In-Reply-To: <5206DDED.8030506@islandtraining.com> References: <20130810114040.6ac78fe8@bigbox.christie.dr> <52067FDA.2030908@gmail.com> <5206B527.6080700@islandtraining.com> <5206DDED.8030506@islandtraining.com> Message-ID: On Sun, Aug 11, 2013 at 1:42 AM, Gary Herron wrote: > On 08/10/2013 03:09 PM, Chris Angelico wrote: >> _notpassed = object() >> def frob(appendage, device=_notpassed): >> """Use some appendage to frob some device, or None to frob nothing. >> Omit device to frob whatever is currently held in that appendage""" >> if device is _notpassed: >> device = ... # whatever you need >> if device is not None: >> # frob the device >> >> But granted, equality comparisons are a LOT more common than identity >> comparisons. >> >> ChrisA > > > Everything you say is true, and even reasonable for those who know what's > up. > > But for each of your examples, using "==" is equivalent to using "is". Each > of > if something == None > if device == _not passed > if device != None > would all work as expected. In none of those cases is "is" actually needed. Wrong. If you do equality comparisons, it's entirely possible for something to be passed in that compares equal to the RHS without actually being it, so "is" is precisely what's wanted. (Plus, why go through a potentially expensive comparison check when you can simply check object identity - which could be, for instance, an address check? But performance is a distant second to correctness here.) > Given that, and the implementation dependent oddities, I still believe that > it is *highly* misleading to teach a beginner about "is". > > Here's a challenge: What is the simplest non-contrived example where an > "is" comparison is *required*. Where substitution of an "==" comparison > would cause the program to fail or be significantly less efficient? (I'm > not including the nearly immeasurably small timing difference between > v==None and v is None.) All it takes is a slightly odd or buggy __eq__ implementation and the == versions will misbehave. To check if an argument is something, you use "is", not ==. ChrisA From gary.herron at islandtraining.com Sat Aug 10 23:21:46 2013 From: gary.herron at islandtraining.com (Gary Herron) Date: Sat, 10 Aug 2013 20:21:46 -0700 Subject: Python Basic Doubt In-Reply-To: References: <20130810114040.6ac78fe8@bigbox.christie.dr> <52067FDA.2030908@gmail.com> <5206B527.6080700@islandtraining.com> <5206DDED.8030506@islandtraining.com> Message-ID: <5207034A.6070608@islandtraining.com> On 08/10/2013 06:00 PM, Chris Angelico wrote: > On Sun, Aug 11, 2013 at 1:42 AM, Gary Herron > wrote: >> On 08/10/2013 03:09 PM, Chris Angelico wrote: >>> _notpassed = object() >>> def frob(appendage, device=_notpassed): >>> """Use some appendage to frob some device, or None to frob nothing. >>> Omit device to frob whatever is currently held in that appendage""" >>> if device is _notpassed: >>> device = ... # whatever you need >>> if device is not None: >>> # frob the device >>> >>> But granted, equality comparisons are a LOT more common than identity >>> comparisons. >>> >>> ChrisA >> >> Everything you say is true, and even reasonable for those who know what's >> up. >> >> But for each of your examples, using "==" is equivalent to using "is". Each >> of >> if something == None >> if device == _not passed >> if device != None >> would all work as expected. In none of those cases is "is" actually needed. > Wrong. If you do equality comparisons, it's entirely possible for > something to be passed in that compares equal to the RHS without > actually being it, so "is" is precisely what's wanted. (Plus, why go > through a potentially expensive comparison check when you can simply > check object identity - which could be, for instance, an address > check? But performance is a distant second to correctness here.) You're missing my point. Our knee-jerk reaction to beginners using "is" should be: Don't do that! You almost certainly want "==". Consider "is" an advanced topic. Then you can spend as much time as you want trying to coach them into an understanding of the precise details. But until they have that understanding, they are well served by a rule-of-thumb that says: Use "==" not "is" for comparisons. > >> Given that, and the implementation dependent oddities, I still believe that >> it is *highly* misleading to teach a beginner about "is". >> >> Here's a challenge: What is the simplest non-contrived example where an >> "is" comparison is *required*. Where substitution of an "==" comparison >> would cause the program to fail or be significantly less efficient? (I'm >> not including the nearly immeasurably small timing difference between >> v==None and v is None.) > All it takes is a slightly odd or buggy __eq__ implementation and the > == versions will misbehave. To check if an argument is something, you > use "is", not ==. No, sorry, but any use of the word "is" in an English sentence is way too ambiguous to specify a correct translation into code. To check "if a calculation of some value is a million", you'd write value == 1000000 not value is 1000000 even though there are plenty of other examples where "is" would be correct. > > ChrisA From rosuav at gmail.com Sat Aug 10 23:43:04 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 11 Aug 2013 04:43:04 +0100 Subject: Python Basic Doubt In-Reply-To: <5207034A.6070608@islandtraining.com> References: <20130810114040.6ac78fe8@bigbox.christie.dr> <52067FDA.2030908@gmail.com> <5206B527.6080700@islandtraining.com> <5206DDED.8030506@islandtraining.com> <5207034A.6070608@islandtraining.com> Message-ID: On Sun, Aug 11, 2013 at 4:21 AM, Gary Herron wrote: > On 08/10/2013 06:00 PM, Chris Angelico wrote: >> Wrong. If you do equality comparisons, it's entirely possible for >> something to be passed in that compares equal to the RHS without >> actually being it, so "is" is precisely what's wanted. (Plus, why go >> through a potentially expensive comparison check when you can simply >> check object identity - which could be, for instance, an address >> check? But performance is a distant second to correctness here.) > > You're missing my point. > > Our knee-jerk reaction to beginners using "is" should be: > Don't do that! You almost certainly want "==". Consider "is" an > advanced topic. > > Then you can spend as much time as you want trying to coach them into an > understanding of the precise details. But until they have that > understanding, they are well served by a rule-of-thumb that says: > Use "==" not "is" for comparisons. No, I'm not missing your point; I'm disagreeing with it. I think that 'is' should be taught, that it is every bit as important as '=='; you're walking down the path of "GOTO considered harmful", of decrying some particular language feature because it can be misused. >> All it takes is a slightly odd or buggy __eq__ implementation and the >> == versions will misbehave. To check if an argument is something, you >> use "is", not ==. > > No, sorry, but any use of the word "is" in an English sentence is way too > ambiguous to specify a correct translation into code. To check "if a > calculation of some value is a million", you'd write > value == 1000000 > not > value is 1000000 > even though there are plenty of other examples where "is" would be correct. Granted, English is a poor litmus test for code. But in this particular example, we're talking about immutable types (simple integers), where value and identity are practically the same. A Python implementation would be perfectly justified in interning *every* integer, in which case the 'is' would work perfectly here. The distinction between the two is important when the objects are mutable (so they have an identity that's distinct from their current values). ChrisA From joshua at landau.ws Sun Aug 11 00:04:22 2013 From: joshua at landau.ws (Joshua Landau) Date: Sun, 11 Aug 2013 05:04:22 +0100 Subject: Python Basic Doubt In-Reply-To: References: <20130810114040.6ac78fe8@bigbox.christie.dr> <52067FDA.2030908@gmail.com> <5206B527.6080700@islandtraining.com> <5206DDED.8030506@islandtraining.com> <5207034A.6070608@islandtraining.com> Message-ID: On 11 August 2013 04:43, Chris Angelico wrote: > On Sun, Aug 11, 2013 at 4:21 AM, Gary Herron > wrote: >> On 08/10/2013 06:00 PM, Chris Angelico wrote: >>> All it takes is a slightly odd or buggy __eq__ implementation and the >>> == versions will misbehave. To check if an argument is something, you >>> use "is", not ==. >> >> No, sorry, but any use of the word "is" in an English sentence is way too >> ambiguous to specify a correct translation into code. To check "if a >> calculation of some value is a million", you'd write >> value == 1000000 >> not >> value is 1000000 >> even though there are plenty of other examples where "is" would be correct. > > Granted, English is a poor litmus test for code. But in this > particular example, we're talking about immutable types (simple > integers), where value and identity are practically the same. A Python > implementation would be perfectly justified in interning *every* > integer, in which case the 'is' would work perfectly here. The > distinction between the two is important when the objects are mutable > (so they have an identity that's distinct from their current values). I don't follow this argument. Tuples are immutable yet you're crazy if you check their equality with "is". In Python identity and equality are very distinct. I follow (and agree) with the other arguments: "is" is useful and should be used. It's just this part in particular sounds off. From rosuav at gmail.com Sun Aug 11 00:38:51 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 11 Aug 2013 05:38:51 +0100 Subject: Python Basic Doubt In-Reply-To: References: <20130810114040.6ac78fe8@bigbox.christie.dr> <52067FDA.2030908@gmail.com> <5206B527.6080700@islandtraining.com> <5206DDED.8030506@islandtraining.com> <5207034A.6070608@islandtraining.com> Message-ID: On Sun, Aug 11, 2013 at 5:04 AM, Joshua Landau wrote: > On 11 August 2013 04:43, Chris Angelico wrote: >> The >> distinction between the two is important when the objects are mutable >> (so they have an identity that's distinct from their current values). > > I don't follow this argument. Tuples are immutable yet you're crazy if > you check their equality with "is". In Python identity and equality > are very distinct. True, it's not strictly an issue of mutability of that one level. But anything that's truly immutable (a tuple/frozenset of ints/strings) can in theory be interned. In some languages (no Pythons as far as I'm aware, though one could easily do so and still be fully compliant), all strings are automatically interned, so there's no difference between value and identity for them. A tuple containing a list, for instance, needs its identity; a tuple of three integers is identifiable entirely by its value. ChrisA From gary.herron at islandtraining.com Sun Aug 11 00:29:20 2013 From: gary.herron at islandtraining.com (Gary Herron) Date: Sat, 10 Aug 2013 21:29:20 -0700 Subject: Python Basic Doubt In-Reply-To: References: <20130810114040.6ac78fe8@bigbox.christie.dr> <52067FDA.2030908@gmail.com> <5206B527.6080700@islandtraining.com> <5206DDED.8030506@islandtraining.com> <5207034A.6070608@islandtraining.com> Message-ID: <52071320.9090308@islandtraining.com> On 08/10/2013 08:43 PM, Chris Angelico wrote: > On Sun, Aug 11, 2013 at 4:21 AM, Gary Herron > wrote: >> On 08/10/2013 06:00 PM, Chris Angelico wrote: >>> Wrong. If you do equality comparisons, it's entirely possible for >>> something to be passed in that compares equal to the RHS without >>> actually being it, so "is" is precisely what's wanted. (Plus, why go >>> through a potentially expensive comparison check when you can simply >>> check object identity - which could be, for instance, an address >>> check? But performance is a distant second to correctness here.) >> You're missing my point. >> >> Our knee-jerk reaction to beginners using "is" should be: >> Don't do that! You almost certainly want "==". Consider "is" an >> advanced topic. >> >> Then you can spend as much time as you want trying to coach them into an >> understanding of the precise details. But until they have that >> understanding, they are well served by a rule-of-thumb that says: >> Use "==" not "is" for comparisons. > No, I'm not missing your point; I'm disagreeing with it. I think that > 'is' should be taught, that it is every bit as important as '=='; > you're walking down the path of "GOTO considered harmful", of decrying > some particular language feature because it can be misused. // I agree that both "==" and "is" must be taught. But it's the order in which things are introduced which I'm quibbling about. Something like this makes sense (to me): Lesson 1: Use "==" for comparisons, save "is" for a more advanced lesson. Lesson 2: Use "is" for singleton types like "if a is None:" and other easily defined circumstances. Lesson 3: The whole truth, accompanied by a whole chapter's worth of material that describes Python's data model and the difference between value versus identity and assignment versus binding ... A beginner, on his first program or two, can understand 1, and perhaps parrot 2 without understanding (or needing to). But the step from there to 3 is huge. It's folly to dump that on a first-time programmer. (It's probably even folly to dump that on a seasoned programmer just starting in Python. I still remember not understanding the explanation for "is" when I first read it. And it continued to make no sense until I had enough experience to understand the difference betwen C/C++ assignment to variables and Python's binding of variables.) > >>> All it takes is a slightly odd or buggy __eq__ implementation and the >>> == versions will misbehave. To check if an argument is something, you >>> use "is", not ==. >> No, sorry, but any use of the word "is" in an English sentence is way too >> ambiguous to specify a correct translation into code. To check "if a >> calculation of some value is a million", you'd write >> value == 1000000 >> not >> value is 1000000 >> even though there are plenty of other examples where "is" would be correct. > Granted, English is a poor litmus test for code. But in this > particular example, we're talking about immutable types (simple > integers), where value and identity are practically the same. A Python > implementation would be perfectly justified in interning *every* > integer, in which case the 'is' would work perfectly here. The > distinction between the two is important when the objects are mutable > (so they have an identity that's distinct from their current values). Granted. But please note: There is *nothing* in that sentence which is fit for a beginner programmer. ... "immutable", "value/identity", "interning" ... In one ear and out the other. :-) > > ChrisA -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Sun Aug 11 00:51:36 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 11 Aug 2013 05:51:36 +0100 Subject: Python Basic Doubt In-Reply-To: <52071320.9090308@islandtraining.com> References: <20130810114040.6ac78fe8@bigbox.christie.dr> <52067FDA.2030908@gmail.com> <5206B527.6080700@islandtraining.com> <5206DDED.8030506@islandtraining.com> <5207034A.6070608@islandtraining.com> <52071320.9090308@islandtraining.com> Message-ID: On Sun, Aug 11, 2013 at 5:29 AM, Gary Herron wrote: > A beginner, on his first program or two, can understand 1, and perhaps > parrot 2 without understanding (or needing to). But the step from there to > 3 is huge. It's folly to dump that on a first-time programmer. (It's > probably even folly to dump that on a seasoned programmer just starting in > Python. I still remember not understanding the explanation for "is" when I > first read it. And it continued to make no sense until I had enough > experience to understand the difference betwen C/C++ assignment to variables > and Python's binding of variables.) See, that's where the problem is. You will never grok the difference between == and is if you're still thinking about C variables. (Though you *might* be able to explain it by talking solely about char* and the way two C strings can be the same but stored at different places in memory. But that would be unhelpful most of the time.) This is important *early* reading for a new Python programmer: http://mail.python.org/pipermail/tutor/2010-December/080505.html Note that it was originally posted on python-tutor, so it was definitely aimed at the inexperienced. > On 08/10/2013 08:43 PM, Chris Angelico wrote: > Granted, English is a poor litmus test for code. But in this > particular example, we're talking about immutable types (simple > integers), where value and identity are practically the same. A Python > implementation would be perfectly justified in interning *every* > integer, in which case the 'is' would work perfectly here. The > distinction between the two is important when the objects are mutable > (so they have an identity that's distinct from their current values). > > > Granted. But please note: There is *nothing* in that sentence which is fit > for a beginner programmer. ... "immutable", "value/identity", "interning" > ... In one ear and out the other. :-) Right. This isn't my explanation of 'is' and '=='; it's my explanation of why it's important to HAVE an explanation of the aforementioned. :) Though the difference between value and identity is significant and important, and mutability is bound to crop up fairly early on; so really, it's only the concept of interning that would be really advanced. ChrisA From steve+comp.lang.python at pearwood.info Sun Aug 11 04:07:48 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 11 Aug 2013 08:07:48 GMT Subject: Python Basic Doubt References: <20130810114040.6ac78fe8@bigbox.christie.dr> <52067FDA.2030908@gmail.com> <5206B527.6080700@islandtraining.com> <5206DDED.8030506@islandtraining.com> Message-ID: <52074654$0$30000$c3e8da3$5496439d@news.astraweb.com> On Sat, 10 Aug 2013 20:21:46 -0700, Gary Herron wrote: > Our knee-jerk reaction to beginners using "is" should be: > Don't do that! You almost certainly want "==". Consider "is" an > advanced topic. > > Then you can spend as much time as you want trying to coach them into an > understanding of the precise details. But until they have that > understanding, they are well served by a rule-of-thumb that says: > Use "==" not "is" for comparisons. "...except for comparing to None, where 99.99% of the time you do actually want an identity comparison." This can lead into a more detailed explanation for why you should choose one over the other, or the incurious newbie could take it is something to be learned by rote. I have no problem with telling newbies that there is a reason for this apparently arbitrary rule, but they don't need to learn it *right now* if they don't want. In any case, the rule can include "When in doubt, use equals". I'm good with that :-) -- Steven From tjreedy at udel.edu Sat Aug 10 21:25:36 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 10 Aug 2013 21:25:36 -0400 Subject: Python Basic Doubt In-Reply-To: <5206DDED.8030506@islandtraining.com> References: <20130810114040.6ac78fe8@bigbox.christie.dr> <52067FDA.2030908@gmail.com> <5206B527.6080700@islandtraining.com> <5206DDED.8030506@islandtraining.com> Message-ID: On 8/10/2013 8:42 PM, Gary Herron wrote: > But for each of your examples, using "==" is equivalent to using "is". > Each of > if something == None > if device == _not passed > if device != None > would all work as expected. In none of those cases is "is" actually > needed. class EqualAll: def __eq__(self, other): return True ea = EqualAll() print(ea == None) print(ea == float('nan')) >>> True True -- Terry Jan Reedy From rosuav at gmail.com Sat Aug 10 21:32:19 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 11 Aug 2013 02:32:19 +0100 Subject: Python Basic Doubt In-Reply-To: References: <20130810114040.6ac78fe8@bigbox.christie.dr> <52067FDA.2030908@gmail.com> <5206B527.6080700@islandtraining.com> <5206DDED.8030506@islandtraining.com> Message-ID: On Sun, Aug 11, 2013 at 2:25 AM, Terry Reedy wrote: > On 8/10/2013 8:42 PM, Gary Herron wrote: > >> But for each of your examples, using "==" is equivalent to using "is". >> Each of >> if something == None >> if device == _not passed >> if device != None >> would all work as expected. In none of those cases is "is" actually >> needed. > > > class EqualAll: > def __eq__(self, other): return True That's a contrived example, of course, but it's easy to have a bug in __eq__ that results in the same behaviour. I can't imagine any code that would actually WANT that, unless you're trying to represent Animal Farm. class EqualAll: def __eq__(self, other): if (isinstance(other, pig): return 3 # Some are more equal than others return True ChrisA From steve+comp.lang.python at pearwood.info Sun Aug 11 04:03:04 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 11 Aug 2013 08:03:04 GMT Subject: Python Basic Doubt References: <20130810114040.6ac78fe8@bigbox.christie.dr> <52067FDA.2030908@gmail.com> <5206B527.6080700@islandtraining.com> Message-ID: <52074538$0$30000$c3e8da3$5496439d@news.astraweb.com> On Sat, 10 Aug 2013 17:42:21 -0700, Gary Herron wrote: > But for each of your examples, using "==" is equivalent to using "is". > Each of > if something == None > if device == _not passed > if device != None > would all work as expected. In none of those cases is "is" actually > needed. py> class Weird: ... def __eq__(self, other): ... return True ... py> definitely_not_None = Weird() py> definitely_not_None == None True Oops. Now Weird is, naturally, weird. But classes may have a legitimate reason to claim to be equal to None, or at least the creator of the class may believe he has a legitimate reason. It's not forbidden, therefore you have to assume somebody will do so. But more importantly, in checking whether some value is a sentinel, the *intention* of the code is to check that the value *actually is* the sentinel, not merely that it happens to be equal to the sentinel. To give an analogy: when you hand the codes to the nuclear arsenal to the President, you want to make sure that it actually *is* the President, not some impostor who merely looks and sounds like him. "value == None" not only risks doing the wrong thing if passed some weird object, but it fails to match the intention of the code. Whereas "value is None" is idiot-proof (it cannot fail -- short of hacking the compiler, you cannot change the meaning of either "is" or "None"), it matches the intention of the code, and therefore is clearer, more explicit, and more idiomatic. And as a bonus, it's faster too. But speed is not why you should use it. You should use it because it matches the intention of the code. You don't want to test for some arbitrary person who happens to look like the President, you want to test for the President himself, and nobody but the President. On the other hand, code like "x is 42.9" does not match the intention of the code, and is likely to fail. "is" is not a synonym for "==". Conflating them is a bad thing, whether you use "is" to check for equality or "==" to check for identity. > Given that, and the implementation dependent oddities, I still believe > that it is *highly* misleading to teach a beginner about "is". Careful -- it's not "is" which is implementation-dependent. The "is" operator works the same way in every Python implementation. What differs are the rules for when new instances are created from literals. > Here's a challenge: What is the simplest non-contrived example where an > "is" comparison is *required*. Where substitution of an "==" comparison > would cause the program to fail or be significantly less efficient? (I'm > not including the nearly immeasurably small timing difference between > v==None and v is None.) Easy-peasey. Test code often relies on "is" to check object identity. Occasionally such comparisons work there way into production code, but they're common in tests. In one of my test suites, I have code that verifies that a certain function which takes a list as argument does not modify it in place. So my unit test looks like this: def testNoInPlaceModifications(self): # Test that the function does not modify its input data. data = self.prepare_data() assert len(data) != 1 # Necessary to avoid infinite loop. assert data != sorted(data) saved = data[:] assert data is not saved # <<<=== LOOK HERE === _ = self.func(data) self.assertListEqual(data, saved, "data has been modified") Note that is would be *wrong* to replace the "is not" comparison with not equal to. The list and its copy are, in fact, equal. If they aren't equal, the test fails. In this case, the assertions are there as verifiable comments. They communicate to the reader, "These are the assumptions this test relies on", except unlike comments, if the assumptions are violated, the test will fail. Unlike comments, they cannot ever get out of date. -- Steven From steve+comp.lang.python at pearwood.info Sat Aug 10 15:32:10 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 Aug 2013 19:32:10 GMT Subject: Python Basic Doubt References: <20130810114040.6ac78fe8@bigbox.christie.dr> Message-ID: <52069539$0$30000$c3e8da3$5496439d@news.astraweb.com> On Sat, 10 Aug 2013 20:00:58 +0200, Xavi wrote: > Now I have one doubt, I use 'is' to compare basic types in python 3, for > example .- > > v = [] > if type(v) is list: > print('Is list...') No, do not do this. This is unnecessarily restrictive. > Because I think it is more clear and faster than .- Clear? Maybe. Clear, but does the wrong thing. Using type rejects subclasses, which is normally a bad idea. Using isinstance accepts subclasses, which is what we nearly always should do. As for being faster -- who cares? The difference between calling type and calling isinstance is about 0.02 microseconds on my slow computer. You should not try to optimize things which are so unimportant. The first rule of optimization: Don't do it. For experts only: Don't do it yet. Until you have profiled your application, and discovered calling isinstance is the bottleneck making your application too slow, you are wasting your time trying to guess what will make it go faster. > type(v) == [].__class__ You should not do that either. Names starting and ending with double- underscore are reserved for Python. They are not quite private implementation details, but you almost never need to use them directly. Why keep a dog and then bark yourself? Python will check __class__ for you, when and if needed. That is not your job. It is very rare to need to use __dunder__ attributes by hand. > ... or ... isinstance(v, list) That's the right solution, 99.9% of the time. Actually, 99% of the time you should not call isinstance at all, but just catch any errors that occur; or better still, only catch them if you can do something about it. Otherwise, just allow the exception to propagate to the caller, who may catch it. Calling isinstance should be rare; calling type to check for an exact class even rarer. -- Steven From jarabal at gmail.com Sun Aug 11 12:58:25 2013 From: jarabal at gmail.com (Xavi) Date: Sun, 11 Aug 2013 18:58:25 +0200 Subject: Python Basic Doubt In-Reply-To: <52069539$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <20130810114040.6ac78fe8@bigbox.christie.dr> <52069539$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5207C2B1.6090401@gmail.com> Thanks to all for your answers, I guess it is more flexible with isinstance (the duck test :) I'm going to change the type checks. Respect to the "Names starting and ending with double-underscore". I don't know how to get the name of a classe without them. obj.__class__.__name__ Thanks. -- Xavi From steve+comp.lang.python at pearwood.info Sun Aug 11 21:08:23 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 12 Aug 2013 01:08:23 GMT Subject: Python Basic Doubt References: <20130810114040.6ac78fe8@bigbox.christie.dr> <52069539$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: <52083587$0$30000$c3e8da3$5496439d@news.astraweb.com> On Sun, 11 Aug 2013 18:58:25 +0200, Xavi wrote: > Respect to the "Names starting and ending with double-underscore". I > don't know how to get the name of a classe without them. > obj.__class__.__name__ I didn't say you should *never* use them, but most of the time, you don't. However type(obj).__name__ should be better. -- Steven From i.am.songoku at gmail.com Sat Aug 10 23:09:39 2013 From: i.am.songoku at gmail.com (Krishnan Shankar) Date: Sun, 11 Aug 2013 08:39:39 +0530 Subject: Python Basic Doubt In-Reply-To: <20130810114040.6ac78fe8@bigbox.christie.dr> References: <20130810114040.6ac78fe8@bigbox.christie.dr> Message-ID: Thanks Tim, This takes me to one more question. 'is' operator is used to compare objects and it should not be used to compare data. So can it be compared with 'False'. i.e. Is this code possible if a is False: print 'Yes' if b is False: print 'No' Because i recommended this should not be done. But my colleagues say it is correct. Regards, Krishnan On Sat, Aug 10, 2013 at 10:10 PM, Tim Chase wrote: > On 2013-08-10 21:03, Krishnan Shankar wrote: > > >>> a=10 > > >>> id(a) > > 21665504 > > >>> b=a > > >>> id(b) > > 21665504 > > >>> c=10 > > >>> id(c) > > 21665504 > > > > I am actually assigning new value to c. But from the value of id() > > all three variables take same location. With variables a and b it > > is ok. But why c taking the same location? > > As an internal optimization, CPython caches small integer values > > >>> a = 256 > >>> b = 256 > >>> a is b > True > >>> a = 257 > >>> b = 257 > >>> a is b > False > > Because it's an internal implementation detail, you shouldn't count > on this behavior (Jython or Cython or IronPython may differ; or > future versions of Python may cache a different range of numbers). > > Generally, if you are using the "is" operator to compare against > anything other than None, you're doing it wrong. There are exceptions > to this, but it takes knowing the particulars. > > -tkc > > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Sat Aug 10 23:20:03 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 11 Aug 2013 04:20:03 +0100 Subject: Python Basic Doubt In-Reply-To: References: <20130810114040.6ac78fe8@bigbox.christie.dr> Message-ID: On Sun, Aug 11, 2013 at 4:09 AM, Krishnan Shankar wrote: > i.e. Is this code possible > > if a is False: > print 'Yes' > if b is False: > print 'No' You would use that if you want to check if a/b is the exact bool value False. Normally you would simply spell it thus: if not a: print 'Yes' if not b: print 'No' which will accept any value and interpret it as either empty (false) or non-empty (true). Using the equality operator here adds another level of potential confusion: >>> 0 == False True >>> [] == False False >>> 0.0 == False True >>> () == False False whereas if you use the normal boolean conversion, those ARE all false: >>> bool(0) False >>> bool([]) False >>> bool(0.0) False >>> bool(()) False ChrisA From gary.herron at islandtraining.com Sat Aug 10 23:30:17 2013 From: gary.herron at islandtraining.com (Gary Herron) Date: Sat, 10 Aug 2013 20:30:17 -0700 Subject: Python Basic Doubt In-Reply-To: References: <20130810114040.6ac78fe8@bigbox.christie.dr> Message-ID: <52070549.7090508@islandtraining.com> On 08/10/2013 08:09 PM, Krishnan Shankar wrote: > Thanks Tim, > > This takes me to one more question. > > 'is' operator is used to compare objects and it should not be used to > compare data. > > So can it be compared with 'False'. > > i.e. Is this code possible > > if a is False: > print 'Yes' > if b is False: > print 'No' Depends on what you want. If you want to differentiate between a value of False, and other false-like values 0, (), [], {} and so on, then you need to be explicit with if a is False: Normally, that's not what you want, so you use if not a: to catch any of those false-like values. > > Because i recommended this should not be done. But my colleagues say > it is correct. > > Regards, > Krishnan > > > On Sat, Aug 10, 2013 at 10:10 PM, Tim Chase > > > wrote: > > On 2013-08-10 21:03, Krishnan Shankar wrote: > > >>> a=10 > > >>> id(a) > > 21665504 > > >>> b=a > > >>> id(b) > > 21665504 > > >>> c=10 > > >>> id(c) > > 21665504 > > > > I am actually assigning new value to c. But from the value of id() > > all three variables take same location. With variables a and b it > > is ok. But why c taking the same location? > > As an internal optimization, CPython caches small integer values > > >>> a = 256 > >>> b = 256 > >>> a is b > True > >>> a = 257 > >>> b = 257 > >>> a is b > False > > Because it's an internal implementation detail, you shouldn't count > on this behavior (Jython or Cython or IronPython may differ; or > future versions of Python may cache a different range of numbers). > > Generally, if you are using the "is" operator to compare against > anything other than None, you're doing it wrong. There are exceptions > to this, but it takes knowing the particulars. > > -tkc > > > > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From torriem at gmail.com Sat Aug 10 23:35:31 2013 From: torriem at gmail.com (Michael Torrie) Date: Sat, 10 Aug 2013 21:35:31 -0600 Subject: Python Basic Doubt In-Reply-To: References: <20130810114040.6ac78fe8@bigbox.christie.dr> Message-ID: <52070683.3040208@gmail.com> On 08/10/2013 09:09 PM, Krishnan Shankar wrote: > i.e. Is this code possible > > if a is False: > print 'Yes' > if b is False: > print 'No' > > Because i recommended this should not be done. But my colleagues say it is > correct. You are probably correct in your believe that this idiom should be avoided. As Chris says, it's much more pythonic to just use if not a. There is one case where the recommended idiom is to use the 'is' operator. That's when you want an empty list as a default parameter to a function. Since lists are mutable, often times using [] as a default parameter is the wrong thing to do. This is the recommended idiom: def my_func(mylist = None): if mylist is None: mylist = [] From roy at panix.com Sat Aug 10 12:35:30 2013 From: roy at panix.com (Roy Smith) Date: Sat, 10 Aug 2013 12:35:30 -0400 Subject: Python Basic Doubt References: Message-ID: In article , Krishnan Shankar wrote: > Hi Fellow Python Friends, > > I am new to Python and recently subscribed to the mailing list.I have a > doubt regarding the basics of Python. Please help me in understanding the > below concept. > > So doubt is on variables and their contained value. > > Why does in the below example from Interpreter exploration value of c take > pre existing memory location. > > >>> a=10 > >>> id(a) > 21665504 > >>> b=a > >>> id(b) > 21665504 > >>> c=10 > >>> id(c) > 21665504 Python doesn't really expose anything about memory locations. The fact that id() returns something which looks like it might be a memory location is purely a detail of the particular implementation you're using. The next thing to understand is that python doesn't have variables. It has objects and names which are bound to those objects. So, what's happening in your example is: 1) a = 10 You're creating an integer object with the value 10, and binding the name "a" to that object. 2) b = a You're binding another name, "b" to the same object that "a" is bound to. 3) c = 10 This is the tricky one. You're using 10 again as a literal, and the interpreter is reusing the same existing (interned) integer object, and binding yet another name, "c" to it. This part is implementation dependent. Nothing says Python must intern integer literals, it's entirely free to create a new integer object every time you utter 10 in your source code. From rosuav at gmail.com Sat Aug 10 12:42:24 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 10 Aug 2013 17:42:24 +0100 Subject: Python Basic Doubt In-Reply-To: References: Message-ID: On Sat, Aug 10, 2013 at 4:33 PM, Krishnan Shankar wrote: > Hi Fellow Python Friends, > > I am new to Python and recently subscribed to the mailing list.I have a > doubt regarding the basics of Python. Please help me in understanding the > below concept. > > So doubt is on variables and their contained value. Tangential to this: Python doesn't have "variables" that "contain" anything, but rather has names that are bound to (point to, if you like) objects. You're mostly right, this is just a terminology point. > Why does in the below example from Interpreter exploration value of c take > pre existing memory location. > >>>> a=10 >>>> id(a) > 21665504 >>>> b=a >>>> id(b) > 21665504 >>>> c=10 >>>> id(c) > 21665504 > > I am actually assigning new value to c. But from the value of id() all three > variables take same location. With variables a and b it is ok. But why c > taking the same location? CPython caches a number of integer objects for efficiency. Whenever you ask for the integer 10, you'll get the _same_ integer 10. But if you try the same exercise with a much higher number, or with a different value, you should get a unique id. With immutable literals, the interpreter's allowed to reuse them. You don't normally care about the id() of an integer, and nor should you. Same goes for strings; the interpreter's allowed to intern them if it chooses. Generally, don't assume that they're different, don't assume they're the same either. ChrisA From tjreedy at udel.edu Sat Aug 10 13:44:20 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 10 Aug 2013 13:44:20 -0400 Subject: Python Basic Doubt In-Reply-To: References: Message-ID: On 8/10/2013 11:33 AM, Krishnan Shankar wrote: > Hi Fellow Python Friends, > > I am new to Python and recently subscribed to the mailing list.I have a > doubt regarding the basics of Python. Please help me in understanding > the below concept. > > So doubt is on variables and their contained value. It would be better English to say that you have a 'question' or even 'confusion', rather than a 'doubt'. From your subject line, I got the impression that you doubted that you should learn or use Python. That clearly is not what you meant. -- Terry Jan Reedy From __peter__ at web.de Sat Aug 10 14:36:52 2013 From: __peter__ at web.de (Peter Otten) Date: Sat, 10 Aug 2013 20:36:52 +0200 Subject: The meaning of "doubt", was Re: Python Basic Doubt References: Message-ID: Terry Reedy wrote: > On 8/10/2013 11:33 AM, Krishnan Shankar wrote: >> Hi Fellow Python Friends, >> >> I am new to Python and recently subscribed to the mailing list.I have a >> doubt regarding the basics of Python. Please help me in understanding >> the below concept. >> >> So doubt is on variables and their contained value. > > It would be better English to say that you have a 'question' or even > 'confusion', rather than a 'doubt'. From your subject line, I got the > impression that you doubted that you should learn or use Python. That > clearly is not what you meant. Quoting http://en.wikipedia.org/wiki/Indian_English """ doubt = question or query; e.g. one would say, 'I have a doubt' when one wishes to ask a question. """ I'd say if Brits can cope (hard as it may be) with the American variant of the language, and native speakers can live with the broken English used to communicate in the rest of the world there is ample room for an Indian flavo(u)r now and then... From tjreedy at udel.edu Sat Aug 10 15:51:32 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 10 Aug 2013 15:51:32 -0400 Subject: The meaning of "doubt", was Re: Python Basic Doubt In-Reply-To: References: Message-ID: On 8/10/2013 2:36 PM, Peter Otten wrote: > Terry Reedy wrote: > >> On 8/10/2013 11:33 AM, Krishnan Shankar wrote: >>> Hi Fellow Python Friends, >>> >>> I am new to Python and recently subscribed to the mailing list.I have a >>> doubt regarding the basics of Python. Please help me in understanding >>> the below concept. >>> >>> So doubt is on variables and their contained value. >> >> It would be better English to say that you have a 'question' or even >> 'confusion', rather than a 'doubt'. From your subject line, I got the >> impression that you doubted that you should learn or use Python. That >> clearly is not what you meant. > > Quoting http://en.wikipedia.org/wiki/Indian_English > > """ > doubt = question or query; e.g. one would say, 'I have a doubt' when one > wishes to ask a question. > """ Thank you for verifying by suspicion (in the neutral sense) that this might be a generic Indian English usage. > I'd say if Brits can cope (hard as it may be) with the American variant of > the language, and native speakers can live with the broken English used to > communicate in the rest of the world there is ample room for an Indian > flavo(u)r now and then... The issue I raised was one of avoiding misunderstanding, especially in a short subject line. I almost skipped over the post because of it. I could have added a recommendation to be more specific. Any of "Question/confusion/doubt about int identity" would have been better. -- Terry Jan Reedy From roy at panix.com Sat Aug 10 14:55:51 2013 From: roy at panix.com (Roy Smith) Date: Sat, 10 Aug 2013 14:55:51 -0400 Subject: The meaning of "doubt", was Re: Python Basic Doubt References: Message-ID: In article , Peter Otten <__peter__ at web.de> wrote: > Quoting http://en.wikipedia.org/wiki/Indian_English > > """ > doubt = question or query; e.g. one would say, 'I have a doubt' when one > wishes to ask a question. > """ > > I'd say if Brits can cope (hard as it may be) with the American variant of > the language, and native speakers can live with the broken English used to > communicate in the rest of the world there is ample room for an Indian > flavo(u)r now and then... Yup. I used to work with a development team in Bangalore. One of the amusing aspects of the collaboration was the subtle language issues. For example, apparently, "Roy" is a common *last* name in India. No matter how many times I explained it, the guys over there couldn't seem to get that Roy is my first name and Smith is my last name. So, in settings where everybody was using first names, they would always call me Smith. I shudder to think what carnage I accidentally inflicted on their names :-) And, of course, since we're on the subject, this should be required reading for all programmers: http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about- names/ From alister.ware at ntlworld.com Sat Aug 10 15:37:38 2013 From: alister.ware at ntlworld.com (Alister) Date: Sat, 10 Aug 2013 19:37:38 GMT Subject: The meaning of "doubt", was Re: Python Basic Doubt Message-ID: <6EwNt.3685$BT7.2788@fx14.am4> On Sat, 10 Aug 2013 20:36:52 +0200, Peter Otten wrote: > Terry Reedy wrote: > >> On 8/10/2013 11:33 AM, Krishnan Shankar wrote: >>> Hi Fellow Python Friends, >>> >>> I am new to Python and recently subscribed to the mailing list.I have >>> a doubt regarding the basics of Python. Please help me in >>> understanding the below concept. >>> >>> So doubt is on variables and their contained value. >> >> It would be better English to say that you have a 'question' or even >> 'confusion', rather than a 'doubt'. From your subject line, I got the >> impression that you doubted that you should learn or use Python. That >> clearly is not what you meant. > > Quoting http://en.wikipedia.org/wiki/Indian_English > > """ > doubt = question or query; e.g. one would say, 'I have a doubt' when one > wishes to ask a question. > """ > > I'd say if Brits can cope (hard as it may be) with the American variant > of the language, and native speakers can live with the broken English > used to communicate in the rest of the world there is ample room for an > Indian flavo(u)r now and then... +1 Ill take broken English from a non native speaker over the confusion caused by our American cousins who seam to have everything arse about face (especially if you consider the word "Fanny" ) Seriously though I can usually work out what the meaning was, & it is certainly clearer than any attempt I could make in any other language, which is actually quite shameful. -- My way of joking is to tell the truth. That's the funniest joke in the world. -- Muhammad Ali From cousinstanley at gmail.com Sat Aug 10 16:57:17 2013 From: cousinstanley at gmail.com (Cousin Stanley) Date: Sat, 10 Aug 2013 20:57:17 +0000 (UTC) Subject: The meaning of "doubt", was Re: Python Basic Doubt References: Message-ID: Peter Otten wrote: > .... > doubt > .... Oh bother, said Pooh, what's in a word ? http://en.wikipedia.org/wiki/Curry https://pypi.python.org/pypi/curry/ http://en.wikipedia.org/wiki/Currying -- Stanley C. Kitching Human Being Phoenix, Arizona From dwightdhutto at gmail.com Fri Aug 16 21:09:14 2013 From: dwightdhutto at gmail.com (David Hutto) Date: Fri, 16 Aug 2013 21:09:14 -0400 Subject: The meaning of "doubt", was Re: Python Basic Doubt In-Reply-To: References: Message-ID: You could say that all translated languages lose something in translation. It's all symbolism. I say sunshine, and you might say Great Ball of' Fire in the s ky. Isay x = 10 in python print x and in c++ something like unsigned int x cin << x; cout >> x; or something like that. It's something you have to think about on a level of the individual knowing, a term, and then symbolizing, which can lose meaning, or equate meaning. Look at the former in language of humans, and the latter of computer language. One can equate or symbolize. On Sat, Aug 10, 2013 at 4:57 PM, Cousin Stanley wrote: > Peter Otten wrote: > > > .... > > doubt > > .... > > Oh bother, said Pooh, what's in a word ? > > http://en.wikipedia.org/wiki/Curry > > https://pypi.python.org/pypi/curry/ > > http://en.wikipedia.org/wiki/Currying > > > -- > Stanley C. Kitching > Human Being > Phoenix, Arizona > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Best Regards, David Hutto *CEO:* *http://www.hitwebdevelopment.com* -------------- next part -------------- An HTML attachment was scrubbed... URL: From roy at panix.com Sat Aug 10 16:42:22 2013 From: roy at panix.com (Roy Smith) Date: Sat, 10 Aug 2013 16:42:22 -0400 Subject: Python Basic Doubt References: Message-ID: In article , Dennis Lee Bieber wrote: > Because id(n) is not giving you the address of the NAME. It is giving > you the address of the "10" Actually, it is giving you the id of the int(10) object. Maybe it's an address, maybe it's not. Only your implementation knows for sure. From steve+comp.lang.python at pearwood.info Sun Aug 11 04:29:46 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 11 Aug 2013 08:29:46 GMT Subject: Python Basic Doubt References: Message-ID: <52074b7a$0$30000$c3e8da3$5496439d@news.astraweb.com> On Sat, 10 Aug 2013 16:42:22 -0400, Roy Smith wrote: > In article , > Dennis Lee Bieber wrote: > >> Because id(n) is not giving you the address of the NAME. It is giving >> you the address of the "10" > > Actually, it is giving you the id of the int(10) object. Maybe it's an > address, maybe it's not. Only your implementation knows for sure. /steve cheers from the audience Thank you for mentioning this. Using Jython: >>> x = 10 >>> id(x) 1 And using IronPython: >>> x = 10 >>> id(x) 43 "id" does not stand for "memory address". It stands for "identity". -- Steven From Joshua.R.English at gmail.com Sat Aug 10 16:00:30 2013 From: Joshua.R.English at gmail.com (Josh English) Date: Sat, 10 Aug 2013 13:00:30 -0700 (PDT) Subject: How many times does unittest run each test? Message-ID: I am working on a library, and adding one feature broke a seemingly unrelated feature. As I already had Test Cases written, I decided to try to incorporate the logging module into my class, and turn on debugging at the logger before the newly-broken test. Here is an example script: # ----- #!/usr/bin/env python import logging def get_vals(): return ['a','b','c'] class SimpleChecker(object): def __init__(self, callback=None): self.callback = callback self.logger = logging.getLogger(self.__class__.__name__) h = logging.StreamHandler() f = logging.Formatter("%(name)s - %(levelname)s - %(message)s") h.setFormatter(f) self.logger.addHandler(h) def __call__(self, thing): self.logger.debug('calling %s' % thing) vals = self.callback() return thing in vals import unittest class LoaderTC(unittest.TestCase): def setUp(self): self.checker = SimpleChecker(get_vals) def tearDown(self): del self.checker def test_callable(self): self.assertTrue(callable(self.checker), 'loader did not create callable object') self.assertTrue(callable(self.checker.callback), 'loader did not create callable callback') self.checker.logger.setLevel(logging.DEBUG) self.assertTrue(self.checker('q') is False, "checker accepted bad input") class NameSpaceTC(unittest.TestCase): def setUp(self): self.checker = SimpleChecker(get_vals) def tearDown(self): del self.checker def test_callable(self): self.assertTrue(callable(self.checker), 'loader did not create callable object') self.assertTrue(callable(self.checker.callback), 'loader did not create callable callback') self.checker.logger.setLevel(logging.DEBUG) self.assertTrue(self.checker('a'), "checker did not accept good value") self.assertFalse(self.checker('f'), "checker accepted bad value") if __name__=='__main__': unittest.main(verbosity=0) # ------- When I run this, I get: SimpleChecker - DEBUG - calling q SimpleChecker - DEBUG - calling a SimpleChecker - DEBUG - calling a SimpleChecker - DEBUG - calling f SimpleChecker - DEBUG - calling f ---------------------------------------------------------------------- Ran 2 tests in 0.013s OK Exit code: False Why am I seeing those extra debugging lines? In the script I'm really trying to debug, I see 12-13 debug messages repeated, making actual debugging difficult. Josh English From roy at panix.com Sat Aug 10 16:40:43 2013 From: roy at panix.com (Roy Smith) Date: Sat, 10 Aug 2013 16:40:43 -0400 Subject: How many times does unittest run each test? References: Message-ID: In article , Josh English wrote: > I am working on a library, and adding one feature broke a seemingly unrelated > feature. As I already had Test Cases written, I decided to try to incorporate > the logging module into my class, and turn on debugging at the logger before > the newly-broken test. > > Here is an example script: [followed by 60 lines of code] The first thing to do is get this down to some minimal amount of code that demonstrates the problem. For example, you drag in the logging module, and do some semi-complex configuration. Are you SURE your tests are getting run multiple times, or maybe it's just that they're getting LOGGED multiple times. Tear out all the logging stuff. Just use a plain print statement. You've got two different TestCases here. Does the problem happen with just LoaderTC, or with just NameSpaceTC? Keep tearing out code until you can no longer demonstrate the problem. Keep at it until there is not a single line of code remaining which isn't required to demonstrate. Then come back and ask your question again. From Joshua.R.English at gmail.com Sat Aug 10 18:53:55 2013 From: Joshua.R.English at gmail.com (Josh English) Date: Sat, 10 Aug 2013 15:53:55 -0700 (PDT) Subject: How many times does unittest run each test? In-Reply-To: References: Message-ID: <35d12db6-c367-4a45-a68e-8ed4c0ae1ee7@googlegroups.com> On Saturday, August 10, 2013 1:40:43 PM UTC-7, Roy Smith wrote: > In article , > > Josh English wrote: > The first thing to do is get this down to some minimal amount of code > that demonstrates the problem. > > > > For example, you drag in the logging module, and do some semi-complex > configuration. Are you SURE your tests are getting run multiple times, > or maybe it's just that they're getting LOGGED multiple times. Tear out > all the logging stuff. Just use a plain print statement. > > You've got two different TestCases here. Does the problem happen with > just LoaderTC, or with just NameSpaceTC? > Ok, then why would things get logged multiple times? The two test cases actually test a loader function that I could strip out, because it wasn't relevant. In these cases, the loader was being called with two different configurations in the individual setUp methods. I left them there to show that in LoaderTC, there is one debug log coming from SimpleChecker, but in the NameSpaceTC, each debug message is printed twice. If I print statements on each test_ method, they are called once. As far as stripping down the code, I suppose 15 lines can be culled: #--------- import logging class SimpleChecker(object): def __init__(self,): self.logger = logging.getLogger(self.__class__.__name__) h = logging.StreamHandler() f = logging.Formatter("%(name)s - %(levelname)s - %(message)s") h.setFormatter(f) self.logger.addHandler(h) def __call__(self, thing): self.logger.debug('calling %s' % thing) return thing in ['a','b','c'] import unittest class LoaderTC(unittest.TestCase): def setUp(self): self.checker = SimpleChecker() def tearDown(self): del self.checker def test_callable(self): self.checker.logger.setLevel(logging.DEBUG) self.assertTrue(self.checker('q') is False, "checker accepted bad input") class NameSpaceTC(unittest.TestCase): def setUp(self): self.checker = SimpleChecker() def tearDown(self): del self.checker def test_callable(self): print "testing callable" self.checker.logger.setLevel(logging.DEBUG) self.assertTrue(self.checker('a'), "checker did not accept good value") self.assertFalse(self.checker('f'), "checker accepted bad value") if __name__=='__main__': unittest.main(verbosity=0) #----------- The output: SimpleChecker - DEBUG - calling q setting up NameSpace testing callable SimpleChecker - DEBUG - calling a SimpleChecker - DEBUG - calling a SimpleChecker - DEBUG - calling f SimpleChecker - DEBUG - calling f ---------------------------------------------------------------------- Ran 2 tests in 0.014s OK Exit code: False Josh From roy at panix.com Sat Aug 10 19:14:09 2013 From: roy at panix.com (Roy Smith) Date: Sat, 10 Aug 2013 19:14:09 -0400 Subject: How many times does unittest run each test? References: <35d12db6-c367-4a45-a68e-8ed4c0ae1ee7@googlegroups.com> Message-ID: On Saturday, August 10, 2013 1:40:43 PM UTC-7, Roy Smith wrote: > > For example, you drag in the logging module, and do some semi-complex > > configuration. Are you SURE your tests are getting run multiple times, > > or maybe it's just that they're getting LOGGED multiple times. Tear out > > all the logging stuff. Just use a plain print statement. In article <35d12db6-c367-4a45-a68e-8ed4c0ae1ee7 at googlegroups.com>, Josh English wrote: > Ok, then why would things get logged multiple times? Maybe you've got two different handlers which are both getting the same logging events and somehow they both end up in your stderr stream. Likely? Maybe not, but if you don't have any logging code in the test at all, it becomes impossible. You can't have a bug in a line of code that doesn't exist (yeah, I know, that's a bit of a handwave). When a test (or any other code) is doing something you don't understand, the best way to start understanding it is to create a minimal test case; the absolute smallest amount of code that demonstrates the problem. I don't understand the whole SimpleChecker class. You've created a class, and defined your own __call__(), just so you can check if a string is in a list? Couldn't this be done much simpler with a plain old function: def checker(thing): print "calling %s" % thing return thing in ['a','b','c'] From rosuav at gmail.com Sat Aug 10 19:21:35 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 11 Aug 2013 00:21:35 +0100 Subject: How many times does unittest run each test? In-Reply-To: References: <35d12db6-c367-4a45-a68e-8ed4c0ae1ee7@googlegroups.com> Message-ID: On Sun, Aug 11, 2013 at 12:14 AM, Roy Smith wrote: > Maybe you've got two different handlers which are both getting the same > logging events and somehow they both end up in your stderr stream. > Likely? Maybe not, but if you don't have any logging code in the test > at all, it becomes impossible. You can't have a bug in a line of code > that doesn't exist (yeah, I know, that's a bit of a handwave). Likely? Very much so, to the extent that it is, if not a FAQ, certainly a Not All That Uncommonly Asked Question. So many times someone places logging code in something that gets called twice, and ends up with two handlers. Personally, I much prefer to debug with straight-up 'print' - much less hassle. I'd turn to the logging module only if I actually need its functionality (logging to some place other than the console, or leaving the log statements in and {en|dis}abling them at run-time). ChrisA From Joshua.R.English at gmail.com Sat Aug 10 20:47:44 2013 From: Joshua.R.English at gmail.com (Josh English) Date: Sat, 10 Aug 2013 17:47:44 -0700 (PDT) Subject: How many times does unittest run each test? In-Reply-To: References: <35d12db6-c367-4a45-a68e-8ed4c0ae1ee7@googlegroups.com> Message-ID: <82080a1b-098c-44df-a230-5ae9e3be86f8@googlegroups.com> On Saturday, August 10, 2013 4:14:09 PM UTC-7, Roy Smith wrote: > > > I don't understand the whole SimpleChecker class. You've created a > class, and defined your own __call__(), just so you can check if a > string is in a list? Couldn't this be done much simpler with a plain > old function: > > def checker(thing): > print "calling %s" % thing > return thing in ['a','b','c'] SimpleCheck is an extremely watered down version of my XML checker (https://pypi.python.org/pypi/XMLCheck/0.6.7). I'm working a feature that allows the checker to call a function to get acceptable values, instead of defining them at the start of the program. I included all of that because it's the shape of the script I'm working with. The real problem was setting additional handlers where they shouldn't be. Josh From Joshua.R.English at gmail.com Sat Aug 10 20:52:05 2013 From: Joshua.R.English at gmail.com (Josh English) Date: Sat, 10 Aug 2013 17:52:05 -0700 (PDT) Subject: How many times does unittest run each test? In-Reply-To: References: <35d12db6-c367-4a45-a68e-8ed4c0ae1ee7@googlegroups.com> Message-ID: <1d4f1667-9234-403a-b33d-f6636f081c72@googlegroups.com> On Saturday, August 10, 2013 4:21:35 PM UTC-7, Chris Angelico wrote: > On Sun, Aug 11, 2013 at 12:14 AM, Roy Smith <> wrote: > > > Maybe you've got two different handlers which are both getting the same > > loggingvents and somehow they both end up in your stderr stream. > > Likely? Maybe not, but if you don't have any logging code in the test > > at all, it becomes impossible. You can't have a bug in a line of code > > that doesn't exist (yeah, I know, that's a bit of a handwave). > > Likely? Very much so, to the extent that it is, if not a FAQ, > certainly a Not All That Uncommonly Asked Question. So many times > someone places logging code in something that gets called twice, and > ends up with two handlers. Personally, I much prefer to debug with > straight-up 'print' - much less hassle. I'd turn to the logging module > only if I actually need its functionality (logging to some place other > than the console, or leaving the log statements in and {en|dis}abling > them at run-time). Yes, I definitely need the NUATAQ sheet for Python. I'm using logging for debugging, because it is pretty straightforward and can be activated for a small section of the module. My modules run long (3,000 lines or so) and finding all those dastardly print statements is a pain, and littering my code with "if debug: print message" clauses. Logging just makes it simple. Josh From rosuav at gmail.com Sat Aug 10 21:10:30 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 11 Aug 2013 02:10:30 +0100 Subject: How many times does unittest run each test? In-Reply-To: <1d4f1667-9234-403a-b33d-f6636f081c72@googlegroups.com> References: <35d12db6-c367-4a45-a68e-8ed4c0ae1ee7@googlegroups.com> <1d4f1667-9234-403a-b33d-f6636f081c72@googlegroups.com> Message-ID: On Sun, Aug 11, 2013 at 1:52 AM, Josh English wrote: > I'm using logging for debugging, because it is pretty straightforward and can be activated for a small section of the module. My modules run long (3,000 lines or so) and finding all those dastardly print statements is a pain, and littering my code with "if debug: print message" clauses. Logging just makes it simple. So logging might be the right tool for your job. Tip: Sometimes it helps, when trying to pin down an issue, to use an additional debugging aid. You're already using logging? Add print calls. Already got a heartbeat function? Run it through a single-stepping debugger as well. Usually that sort of thing just gives you multiple probes at the actual problem, but occasionally you'll get an issue like this, and suddenly it's really obvious because one probe behaves completely differently from the other. ChrisA From ned at nedbatchelder.com Sat Aug 10 18:52:53 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Sat, 10 Aug 2013 18:52:53 -0400 Subject: How many times does unittest run each test? In-Reply-To: References: Message-ID: <5206C445.50903@nedbatchelder.com> On 8/10/13 4:40 PM, Roy Smith wrote: > In article , > Josh English wrote: > >> I am working on a library, and adding one feature broke a seemingly unrelated >> feature. As I already had Test Cases written, I decided to try to incorporate >> the logging module into my class, and turn on debugging at the logger before >> the newly-broken test. >> >> Here is an example script: > [followed by 60 lines of code] > > The first thing to do is get this down to some minimal amount of code > that demonstrates the problem. > > For example, you drag in the logging module, and do some semi-complex > configuration. Are you SURE your tests are getting run multiple times, > or maybe it's just that they're getting LOGGED multiple times. Tear out > all the logging stuff. Just use a plain print statement. Roy is right: the problem isn't the tests, it's the logging. You are calling .addHandler in the SimpleChecker.__init__, then you are constructing two SimpleCheckers, each of which adds a handler. In the LoaderTC test, you've only constructed one, adding only one handler, so the "calling q" line only appears once. Then the NameSpaceTC tests runs, constructs another SimplerChecker, which adds another handler, so now there are two. That's why the "calling a" and "calling f" lines appear twice. Move your logging configuration to a place that executes only once. Also, btw, you don't need the "del self.checker" in your tearDown methods: the test object is destroyed after each test, so any objects it holds will be released after each test with no special action needed on your part. --Ned. From joshua.r.english at gmail.com Sat Aug 10 18:58:39 2013 From: joshua.r.english at gmail.com (Josh English) Date: Sat, 10 Aug 2013 15:58:39 -0700 Subject: How many times does unittest run each test? In-Reply-To: <5206C445.50903@nedbatchelder.com> References: <5206C445.50903@nedbatchelder.com> Message-ID: Aha. Thanks, Ned. This is the answer I was looking for. I use logging in the real classes, and thought that turning setting the level to logging.DEBUG once was easier than hunting down four score of print statements. Josh On Sat, Aug 10, 2013 at 3:52 PM, Ned Batchelder wrote: > On 8/10/13 4:40 PM, Roy Smith wrote: >> >> In article , >> Josh English wrote: >> >>> I am working on a library, and adding one feature broke a seemingly >>> unrelated >>> feature. As I already had Test Cases written, I decided to try to >>> incorporate >>> the logging module into my class, and turn on debugging at the logger >>> before >>> the newly-broken test. >>> >>> Here is an example script: >> >> [followed by 60 lines of code] >> >> The first thing to do is get this down to some minimal amount of code >> that demonstrates the problem. >> >> For example, you drag in the logging module, and do some semi-complex >> configuration. Are you SURE your tests are getting run multiple times, >> or maybe it's just that they're getting LOGGED multiple times. Tear out >> all the logging stuff. Just use a plain print statement. > > Roy is right: the problem isn't the tests, it's the logging. You are > calling .addHandler in the SimpleChecker.__init__, then you are constructing > two SimpleCheckers, each of which adds a handler. In the LoaderTC test, > you've only constructed one, adding only one handler, so the "calling q" > line only appears once. Then the NameSpaceTC tests runs, constructs another > SimplerChecker, which adds another handler, so now there are two. That's > why the "calling a" and "calling f" lines appear twice. > > Move your logging configuration to a place that executes only once. > > Also, btw, you don't need the "del self.checker" in your tearDown methods: > the test object is destroyed after each test, so any objects it holds will > be released after each test with no special action needed on your part. > > --Ned. -- Josh English Joshua.R.English at gmail.com http://www.joshuarenglish.com From devyncjohnson at gmail.com Sat Aug 10 22:19:23 2013 From: devyncjohnson at gmail.com (Devyn Collier Johnson) Date: Sat, 10 Aug 2013 22:19:23 -0400 Subject: Am I not seeing the Error? Message-ID: <5206F4AB.8050204@Gmail.com> I am checking my 1292-line script for syntax errors. I ran the following commands in a terminal to check for errors, but I do not see the error. collier at Nacho-Laptop:/media/collier/AI/Pysh$ python3 -m py_compile ./beta_engine File "./beta_engine", line 344 JOB_WRITEURGFILES = multiprocessing.Process(write2file('./mem/ENGINE_PID', ENGINEPID); write2file(SENTEMPPATH, ''); write2file(INPUTMEM, '')); JOB_WRITEURGFILES.start() ^ SyntaxError: invalid syntax collier at Nacho-Laptop:/media/collier/AI/Pysh$ pylint ./beta_engine No config file found, using default configuration ************* Module beta_engine E:344,0: invalid syntax Here is line 344: JOB_WRITEURGFILES = multiprocessing.Process(write2file('./mem/ENGINE_PID', ENGINEPID); write2file(SENTEMPPATH, ''); write2file(INPUTMEM, '')); JOB_WRITEURGFILES.start() The ENGINEPID is a variable containing a string. My write2file function is def write2file(openfile, WRITE): with open(openfile, 'rw') as file: file.write(WRITE) Mahalo, DevynCJohnson at Gmail.com From rosuav at gmail.com Sat Aug 10 22:33:52 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 11 Aug 2013 03:33:52 +0100 Subject: Am I not seeing the Error? In-Reply-To: <5206F4AB.8050204@Gmail.com> References: <5206F4AB.8050204@Gmail.com> Message-ID: On Sun, Aug 11, 2013 at 3:19 AM, Devyn Collier Johnson wrote: > am checking my 1292-line script for syntax errors. I ran the following > commands in a terminal to check for errors, but I do not see the error. > > collier at Nacho-Laptop:/media/collier/AI/Pysh$ python3 -m py_compile > ./beta_engine > File "./beta_engine", line 344 > JOB_WRITEURGFILES = > multiprocessing.Process(write2file('./mem/ENGINE_PID', ENGINEPID); > write2file(SENTEMPPATH, ''); write2file(INPUTMEM, '')); > JOB_WRITEURGFILES.start() > ^ > SyntaxError: invalid syntax When you get a syntax error you can't understand, look at the previous line of code. Perhaps something there is incomplete; maybe you have mismatched parentheses, so this line is considered to be part of the same expression. Next thing to do is split it into more lines. Why is all that in a single line? ChrisA From roy at panix.com Sat Aug 10 22:43:10 2013 From: roy at panix.com (Roy Smith) Date: Sat, 10 Aug 2013 22:43:10 -0400 Subject: Am I not seeing the Error? References: <5206F4AB.8050204@Gmail.com> Message-ID: In article , Chris Angelico wrote: > When you get a syntax error you can't understand, look at the previous > line of code. Perhaps something there is incomplete; maybe you have > mismatched parentheses, so this line is considered to be part of the > same expression. > > Next thing to do is split it into more lines. Why is all that in a single > line? Also, try reformatting the code in a tool like emacs or eclipse which does syntax coloring and auto indenting. Often, if you're missing some piece of punctuation, it will become obvious when your tool tries to indent things in some unexpected way. Or suddenly starts coloring all of your program text as if it were a string literal :-) From rosuav at gmail.com Sat Aug 10 22:47:52 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 11 Aug 2013 03:47:52 +0100 Subject: Am I not seeing the Error? In-Reply-To: References: <5206F4AB.8050204@Gmail.com> Message-ID: On Sun, Aug 11, 2013 at 3:43 AM, Roy Smith wrote: > In article , > Chris Angelico wrote: > >> When you get a syntax error you can't understand, look at the previous >> line of code. Perhaps something there is incomplete; maybe you have >> mismatched parentheses, so this line is considered to be part of the >> same expression. >> >> Next thing to do is split it into more lines. Why is all that in a single >> line? > > Also, try reformatting the code in a tool like emacs or eclipse which > does syntax coloring and auto indenting. Often, if you're missing some > piece of punctuation, it will become obvious when your tool tries to > indent things in some unexpected way. Or suddenly starts coloring all > of your program text as if it were a string literal :-) Agreed. Though I've had some odd issues with SciTE in that way; I think its Python handling may have bugs in it here and there. But 95% of the time it's helpful. ChrisA From devyncjohnson at gmail.com Mon Aug 12 08:33:41 2013 From: devyncjohnson at gmail.com (Devyn Collier Johnson) Date: Mon, 12 Aug 2013 08:33:41 -0400 Subject: Am I not seeing the Error? In-Reply-To: References: <5206F4AB.8050204@Gmail.com> Message-ID: <5208D625.2000103@Gmail.com> On 08/10/2013 10:47 PM, Chris Angelico wrote: > On Sun, Aug 11, 2013 at 3:43 AM, Roy Smith wrote: >> In article , >> Chris Angelico wrote: >> >>> When you get a syntax error you can't understand, look at the previous >>> line of code. Perhaps something there is incomplete; maybe you have >>> mismatched parentheses, so this line is considered to be part of the >>> same expression. >>> >>> Next thing to do is split it into more lines. Why is all that in a single >>> line? >> Also, try reformatting the code in a tool like emacs or eclipse which >> does syntax coloring and auto indenting. Often, if you're missing some >> piece of punctuation, it will become obvious when your tool tries to >> indent things in some unexpected way. Or suddenly starts coloring all >> of your program text as if it were a string literal :-) > Agreed. Though I've had some odd issues with SciTE in that way; I > think its Python handling may have bugs in it here and there. But 95% > of the time it's helpful. > > ChrisA Thanks everyone. Unfortunately, I have not found the problem yet. I use the Geany IDE which has syntax highlighting, but nothing wrong is seen. None of the suggestions helped. The lines before this one set variables. The lines further up "appear" fine. I will keep looking. If I ever figure it out, I will share with all of you. As for the code being one line, my style of coding is very different from others. I try to keep similar or related tasks on one line. Programming like that is called trolling. A programmer that uses trolling is called a troll. A troll can also refer to such a line of code itself. My scripts contain a lot of trolls. It is easier for me to read trolls than "typical" coding styles. (Yes, I am a weird troll (^u^)) Mahalo, DevynCJohnson at Gmail.com From zachary.ware+pylist at gmail.com Mon Aug 12 09:20:15 2013 From: zachary.ware+pylist at gmail.com (Zachary Ware) Date: Mon, 12 Aug 2013 08:20:15 -0500 Subject: Am I not seeing the Error? In-Reply-To: <5208D625.2000103@Gmail.com> References: <5206F4AB.8050204@Gmail.com> <5208D625.2000103@Gmail.com> Message-ID: On Mon, Aug 12, 2013 at 7:33 AM, Devyn Collier Johnson wrote: > > On 08/10/2013 10:47 PM, Chris Angelico wrote: >> >> On Sun, Aug 11, 2013 at 3:43 AM, Roy Smith wrote: >>> >>> In article , >>> Chris Angelico wrote: >>> >>>> When you get a syntax error you can't understand, look at the previous >>>> line of code. Perhaps something there is incomplete; maybe you have >>>> mismatched parentheses, so this line is considered to be part of the >>>> same expression. >>>> >>>> Next thing to do is split it into more lines. Why is all that in a >>>> single >>>> line? >>> >>> Also, try reformatting the code in a tool like emacs or eclipse which >>> does syntax coloring and auto indenting. Often, if you're missing some >>> piece of punctuation, it will become obvious when your tool tries to >>> indent things in some unexpected way. Or suddenly starts coloring all >>> of your program text as if it were a string literal :-) >> >> Agreed. Though I've had some odd issues with SciTE in that way; I >> think its Python handling may have bugs in it here and there. But 95% >> of the time it's helpful. >> >> ChrisA > > > Thanks everyone. Unfortunately, I have not found the problem yet. I use the > Geany IDE which has syntax highlighting, but nothing wrong is seen. None of > the suggestions helped. The lines before this one set variables. The lines > further up "appear" fine. I will keep looking. If I ever figure it out, I > will share with all of you. > > As for the code being one line, my style of coding is very different from > others. I try to keep similar or related tasks on one line. Programming like > that is called trolling. A programmer that uses trolling is called a troll. > A troll can also refer to such a line of code itself. My scripts contain a > lot of trolls. It is easier for me to read trolls than "typical" coding > styles. Obviously not, since you can't find the syntax error. If you replace each semicolon in that line with a newline, the syntax error will be immediately obvious. I'll even give you a hint: it's on the third line. -- Zach From devyncjohnson at gmail.com Mon Aug 12 10:04:46 2013 From: devyncjohnson at gmail.com (Devyn Collier Johnson) Date: Mon, 12 Aug 2013 10:04:46 -0400 Subject: Am I not seeing the Error? In-Reply-To: References: <5206F4AB.8050204@Gmail.com> <5208D625.2000103@Gmail.com> Message-ID: <5208EB7E.6030907@Gmail.com> On 08/12/2013 09:20 AM, Zachary Ware wrote: > On Mon, Aug 12, 2013 at 7:33 AM, Devyn Collier Johnson > wrote: >> On 08/10/2013 10:47 PM, Chris Angelico wrote: >>> On Sun, Aug 11, 2013 at 3:43 AM, Roy Smith wrote: >>>> In article , >>>> Chris Angelico wrote: >>>> >>>>> When you get a syntax error you can't understand, look at the previous >>>>> line of code. Perhaps something there is incomplete; maybe you have >>>>> mismatched parentheses, so this line is considered to be part of the >>>>> same expression. >>>>> >>>>> Next thing to do is split it into more lines. Why is all that in a >>>>> single >>>>> line? >>>> Also, try reformatting the code in a tool like emacs or eclipse which >>>> does syntax coloring and auto indenting. Often, if you're missing some >>>> piece of punctuation, it will become obvious when your tool tries to >>>> indent things in some unexpected way. Or suddenly starts coloring all >>>> of your program text as if it were a string literal :-) >>> Agreed. Though I've had some odd issues with SciTE in that way; I >>> think its Python handling may have bugs in it here and there. But 95% >>> of the time it's helpful. >>> >>> ChrisA >> >> Thanks everyone. Unfortunately, I have not found the problem yet. I use the >> Geany IDE which has syntax highlighting, but nothing wrong is seen. None of >> the suggestions helped. The lines before this one set variables. The lines >> further up "appear" fine. I will keep looking. If I ever figure it out, I >> will share with all of you. >> >> As for the code being one line, my style of coding is very different from >> others. I try to keep similar or related tasks on one line. Programming like >> that is called trolling. A programmer that uses trolling is called a troll. >> A troll can also refer to such a line of code itself. My scripts contain a >> lot of trolls. It is easier for me to read trolls than "typical" coding >> styles. > Obviously not, since you can't find the syntax error. If you replace > each semicolon in that line with a newline, the syntax error will be > immediately obvious. I'll even give you a hint: it's on the third > line. > Zachary, are you, Ned, and Terry trying to say the syntax should be job = multiprocessing.Process(func1(), func2()) not job = multiprocessing.Process(func1(); func2()) DCJ From zachary.ware+pylist at gmail.com Mon Aug 12 11:37:21 2013 From: zachary.ware+pylist at gmail.com (Zachary Ware) Date: Mon, 12 Aug 2013 10:37:21 -0500 Subject: Am I not seeing the Error? In-Reply-To: <5208EB7E.6030907@Gmail.com> References: <5206F4AB.8050204@Gmail.com> <5208D625.2000103@Gmail.com> <5208EB7E.6030907@Gmail.com> Message-ID: On Mon, Aug 12, 2013 at 9:04 AM, Devyn Collier Johnson wrote: > > Zachary, are you, Ned, and Terry trying to say the syntax should be > > job = multiprocessing.Process(func1(), func2()) > > not > > job = multiprocessing.Process(func1(); func2()) > Basically, yes. The first option there is equivalent to this: func_returns = (func1(), func2()) job = multiprocessing.Process(*func_returns) The second option is equivalent to this: job = multiprocessing.Process(func1() func2()) ...which is actually several different syntax errors, depending on how you look at it. Semi-colon is only ever used in Python as a substitute for \n-plus-some-spaces. And, since in your original example, your semi-colons are inside parenthesis, they really have no effect at all due to implicit line continuation within parens. So your original line: JOB_WRITEURGFILES = multiprocessing.Process(write2file('./mem/ENGINE_PID', ENGINEPID); write2file(SENTEMPPATH, ''); write2file(INPUTMEM, '')); JOB_WRITEURGFILES.start() is really: JOB_WRITEURGFILES = multiprocessing.Process(write2file('./mem/ENGINE_PID', ENGINEPID) write2file(SENTEMPPATH, '') write2file(INPUTMEM, '')) JOB_WRITEURGFILES.start() It should be obvious now that the syntax error comes from not separating the arguments to Process. Trying to read between the lines a little here, I don't think you have quite figured out how multiprocessing.Process works; that first option above will only work if func1 returns None and func2 returns a callable object and the second has no hope. I think this is more along the lines of what you're really after: def process_func(): func1() func2() if __name__ == '__main__': job = multiprocessing.Process(target=process_func) # note: no () after process_func! job.start() I'd suggest giving the multiprocessing.Process docs [0] a good read-through. Keep in mind that Process is just a type like any other (str, int, list, etc.), and calling its constructor is subject to the same rules as any other function call. -- Zach [0] http://docs.python.org/3/library/multiprocessing#the-process-class From zachary.ware+pylist at gmail.com Mon Aug 12 11:47:03 2013 From: zachary.ware+pylist at gmail.com (Zachary Ware) Date: Mon, 12 Aug 2013 10:47:03 -0500 Subject: Am I not seeing the Error? In-Reply-To: References: <5206F4AB.8050204@Gmail.com> <5208D625.2000103@Gmail.com> <5208EB7E.6030907@Gmail.com> Message-ID: On Mon, Aug 12, 2013 at 10:37 AM, Zachary Ware wrote: > [snip my last reply with a few code samples] My apologies for Gmail's mangling of my samples. Any code that is not indented should be on the previous line. -- Zach From roy at panix.com Mon Aug 12 11:47:18 2013 From: roy at panix.com (Roy Smith) Date: Mon, 12 Aug 2013 11:47:18 -0400 Subject: Am I not seeing the Error? References: <5206F4AB.8050204@Gmail.com> <5208D625.2000103@Gmail.com> Message-ID: I can't quite sort out the multiple quoting levels, but somebody said: >>> Programming like that is called trolling. A programmer that uses >>> trolling is called a troll. A troll can also refer to such a line >>> of code itself. My scripts contain a lot of trolls. It is easier >>> for me to read trolls than "typical" coding styles. Please tell me this is all just an elaborate joke. From joel.goldstick at gmail.com Mon Aug 12 12:56:45 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 12 Aug 2013 12:56:45 -0400 Subject: Am I not seeing the Error? In-Reply-To: References: <5206F4AB.8050204@Gmail.com> <5208D625.2000103@Gmail.com> Message-ID: On Mon, Aug 12, 2013 at 11:47 AM, Roy Smith wrote: > I can't quite sort out the multiple quoting levels, but somebody said: > >>>> Programming like that is called trolling. A programmer that uses >>>> trolling is called a troll. A troll can also refer to such a line >>>> of code itself. My scripts contain a lot of trolls. It is easier >>>> for me to read trolls than "typical" coding styles. > > Please tell me this is all just an elaborate joke. I was thinking something similar Roy. Devyn, you may think you code differently, but you don't. You have a half of dozen people trying to show you how your style causes confusion between what you think you are writing and what you actually coded. There is plenty of room in coding for personal expression, but what you call 'trolling' is not that. If you like semicolons, use another language that needs them. I think you think it is some version of premature optimization. Since you are a novice at the language, stick with the standards, and learn to embrace them. Ultimately standard coding styles has nothing to do with code optimization. It has to do with readability. Although this is a small example, you can see that if several people get involved debugging it, the first thing that gets in the way is your non-standard coding style. If you want to code alone your whole life, do as you like. But the time spent reading and fixing code in the lifetime of any useful software system is greater than the time spent creating the original code. > -- > http://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com From rosuav at gmail.com Mon Aug 12 13:19:34 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 12 Aug 2013 18:19:34 +0100 Subject: Am I not seeing the Error? In-Reply-To: References: <5206F4AB.8050204@Gmail.com> <5208D625.2000103@Gmail.com> Message-ID: On Mon, Aug 12, 2013 at 5:56 PM, Joel Goldstick wrote: > If you like semicolons, use another language that needs them. > I think you think it is some version of premature optimization. Since > you are a novice at the language, stick with the standards, and learn > to embrace them. I'm a C programmer who really likes his semicolons, and I often write long lines. But I still was right up there early on saying "split this line". It definitely wants to be split. And when *I* say that, it must be pretty notably splittable. :) ChrisA From devyncjohnson at gmail.com Mon Aug 12 16:16:04 2013 From: devyncjohnson at gmail.com (Devyn Collier Johnson) Date: Mon, 12 Aug 2013 16:16:04 -0400 Subject: Am I not seeing the Error? In-Reply-To: References: <5206F4AB.8050204@Gmail.com> <5208D625.2000103@Gmail.com> Message-ID: <52094284.2080403@Gmail.com> On 08/12/2013 12:56 PM, Joel Goldstick wrote: > On Mon, Aug 12, 2013 at 11:47 AM, Roy Smith wrote: >> I can't quite sort out the multiple quoting levels, but somebody said: >> >>>>> Programming like that is called trolling. A programmer that uses >>>>> trolling is called a troll. A troll can also refer to such a line >>>>> of code itself. My scripts contain a lot of trolls. It is easier >>>>> for me to read trolls than "typical" coding styles. >> Please tell me this is all just an elaborate joke. > I was thinking something similar Roy. Devyn, you may think you code > differently, but you don't. You have a half of dozen people trying to > show you how your style causes confusion between what you think you > are writing and what you actually coded. There is plenty of room in > coding for personal expression, but what you call 'trolling' is not > that. If you like semicolons, use another language that needs them. > I think you think it is some version of premature optimization. Since > you are a novice at the language, stick with the standards, and learn > to embrace them. Ultimately standard coding styles has nothing to do > with code optimization. It has to do with readability. Although this > is a small example, you can see that if several people get involved > debugging it, the first thing that gets in the way is your > non-standard coding style. If you want to code alone your whole life, > do as you like. But the time spent reading and fixing code in the > lifetime of any useful software system is greater than the time spent > creating the original code. >> -- >> http://mail.python.org/mailman/listinfo/python-list > > I know using semicolons will not optimize the code, but it is actually easier for me to read. I can handle such code better than spacing it out. DCJ From joel.goldstick at gmail.com Mon Aug 12 16:34:53 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 12 Aug 2013 16:34:53 -0400 Subject: Am I not seeing the Error? In-Reply-To: <52094284.2080403@Gmail.com> References: <5206F4AB.8050204@Gmail.com> <5208D625.2000103@Gmail.com> <52094284.2080403@Gmail.com> Message-ID: On Mon, Aug 12, 2013 at 4:16 PM, Devyn Collier Johnson wrote: > > On 08/12/2013 12:56 PM, Joel Goldstick wrote: >> >> On Mon, Aug 12, 2013 at 11:47 AM, Roy Smith wrote: >>> >>> I can't quite sort out the multiple quoting levels, but somebody said: >>> >>>>>> Programming like that is called trolling. A programmer that uses >>>>>> trolling is called a troll. A troll can also refer to such a line >>>>>> of code itself. My scripts contain a lot of trolls. It is easier >>>>>> for me to read trolls than "typical" coding styles. >>> >>> Please tell me this is all just an elaborate joke. >> >> I was thinking something similar Roy. Devyn, you may think you code >> differently, but you don't. You have a half of dozen people trying to >> show you how your style causes confusion between what you think you >> are writing and what you actually coded. There is plenty of room in >> coding for personal expression, but what you call 'trolling' is not >> that. If you like semicolons, use another language that needs them. >> I think you think it is some version of premature optimization. Since >> you are a novice at the language, stick with the standards, and learn >> to embrace them. Ultimately standard coding styles has nothing to do >> with code optimization. It has to do with readability. Although this >> is a small example, you can see that if several people get involved >> debugging it, the first thing that gets in the way is your >> non-standard coding style. If you want to code alone your whole life, >> do as you like. But the time spent reading and fixing code in the >> lifetime of any useful software system is greater than the time spent >> creating the original code. >>> >>> -- >>> http://mail.python.org/mailman/listinfo/python-list >> >> >> > > I know using semicolons will not optimize the code, but it is actually > easier for me to read. I can handle such code better than spacing it out. Except it doesn't make it easier to read. JOB_WRITEURGFILES = multiprocessing.Process(write2file('./mem/ENGINE_PID', ENGINEPID); write2file(SENTEMPPATH, ''); write2file(INPUTMEM, '')); JOB_WRITEURGFILES.start() As I understand it the above line of code caused you problems. I don't know what muliprocessing.Process() is supposed to have as arguments, but I don't think is can have arguments with semicolons, unless they appeared in a string. So your semicolons, that you insist make your code easier to read, have made code that doesn't work, and you don't know why. All I am saying is you are stating a fact not in evidence. You only think the semicolons are useful because you haven't taken the time to read and write enough python code to appreciate that they are not only not helpful, but they serve no useful purpose. Sure, they allow you to put several statements on a single line. But that isn't what you were doing. good luck. I can see you are interested. keep at it. > > DCJ > -- > http://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com From ned at nedbatchelder.com Mon Aug 12 16:35:45 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Mon, 12 Aug 2013 16:35:45 -0400 Subject: Am I not seeing the Error? In-Reply-To: <52094284.2080403@Gmail.com> References: <5206F4AB.8050204@Gmail.com> <5208D625.2000103@Gmail.com> <52094284.2080403@Gmail.com> Message-ID: <52094721.9010908@nedbatchelder.com> On 8/12/13 4:16 PM, Devyn Collier Johnson wrote: > > On 08/12/2013 12:56 PM, Joel Goldstick wrote: >> On Mon, Aug 12, 2013 at 11:47 AM, Roy Smith wrote: >>> I can't quite sort out the multiple quoting levels, but somebody said: >>> >>>>>> Programming like that is called trolling. A programmer that uses >>>>>> trolling is called a troll. A troll can also refer to such a line >>>>>> of code itself. My scripts contain a lot of trolls. It is easier >>>>>> for me to read trolls than "typical" coding styles. >>> Please tell me this is all just an elaborate joke. >> I was thinking something similar Roy. Devyn, you may think you code >> differently, but you don't. You have a half of dozen people trying to >> show you how your style causes confusion between what you think you >> are writing and what you actually coded. There is plenty of room in >> coding for personal expression, but what you call 'trolling' is not >> that. If you like semicolons, use another language that needs them. >> I think you think it is some version of premature optimization. Since >> you are a novice at the language, stick with the standards, and learn >> to embrace them. Ultimately standard coding styles has nothing to do >> with code optimization. It has to do with readability. Although this >> is a small example, you can see that if several people get involved >> debugging it, the first thing that gets in the way is your >> non-standard coding style. If you want to code alone your whole life, >> do as you like. But the time spent reading and fixing code in the >> lifetime of any useful software system is greater than the time spent >> creating the original code. >>> -- >>> http://mail.python.org/mailman/listinfo/python-list >> >> > > I know using semicolons will not optimize the code, but it is actually > easier for me to read. I can handle such code better than spacing it out. There's no point debating the readability of code that doesn't work. Get it functioning first, then let's talk about how best to format it. --Ned. > > DCJ From joshua at landau.ws Tue Aug 13 04:19:07 2013 From: joshua at landau.ws (Joshua Landau) Date: Tue, 13 Aug 2013 09:19:07 +0100 Subject: Am I not seeing the Error? In-Reply-To: References: <5206F4AB.8050204@Gmail.com> <5208D625.2000103@Gmail.com> Message-ID: On 12 August 2013 16:47, Roy Smith wrote: > I can't quite sort out the multiple quoting levels, but somebody said: > >>>> Programming like that is called trolling. A programmer that uses >>>> trolling is called a troll. A troll can also refer to such a line >>>> of code itself. My scripts contain a lot of trolls. It is easier >>>> for me to read trolls than "typical" coding styles. > > Please tell me this is all just an elaborate joke. That was the OP (Devyn Collier Johnson, ) and I, too, am hoping this is an elaborate joke. However, see "http://bazaar.launchpad.net/~devyncjohnson-d/neobot/trunk/view/head:/lib/beta_engine". I am struggling to see what the punchline is... From steve at pearwood.info Tue Aug 13 04:50:41 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 13 Aug 2013 08:50:41 GMT Subject: Am I not seeing the Error? References: <5206F4AB.8050204@Gmail.com> <5208D625.2000103@Gmail.com> Message-ID: <5209f360$0$29885$c3e8da3$5496439d@news.astraweb.com> On Tue, 13 Aug 2013 09:19:07 +0100, Joshua Landau wrote: > On 12 August 2013 16:47, Roy Smith wrote: >> I can't quite sort out the multiple quoting levels, but somebody said: >> >>>>> Programming like that is called trolling. A programmer that uses >>>>> trolling is called a troll. A troll can also refer to such a line of >>>>> code itself. My scripts contain a lot of trolls. It is easier for me >>>>> to read trolls than "typical" coding styles. >> >> Please tell me this is all just an elaborate joke. > > That was the OP (Devyn Collier Johnson, ) and > I, too, am hoping this is an elaborate joke. However, see > "http://bazaar.launchpad.net/~devyncjohnson-d/neobot/trunk/view/head:/ lib/beta_engine". > > I am struggling to see what the punchline is... If Artificial Intelligence code is hard to write, it damn well should be hard to read too. Otherwise, how will people know how brilliant you are? But seriously... I don't know what language Devyn is writing in Python, but I've seen code like that before. Mostly code written back in the early to mid 1980s. I may have even written some of it myself :-) Anyway, I don't think we should badger Devyn, we're not his dad and we're not the code police. All we can do is continue to demonstrate good, idiomatic, *working* Python code, and hopefully he will learn to write the same. -- Steven From devyncjohnson at gmail.com Tue Aug 13 06:31:02 2013 From: devyncjohnson at gmail.com (Devyn Collier Johnson) Date: Tue, 13 Aug 2013 06:31:02 -0400 Subject: Am I not seeing the Error? In-Reply-To: <5209f360$0$29885$c3e8da3$5496439d@news.astraweb.com> References: <5206F4AB.8050204@Gmail.com> <5208D625.2000103@Gmail.com> <5209f360$0$29885$c3e8da3$5496439d@news.astraweb.com> Message-ID: <520A0AE6.2010403@Gmail.com> On 08/13/2013 04:50 AM, Steven D'Aprano wrote: > On Tue, 13 Aug 2013 09:19:07 +0100, Joshua Landau wrote: > >> On 12 August 2013 16:47, Roy Smith wrote: >>> I can't quite sort out the multiple quoting levels, but somebody said: >>> >>>>>> Programming like that is called trolling. A programmer that uses >>>>>> trolling is called a troll. A troll can also refer to such a line of >>>>>> code itself. My scripts contain a lot of trolls. It is easier for me >>>>>> to read trolls than "typical" coding styles. >>> Please tell me this is all just an elaborate joke. >> That was the OP (Devyn Collier Johnson, ) and >> I, too, am hoping this is an elaborate joke. However, see >> "http://bazaar.launchpad.net/~devyncjohnson-d/neobot/trunk/view/head:/ > lib/beta_engine". >> I am struggling to see what the punchline is... > > If Artificial Intelligence code is hard to write, it damn well should be > hard to read too. Otherwise, how will people know how brilliant you are? > > But seriously... I don't know what language Devyn is writing in Python, > but I've seen code like that before. Mostly code written back in the > early to mid 1980s. I may have even written some of it myself :-) > > Anyway, I don't think we should badger Devyn, we're not his dad and we're > not the code police. All we can do is continue to demonstrate good, > idiomatic, *working* Python code, and hopefully he will learn to write > the same. > > I now see why this programming style is called trolling. It brings the worst out of people (^u^). Can I have the number for the coding police, I want to turn myself in (^u^). For me, this style is easier to read. I have tried the "typical" style, but I find this one to be easier. Good thing no one knows my address. A mob might show up at my door (^u^). Once I have the next version of my chatbot released, I will tell you all so you can see it work. Mahalo, DevynCJohnson at Gmail.com From robert.kern at gmail.com Tue Aug 13 08:11:40 2013 From: robert.kern at gmail.com (Robert Kern) Date: Tue, 13 Aug 2013 13:11:40 +0100 Subject: Am I not seeing the Error? In-Reply-To: <520A0AE6.2010403@Gmail.com> References: <5206F4AB.8050204@Gmail.com> <5208D625.2000103@Gmail.com> <5209f360$0$29885$c3e8da3$5496439d@news.astraweb.com> <520A0AE6.2010403@Gmail.com> Message-ID: On 2013-08-13 11:31, Devyn Collier Johnson wrote: > I now see why this programming style is called trolling. This programming style is not called "trolling". It does not have a name. It's just your particular style. I think you misinterpreted someone who accused you of deliberately coding in this style to troll. http://en.wikipedia.org/wiki/Troll_(Internet) > For me, this style is easier to read. I have tried the "typical" style, but I > find this one to be easier. Consider the topic of this thread as evidence against that proposition. -- 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 tjreedy at udel.edu Tue Aug 13 12:12:08 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 13 Aug 2013 12:12:08 -0400 Subject: Am I not seeing the Error? In-Reply-To: <520A0AE6.2010403@Gmail.com> References: <5206F4AB.8050204@Gmail.com> <5208D625.2000103@Gmail.com> <5209f360$0$29885$c3e8da3$5496439d@news.astraweb.com> <520A0AE6.2010403@Gmail.com> Message-ID: On 8/13/2013 6:31 AM, Devyn Collier Johnson wrote: > I now see why this programming style is called trolling. 'Trolling' is a posting style. Your initial post was legitimate. When the first three respondents missed the syntax error, ';' instead of ',' within function() calls, I reported it. You could have easily verified that this change removed the syntax error, thanked me, and moved on. Most of what you did instead, including pretending that there was still a mystery, could be seen as trolling. -- Terry Jan Reedy From torriem at gmail.com Tue Aug 13 21:55:42 2013 From: torriem at gmail.com (Michael Torrie) Date: Tue, 13 Aug 2013 19:55:42 -0600 Subject: Am I not seeing the Error? In-Reply-To: <520A0AE6.2010403@Gmail.com> References: <5206F4AB.8050204@Gmail.com> <5208D625.2000103@Gmail.com> <5209f360$0$29885$c3e8da3$5496439d@news.astraweb.com> <520A0AE6.2010403@Gmail.com> Message-ID: <520AE39E.2000503@gmail.com> On 08/13/2013 04:31 AM, Devyn Collier Johnson wrote: > For me, this style is easier to read. I have tried the "typical" style, > but I find this one to be easier. One thing I do know is that your style makes it very hard to find errors, even when the parser flags them. And the fact that you posted about not being able to find the syntax error proves that it's, in fact, not easier to read. That could be why people are pointing out your unorthodox style and criticizing it. I'm curious as to what language you first learned this style of coding on. It certainly appears reminiscent of earlier, very verbose languages. Capital letter variables, large, wordy blocks, etc. From invalid at invalid.invalid Tue Aug 13 09:55:37 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 13 Aug 2013 13:55:37 +0000 (UTC) Subject: Am I not seeing the Error? References: <5206F4AB.8050204@Gmail.com> <5208D625.2000103@Gmail.com> Message-ID: On 2013-08-13, Joshua Landau wrote: > On 12 August 2013 16:47, Roy Smith wrote: >> I can't quite sort out the multiple quoting levels, but somebody said: >> >>>>> Programming like that is called trolling. A programmer that uses >>>>> trolling is called a troll. A troll can also refer to such a line of >>>>> code itself. My scripts contain a lot of trolls. It is easier for me >>>>> to read trolls than "typical" coding styles. >> >> Please tell me this is all just an elaborate joke. > > That was the OP (Devyn Collier Johnson, ) and I, too, > am hoping this is an elaborate joke. However, see > "http://bazaar.launchpad.net/~devyncjohnson-d/neobot/trunk/view/head:/lib/beta_engine". > > I am struggling to see what the punchline is... The mind wobbles. -- Grant Edwards grant.b.edwards Yow! Vote for ME -- I'm at well-tapered, half-cocked, gmail.com ill-conceived and TAX-DEFERRED! From ned at nedbatchelder.com Mon Aug 12 10:01:55 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Mon, 12 Aug 2013 10:01:55 -0400 Subject: Am I not seeing the Error? In-Reply-To: <5208D625.2000103@Gmail.com> References: <5206F4AB.8050204@Gmail.com> <5208D625.2000103@Gmail.com> Message-ID: <5208EAD3.2020604@nedbatchelder.com> On 8/12/13 8:33 AM, Devyn Collier Johnson wrote: > > On 08/10/2013 10:47 PM, Chris Angelico wrote: >> On Sun, Aug 11, 2013 at 3:43 AM, Roy Smith wrote: >>> In article , >>> Chris Angelico wrote: >>> >>>> When you get a syntax error you can't understand, look at the previous >>>> line of code. Perhaps something there is incomplete; maybe you have >>>> mismatched parentheses, so this line is considered to be part of the >>>> same expression. >>>> >>>> Next thing to do is split it into more lines. Why is all that in a >>>> single >>>> line? >>> Also, try reformatting the code in a tool like emacs or eclipse which >>> does syntax coloring and auto indenting. Often, if you're missing some >>> piece of punctuation, it will become obvious when your tool tries to >>> indent things in some unexpected way. Or suddenly starts coloring all >>> of your program text as if it were a string literal :-) >> Agreed. Though I've had some odd issues with SciTE in that way; I >> think its Python handling may have bugs in it here and there. But 95% >> of the time it's helpful. >> >> ChrisA > > Thanks everyone. Unfortunately, I have not found the problem yet. I > use the Geany IDE which has syntax highlighting, but nothing wrong is > seen. None of the suggestions helped. The lines before this one set > variables. The lines further up "appear" fine. I will keep looking. If > I ever figure it out, I will share with all of you. > As Terry Reedy pointed out, you have semicolons separating arguments in a function call. This is your line of code: JOB_WRITEURGFILES = multiprocessing.Process(write2file('./mem/ENGINE_PID', ENGINEPID); write2file(SENTEMPPATH, ''); write2file(INPUTMEM, '')); JOB_WRITEURGFILES.start() Replacing names with shorter ones to see the structure, it's like this: J = m.P( w('', E); w(S, ''); w(I, '') ); J.s() You have three semicolons in that line. Two are inside a call, though I'm not sure that's what you intended. One is separating statements. You might be a little too attached to your "more readable" style. Putting things on different lines really does help you see what is going on. --Ned. From steve+comp.lang.python at pearwood.info Sun Aug 11 04:28:51 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 11 Aug 2013 08:28:51 GMT Subject: Am I not seeing the Error? References: <5206F4AB.8050204@Gmail.com> Message-ID: <52074b43$0$30000$c3e8da3$5496439d@news.astraweb.com> On Sun, 11 Aug 2013 03:33:52 +0100, Chris Angelico wrote: > Next thing to do is split it into more lines. Why is all that in a > single line? The only good excuse for writing multiple statements on a single line separated by semi-colons is if the Enter key on your keyboard is broken. :-) -- Steven From joshua at landau.ws Sun Aug 11 06:18:01 2013 From: joshua at landau.ws (Joshua Landau) Date: Sun, 11 Aug 2013 11:18:01 +0100 Subject: Am I not seeing the Error? In-Reply-To: <52074b43$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <5206F4AB.8050204@Gmail.com> <52074b43$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 11 August 2013 09:28, Steven D'Aprano wrote: > into more lines. Why is all that in a >> single line? > > The only good excuse for writing multiple statements on a single line > separated by semi-colons is if the Enter key on your keyboard is broken. That's not a good excuse. It *is* a good excuse for any of the following: * Buying a new keyboard * Using a new keymap, possibly replacing cAPSLOCK with Return * Crying From roy at panix.com Sun Aug 11 09:27:05 2013 From: roy at panix.com (Roy Smith) Date: Sun, 11 Aug 2013 09:27:05 -0400 Subject: Am I not seeing the Error? References: <5206F4AB.8050204@Gmail.com> <52074b43$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <52074b43$0$30000$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > On Sun, 11 Aug 2013 03:33:52 +0100, Chris Angelico wrote: > > > Next thing to do is split it into more lines. Why is all that in a > > single line? > > The only good excuse for writing multiple statements on a single line > separated by semi-colons is if the Enter key on your keyboard is broken. Well, maybe if you're testing something on the command line with "python -c". From tjreedy at udel.edu Sat Aug 10 23:07:33 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 10 Aug 2013 23:07:33 -0400 Subject: Am I not seeing the Error? In-Reply-To: <5206F4AB.8050204@Gmail.com> References: <5206F4AB.8050204@Gmail.com> Message-ID: On 8/10/2013 10:19 PM, Devyn Collier Johnson wrote: > I am checking my 1292-line script for syntax errors. I ran the following > commands in a terminal to check for errors, but I do not see the error. > > collier at Nacho-Laptop:/media/collier/AI/Pysh$ python3 -m py_compile > ./beta_engine > File "./beta_engine", line 344 > JOB_WRITEURGFILES = > multiprocessing.Process(write2file('./mem/ENGINE_PID', ENGINEPID); That should be a comma; so should the next semicolon below. > write2file(SENTEMPPATH, ''); write2file(INPUTMEM, '')); This last one is 'correct', but delete it > JOB_WRITEURGFILES.start() and put this statement on a line by itself. -- Terry Jan Reedy From gordon at panix.com Mon Aug 12 10:47:43 2013 From: gordon at panix.com (John Gordon) Date: Mon, 12 Aug 2013 14:47:43 +0000 (UTC) Subject: Am I not seeing the Error? References: Message-ID: In Devyn Collier Johnson writes: > I am checking my 1292-line script for syntax errors. I ran the following > commands in a terminal to check for errors, but I do not see the error. > File "./beta_engine", line 344 > JOB_WRITEURGFILES = > multiprocessing.Process(write2file('./mem/ENGINE_PID', ENGINEPID); You have too many ('s this line. > write2file(SENTEMPPATH, ''); write2file(INPUTMEM, '')); And too many )'s on this one. -- 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 denismfmcmahon at gmail.com Tue Aug 13 16:28:57 2013 From: denismfmcmahon at gmail.com (Denis McMahon) Date: Tue, 13 Aug 2013 20:28:57 +0000 (UTC) Subject: Am I not seeing the Error? References: Message-ID: On Sat, 10 Aug 2013 22:19:23 -0400, Devyn Collier Johnson wrote: > I am checking my 1292-line script for syntax errors. I ran the following > commands in a terminal to check for errors, but I do not see the error. > JOB_WRITEURGFILES = > multiprocessing.Process(write2file('./mem/ENGINE_PID', ENGINEPID); > write2file(SENTEMPPATH, ''); write2file(INPUTMEM, '')); > JOB_WRITEURGFILES.start() When I expand this out to one item per line, JOB_WRITEURGFILES = multiprocessing.Process ( write2file ( './mem/ENGINE_PID' , ENGINEPID ) ; write2file ( SENTEMPPATH , '' ) ; write2file ( INPUTMEM , '' ) ) ; JOB_WRITEURGFILES.start() and I wonder (not being familiar with multiprocessing) if perhaps there should have been a third ";" after the third write2file in the job definition. -- Denis McMahon, denismfmcmahon at gmail.com From davea at davea.name Tue Aug 13 17:16:18 2013 From: davea at davea.name (Dave Angel) Date: Tue, 13 Aug 2013 21:16:18 +0000 (UTC) Subject: Am I not seeing the Error? References: Message-ID: Denis McMahon wrote: > On Sat, 10 Aug 2013 22:19:23 -0400, Devyn Collier Johnson wrote: > >> I am checking my 1292-line script for syntax errors. I ran the following >> commands in a terminal to check for errors, but I do not see the error. > >> JOB_WRITEURGFILES = >> multiprocessing.Process(write2file('./mem/ENGINE_PID', ENGINEPID); >> write2file(SENTEMPPATH, ''); write2file(INPUTMEM, '')); >> JOB_WRITEURGFILES.start() > > When I expand this out to one item per line, > > JOB_WRITEURGFILES = > multiprocessing.Process > ( > write2file > ( > './mem/ENGINE_PID' > , > ENGINEPID > ) > ; > write2file > ( > SENTEMPPATH > , > '' > ) > ; > write2file > ( > INPUTMEM > , > '' > ) > ) > ; > JOB_WRITEURGFILES.start() > > and I wonder (not being familiar with multiprocessing) if perhaps there > should have been a third ";" after the third write2file in the job > definition. > The mistake is not that it's missing the 3rd, but that the first two semicolons should have been commas. These are parameters to a function call multiprocessing.Process() -- Signature file not found From ned at nedbatchelder.com Tue Aug 13 20:00:57 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Tue, 13 Aug 2013 20:00:57 -0400 Subject: Am I not seeing the Error? In-Reply-To: References: Message-ID: <520AC8B9.1080305@nedbatchelder.com> On 8/13/13 5:16 PM, Dave Angel wrote: > Denis McMahon wrote: > >> On Sat, 10 Aug 2013 22:19:23 -0400, Devyn Collier Johnson wrote: >> >>> I am checking my 1292-line script for syntax errors. I ran the following >>> commands in a terminal to check for errors, but I do not see the error. >>> JOB_WRITEURGFILES = >>> multiprocessing.Process(write2file('./mem/ENGINE_PID', ENGINEPID); >>> write2file(SENTEMPPATH, ''); write2file(INPUTMEM, '')); >>> JOB_WRITEURGFILES.start() >> When I expand this out to one item per line, >> >> JOB_WRITEURGFILES = >> multiprocessing.Process >> ( >> write2file >> ( >> './mem/ENGINE_PID' >> , >> ENGINEPID >> ) >> ; >> write2file >> ( >> SENTEMPPATH >> , >> '' >> ) >> ; >> write2file >> ( >> INPUTMEM >> , >> '' >> ) >> ) >> ; >> JOB_WRITEURGFILES.start() >> >> and I wonder (not being familiar with multiprocessing) if perhaps there >> should have been a third ";" after the third write2file in the job >> definition. >> > The mistake is not that it's missing the 3rd, but that the first two > semicolons should have been commas. These are parameters to a function > call multiprocessing.Process() Everyone: this program seems to be a direct and misguided transliteration from a bash script. There are dozens of mis-uses like this of multiprocessing.Process(), due to a severe misunderstanding of what it does and how it works. We've tried offering help, and all that's happened is we've been told that this strange coding style is easier to read. --Ned. From greg.ewing at canterbury.ac.nz Tue Aug 13 21:20:02 2013 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Wed, 14 Aug 2013 13:20:02 +1200 Subject: Am I not seeing the Error? In-Reply-To: References: Message-ID: Ned Batchelder wrote: > Everyone: this program seems to be a direct and misguided > transliteration from a bash script. Not a particularly well-written bash script, either -- it's full of superfluous uses of 'cat'. -- Greg From joshua at landau.ws Wed Aug 14 02:59:11 2013 From: joshua at landau.ws (Joshua Landau) Date: Wed, 14 Aug 2013 07:59:11 +0100 Subject: Am I not seeing the Error? In-Reply-To: References: Message-ID: On 14 August 2013 02:20, Gregory Ewing wrote: > Ned Batchelder wrote: >> >> Everyone: this program seems to be a direct and misguided transliteration >> from a bash script. > > Not a particularly well-written bash script, either -- > it's full of superfluous uses of 'cat'. What's wrong with cat? Sure it's superfluous but what makes it *bad*? Personally I often prefer the pipe "cat x | y" form to "x < y"... or "< y x". There seems to be a militant "cat is evil" attitude where I feel it's just normally just people who want to show others they know more bash than they do. From rosuav at gmail.com Wed Aug 14 08:07:38 2013 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 14 Aug 2013 13:07:38 +0100 Subject: Am I not seeing the Error? In-Reply-To: References: Message-ID: On Wed, Aug 14, 2013 at 7:59 AM, Joshua Landau wrote: > On 14 August 2013 02:20, Gregory Ewing wrote: >> Ned Batchelder wrote: >>> >>> Everyone: this program seems to be a direct and misguided transliteration >>> from a bash script. >> >> Not a particularly well-written bash script, either -- >> it's full of superfluous uses of 'cat'. > > What's wrong with cat? Sure it's superfluous but what makes it *bad*? > Personally I often prefer the pipe "cat x | y" form to "x < y"... or > "< y x". What's the use of it, in that situation? Why not simply use redirection? (Though you have the letters backward; "cat y | x" would be the equivalent of your others. Typo, I assume.) You're forking a process that achieves nothing, if your cat has just one argument. Of course, there ARE many good uses for cat. If you give it multiple arguments, or if you have arguments that modify the output on the way through (eg "cat -n"), then it's not the same as redirection. And some programs behave differently if stdout is a tty, so the quickest way to get the porcelain version of something is to append "|cat" to the command. Or maybe you need to retrieve something that only root can read, so you use "sudo cat /x/y/z|somescript". But if you could spell it "x < y", then why not do so? ChrisA From joshua at landau.ws Wed Aug 14 08:34:26 2013 From: joshua at landau.ws (Joshua Landau) Date: Wed, 14 Aug 2013 13:34:26 +0100 Subject: Am I not seeing the Error? In-Reply-To: References: Message-ID: On 14 August 2013 13:07, Chris Angelico wrote: > On Wed, Aug 14, 2013 at 7:59 AM, Joshua Landau wrote: >> >> What's wrong with cat? Sure it's superfluous but what makes it *bad*? >> Personally I often prefer the pipe "cat x | y" form to "x < y"... or >> "< y x". > > What's the use of it, in that situation? Why not simply use > redirection? (Though you have the letters backward; "cat y | x" would > be the equivalent of your others. Typo, I assume.) You're forking a > process that achieves nothing, if your cat has just one argument. > > Of course, there ARE many good uses for cat. If you give it multiple > arguments, or if you have arguments that modify the output on the way > through (eg "cat -n"), then it's not the same as redirection. And some > programs behave differently if stdout is a tty, so the quickest way to > get the porcelain version of something is to append "|cat" to the > command. Or maybe you need to retrieve something that only root can > read, so you use "sudo cat /x/y/z|somescript". But if you could spell > it "x < y", then why not do so? Because "cat y | x" often reads nicer. It's the whole "input -> function -> function -> ... -> output" thing. I especially hate "y < input > output" which reads awfully not matter where you chuck the spaces. "cat input | y > output" however, is acceptable. Honestly I do think Python would do well to get a pipe operator, because in some circumstances it's just cleaner. From roy at panix.com Wed Aug 14 08:39:25 2013 From: roy at panix.com (Roy Smith) Date: Wed, 14 Aug 2013 08:39:25 -0400 Subject: Am I not seeing the Error? References: Message-ID: In article , Chris Angelico wrote: > On Wed, Aug 14, 2013 at 7:59 AM, Joshua Landau wrote: > > On 14 August 2013 02:20, Gregory Ewing wrote: > >> Ned Batchelder wrote: > >>> > >>> Everyone: this program seems to be a direct and misguided transliteration > >>> from a bash script. > >> > >> Not a particularly well-written bash script, either -- > >> it's full of superfluous uses of 'cat'. > > > > What's wrong with cat? Sure it's superfluous but what makes it *bad*? > > Personally I often prefer the pipe "cat x | y" form to "x < y"... or > > "< y x". > > What's the use of it, in that situation? Why not simply use > redirection? (Though you have the letters backward; "cat y | x" would > be the equivalent of your others. Typo, I assume.) You're forking a > process that achieves nothing, if your cat has just one argument. This is waaaaayyyy off-topic for a Python discussion, but... There's two reasons UUOC is a silly issue. First, it may save human effort. I like to build up long complicated commands and pipelines one bit at a time, and look at the intermediate results. Let's say I'm starting with a sed command (abstracted from my current shell history) $ sed -e 's/.*; iOS/iOS/' -e 's/;.*//' -e 's/\..*//' x When I want to add the next "-e whatever" to the command, I need to get it in front of the "x". If I had written it as: $ cat x | sed -e 's/.*; iOS/iOS/' -e 's/;.*//' -e 's/\..*//' I just have to stick it at the end, which is easier; I just type control-p and add what I want. Or, "!!" and keep typing. A small amount of human convenience (especially when it's mine) is worth a lot of wasted CPU time. Second, in some cases, the extra "cat" process may actually speed up overall command execution because it provides additional I/O buffering. The cat process will read ahead from the disk file and block only when its output pipe buffers are full. When the sed command is ready to process more input, it only has to read from the pipe, not wait for a (very slow, by comparison) disk read. Yeah, I know, modern kernels do lots of read-ahead buffing on their own. This gives you more. Sure, it costs something to fork/exec another process. So what? The computer exists to do my bidding, not the other way around. From python at mrabarnett.plus.com Tue Aug 13 19:44:42 2013 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 14 Aug 2013 00:44:42 +0100 Subject: Am I not seeing the Error? In-Reply-To: References: Message-ID: <520AC4EA.1030903@mrabarnett.plus.com> On 13/08/2013 21:28, Denis McMahon wrote: > On Sat, 10 Aug 2013 22:19:23 -0400, Devyn Collier Johnson wrote: > >> I am checking my 1292-line script for syntax errors. I ran the following >> commands in a terminal to check for errors, but I do not see the error. > >> JOB_WRITEURGFILES = >> multiprocessing.Process(write2file('./mem/ENGINE_PID', ENGINEPID); >> write2file(SENTEMPPATH, ''); write2file(INPUTMEM, '')); >> JOB_WRITEURGFILES.start() > > When I expand this out to one item per line, > > JOB_WRITEURGFILES = > multiprocessing.Process > ( > write2file > ( > './mem/ENGINE_PID' > , > ENGINEPID > ) > ; > write2file > ( > SENTEMPPATH > , > '' > ) > ; > write2file > ( > INPUTMEM > , > '' > ) > ) > ; > JOB_WRITEURGFILES.start() > > and I wonder (not being familiar with multiprocessing) if perhaps there > should have been a third ";" after the third write2file in the job > definition. > No, there shouldn't be _any_ semicolons. Basically it should be something like: my_process = multiprocessing.Process(target=my_function) my_process.start() From jsf80238 at gmail.com Sat Aug 10 23:41:00 2013 From: jsf80238 at gmail.com (Jason Friedman) Date: Sat, 10 Aug 2013 21:41:00 -0600 Subject: Elegant compare Message-ID: class my_class: def __init__(self, attr1, attr2): self.attr1 = attr1 #string self.attr2 = attr2 #string def __lt__(self, other): if self.attr1 < other.attr1: return True else: return self.attr2 < other.attr2 I will run into problems if attr1 or attr2 is None, and they legitimately can be. I know I can check for attr1 or attr2 or both being None and react accordingly, but my real class has ten attributes and that approach will be long. What are my alternatives? From rosuav at gmail.com Sat Aug 10 23:45:44 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 11 Aug 2013 04:45:44 +0100 Subject: Elegant compare In-Reply-To: References: Message-ID: On Sun, Aug 11, 2013 at 4:41 AM, Jason Friedman wrote: > class my_class: > def __init__(self, attr1, attr2): > self.attr1 = attr1 #string > self.attr2 = attr2 #string > def __lt__(self, other): > if self.attr1 < other.attr1: > return True > else: > return self.attr2 < other.attr2 > > I will run into problems if attr1 or attr2 is None, and they > legitimately can be. > > I know I can check for attr1 or attr2 or both being None and react > accordingly, but my real class has ten attributes and that approach > will be long. What are my alternatives? The first question is: What should the comparison do with a None value? Should it be considered less than every string? If so, you could simply use: if (self.attr1 or "") < (other.attr1 or ""): which will treat any falsy value as blank. ChrisA From steve+comp.lang.python at pearwood.info Sun Aug 11 04:26:28 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 11 Aug 2013 08:26:28 GMT Subject: Elegant compare References: Message-ID: <52074ab3$0$30000$c3e8da3$5496439d@news.astraweb.com> On Sat, 10 Aug 2013 21:41:00 -0600, Jason Friedman wrote: > class my_class: > def __init__(self, attr1, attr2): > self.attr1 = attr1 #string > self.attr2 = attr2 #string > def __lt__(self, other): > if self.attr1 < other.attr1: > return True > else: > return self.attr2 < other.attr2 > > I will run into problems if attr1 or attr2 is None, and they > legitimately can be. > > I know I can check for attr1 or attr2 or both being None and react > accordingly, but my real class has ten attributes and that approach will > be long. What are my alternatives? This is a hard question to answer, because your code snippet isn't clearly extensible to the case where you have ten attributes. What's the rule for combining them? If instance A has five attributes less than those of instance B, and five attributes greater than those of instance B, which wins? But if I had to guess an approach, I'd start with a helper function (or method) that compares two raw values: def compare(a, b): """Return -ve for less than, 0 for equal, +ve for greater than.""" if a is None: return 0 if b is None else -1 if b is None: return 1 return (b < a) - (a < b) Now, in your class, you can use this helper function to check each attribute in turn. Assuming that if an attribute is equal, you move on to check the next one: class MyClass: def _compare(self, other): for attr in 'attr1 attr2 attr3 attr4'.split(): a, b = getattr(self, attr), getattr(other, attr) triflag = compare(a, b) if triflag: return triflag return 0 def __lt__(self, other): if not isinstance(other, MyClass): return NotImplemented return self._compare(other) < 0 def __eq__(self, other): if not isinstance(other, MyClass): return NotImplemented return not self._compare(other) def __ne__(self, other): if not isinstance(other, MyClass): return NotImplemented return bool(self._compare(other)) and so on. You can save a certain amount of repetition (by my count, six lines of code) by pulling out the "if not isinstance" check into a decorator, but since the decorator is likely to be about six lines long, I wouldn't bother :-) -- Steven From jsf80238 at gmail.com Sun Aug 11 11:17:13 2013 From: jsf80238 at gmail.com (Jason Friedman) Date: Sun, 11 Aug 2013 09:17:13 -0600 Subject: Elegant compare In-Reply-To: <52074ab3$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <52074ab3$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: > This is a hard question to answer, because your code snippet isn't > clearly extensible to the case where you have ten attributes. What's the > rule for combining them? If instance A has five attributes less than > those of instance B, and five attributes greater than those of instance > B, which wins? Yes, my code snippet was too short, I should have said: class my_class: def __init__(self, attr1, attr2, attr3): self.attr1 = attr1 #string self.attr2 = attr2 #string self.attr3 = attr3 #string def __lt__(self, other): if self.attr1 < other.attr1: return True elif self.attr2 < other.attr2: return True else: return self.attr3 < other.attr3 Chris's answer is actually perfectly adequate for my needs. Thank you Steve and Chris. From joshua at landau.ws Sun Aug 11 02:17:42 2013 From: joshua at landau.ws (Joshua Landau) Date: Sun, 11 Aug 2013 07:17:42 +0100 Subject: Could you verify this, Oh Great Unicode Experts of the Python-List? Message-ID: Basically, I think Twitter's broken. For my full discusion on the matter, see: http://www.reddit.com/r/learnpython/comments/1k2yrn/help_with_len_and_input_function_33/cbku5e8 Here's the first post of mine, ineffectually edited for this list: """ The obvious solution [to getting the length of a tweet] is wrong. Like, slightly wrong?. Given tweet = b"caf\x65\xCC\x81".decode(): >>> tweet 'caf?' But: >>> len(tweet) 5 So the solution is: >>> import unicodedata >>> len(unicodedata.normalize("NFC", tweet)) 4 Read twitter's commentary? for proof. There are additional complications I'm trying to sort out. ________________________________ After further testing (I don't actually use Twitter) it seems the whole thing was just smoke and mirrors. The linked article is a lie, at least on the user's end. On Linux you can prove this by running: >>> p = subprocess.Popen(['xsel', '-bi'], stdin=subprocess.PIPE) >>> p.communicate(input=b"caf\x65\xCC\x81") (None, None) "cafe?" will be in your Copy-Paste buffer, and you can paste it in to the tweet-box. It takes 5 characters. So much for testing ;). ________________________________ ? https://dev.twitter.com/docs/counting-characters#Definition_of_a_Character """ I know this isn't *really* Python-related, but there's Python involved and you're the sort of people who'll be able to tell me what I've done wrong, if anything. From rosuav at gmail.com Sun Aug 11 02:24:23 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 11 Aug 2013 07:24:23 +0100 Subject: Could you verify this, Oh Great Unicode Experts of the Python-List? In-Reply-To: References: Message-ID: On Sun, Aug 11, 2013 at 7:17 AM, Joshua Landau wrote: > Given tweet = b"caf\x65\xCC\x81".decode(): > > >>> tweet > 'caf?' > > But: > > >>> len(tweet) > 5 You're now looking at the difference between glyphs and combining characters. Twitter counts combining characters, so when you build one "thing" out of lots of separately-typed parts, it does count as more characters. Read this article for some arguments on the subject, including a number of references to Twitter itself: http://unspecified.wordpress.com/2012/04/19/the-importance-of-language-level-abstract-unicode-strings/ ChrisA From joshua at landau.ws Sun Aug 11 05:54:43 2013 From: joshua at landau.ws (Joshua Landau) Date: Sun, 11 Aug 2013 10:54:43 +0100 Subject: Could you verify this, Oh Great Unicode Experts of the Python-List? In-Reply-To: References: Message-ID: On 11 August 2013 07:24, Chris Angelico wrote: > On Sun, Aug 11, 2013 at 7:17 AM, Joshua Landau wrote: >> Given tweet = b"caf\x65\xCC\x81".decode(): >> >> >>> tweet >> 'caf?' >> >> But: >> >> >>> len(tweet) >> 5 > > You're now looking at the difference between glyphs and combining > characters. Twitter counts combining characters, so when you build one > "thing" out of lots of separately-typed parts, it does count as more > characters. @https://dev.twitter.com/docs/counting-characters#Definition_of_a_Character > The "caf?" issue mentioned above raises the question of how you count > the characters in the Tweet string "caf?". To the human eye the length is > clearly four characters. Depending on how the data is represented this > could be either five or six UTF-8 bytes. Twitter does not want to penalize > a user for the fact we use UTF-8 or for the fact that the API client in > question used the longer representation. Therefore, Twitter does count > "caf?" as four characters no matter which representation is sent. Which would imply that twitter doesn't count combining characters, even though the web interface seems to. > Read this article for some arguments on the subject, including a > number of references to Twitter itself: > > http://unspecified.wordpress.com/2012/04/19/the-importance-of-language-level-abstract-unicode-strings/ I read that *last* time you pointed it out :P. It's a good link, though. -- Anyhow, it's good to know I haven't been obviously stupid with my understanding of Unicode. I learnt it all from this list anyway; wouldn't want to disappoint! From python at mrabarnett.plus.com Sun Aug 11 11:34:45 2013 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 11 Aug 2013 16:34:45 +0100 Subject: Could you verify this, Oh Great Unicode Experts of the Python-List? In-Reply-To: References: Message-ID: <5207AF15.7040604@mrabarnett.plus.com> On 11/08/2013 10:54, Joshua Landau wrote: > On 11 August 2013 07:24, Chris Angelico wrote: >> On Sun, Aug 11, 2013 at 7:17 AM, Joshua Landau wrote: >>> Given tweet = b"caf\x65\xCC\x81".decode(): >>> >>> >>> tweet >>> 'caf?' >>> >>> But: >>> >>> >>> len(tweet) >>> 5 >> >> You're now looking at the difference between glyphs and combining >> characters. Twitter counts combining characters, so when you build one >> "thing" out of lots of separately-typed parts, it does count as more >> characters. > > @https://dev.twitter.com/docs/counting-characters#Definition_of_a_Character >> The "caf?" issue mentioned above raises the question of how you count >> the characters in the Tweet string "caf?". To the human eye the length is >> clearly four characters. Depending on how the data is represented this >> could be either five or six UTF-8 bytes. Twitter does not want to penalize >> a user for the fact we use UTF-8 or for the fact that the API client in >> question used the longer representation. Therefore, Twitter does count >> "caf?" as four characters no matter which representation is sent. > > Which would imply that twitter doesn't count combining characters, > even though the web interface seems to. > >> Read this article for some arguments on the subject, including a >> number of references to Twitter itself: >> >> http://unspecified.wordpress.com/2012/04/19/the-importance-of-language-level-abstract-unicode-strings/ > > I read that *last* time you pointed it out :P. It's a good link, though. > > -- > Anyhow, it's good to know I haven't been obviously stupid with my > understanding of Unicode. I learnt it all from this list anyway; > wouldn't want to disappoint! > If twitter counts characters, not codepoints, you could then ask whether it passes the codepoints through as given. If it does, then you experiment to see how much data you could send encoded as a sequence of combining codepoints. (You might want to check the Term of Use first, though! :-)) From torriem at gmail.com Sun Aug 11 21:26:54 2013 From: torriem at gmail.com (Michael Torrie) Date: Sun, 11 Aug 2013 19:26:54 -0600 Subject: Could you verify this, Oh Great Unicode Experts of the Python-List? In-Reply-To: <5207AF15.7040604@mrabarnett.plus.com> References: <5207AF15.7040604@mrabarnett.plus.com> Message-ID: <520839DE.6040909@gmail.com> On 08/11/2013 09:34 AM, MRAB wrote: > If twitter counts characters, not codepoints, you could then ask > whether it passes the codepoints through as given. If it does, then you > experiment to see how much data you could send encoded as a sequence of > combining codepoints. (You might want to check the Term of Use first, > though! :-)) I've always wondered if the 160 character limit or whatever it is is a hard limit in their system, or if it's just a variable they could tweak if they felt like it. From greg.ewing at canterbury.ac.nz Mon Aug 12 01:54:11 2013 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Mon, 12 Aug 2013 17:54:11 +1200 Subject: Could you verify this, Oh Great Unicode Experts of the Python-List? In-Reply-To: References: <5207AF15.7040604@mrabarnett.plus.com> Message-ID: Michael Torrie wrote: > I've always wondered if the 160 character limit or whatever it is is a > hard limit in their system, or if it's just a variable they could tweak > if they felt like it. Isn't it for compatibility with SMS? Twitter could probably change it, but persuading all the cell phone networks to change at the same time might be rather difficult. -- Greg From torriem at gmail.com Mon Aug 12 21:48:56 2013 From: torriem at gmail.com (Michael Torrie) Date: Mon, 12 Aug 2013 19:48:56 -0600 Subject: Could you verify this, Oh Great Unicode Experts of the Python-List? In-Reply-To: References: <5207AF15.7040604@mrabarnett.plus.com> Message-ID: <52099088.7030300@gmail.com> On 08/11/2013 11:54 PM, Gregory Ewing wrote: > Michael Torrie wrote: >> I've always wondered if the 160 character limit or whatever it is is a >> hard limit in their system, or if it's just a variable they could tweak >> if they felt like it. > > Isn't it for compatibility with SMS? Twitter could > probably change it, but persuading all the cell phone > networks to change at the same time might be rather > difficult. Yes I think you're correct about it being limited for SMS. However I know of no phone or network that won't let you use longer messages; multiple SMS packets are used and most phone paste them back together. So no there's nothing that anyone needs to change to use longer messages if they so chose. It's now just an arbitrary limit, part of the twitter culture. From rosuav at gmail.com Mon Aug 12 21:54:38 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 13 Aug 2013 02:54:38 +0100 Subject: Could you verify this, Oh Great Unicode Experts of the Python-List? In-Reply-To: <52099088.7030300@gmail.com> References: <5207AF15.7040604@mrabarnett.plus.com> <52099088.7030300@gmail.com> Message-ID: On Tue, Aug 13, 2013 at 2:48 AM, Michael Torrie wrote: > On 08/11/2013 11:54 PM, Gregory Ewing wrote: >> Michael Torrie wrote: >>> I've always wondered if the 160 character limit or whatever it is is a >>> hard limit in their system, or if it's just a variable they could tweak >>> if they felt like it. >> >> Isn't it for compatibility with SMS? Twitter could >> probably change it, but persuading all the cell phone >> networks to change at the same time might be rather >> difficult. > > Yes I think you're correct about it being limited for SMS. > > However I know of no phone or network that won't let you use longer > messages; multiple SMS packets are used and most phone paste them back > together. So no there's nothing that anyone needs to change to use > longer messages if they so chose. It's now just an arbitrary limit, > part of the twitter culture. It's unlikely to be changed; the limit demands brevity. 160 may be arbitrary now, but without strong argument for another cutoff, there's no reason to alter it. And that's my response, in 160 characters. :) ChrisA From jsf80238 at gmail.com Mon Aug 12 23:20:06 2013 From: jsf80238 at gmail.com (Jason Friedman) Date: Mon, 12 Aug 2013 21:20:06 -0600 Subject: Could you verify this, Oh Great Unicode Experts of the Python-List? In-Reply-To: <52099088.7030300@gmail.com> References: <5207AF15.7040604@mrabarnett.plus.com> <52099088.7030300@gmail.com> Message-ID: >>> I've always wondered if the 160 character limit or whatever it is is a >>> hard limit in their system, or if it's just a variable they could tweak >>> if they felt like it. I thought it was 140 characters? https://twitter.com/about From python at mrabarnett.plus.com Mon Aug 12 23:32:40 2013 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 13 Aug 2013 04:32:40 +0100 Subject: Could you verify this, Oh Great Unicode Experts of the Python-List? In-Reply-To: References: <5207AF15.7040604@mrabarnett.plus.com> <52099088.7030300@gmail.com> Message-ID: <5209A8D8.9020509@mrabarnett.plus.com> On 13/08/2013 04:20, Jason Friedman wrote: >>>> I've always wondered if the 160 character limit or whatever it is is a >>>> hard limit in their system, or if it's just a variable they could tweak >>>> if they felt like it. > > I thought it was 140 characters? > https://twitter.com/about > He did say "or whatever". :-) From rosuav at gmail.com Tue Aug 13 04:14:33 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 13 Aug 2013 09:14:33 +0100 Subject: Could you verify this, Oh Great Unicode Experts of the Python-List? In-Reply-To: <5209A8D8.9020509@mrabarnett.plus.com> References: <5207AF15.7040604@mrabarnett.plus.com> <52099088.7030300@gmail.com> <5209A8D8.9020509@mrabarnett.plus.com> Message-ID: On Tue, Aug 13, 2013 at 4:32 AM, MRAB wrote: > On 13/08/2013 04:20, Jason Friedman wrote: >>>>> >>>>> I've always wondered if the 160 character limit or whatever it is is a >>>>> hard limit in their system, or if it's just a variable they could tweak >>>>> if they felt like it. >> >> >> I thought it was 140 characters? >> https://twitter.com/about >> > He did say "or whatever". :-) I don't personally use the service, so I just followed the figure that people were bandying about in this thread. 140 it is, then. ChrisA From ramit.prasad at jpmorgan.com.dmarc.invalid Tue Aug 13 11:34:45 2013 From: ramit.prasad at jpmorgan.com.dmarc.invalid (Prasad, Ramit) Date: Tue, 13 Aug 2013 15:34:45 +0000 Subject: Could you verify this, Oh Great Unicode Experts of the Python-List? In-Reply-To: <52099088.7030300@gmail.com> References: <5207AF15.7040604@mrabarnett.plus.com> <52099088.7030300@gmail.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF474186581AB@SCACMX008.exchad.jpmchase.net> Michael Torrie wrote: > On 08/11/2013 11:54 PM, Gregory Ewing wrote: > > Michael Torrie wrote: > >> I've always wondered if the 160 character limit or whatever it is is a > >> hard limit in their system, or if it's just a variable they could tweak > >> if they felt like it. > > > > Isn't it for compatibility with SMS? Twitter could > > probably change it, but persuading all the cell phone > > networks to change at the same time might be rather > > difficult. > > Yes I think you're correct about it being limited for SMS. > > However I know of no phone or network that won't let you use longer > messages; multiple SMS packets are used and most phone paste them back > together. So no there's nothing that anyone needs to change to use > longer messages if they so chose. It's now just an arbitrary limit, > part of the twitter culture. True, but order of delivery is not guaranteed. I still sometimes get out of order text message when multiple messages are sent at once. ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From steve at pearwood.info Wed Aug 14 02:36:28 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 14 Aug 2013 06:36:28 GMT Subject: Could you verify this, Oh Great Unicode Experts of the Python-List? References: <5207AF15.7040604@mrabarnett.plus.com> <52099088.7030300@gmail.com> Message-ID: <520b256b$0$29885$c3e8da3$5496439d@news.astraweb.com> On Tue, 13 Aug 2013 15:34:45 +0000, Prasad, Ramit wrote: > Michael Torrie wrote: [...] >> However I know of no phone or network that won't let you use longer >> messages; multiple SMS packets are used and most phone paste them back >> together. So no there's nothing that anyone needs to change to use >> longer messages if they so chose. It's now just an arbitrary limit, >> part of the twitter culture. > > > True, but order of delivery is not guaranteed. I still sometimes get out > of order text message when multiple messages are sent at once. SMS delivery is not guaranteed *at all*. It's a best-effort delivery service, which means the telco can drop any SMSes it feels like, for any reason it likes, without notice or notification. -- Steven From steve+comp.lang.python at pearwood.info Sun Aug 11 05:09:44 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 11 Aug 2013 09:09:44 GMT Subject: Could you verify this, Oh Great Unicode Experts of the Python-List? References: Message-ID: <520754d7$0$30000$c3e8da3$5496439d@news.astraweb.com> On Sun, 11 Aug 2013 07:17:42 +0100, Joshua Landau wrote: > Basically, I think Twitter's broken. Oh, in about a million ways, but apparently people like it :-( > For my full discusion on the matter, see: > http://www.reddit.com/r/learnpython/comments/1k2yrn/ help_with_len_and_input_function_33/cbku5e8 > > Here's the first post of mine, ineffectually edited for this list: > > """ > The obvious solution [to getting the length of a tweet] > is wrong. Like, slightly wrong?. > > Given tweet = b"caf\x65\xCC\x81".decode(): I assume you're using Python 3, where UTF-8 is the default encoding. > >>> tweet > 'caf?' > > But: > > >>> len(tweet) > 5 Yes, that's correct. Unicode doesn't promise to have a single unique representation for all human-readable strings. In this case, the string "cafe" with an accent on the "e" can be generated by two sequences of code points: LATIN SMALL LETTER C LATIN SMALL LETTER A LATIN SMALL LETTER F LATIN SMALL LETTER E COMBINING ACUTE ACCENT or LATIN SMALL LETTER C LATIN SMALL LETTER A LATIN SMALL LETTER F LATIN SMALL LETTER E WITH ACUTE The reason some accented letters have single code point forms is to support legacy charsets; the reason some only exist as combining characters is due to the combinational explosion. Some languages allow you to add up to five or six different accent on any of dozens of different letters. If each combination needed its own unique code point, there wouldn't be enough code points. For bonus points, if there are five accents that can be placed in any combination of zero or more on any of four characters, how many code points would be needed? Neither form is "right" or "wrong", they are both equally valid. They encode differently, of course, since UTF-8 does guarantee that every sequence of code points has a unique byte representation: py> tweet.encode('utf-8') 'cafe\xcc\x81' py> u'caf?'.encode('utf-8') 'caf\xc3\xa9' Note that the form you used, b"caf\x65\xCC\x81", is the same as the first except that you have shown "e" in hex for some reason: py> b'\x65' == b'e' True > So the solution is: > > >>> import unicodedata > >>> len(unicodedata.normalize("NFC", tweet)) > 4 In this particular case, this will reduce the tweet to the normalised form that Twitter uses. [...] > After further testing (I don't actually use Twitter) it seems the whole > thing was just smoke and mirrors. The linked article is a lie, at least > on the user's end. Which linked article? The one on dev.twitter.com seems to be okay to me. Of course, they might be lying when they say "Twitter counts the length of a Tweet using the Normalization Form C (NFC) version of the text", I have no idea. But the seem to have a good grasp of the issues involved, and assuming they do what they say, at least Western European users should be happy. > On Linux you can prove this by running: > > >>> p = subprocess.Popen(['xsel', '-bi'], stdin=subprocess.PIPE) > >>> p.communicate(input=b"caf\x65\xCC\x81") > (None, None) > > "cafe?" will be in your Copy-Paste buffer, and you can paste it in to > the tweet-box. It takes 5 characters. So much for testing ;). How do you know that it takes 5 characters? Is that some Javascript widget? I'd blame buggy Javascript before Twitter. If this shows up in your application as cafe? rather than caf?, it is a bug in the text rendering engine. Some applications do not deal with combining characters correctly. (It's a hard problem to solve, and really needs support from the font. In some languages, the same accent will appear in different places depending on the character they are attached to, or the other accents there as well. Or so I've been lead to believe.) > ? https://dev.twitter.com/docs/counting- > characters#Definition_of_a_Character Looks reasonable to me. No obvious errors to my eyes. -- Steven From joshua at landau.ws Sun Aug 11 05:44:40 2013 From: joshua at landau.ws (Joshua Landau) Date: Sun, 11 Aug 2013 10:44:40 +0100 Subject: Could you verify this, Oh Great Unicode Experts of the Python-List? In-Reply-To: <520754d7$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <520754d7$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 11 August 2013 10:09, Steven D'Aprano wrote: > The reason some accented letters have single code point forms is to > support legacy charsets; the reason some only exist as combining > characters is due to the combinational explosion. Some languages allow > you to add up to five or six different accent on any of dozens of > different letters. If each combination needed its own unique code point, > there wouldn't be enough code points. For bonus points, if there are five > accents that can be placed in any combination of zero or more on any of > four characters, how many code points would be needed? 52? > Note that the form you used, b"caf\x65\xCC\x81", is the same as the first > except that you have shown "e" in hex for some reason: > > py> b'\x65' == b'e' > True Yeah.. I did that because the linked post did it. I'm not sure why either ;). > On Sun, 11 Aug 2013 07:17:42 +0100, Joshua Landau wrote: >> >> So the solution is: >> >> >>> import unicodedata >> >>> len(unicodedata.normalize("NFC", tweet)) >> 4 > > In this particular case, this will reduce the tweet to the normalised > form that Twitter uses. > > [...] >> After further testing (I don't actually use Twitter) it seems the whole >> thing was just smoke and mirrors. The linked article is a lie, at least >> on the user's end. > > Which linked article? The one on dev.twitter.com seems to be okay to me. That's the one. > Of course, they might be lying when they say "Twitter counts the length > of a Tweet using the Normalization Form C (NFC) version of the text", I > have no idea. But the seem to have a good grasp of the issues involved, > and assuming they do what they say, at least Western European users > should be happy. They *don't* seem to be doing what they say. >> On Linux you can prove this by running: >> >> >>> p = subprocess.Popen(['xsel', '-bi'], stdin=subprocess.PIPE) >> >>> p.communicate(input=b"caf\x65\xCC\x81") >> (None, None) >> >> "cafe?" will be in your Copy-Paste buffer, and you can paste it in to >> the tweet-box. It takes 5 characters. So much for testing ;). > > How do you know that it takes 5 characters? Is that some Javascript > widget? I'd blame buggy Javascript before Twitter. I go to twitter.com, log in and press that odd blue compose button in the top-right. After pasting at says I have 135 (down from 140) characters left. My only question here is, since you can't post after 140 non-normalised characters, who cares if the server counts it as less? > If this shows up in your application as cafe? rather than caf?, it is a > bug in the text rendering engine. Some applications do not deal with > combining characters correctly. Why the rendering engine? > (It's a hard problem to solve, and really needs support from the font. In > some languages, the same accent will appear in different places depending > on the character they are attached to, or the other accents there as > well. Or so I've been lead to believe.) > > >> ? https://dev.twitter.com/docs/counting- >> characters#Definition_of_a_Character > > Looks reasonable to me. No obvious errors to my eyes. *Not sure whether talking about the link or my post* From steve+comp.lang.python at pearwood.info Sun Aug 11 07:14:52 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 11 Aug 2013 11:14:52 GMT Subject: Could you verify this, Oh Great Unicode Experts of the Python-List? References: <520754d7$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5207722c$0$30000$c3e8da3$5496439d@news.astraweb.com> On Sun, 11 Aug 2013 10:44:40 +0100, Joshua Landau wrote: > On 11 August 2013 10:09, Steven D'Aprano > wrote: >> The reason some accented letters have single code point forms is to >> support legacy charsets; the reason some only exist as combining >> characters is due to the combinational explosion. Some languages allow >> you to add up to five or six different accent on any of dozens of >> different letters. If each combination needed its own unique code >> point, there wouldn't be enough code points. For bonus points, if there >> are five accents that can be placed in any combination of zero or more >> on any of four characters, how many code points would be needed? > > 52? More than double that. Consider a single character. It can have 0 to 5 accents, in any combination. Order doesn't matter, and there are no duplicates, so there are: 0 accent: take 0 from 5 = 1 combination; 1 accent: take 1 from 5 = 5 combinations; 2 accents: take 2 from 5 = 5!/(2!*3!) = 10 combinations; 3 accents: take 3 from 5 = 5!/(3!*2!) = 10 combinations; 4 accents: take 4 from 5 = 5 combinations; 5 accents: take 5 from 5 = 1 combination giving a total of 32 combinations for a single character. Since there are four characters in this hypothetical language that take accents, that gives a total of 4*32 = 128 distinct code points needed. In reality, Unicode has currently code points U+0300 to U+036F (112 code points) to combining characters. It's not really meaningful to combine all 112 of them, or even most of 112 of them, but let's assume that we can legitimately combine up to three of them on average (some languages will allow more, some less) on just six different letters. That gives us: 0 accent: 1 combination 1 accent: 112 combinations 2 accents: 112!/(2!*110!) = 6216 combinations 3 accents: 112!/(3!*109!) = 227920 combinations giving 234249 combinations, by six base characters, = 1405494 code points. Which is comfortably more than the 1114112 code points Unicode has in total :-) This calculation is horribly inaccurate, since you can't arbitrarily combine (say) accents from Greek with accents from IPA, but I reckon that the combinational explosion of accented letters is still real. [...] >> Of course, they might be lying when they say "Twitter counts the length >> of a Tweet using the Normalization Form C (NFC) version of the text", I >> have no idea. But the seem to have a good grasp of the issues involved, >> and assuming they do what they say, at least Western European users >> should be happy. > > They *don't* seem to be doing what they say. [...] >>> "cafe?" will be in your Copy-Paste buffer, and you can paste it in to >>> the tweet-box. It takes 5 characters. So much for testing ;). >> >> How do you know that it takes 5 characters? Is that some Javascript >> widget? I'd blame buggy Javascript before Twitter. > > I go to twitter.com, log in and press that odd blue compose button in > the top-right. After pasting at says I have 135 (down from 140) > characters left. I'm pretty sure that will be a piece of Javascript running in your browser that reports the number of characters in the text box. So, I would expect that either: - Javascript doesn't provide a way to normalize text; - Twitter's Javascript developer(s) don't know how to normalize text, or can't be bothered to follow company policy (shame on them); - the Javascript just asks the browser, and the browser doesn't know how to count characters the Twitter way; etc. But of course posting to Twitter via your browser isn't the only way to post. Twitter provide an API to twit, and *that* is the ultimate test of whether Twitter's dev guide is lying or not. > My only question here is, since you can't post after 140 non-normalised > characters, who cares if the server counts it as less? People who bypass the browser and write their own Twitter client. >> If this shows up in your application as cafe? rather than caf?, it is a >> bug in the text rendering engine. Some applications do not deal with >> combining characters correctly. > > Why the rendering engine? If the text renderer assumes it can draw once code point at a time, it will draw the "e", then reach the combining accent. It could, in principle, backspace and draw it over the "e", but more likely it will just draw it next to it. What the renderer should do is walk the string, collecting characters until it reaches one which is not a combining character, then draw them all at once one on top of each other. A good font may have special glyphs, or at least hints, for combining accents. For instance, if you have a dot accent and a comma accent drawn one on top of the other, it looks like a comma; what you are supposed to do is move them side by side, so you have separate dot and comma glyphs. >> (It's a hard problem to solve, and really needs support from the font. >> In some languages, the same accent will appear in different places >> depending on the character they are attached to, or the other accents >> there as well. Or so I've been lead to believe.) >> >> >>> ? https://dev.twitter.com/docs/counting- >>> characters#Definition_of_a_Character >> >> Looks reasonable to me. No obvious errors to my eyes. > > *Not sure whether talking about the link or my post* The dev.twitter.com post. -- Steven From rosuav at gmail.com Sun Aug 11 07:45:41 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 11 Aug 2013 12:45:41 +0100 Subject: Could you verify this, Oh Great Unicode Experts of the Python-List? In-Reply-To: <5207722c$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <520754d7$0$30000$c3e8da3$5496439d@news.astraweb.com> <5207722c$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Aug 11, 2013 at 12:14 PM, Steven D'Aprano wrote: > Consider a single character. It can have 0 to 5 accents, in any > combination. Order doesn't matter, and there are no duplicates, so there > are: > > 0 accent: take 0 from 5 = 1 combination; > 1 accent: take 1 from 5 = 5 combinations; > 2 accents: take 2 from 5 = 5!/(2!*3!) = 10 combinations; > 3 accents: take 3 from 5 = 5!/(3!*2!) = 10 combinations; > 4 accents: take 4 from 5 = 5 combinations; > 5 accents: take 5 from 5 = 1 combination > > giving a total of 32 combinations for a single character. Since there are > four characters in this hypothetical language that take accents, that > gives a total of 4*32 = 128 distinct code points needed. There's an easy way to calculate it. Instead of the "take N from 5" notation, simply look at it as a set of independent bits - each of your accents may be either present or absent. So it's 1<<5 combinations for a single character, which is the same 32 figure you came up with, but easier to work with in the ridiculous case. > In reality, Unicode has currently code points U+0300 to U+036F (112 code > points) to combining characters. It's not really meaningful to combine > all 112 of them, or even most of 112 of them... If you *were* to use literally ANY combination, that would be 1<<112 which is... uhh... five billion yottacombinations. Don't bother working that one out by the "take N" method, it'll take you too long :) Oh, and that's 1<<112 possible combining character combinations, so you then need to multiply that by the number of base characters you could use.... ChrisA From joshua at landau.ws Sun Aug 11 07:59:09 2013 From: joshua at landau.ws (Joshua Landau) Date: Sun, 11 Aug 2013 12:59:09 +0100 Subject: Could you verify this, Oh Great Unicode Experts of the Python-List? In-Reply-To: <5207722c$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <520754d7$0$30000$c3e8da3$5496439d@news.astraweb.com> <5207722c$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 11 August 2013 12:14, Steven D'Aprano wrote: > On Sun, 11 Aug 2013 10:44:40 +0100, Joshua Landau wrote: > >> On 11 August 2013 10:09, Steven D'Aprano >> wrote: >>> The reason some accented letters have single code point forms is to >>> support legacy charsets; the reason some only exist as combining >>> characters is due to the combinational explosion. Some languages allow >>> you to add up to five or six different accent on any of dozens of >>> different letters. If each combination needed its own unique code >>> point, there wouldn't be enough code points. For bonus points, if there >>> are five accents that can be placed in any combination of zero or more >>> on any of four characters, how many code points would be needed? >> >> 52? > > More than double that. > > Consider a single character. It can have 0 to 5 accents, in any > combination. Order doesn't matter, and there are no duplicates, so there > are: > > 0 accent: take 0 from 5 = 1 combination; > 1 accent: take 1 from 5 = 5 combinations; > 2 accents: take 2 from 5 = 5!/(2!*3!) = 10 combinations; > 3 accents: take 3 from 5 = 5!/(3!*2!) = 10 combinations; > 4 accents: take 4 from 5 = 5 combinations; > 5 accents: take 5 from 5 = 1 combination > > giving a total of 32 combinations for a single character. Since there are > four characters in this hypothetical language that take accents, that > gives a total of 4*32 = 128 distinct code points needed. I didn't see "four characters", and I did (1 + 5 + 10) * 2 and came up with 52... Maybe I should get more sleep. From joshua at landau.ws Tue Aug 13 04:40:11 2013 From: joshua at landau.ws (Joshua Landau) Date: Tue, 13 Aug 2013 09:40:11 +0100 Subject: Could you verify this, Oh Great Unicode Experts of the Python-List? In-Reply-To: <5207722c$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <520754d7$0$30000$c3e8da3$5496439d@news.astraweb.com> <5207722c$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 11 August 2013 12:14, Steven D'Aprano wrote: > On Sun, 11 Aug 2013 10:44:40 +0100, Joshua Landau wrote: >>>> "cafe?" will be in your Copy-Paste buffer, and you can paste it in to >>>> the tweet-box. It takes 5 characters. So much for testing ;). >>> >>> How do you know that it takes 5 characters? Is that some Javascript >>> widget? I'd blame buggy Javascript before Twitter. >> >> I go to twitter.com, log in and press that odd blue compose button in >> the top-right. After pasting at says I have 135 (down from 140) >> characters left. > > I'm pretty sure that will be a piece of Javascript running in your > browser that reports the number of characters in the text box. So, I > would expect that either: > > - Javascript doesn't provide a way to normalize text; > > - Twitter's Javascript developer(s) don't know how to normalize text, or > can't be bothered to follow company policy (shame on them); > > - the Javascript just asks the browser, and the browser doesn't know how > to count characters the Twitter way; > > etc. But of course posting to Twitter via your browser isn't the only way > to post. Twitter provide an API to twit, and *that* is the ultimate test > of whether Twitter's dev guide is lying or not. Well, I've done some further testing and it seems you're right. It's just the javascript that's wrong. I guess they did it for better load-times. From wxjmfauth at gmail.com Sun Aug 11 08:51:05 2013 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Sun, 11 Aug 2013 05:51:05 -0700 (PDT) Subject: Could you verify this, Oh Great Unicode Experts of the Python-List? In-Reply-To: <520754d7$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <520754d7$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: Le dimanche 11 ao?t 2013 11:09:44 UTC+2, Steven D'Aprano a ?crit?: > On Sun, 11 Aug 2013 07:17:42 +0100, Joshua Landau wrote: > > > > > The reason some accented letters have single code point forms is to > > support legacy charsets; ... No. jmf PS Unicode normalization is failing expectedly very well with the FSR. From joshua at landau.ws Sun Aug 11 09:07:00 2013 From: joshua at landau.ws (Joshua Landau) Date: Sun, 11 Aug 2013 14:07:00 +0100 Subject: Could you verify this, Oh Great Unicode Experts of the Python-List? In-Reply-To: References: <520754d7$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 11 August 2013 13:51, wrote: > Le dimanche 11 ao?t 2013 11:09:44 UTC+2, Steven D'Aprano a ?crit : >> On Sun, 11 Aug 2013 07:17:42 +0100, Joshua Landau wrote: >> >> The reason some accented letters have single code point forms is to >> support legacy charsets; ... > > No. > > jmf > > PS Unicode normalization is failing expectedly very well > with the FSR. No. Joshua Landau PS Proper arguments are falling expectedly very well with the internet From i.am.songoku at gmail.com Sun Aug 11 04:17:41 2013 From: i.am.songoku at gmail.com (Krishnan Shankar) Date: Sun, 11 Aug 2013 13:47:41 +0530 Subject: Introduction to my fellow Python Friends Message-ID: Hi Friends, I would like to introduce myself. I am Krishnan from Chennai, India. I am using python for 2 years for Test Automation. I am fascinated by the language and its capabilities. I am willing to move into Python development and I am doing the best i can to learn the language completely and contribute to open source. I figured out that the best way is to talk to the experts and so i subscribed to this mailing list. It will be cool if anybody can help me out by telling the etiquette of this mailing list, like 1. How to acknowledge a reply? Should i put a one to one mail or send it to the mailing list itself? 2. How can i see or get a question asked by someone else? (So that i can reply for that with my best possible knowledge. I currently get as Python mail Digest) 3. How can i use this mailing list in the best possible way? I hope to have a wonderful time with Python here. I hope i am not wasting your time. Sorry for the inconvenience if i am. Regards, Krishnan -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Sun Aug 11 05:31:33 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 11 Aug 2013 10:31:33 +0100 Subject: Introduction to my fellow Python Friends In-Reply-To: References: Message-ID: On Sun, Aug 11, 2013 at 9:17 AM, Krishnan Shankar wrote: > I figured out that the best way is to talk to the experts and so i > subscribed to this mailing list. It will be cool if anybody can help me out > by telling the etiquette of this mailing list, like Hi! Welcome! > 1. How to acknowledge a reply? Should i put a one to one mail or send it to > the mailing list itself? Normally, you needn't simply "acknowledge" a reply; but if you're responding in a way that will be beneficial to the list, you would want to send to the list. > 2. How can i see or get a question asked by someone else? (So that i can > reply for that with my best possible knowledge. I currently get as Python > mail Digest) Now that you're subscribed, you should be able to read every message sent to the list (modulo spam filtering, but ignore that). Other threads should come up just fine > 3. How can i use this mailing list in the best possible way? First and foremost, don't use Google Groups :) But you're already doing that. Quote people's text with angle-bracket markers (see my quoting of your text), and put your responses underneath (called "bottom-posting" - the opposite is "top-posting" and is not preferred). Trim quoted text to just what you need for context, rather than quoting an entire conversation. Always cite people accurately; normally your mail client will handle that for you, but when you trim, sometimes you'll need to be careful of that. Don't accidentally cross-post from savoynet at bridgewater.edu, like I've been known to do *whistles innocently* Be courteous, even if someone flames you :) And post real content, with real meaning. Even if you ignore all else, do this and you'll be valuable to the community, and people will forgive (to an extent!) failures in the other areas. ChrisA From davea at davea.name Sun Aug 11 09:37:49 2013 From: davea at davea.name (Dave Angel) Date: Sun, 11 Aug 2013 13:37:49 +0000 (UTC) Subject: Introduction to my fellow Python Friends References: Message-ID: Krishnan Shankar wrote: > Hi Friends, Hi, and welcome to the mailing list. > I figured out that the best way is to talk to the experts and so i > subscribed to this mailing list. It will be cool if anybody can help me out > by telling the etiquette of this mailing list, like > > 1. How to acknowledge a reply? Should i put a one to one mail or send it to > the mailing list itself? Acknowledgement is not usually necessary. Sometimes you want to make a special thanks, with no useful information for others. In that case, an email to the individual is called for. But in most cases, you should do a "reply" to the list, and not to the individual. See below for caveats on what I mean by reply. > 2. How can i see or get a question asked by someone else? (So that i can > reply for that with my best possible knowledge. I currently get as Python > mail Digest) The digest is a combination of everything posted to the list for some period of time. Its title is generic, and replies to it are not threaded into the list in a meaningful way. If your email program can handle it, consider turning off the digest feature. if your email program cannot handle it, consider using a different way to access the list. What I used to do with a digest was to have my email program treat the indivdual messages in the digest as attachments, and reply to the attachment itself. Then I discovered that by turning off digest and telling my email program to track threads, I could more easily place each message in context. Then I realized that my email program could treat the list as a newsgroup (which is what it really is), and not affect the way I accessed it very much. Finally, I switched to a dedicated newsreader. In order to relate messages that are intended to be threaded together, you want to use the same subject line (Notice that my newsreader added the letters "Re:" in front of your subject line). Don't post a message with the subject like: Re: Python-list Digest, Vol 119, Issue 37 Even better, you want to actually reply-all to the individual message. This puts some invisible stuff in the header that makes it practical for savvy newsreaders & mailreaders to see just where in the thread you're replying. Better still, you use a Reply-list, which sends to the list only, not to the individual. You can do that with reply-all, just by removing the extra name(s). > 3. How can i use this mailing list in the best possible way? Don't send HTML mails (as you have here, see a little nonsense at the end which I left in for your edification). In many cases, it'll distort your response, either because of the encoding in your mail program, or the decoding at our end. A text forum should get only text messages, where what you send is what we see. This also means we don't see colors, special fonts, or funny formatting. If you're using email, specify TEXT email. It also saves space in every message, and for those of us paying per megabyte, it can add up. Don't use tabs. Indent your code by 4 column intervals, represented by spaces. If you use tabs, some people won't see any indentation, others will see one column, and others will see 8 columns. Do pick a good subject line (as you have here). "Help" isn't a good subject, and neither is "urgent problem". Describe something about what's not working for you, even if you turn out to guess wrong. Do specify your environment with your original post on any thread. You may not think the problem you're having with input() is related to which OS you're using AND which version of Python, but it probably does. And for other problems there can at least be subtle differences. So at a minimum, say "I'm running Python 3.3 on Linux" or whatever. Finally, show your code, explain what you expected to happen, and be explicit about what did happen, preferably by copy/pasting the responses. If there's an error, post the entire traceback. > I hope to have a wonderful time with Python here. I hope i am not wasting > your time. Sorry for the inconvenience if i am. No inconvenience. A real pleasure to see someone who explicitly wants to fit in. Most do, but assume that it'll be automatic. Following is a little piece of the html in the original message. > >
Hi Friends,

I would like > -- DaveA From cs at zip.com.au Mon Aug 12 00:37:12 2013 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 12 Aug 2013 14:37:12 +1000 Subject: Introduction to my fellow Python Friends In-Reply-To: References: Message-ID: <20130812043712.GA72641@cskk.homeip.net> On 11Aug2013 13:47, Krishnan Shankar wrote: | 1. How to acknowledge a reply? Should i put a one to one mail or send it to | the mailing list itself? Generally, a personal acknowledgement email is not necessary; usually one would reply to the list; by citing the previous message author (as I have cited you, above), one acknowledges the help. If the help was unusually generous or insightful, of course we sometimes say "special thanks to Bill The Clever for pointing out X or Y", or something like that. Feel free. Again, unless there is special reason not to, reply to the list. That way everyone benefits from your response, and any special thanks you may have added is visible to all. There are sometimes reasons to not reply to the list: wandering well off topic - away from Python and Python-related things, or discussing genuinely confidential stuff. If you take the whole discussion off list, a note to the list that you've done so and why may be appropriate. Use your own judgement there. | 2. How can i see or get a question asked by someone else? (So that i can | reply for that with my best possible knowledge. I currently get as Python | mail Digest) Personally, I strongly recomment getting the list as individual messages. If the volume bothers you, I suggested having your mail program file the list messages to a special folder. That keeps them out of your inbox and lets you visit that folder for some one-on-one Python time. Having the messages distinct has several advantages: you can easily reply to a specific message and also your mailer can group all the messages of a particular discussion together, making it far far easier to follow discussions and conversely to delete or archive the uninteresting discussions. | 3. How can i use this mailing list in the best possible way? | I hope to have a wonderful time with Python here. I hope i am not wasting | your time. Sorry for the inconvenience if i am. Not at all. Cheers, -- Cameron Simpson I have no help to send, therefore I must go myself. - Aragorn son of Arathorn From the_isz at gmx.de Sun Aug 11 08:05:11 2013 From: the_isz at gmx.de (Timo Schmiade) Date: Sun, 11 Aug 2013 14:05:11 +0200 Subject: Reading from stdin first, then use curses Message-ID: <20130811120511.GA8223@Discworld.fritz.box> Hi all, I wrote a replacement for urlview to properly extract URLs from emails. You can find the first draft here: https://github.com/the-isz/pyurlview When I call it with an email file passed to the '-f' argument, it does pretty much what I want already. However, I intend to use it in mutt, which pipes the message to the program like so: macro pager \cu 'pyurlview.py' 'Follow links with pyurlview' The problem is rather obvious but - unfortunately - not so easy to solve: * The program reads the mail from stdin * The terminal in which it runs is a pseudo-terminal (pipe) * curses is not able to accept user input from the pseudo-terminal The question is: How do I read from stdin first and afterwards allow curses to read user input? Thanks in advance and kind regards, Timo From timo.schmiade at gmx.de Sat Aug 31 04:31:03 2013 From: timo.schmiade at gmx.de (Timo Schmiade) Date: Sat, 31 Aug 2013 10:31:03 +0200 Subject: Reading from stdin first, then use curses In-Reply-To: <20130811120511.GA8223@Discworld.fritz.box> References: <20130811120511.GA8223@Discworld.fritz.box> Message-ID: <20130831083103.GA570@Discworld.fritz.box> Hi again, sorry for replying to my own mail, but is there really no solution? Can curses really not be used in this situation? Thanks again, Timo On Sun, Aug 11, 2013 at 02:05:11PM +0200, Timo Schmiade wrote: > Hi all, > > I wrote a replacement for urlview to properly extract URLs from emails. > You can find the first draft here: > > https://github.com/the-isz/pyurlview > > When I call it with an email file passed to the '-f' argument, it does > pretty much what I want already. However, I intend to use it in mutt, > which pipes the message to the program like so: > > macro pager \cu 'pyurlview.py' 'Follow links with pyurlview' > > The problem is rather obvious but - unfortunately - not so easy to solve: > > * The program reads the mail from stdin > * The terminal in which it runs is a pseudo-terminal (pipe) > * curses is not able to accept user input from the pseudo-terminal > > The question is: > > How do I read from stdin first and afterwards allow curses to read user > input? > > Thanks in advance and kind regards, > > Timo From krismesenbrink at gmail.com Sun Aug 11 23:33:47 2013 From: krismesenbrink at gmail.com (Kris Mesenbrink) Date: Sun, 11 Aug 2013 20:33:47 -0700 (PDT) Subject: back with more issues Message-ID: import random def player(): hp = 10 speed = 5 attack = random.randint(0,5) def monster (): hp = 10 speed = 4 def battle(player): print ("a wild mosnter appered!") print ("would you like to battle?") answer = input() if answer == ("yes"): return player(attack) else: print("nope") battle() ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ this was a variation on a code that you guys already had helped me with,in the long run i plan to incorporate them together but as it stand i don't know how to call a specific variable from one function (attack from player) to use in another function (battle). what i want is to be able to use the variables from both player and monster to use in battle. any idea's? From joel.goldstick at gmail.com Mon Aug 12 00:21:03 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 12 Aug 2013 00:21:03 -0400 Subject: back with more issues In-Reply-To: References: Message-ID: On Sun, Aug 11, 2013 at 11:33 PM, Kris Mesenbrink wrote: > import random > > def player(): > hp = 10 > speed = 5 > attack = random.randint(0,5) # add the following line to return attack value: return attack > > def monster (): > hp = 10 > speed = 4 > > def battle(player): > print ("a wild mosnter appered!") > print ("would you like to battle?") > answer = input() > if answer == ("yes"): you don't need the parentheses around "yes" > return player(attack) you can't do that above because you defined the function with no parameters. If you alter player to return attack you can alter the above line to: return player() > else: > print("nope") Its a bad idea to have a function return something down one path (If True), then return nothing down another path. > > > battle() > > > ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > > this was a variation on a code that you guys already had helped me with,in the long run i plan to incorporate them together but as it stand i don't know how to call a specific variable from one function (attack from player) to use in another function (battle). what i want is to be able to use the variables from both player and monster to use in battle. any idea's? I wrote some quick changes above to give you what you want. But you need to understand more about functions. Your player function does next to nothing. It defines two variables to fixed values, then gets a random number and returns it. Can you try to explain what you think each of your functions is doing? Every line should serve a purpose, or it shouldn't be in the function. > -- > http://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com From krismesenbrink at gmail.com Mon Aug 12 00:35:24 2013 From: krismesenbrink at gmail.com (Kris Mesenbrink) Date: Sun, 11 Aug 2013 21:35:24 -0700 (PDT) Subject: back with more issues In-Reply-To: References: Message-ID: the idea was to store variables for later use, but you are correct i don't understand functions or if that is even the best way to do it. i guess i'd want to be able to call the HP and ATTACK variables of player for when the battle gets called. i would then use the variables in battle to figure out who would win. is there a better way to store these variables in the functions? i also read somewhere about classes but that makes even less sense to me. From joel.goldstick at gmail.com Mon Aug 12 00:51:28 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 12 Aug 2013 00:51:28 -0400 Subject: back with more issues In-Reply-To: References: Message-ID: On Mon, Aug 12, 2013 at 12:35 AM, Kris Mesenbrink wrote: > the idea was to store variables for later use, but you are correct i don't understand functions or if that is even the best way to do it. i guess i'd want to be able to call the HP and ATTACK variables of player for when the battle gets called. i would then use the variables in battle to figure out who would win. is there a better way to store these variables in the functions? i also read somewhere about classes but that makes even less sense to me. > -- > http://mail.python.org/mailman/listinfo/python-list I'm not sure where you are learning. I really recommend you go to python.org and go the the section on tutorials. (http://wiki.python.org/moin/BeginnersGuide) They have some really good stuff there to explain basics. Coding is fun, but until you understand basic concepts its really more magic than anything else. Also, you may get better help in the python-tutor group. -- Joel Goldstick http://joelgoldstick.com From davea at davea.name Mon Aug 12 01:54:38 2013 From: davea at davea.name (Dave Angel) Date: Mon, 12 Aug 2013 05:54:38 +0000 (UTC) Subject: back with more issues References: Message-ID: Kris Mesenbrink wrote: > import random > > def player(): > hp = 10 > speed = 5 > attack = random.randint(0,5) > The net resut of this function is nothing. It assigns values, then they're lost when the function returns. A function is the wrong way to deal with these three names. > def monster (): > hp = 10 > speed = 4 Same here. > > def battle(player): You probably want to have two parameters, player and monster > print ("a wild mosnter appered!") > print ("would you like to battle?") > answer = input() > if answer == ("yes"): > return player(attack) > else: > print("nope") This function makes no sense to me. A function should have three well-defined pieces: what are its parameters, what does it do, what are its side-effects, and what does it return. This function is confusing on all of those. > > > battle() > > > ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > > this was a variation on a code that you guys already had helped me with,in the long run i plan to incorporate them together but as it stand i don't know how to call a specific variable from one function (attack from player) to use in another function (battle). what i want is to be able to use the variables from both player and monster to use in battle. any idea's? What you should want is a class for each type of character. At its simplest, the class can be a storage place for related named attributes. You could make a class Player, which defines attributes called hp, speed, and attack. Then later on you can refer to one of those attributes with synatax like james.attack class Player: def __init__(self): self.hp = 10 self.speed = 5 self.attack = random.randint(0,5) Now, you create a player by james = Player() and the monster by behemoth = Monster() and you pass them into the function battle, by result = battle(james, behemoth) Inside the function, you'd say player.attack to see that random value. And monster.speed to see behemoth's speed. -- DaveA From krismesenbrink at gmail.com Mon Aug 12 02:31:32 2013 From: krismesenbrink at gmail.com (Kris Mesenbrink) Date: Sun, 11 Aug 2013 23:31:32 -0700 (PDT) Subject: back with more issues In-Reply-To: References: Message-ID: <441b03e6-4280-4abf-9987-bb4aea81ae6d@googlegroups.com> darn i was hoping i could put off learning classes for a bit, but it seems that is not the case. i have tested it a bit and it seems to be working correctly now. ++++++++++++++++++++++++++++ import random class player(): hp = 10 speed = 5 attack = random.randint(0,5) print (player.attack) +++++++++++++++++++++++++++++++++++ i know it's not nearly as complicated as your examples but it seems to work. the self part of it always eluded me and continues to do so. and just so you know im learning through codecademy.com , it's based on python 2.7 and im trying to code in 3.3. but thanks for your help again and classes are starting (i think) to make some sort of sense.i'll have to reread both replies over and over again but it looks like a lot of useful info is there. but is the example i posted sorta right? i know i left the self part out but i think im on the right track. From krismesenbrink at gmail.com Mon Aug 12 02:57:33 2013 From: krismesenbrink at gmail.com (Kris Mesenbrink) Date: Sun, 11 Aug 2013 23:57:33 -0700 (PDT) Subject: back with more issues In-Reply-To: <441b03e6-4280-4abf-9987-bb4aea81ae6d@googlegroups.com> References: <441b03e6-4280-4abf-9987-bb4aea81ae6d@googlegroups.com> Message-ID: <180b67b6-b69a-43d5-a7f4-f5d63ce66479@googlegroups.com> import random class player(): hp = 10 attack = random.randint(0,5) class monster(): hp = 10 attack = random.randint(0,4) def battle(): print ("a wild mosnter appered!") print ("would you like to battle?") answer = input() if answer == ("yes"): while monster.hp >=0: print ("you do", player.attack, "damage") monster.hp -= player.attack print (monster.hp) elif answer == ("no"): print ("you run away") else: print("you stand there") battle() Hello! just wanted to show you guys how its coming together, im starting to understand it abit more (hopefully it's right) at the moment it seems to only roll the attack once and uses that value but that's another issue all together that i bother you with (yet anyway). thanks again guys you are awesome From davea at davea.name Mon Aug 12 03:11:43 2013 From: davea at davea.name (Dave Angel) Date: Mon, 12 Aug 2013 07:11:43 +0000 (UTC) Subject: back with more issues References: <441b03e6-4280-4abf-9987-bb4aea81ae6d@googlegroups.com> Message-ID: Kris Mesenbrink wrote: > darn i was hoping i could put off learning classes for a bit, but it seems that is not the case. i have tested it a bit and it seems to be working correctly now. > > ++++++++++++++++++++++++++++ > import random > > class player(): > hp = 10 > speed = 5 > attack = random.randint(0,5) > > print (player.attack) > > +++++++++++++++++++++++++++++++++++ > > i know it's not nearly as complicated as your examples but it seems to work. the self part of it always eluded me and continues to do so. and just so you know im learning through codecademy.com , it's based on python 2.7 and im trying to code in 3.3. but thanks for your help again and classes are starting (i think) to make some sort of sense.i'll have to reread both replies over and over again but it looks like a lot of useful info is there. but is the example i posted sorta right? i know i left the self part out but i think im on the right track. The "complication" was there for good reason. If you are sure you'll never have more than one player, this could work. i don't see the advantage over (ugh) global variables, however. But what happens when you have four monsters instead of one? A class provides you a way to store data for each instance, not just for the class as a whole. And the self convention is kind of analogous to the English "myself." If you're inside an ordinary method, you refer to yourself as "self." By the way, by convention, class names are capitalized. That's why i called it Player. -- DaveA From sg552 at hotmail.co.uk Mon Aug 12 10:56:23 2013 From: sg552 at hotmail.co.uk (Rotwang) Date: Mon, 12 Aug 2013 15:56:23 +0100 Subject: back with more issues In-Reply-To: References: Message-ID: On 12/08/2013 06:54, Dave Angel wrote: > [...] > > This function makes no sense to me. A function should have three > well-defined pieces: what are its parameters, what does it do, what are > its side-effects, and what does it return. No! A function should have *four* well-defined pieces: what are its parameters, what does it do, what are its side-effects, what does it return, and an almost fanatical devotion to the Pope [etc.] From random832 at fastmail.us Mon Aug 12 15:46:12 2013 From: random832 at fastmail.us (random832 at fastmail.us) Date: Mon, 12 Aug 2013 15:46:12 -0400 Subject: back with more issues In-Reply-To: References: Message-ID: <1376336772.29332.8988483.06977FC7@webmail.messagingengine.com> On Mon, Aug 12, 2013, at 10:56, Rotwang wrote: > No! A function should have *four* well-defined pieces: what are its > parameters, what does it do, what are its side-effects, what does it > return, and an almost fanatical devotion to the Pope [etc.] To be fair, I can't think of what "what does it do" includes other than the side-effects and the return. From krismesenbrink at gmail.com Mon Aug 12 23:13:31 2013 From: krismesenbrink at gmail.com (Kris Mesenbrink) Date: Mon, 12 Aug 2013 20:13:31 -0700 (PDT) Subject: back with more issues In-Reply-To: References: Message-ID: <88f95aab-0274-48ee-a68a-14a8c3b36e9c@googlegroups.com> the Classes and __init__ still don't make much sense actually. i have tried and tried again to make it generate numbers between 0 and 5 in a while statement but it just doesn't seem to be working. import random class Player(): hp = 10 def __init__(self, patt): self.att = random.randint(0,5) while Player.hp == 10: print (Player.__init__) atm it seems to be printing "" over and over again, i don't mind the repetition but from my understanding there should be numbers there. numbers that change. crazy frustrating that i just don't understand how this works. From python at mrabarnett.plus.com Mon Aug 12 23:31:15 2013 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 13 Aug 2013 04:31:15 +0100 Subject: back with more issues In-Reply-To: <88f95aab-0274-48ee-a68a-14a8c3b36e9c@googlegroups.com> References: <88f95aab-0274-48ee-a68a-14a8c3b36e9c@googlegroups.com> Message-ID: <5209A883.4060309@mrabarnett.plus.com> On 13/08/2013 04:13, Kris Mesenbrink wrote: > the Classes and __init__ still don't make much sense actually. i have tried and tried again to make it generate numbers between 0 and 5 in a while statement but it just doesn't seem to be working. > > import random > > > class Player(): This sets an attribute of the class: > hp = 10 This method will be called to initialise an instance of the class when one is created: > def __init__(self, patt): This sets an attribute of the instance: > self.att = random.randint(0,5) > > > > while Player.hp == 10: This prints the __init__ method of the class: > print (Player.__init__) > > atm it seems to be printing "" over and over again, i don't mind the repetition but from my understanding there should be numbers there. numbers that change. crazy frustrating that i just don't understand how this works. > At no point does it create an instance of the class, so the __init__ method is never called. You can't return anything from the __init__ method because it's called just to initialise the instance. From steve at pearwood.info Tue Aug 13 02:22:53 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 13 Aug 2013 06:22:53 GMT Subject: back with more issues References: <88f95aab-0274-48ee-a68a-14a8c3b36e9c@googlegroups.com> Message-ID: <5209d0bd$0$29885$c3e8da3$5496439d@news.astraweb.com> On Mon, 12 Aug 2013 20:13:31 -0700, Kris Mesenbrink wrote: > the Classes and __init__ still don't make much sense actually. i have > tried and tried again to make it generate numbers between 0 and 5 in a > while statement but it just doesn't seem to be working. Hi Kris, You might also find that the tutor mailing list is a better match for your status as a complete beginner to Python. You can subscribe to it here: http://mail.python.org/mailman/listinfo/tutor If you do, please take my advice and use individual emails, not daily digests. It is MUCH easier to carry on a back-and-forth conversation with individual emails. But for now, you seem to have misunderstood what your code is doing. Let's start with the basics: The method __init__ is automatically called by Python when you create a new instance. You almost never need to call it by hand, so 99% of the time, if you're writing something like "Player.__init__", you're probably making a mistake. But when you do want to call a method -- and remember, you're not manually calling __init__ -- you need to put round brackets (parentheses for Americans) after the method name. So you would say something like: Player.__init__(arguments go inside here) rather than just Player.__init__. Without the parentheses, you're just referring to the method, not actually calling it. And without the right number and type of arguments, you'll get an error. So how do you create a new Player? Easy -- you just call the *class*, as if it were a function: fred = Player() barney = Player() wilma = Player() betty = Player() Take note of the round brackets. If you leave them out, each of fred, barney, wilma, betty will be aliases to the Player class, rather than separate players. So that's the first thing. Now, another potential problem. Your class starts off like this: class Player(): hp = 10 ...more code follows What this does is set a class-wide attribute called "hp", which every instance shares. Does this matter? Maybe not, it depends on how you use it. Your sample code doesn't show enough to tell if it will be a problem or not. Next, you write this: > while Player.hp == 10: > print (Player.__init__) but since nothing in the loop changes the value of Player.hp, this will loop forever, or until you interrupt it. So I'm not really sure what you actually intend to do. My guess is that what you actually want is something like this: for i in range(10): player = Player() print("Player", i, "has value", player.attr) This ought to get you started. -- Steven From mauriceling at gmail.com Mon Aug 12 03:07:56 2013 From: mauriceling at gmail.com (mauriceling@acm.org) Date: Mon, 12 Aug 2013 00:07:56 -0700 (PDT) Subject: [ANN] New article: Message-ID: <25effac2-c597-4dc2-b736-904e13021526@googlegroups.com> Dear all The following article had been peer-reviewed and accepted by The Python Papers. Title: A Python Module for FITS Files with full C Level Programming Functionality Abstract: A Python module for manipulating files in the FITS format is described. The module was constructed using the capabilites of ctypes to dynamically create foreign function interfaces from a C library. Here this was used to import the CFITSIO library into Python. I describe how this module can be used to call the functions from the C library in their near native form, and how one to manipulate FITS files in a style that Python programmers are accustomed. The ctypes and ctypeslib modules allows one to import all routines and data structures from the C library and avoids the need to manually write language bindings for each routine. Moreover, these modules allow the Python programmer to enjoy the full functionality of the the underlying C library. http://ojs.pythonpapers.org/index.php/tpp/article/view/250 Maurice Ling Co-EIC, The Python Papers From steve at pearwood.info Mon Aug 12 06:01:59 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 12 Aug 2013 10:01:59 GMT Subject: Calling Python macro from ctypes Message-ID: <5208b297$0$29885$c3e8da3$5496439d@news.astraweb.com> Is it possible to call a Python macro from ctypes? For example, Python 3.3 introduces some new macros for querying the internal representation of strings: http://www.python.org/dev/peps/pep-0393/#new-api So I try this in 3.3: py> import ctypes py> ctypes.pythonapi.PyUnicode_MAX_CHAR_VALUE Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.3/ctypes/__init__.py", line 366, in __getattr__ func = self.__getitem__(name) File "/usr/local/lib/python3.3/ctypes/__init__.py", line 371, in __getitem__ func = self._FuncPtr((name_or_ordinal, self)) AttributeError: python3.3: undefined symbol: PyUnicode_MAX_CHAR_VALUE -- Steven From __peter__ at web.de Mon Aug 12 07:42:14 2013 From: __peter__ at web.de (Peter Otten) Date: Mon, 12 Aug 2013 13:42:14 +0200 Subject: Calling Python macro from ctypes References: <5208b297$0$29885$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > Is it possible to call a Python macro from ctypes? For example, Python > 3.3 introduces some new macros for querying the internal representation > of strings: > > http://www.python.org/dev/peps/pep-0393/#new-api > > > So I try this in 3.3: > > py> import ctypes > py> ctypes.pythonapi.PyUnicode_MAX_CHAR_VALUE > Traceback (most recent call last): > File "", line 1, in > File "/usr/local/lib/python3.3/ctypes/__init__.py", line 366, in > __getattr__ > func = self.__getitem__(name) > File "/usr/local/lib/python3.3/ctypes/__init__.py", line 371, in > __getitem__ > func = self._FuncPtr((name_or_ordinal, self)) > AttributeError: python3.3: undefined symbol: PyUnicode_MAX_CHAR_VALUE That's not possible. It may look like a function, but a preprocessor replaces the C macro in the C source before compilation. An example of very bad usage of macros, just to drive the point home: $ cat macro.c #define IF(expr) if (expr) { #define ENDIF ;} main() { IF(1>0) printf("It worked\n") ENDIF } And here's what the compiler sees: $ gcc -E -P macro.c main() { if (1>0) { printf("It worked\n") ;} } From davea at davea.name Mon Aug 12 13:11:30 2013 From: davea at davea.name (Dave Angel) Date: Mon, 12 Aug 2013 17:11:30 +0000 (UTC) Subject: Calling Python macro from ctypes References: <5208b297$0$29885$c3e8da3$5496439d@news.astraweb.com> Message-ID: Peter Otten wrote: > Steven D'Aprano wrote: > >> Is it possible to call a Python macro from ctypes? For example, Python >> 3.3 introduces some new macros for querying the internal representation >> of strings: >> >> http://www.python.org/dev/peps/pep-0393/#new-api >> >> >> So I try this in 3.3: >> >> py> import ctypes >> py> ctypes.pythonapi.PyUnicode_MAX_CHAR_VALUE >> Traceback (most recent call last): >> File "", line 1, in >> File "/usr/local/lib/python3.3/ctypes/__init__.py", line 366, in >> __getattr__ >> func = self.__getitem__(name) >> File "/usr/local/lib/python3.3/ctypes/__init__.py", line 371, in >> __getitem__ >> func = self._FuncPtr((name_or_ordinal, self)) >> AttributeError: python3.3: undefined symbol: PyUnicode_MAX_CHAR_VALUE > > That's not possible. It may look like a function, but a preprocessor > replaces the C macro in the C source before compilation. An example of very > bad usage of macros, just to drive the point home: > > $ cat macro.c > #define IF(expr) if (expr) { > #define ENDIF ;} > > main() > { > IF(1>0) > printf("It worked\n") > ENDIF > } > > And here's what the compiler sees: > > $ gcc -E -P macro.c > > > > main() > { > if (1>0) { > printf("It worked\n") > ;} > } > To elaborate a bit more, Python can only see those symbols that are put into the shared library They can be functions, and they can be "values," but they don't include macros, which are processed by the preprocessor, before the real C compiler even starts. C Macros are actually text-substitution rules. They can look like functions, but those functions do not end up in the shared library. In Windows, you can use dumpbin to examine a DLL and see what symbols it exports. I don't remember the syntax; it's been years. I assume there's a similar tool for Linux to examine a shared library (typically an .so file). Perhaps "readelf" and/or "nm" is such a tool, but I don't really know. Although I've been using Python and C++ in Linux in recent years, I haven't used them together, and neither have I had to examine a shared library. The following link looks interesting, but I haven't read it yet. http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html -- DaveA From steve at pearwood.info Tue Aug 13 02:25:37 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 13 Aug 2013 06:25:37 GMT Subject: Calling Python macro from ctypes References: <5208b297$0$29885$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5209d161$0$29885$c3e8da3$5496439d@news.astraweb.com> On Mon, 12 Aug 2013 13:42:14 +0200, Peter Otten wrote: > Steven D'Aprano wrote: > >> Is it possible to call a Python macro from ctypes? For example, Python >> 3.3 introduces some new macros for querying the internal representation >> of strings: >> >> http://www.python.org/dev/peps/pep-0393/#new-api [...] > That's not possible. It may look like a function, but a preprocessor > replaces the C macro in the C source before compilation. That's what I feared. In that case, how would I use ctypes to access the underlying fields in the new string implementation? -- Steven From stefan_ml at behnel.de Tue Aug 13 04:11:29 2013 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 13 Aug 2013 10:11:29 +0200 Subject: Calling Python macro from ctypes In-Reply-To: <5209d161$0$29885$c3e8da3$5496439d@news.astraweb.com> References: <5208b297$0$29885$c3e8da3$5496439d@news.astraweb.com> <5209d161$0$29885$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven D'Aprano, 13.08.2013 08:25: > On Mon, 12 Aug 2013 13:42:14 +0200, Peter Otten wrote: >> Steven D'Aprano wrote: >> >>> Is it possible to call a Python macro from ctypes? For example, Python >>> 3.3 introduces some new macros for querying the internal representation >>> of strings: >>> >>> http://www.python.org/dev/peps/pep-0393/#new-api > [...] > >> That's not possible. It may look like a function, but a preprocessor >> replaces the C macro in the C source before compilation. > > That's what I feared. > > In that case, how would I use ctypes to access the underlying fields in > the new string implementation? I'd personally use Cython (no surprise here), but in order to use something like ctypes, which works at the ABI level, not the API level, and doesn't use a C compiler to get things properly configured for the local platform, you'd have to manually define the actual PyObject struct in order to access its fields directly. The macros are there to give you source code level portability for that, but if you start defining the struct layout statically, you're pretty much on your own when it comes to stuff like different CPython versions, debug builds, etc. Could you describe your use case a little deeper? Maybe there's a better way all together to do what you want. Stefan From steve at pearwood.info Tue Aug 13 05:13:52 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 13 Aug 2013 09:13:52 GMT Subject: Calling Python macro from ctypes References: <5208b297$0$29885$c3e8da3$5496439d@news.astraweb.com> <5209d161$0$29885$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5209f8cf$0$29885$c3e8da3$5496439d@news.astraweb.com> On Tue, 13 Aug 2013 10:11:29 +0200, Stefan Behnel wrote: > Steven D'Aprano, 13.08.2013 08:25: >> On Mon, 12 Aug 2013 13:42:14 +0200, Peter Otten wrote: >>> Steven D'Aprano wrote: >>> >>>> Is it possible to call a Python macro from ctypes? For example, >>>> Python 3.3 introduces some new macros for querying the internal >>>> representation of strings: >>>> >>>> http://www.python.org/dev/peps/pep-0393/#new-api >> [...] >> >>> That's not possible. It may look like a function, but a preprocessor >>> replaces the C macro in the C source before compilation. >> >> That's what I feared. >> >> In that case, how would I use ctypes to access the underlying fields in >> the new string implementation? > > I'd personally use Cython (no surprise here), but in order to use > something like ctypes, which works at the ABI level, not the API level, > and doesn't use a C compiler to get things properly configured for the > local platform, you'd have to manually define the actual PyObject struct > in order to access its fields directly. > > The macros are there to give you source code level portability for that, > but if you start defining the struct layout statically, you're pretty > much on your own when it comes to stuff like different CPython versions, > debug builds, etc. > > Could you describe your use case a little deeper? Maybe there's a better > way all together to do what you want. One concrete example of what I want to do is introspect Python 3.3 strings. E.g. to tell whether they are 1 byte, 2 byte or 4 byte strings. Strings have a "kind" field, set to 0-3, that specifies how many bytes per char, but it is not exposed to Python. -- Steven From __peter__ at web.de Tue Aug 13 06:25:37 2013 From: __peter__ at web.de (Peter Otten) Date: Tue, 13 Aug 2013 12:25:37 +0200 Subject: Calling Python macro from ctypes References: <5208b297$0$29885$c3e8da3$5496439d@news.astraweb.com> <5209d161$0$29885$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Mon, 12 Aug 2013 13:42:14 +0200, Peter Otten wrote: > >> Steven D'Aprano wrote: >> >>> Is it possible to call a Python macro from ctypes? For example, Python >>> 3.3 introduces some new macros for querying the internal representation >>> of strings: >>> >>> http://www.python.org/dev/peps/pep-0393/#new-api > [...] > >> That's not possible. It may look like a function, but a preprocessor >> replaces the C macro in the C source before compilation. > > That's what I feared. > > In that case, how would I use ctypes to access the underlying fields in > the new string implementation? You'd have to replicate the structure of Python's unicode type with ctypes. You could take shortcuts like determining sizeof(PyObject) and then fill in bytes for that instead of the actual fields, but it's still tedious. So while I have no idea why you would want to do this I took your question as an occasion to take a glimpse into the C extension tutorial. I then adapted the example code to wrap said macro. Here's the result so far (bugfixes welcome): $ cat macrowrapper.c #include static PyObject * macrowrapper_max_char_value(PyObject *self, PyObject *args) { PyObject *unicode; Py_UCS4 maxchar; if (!PyArg_ParseTuple(args, "O", &unicode)) return NULL; if (!PyUnicode_Check(unicode)) { PyErr_BadArgument(); return NULL; } maxchar = PyUnicode_MAX_CHAR_VALUE(unicode); return PyLong_FromLong(maxchar); } static PyMethodDef MacroWrapperMethods[] = { {"max_char_value", macrowrapper_max_char_value, METH_VARARGS, "Wrap PyUnicode_MAX_CHAR_VALUE() macro."}, {NULL, NULL, 0, NULL} /* Sentinel */ }; static struct PyModuleDef macrowrapper = { PyModuleDef_HEAD_INIT, "macrowrapper", /* name of module */ NULL, /* module documentation, may be NULL */ -1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */ MacroWrapperMethods }; PyMODINIT_FUNC PyInit_macrowrapper(void) { return PyModule_Create(¯owrapper); } $ cat setup.py from distutils.core import setup, Extension module = Extension('macrowrapper', sources = ['macrowrapper.c']) setup (name = 'MacroWrapper', version = '1.0', description = 'Wrap the PyUnicode_MAX_CHAR_VALUE macro', ext_modules = [module]) $ python3.3 setup.py install --user [snip] $ python3.3 Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 30 2012, 14:49:00) [GCC 4.6.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from macrowrapper import max_char_value >>> max_char_value("") 127 >>> max_char_value("L?blich") 255 >>> max_char_value("??????") 65535 >>> max_char_value(42) Traceback (most recent call last): File "", line 1, in TypeError: bad argument type for built-in operation >>> max_char_value() Traceback (most recent call last): File "", line 1, in TypeError: function takes exactly 1 argument (0 given) From christian at python.org Mon Aug 12 13:20:42 2013 From: christian at python.org (Christian Heimes) Date: Mon, 12 Aug 2013 19:20:42 +0200 Subject: SSL issues in Python stdlib and 3rd party code Message-ID: <5209196A.70001@python.org> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA512 Hello, (re-post from the Python developer list) last week Ryan Sleevi of the Google Chrome Security Team has informed us about about two issues in Python's SSL module. I already new about the cause of the first bug and suspected that our SSL module suffers from the second bug but I was unable to prove it. Both issues are security issues but their impact is limited if you trust only trustworthy root certification authorities. Any decent root CA would should not sign a malicious cert with NULL bytes in a subjectAltName dNSName field or with wildcards like *.*.com. By the way if you are using the cacert.pem from curl, please update your bundle ASAP. I have found a bug in its Mozilla certdata parser, too. bug #1: ssl.match_hostname() wildcard matching - ---------------------------------------------- ssl.match_hostname() doesn't implement RFC 6125 wildcard matching rules. Affected versions: - - Python 3.2 (< 3.2.5) - - Python 3.3 (< 3.3.3) - - Python 3.4a1 - - requests < 1.2.3 https://pypi.python.org/pypi/requests - - backports.ssl_match_hostname (<3.2a3) https://pypi.python.org/pypi/backports.ssl_match_hostname/ - - urllib3 < 1.6 https://github.com/shazow/urllib3 Bug reports: http://bugs.python.org/issue17997 https://github.com/kennethreitz/requests/issues/1528 https://bitbucket.org/brandon/backports.ssl_match_hostname/issue/2/match_hostname-doesnt-implement-rfc-6125 Patch: http://bugs.python.org/issue17997 has a preliminary patch. The handling of IDN A-labels is still a bit controversial, though. bug #2 failure to handle NULL bytes in subjectAltName - ----------------------------------------------------- It's basically the same issue as CVE-2013-4073. Python uses GENERAL_NAME_print() to turn a GERNAL_NAME entry into a C string. But GENERAL_NAME_print() doesn't handle embedded NULL bytes in ASN1_STRINGs correctly. You can read more about the issue at http://www.ruby-lang.org/en/news/2013/06/27/hostname-check-bypassing-vulnerability-in-openssl-client-cve-2013-4073/ Affected versions: - - Python 2.6 (< 2.6.8) - - Python 2.7 (< 2.7.5) - - Python 3.2 (< 3.2.5) - - Python 3.3 (< 3.3.3) - - Python 3.4a1 - - PyOpenSSL < 0.13 https://pypi.python.org/pypi/pyOpenSSL - - eGenix.com pyOpenSSL Distribution with PyOpenSSL < 0.13 https://pypi.python.org/pypi/M2Crypto - - M2Crypto < 0.21.1 http://www.egenix.com/products/python/pyOpenSSL/ Bug report: http://bugs.python.org/issue18709 Patches: http://bugs.python.org/issue18709 has patches for 2.7, 3.3 and default https://code.launchpad.net/~heimes/pyopenssl/pyopenssl/+merge/179673 Jean-Paul Calderone is going to release 0.13.1 soonish. It's going to contain just my fix for the issue. Marc-Andre Lemburg will build a new version of eGenix.com pyOpenSSL Distribution shortly after. I'm not going to work on a patch for M2Crypto as I don't understand SWIG. I have contacted Heikki Toivonen for M2Crypto but haven't heard back from him yet. related issue: Mozilla's certdata.txt and CKT_NSS_MUST_VERIFY_TRUST - ------------------------------------------------------------------- Recently I found bugs in curl's mk-ca-bundle.pl script, its cacert.pem and in the CA bundle of eGenix.com pyOpenSSL Distribution. Both failed to handle a new option in Mozilla's certdata.txt database correctly. As a consequence the root CA bundles contained additionally and untrustworthy root certificates. I'm not sure about the severity of the issue. Curl has already fixed its script week ago. Marc-Andre Lemburg is going to release a new distribution very soon. https://github.com/bagder/curl/commit/51f0b798fa http://curl.haxx.se/docs/caextract.html Background information: https://www.imperialviolet.org/2012/01/30/mozillaroots.html http://lists.debian.org/debian-release/2012/11/msg00411.html http://p11-glue.freedesktop.org/doc/storing-trust-policy/storing-trust-existing.html I like to thank Ryan Sleevi (Google), Chris Palmer (Google), Marc-Andre Lemburg (eGenix.com, Python core dev), Jean-Paul Calderone (PyOpenSSL), Antoine Pitrou (Python core dev), Daniel Stenberg (curl), G?nter Knauf (curl) and everybody else who was involved in reporting and fixing these issues. Regards, Christian -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.12 (GNU/Linux) Comment: Using GnuPG with undefined - http://www.enigmail.net/ iQIcBAEBCgAGBQJSCRlqAAoJEMeIxMHUVQ1Fmq0QAJoKFK3V1iiB+3hhvPfJGnLn pSMzm9CpIineF7KRSI50GYls7gwO5Nggqol2V7qcEzpRCPFxkV9E2t75Q8OnBW0+ ZHOoTjpLD086HeQ4oIcGN6byQcHwIbIVh74hRFu7ZW4jqLQwvyPrGwRonRv5a6nG 0tRqp/Swqyqvo4KTpxV26MdZNwD+c+ASO0gmQzW6SURS5/gUKv0fDWjUwqC9COdZ Xz9wsLZaul+e6ewH7N5c7EWRXEs8l8cVk68Jw+/Jshct4kfWUnnw67qPjcoDgHLI tZzjVy7aCp2V9vjKvAcM2OUz0Q5h1rIFc5Roh5yH8OxuOn+Zn6zs3M6DKHJI9n12 JTTyi4yBu5hBRFqOWYSicytQpHAT604zpaGY7g4Hd0J98tG0kuT0BC7tozdi+MUW eXuBML7uYOBlI/VCIv/baZAaRj1txPZl5Vf9a75N2lb2seavUJDu8HqOzh+zxSTC 9agPuGAJOrU5mljkBKCCVy6/4njktjOBDmQsf9OHZ0/FolBjQuiJyXp4hLMQMqzY /ji4HtLF7Er0hWfj9L/4bFHf+XO9e9RZY8RtCV4ruc0fjZuZEt6SKx6CDjhMha8A zkWa2X5jk4RivBE1rMLovCaYeI8pt7cYcNmarZHBwjXjAKHR4xbxAPlP8KCQnvHt owYFnlto9YI6ZVndgsD/ =uPVK -----END PGP SIGNATURE----- From demianbrecht at gmail.com Mon Aug 12 19:17:12 2013 From: demianbrecht at gmail.com (Demian Brecht) Date: Mon, 12 Aug 2013 16:17:12 -0700 Subject: Digging into multiprocessing Message-ID: Hi all, Some work that I'm doing atm is in some serious need of parallelization. As such, I've been digging into the multiprocessing module more than I've had to before and I had a few questions come up as a result: (Running 2.7.5+ on OSX) 1. From what I've read, a new Python interpreter instance is kicked off for every worker. My immediate assumption was that the file that the code was in would be reloaded for every instance. After some digging, this is obviously not the case (print __name__ at the top of the file only yield a single output line). So, I'm assuming that there's some optimization that passes of the bytecode within the interpreter? How, exactly does this work? (I couldn't really find much in the docs about it, or am I just not looking in the right place?) 2. For cases using methods such as map_async/wait, once the bytecode has been passed into the child process, `target` is called `n` times until the current queue is empty. Is this correct? 3. Because __main__ is only run when the root process imports, if using global, READ-ONLY objects, such as, say, a database connection, then it might be better from a performance standpoint to initialize that at main, relying on the interpreter references to be passed around correctly. I've read some blogs and such that suggest that you should create a new database connection within your child process targets (or code called into by the targets). This seems to be less than optimal to me if my assumption is correct. 4. Related to 3, read-only objects that are initialized prior to being passed into a sub-process are safe to reuse as long as they are treated as being immutable. Any other objects should use one of the shared memory features. Is this more or less correct, or am I just off my rocker? Thanks, -- Demian Brecht http://demianbrecht.github.com From rosuav at gmail.com Tue Aug 13 11:03:30 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 13 Aug 2013 16:03:30 +0100 Subject: Digging into multiprocessing In-Reply-To: References: Message-ID: On Tue, Aug 13, 2013 at 12:17 AM, Demian Brecht wrote: > Hi all, > > Some work that I'm doing atm is in some serious need of > parallelization. As such, I've been digging into the multiprocessing > module more than I've had to before and I had a few questions come up > as a result: > > (Running 2.7.5+ on OSX) > > 1. From what I've read, a new Python interpreter instance is kicked > off for every worker. My immediate assumption was that the file that > the code was in would be reloaded for every instance. After some > digging, this is obviously not the case (print __name__ at the top of > the file only yield a single output line). So, I'm assuming that > there's some optimization that passes of the bytecode within the > interpreter? How, exactly does this work? (I couldn't really find much > in the docs about it, or am I just not looking in the right place?) I don't know about OSX specifically, but I believe it forks, same as on Linux. That means all your initialization code is done once. Be aware that this is NOT the case on Windows. http://en.wikipedia.org/wiki/Fork_(operating_system) Effectively, code execution proceeds down a single thread until the point of forking, and then the fork call returns twice. Can be messy to explain but it makes great sense once you grok it! > 2. For cases using methods such as map_async/wait, once the bytecode > has been passed into the child process, `target` is called `n` times > until the current queue is empty. Is this correct? That would be about right, yes. The intention is that it's equivalent to map(), only it splits the work across multiple processes; so the expectation is that it will call target for each yielded item in the iterable. > 3. Because __main__ is only run when the root process imports, if > using global, READ-ONLY objects, such as, say, a database connection, > then it might be better from a performance standpoint to initialize > that at main, relying on the interpreter references to be passed > around correctly. I've read some blogs and such that suggest that you > should create a new database connection within your child process > targets (or code called into by the targets). This seems to be less > than optimal to me if my assumption is correct. This depends hugely on the objects you're working with. If your database connection uses a TCP socket, for instance, all forked processes will share the same socket, which will most likely result in interleaved writes and messed-up reads. But with a log file, that might be okay (especially if you have some kind of atomicity guarantee that ensures that individual log entries don't interleave). The problem isn't really the Python objects (which will have been happily cloned by the fork() procedure), but the OS-level resources used. With a good database like PostgreSQL, and reasonable numbers of workers (say, 10-50, rather than 1000-5000), you should be able to simply establish separate connections for each subprocess without worrying about performance. If you really need billions of worker processes, it might be best to use one of the multiprocessing module's queueing/semaphoring facilities and either have one process that does all databasing, or let them all use it but serially. But if you can manage with separate connections, that would be the easiest, safest, and simplest to debug. > 4. Related to 3, read-only objects that are initialized prior to being > passed into a sub-process are safe to reuse as long as they are > treated as being immutable. Any other objects should use one of the > shared memory features. > > Is this more or less correct, or am I just off my rocker? When you fork, each process will get its own clone of the objects in the parent. For read-only objects (module-level constants and such), this is fine, as you say. The issue is if you want another process to "see" the change you made. That's when you need some form of shared data. So, yes, more or less correct; at least, what you've said is mostly right for Unix - there may be some additional caveats for OSX specifically that I'm not aware of. But I expect they'll be minor; it's mainly Windows, which doesn't *have* fork(2), where there are major differences. ChrisA From demianbrecht at gmail.com Wed Aug 14 10:23:26 2013 From: demianbrecht at gmail.com (Demian Brecht) Date: Wed, 14 Aug 2013 07:23:26 -0700 Subject: Digging into multiprocessing In-Reply-To: References: Message-ID: Awesome, thanks for the detailed response Chris. On Tue, Aug 13, 2013 at 8:03 AM, Chris Angelico wrote: > On Tue, Aug 13, 2013 at 12:17 AM, Demian Brecht wrote: >> Hi all, >> >> Some work that I'm doing atm is in some serious need of >> parallelization. As such, I've been digging into the multiprocessing >> module more than I've had to before and I had a few questions come up >> as a result: >> >> (Running 2.7.5+ on OSX) >> >> 1. From what I've read, a new Python interpreter instance is kicked >> off for every worker. My immediate assumption was that the file that >> the code was in would be reloaded for every instance. After some >> digging, this is obviously not the case (print __name__ at the top of >> the file only yield a single output line). So, I'm assuming that >> there's some optimization that passes of the bytecode within the >> interpreter? How, exactly does this work? (I couldn't really find much >> in the docs about it, or am I just not looking in the right place?) > > I don't know about OSX specifically, but I believe it forks, same as > on Linux. That means all your initialization code is done once. Be > aware that this is NOT the case on Windows. > > http://en.wikipedia.org/wiki/Fork_(operating_system) > > Effectively, code execution proceeds down a single thread until the > point of forking, and then the fork call returns twice. Can be messy > to explain but it makes great sense once you grok it! > >> 2. For cases using methods such as map_async/wait, once the bytecode >> has been passed into the child process, `target` is called `n` times >> until the current queue is empty. Is this correct? > > That would be about right, yes. The intention is that it's equivalent > to map(), only it splits the work across multiple processes; so the > expectation is that it will call target for each yielded item in the > iterable. > >> 3. Because __main__ is only run when the root process imports, if >> using global, READ-ONLY objects, such as, say, a database connection, >> then it might be better from a performance standpoint to initialize >> that at main, relying on the interpreter references to be passed >> around correctly. I've read some blogs and such that suggest that you >> should create a new database connection within your child process >> targets (or code called into by the targets). This seems to be less >> than optimal to me if my assumption is correct. > > This depends hugely on the objects you're working with. If your > database connection uses a TCP socket, for instance, all forked > processes will share the same socket, which will most likely result in > interleaved writes and messed-up reads. But with a log file, that > might be okay (especially if you have some kind of atomicity guarantee > that ensures that individual log entries don't interleave). The > problem isn't really the Python objects (which will have been happily > cloned by the fork() procedure), but the OS-level resources used. > > With a good database like PostgreSQL, and reasonable numbers of > workers (say, 10-50, rather than 1000-5000), you should be able to > simply establish separate connections for each subprocess without > worrying about performance. If you really need billions of worker > processes, it might be best to use one of the multiprocessing module's > queueing/semaphoring facilities and either have one process that does > all databasing, or let them all use it but serially. But if you can > manage with separate connections, that would be the easiest, safest, > and simplest to debug. > >> 4. Related to 3, read-only objects that are initialized prior to being >> passed into a sub-process are safe to reuse as long as they are >> treated as being immutable. Any other objects should use one of the >> shared memory features. >> >> Is this more or less correct, or am I just off my rocker? > > When you fork, each process will get its own clone of the objects in > the parent. For read-only objects (module-level constants and such), > this is fine, as you say. The issue is if you want another process to > "see" the change you made. That's when you need some form of shared > data. > > So, yes, more or less correct; at least, what you've said is mostly > right for Unix - there may be some additional caveats for OSX > specifically that I'm not aware of. But I expect they'll be minor; > it's mainly Windows, which doesn't *have* fork(2), where there are > major differences. > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list -- Demian Brecht http://demianbrecht.github.com From samaneh.yahyapour at gmail.com Tue Aug 13 04:06:01 2013 From: samaneh.yahyapour at gmail.com (samaneh.yahyapour at gmail.com) Date: Tue, 13 Aug 2013 01:06:01 -0700 (PDT) Subject: multithreading in python Message-ID: hi my program work by 4 thread but when i use more thread it terminates i use opencv in my image_process.so my code is : #!/usr/bin/python import sys import os import io import time import copy import threading import ctypes class MyClass(): def __init__(self): i = 0 while i<10: thread1 = threading.Thread(target=self.item_thread) thread1.start() i = i+1 time.sleep(0.01) def item_thread(self): imageAnalyzer=ctypes.CDLL("../so/image_process.so") imageAnalyzer.aref_img_score_init("/opt/amniran/etc/face.xml", "/opt/amniran/etc/porn.xml") for filename in os.listdir("../script/images/"): if filename[-4:] == ".jpg" or filename[-4:] == ".png" or filename[-4:] == ".gif" or filename[-5:] == ".jpeg" : path = "../script/images/%s"%filename fo = file(path, "r") content = fo.read() score = imageAnalyzer.score_image(content, len(content)) print "%d : %s " %(score, path) print "ENDDDDDDDDDDDDDDDDDDDDDDDDDDDD" x = MyClass() how can i solve this problem From steve at pearwood.info Tue Aug 13 04:38:01 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 13 Aug 2013 08:38:01 GMT Subject: multithreading in python References: Message-ID: <5209f068$0$29885$c3e8da3$5496439d@news.astraweb.com> On Tue, 13 Aug 2013 01:06:01 -0700, samaneh.yahyapour wrote: > hi > my program work by 4 thread but when i use more thread it terminates Is that a problem? Isn't it supposed to terminate, when it has finished? If it raises an exception, or crashes, you should tell us. > i use opencv in my image_process.so > > my code is : > > > #!/usr/bin/python > import sys > import os > import io > import time > import copy > import threading > import ctypes > > class MyClass(): > > def __init__(self): > i = 0 > while i<10: > thread1 = threading.Thread(target=self.item_thread) > thread1.start() > i = i+1 > time.sleep(0.01) This is better written as: def __init__(self): self.threads = [] for i in range(10): thread = threading.Thread(target=self.item_thread) thread.start() self.threads.append(thread) time.sleep(0.01) # not sure this helps for anything I think it will also help if you keep references to the threads. That will stop them from being garbage collected unexpectedly, and you can check their status before exiting the main thread. > def item_thread(self): > imageAnalyzer=ctypes.CDLL("../so/image_process.so") > imageAnalyzer.aref_img_score_init("/opt/amniran/etc/face.xml", > "/opt/amniran/etc/porn.xml") for filename in > os.listdir("../script/images/"): > if filename[-4:] == ".jpg" or filename[-4:] == ".png" or > filename[-4:] == ".gif" or filename[-5:] == ".jpeg" : > > path = "../script/images/%s"%filename > > fo = file(path, "r") > content = fo.read() > score = imageAnalyzer.score_image(content, len(content)) > print "%d : %s " %(score, path) > print "ENDDDDDDDDDDDDDDDDDDDDDDDDDDDD" > > > x = MyClass() I suspect that when the main thread exits, and your other threads are still running, you may be in trouble. But I'm not a threading expert, so I could be wrong. However, I would put something like this at the end: for thread in x.threads: x.join() that way the main thread cannot finish until each of the subthreads are. -- Steven From davea at davea.name Tue Aug 13 07:22:56 2013 From: davea at davea.name (Dave Angel) Date: Tue, 13 Aug 2013 11:22:56 +0000 (UTC) Subject: multithreading in python References: Message-ID: samaneh.yahyapour at gmail.com wrote: > hi > my program work by 4 thread but when i use more thread it terminates > > I simplified your code so anybody could run it, and tested it inside Komodo IDE, on Python 2.7 #!/usr/bin/env python import sys import os import time import threading class MyClass(): def __init__(self): i = 0 while i<10: work = WorkClass(i) thread1 = threading.Thread(target=work.item_thread) thread1.start() i = i+1 time.sleep(0.01) class WorkClass(): def __init__(self, parm): self.parm = str(parm) def item_thread(self): print "beginning thread", self.parm for filename in os.listdir("."): data = "thread " + self.parm + " filename " + filename + "\n" print data time.sleep(0.5) print "ENDDDDDDDDDDDDDDDDDDDDDDDDDDDD " + self.parm x = MyClass() print "Finishing main thread" When the -- Signature file not found From davea at davea.name Tue Aug 13 07:40:27 2013 From: davea at davea.name (Dave Angel) Date: Tue, 13 Aug 2013 11:40:27 +0000 (UTC) Subject: multithreading in python References: Message-ID: samaneh.yahyapour at gmail.com wrote: > hi > my program work by 4 thread but when i use more thread it terminates > > how can i solve this problem I simplified the code so I could actually run it, and tested it in Python 2.7, both under Komodo IDE and in the terminal. The code: #!/usr/bin/env python import sys import os import time import threading class MyClass(): def __init__(self): self.threads = [] i = 0 while i<10: work = WorkClass(i) thread1 = threading.Thread(target=work.item_thread) self.threads.append(thread1) thread1.start() i = i+1 time.sleep(0.01) for thread in self.threads: #wait for all the threads to end thread.join() class WorkClass(): def __init__(self, parm): self.parm = str(parm) def item_thread(self): print "beginning thread", self.parm for filename in os.listdir("."): data = "thread " + self.parm + " filename " + filename + "\n" print data time.sleep(0.5) print "ENDDDDDDDDDDDDDDDDDDDDDDDDDDDD " + self.parm x = MyClass() print "All done with main thread" The original code couldn't tell us what threads were running, so the only clue you got was how many times it printed "ENDDDD" So I arranged that each thread had a unique "parm" value. Typically you must do something like this so that all the threads aren't doing exactly the same work. Another thing i did was to "atomicize" the print statements. As it originally was, partial lines from different threads could be intermixed in the output. The IDE showed the error message: """ ERROR: dbgp.client: The main thread of this application is exiting while there are still threads alive. When the main thread exits, it is system defined whether the other threads survive. See Caveats at http://docs.python.org/lib/module-thread.html """ which tells you what's wrong. You need to do a join on the threads before exiting. it's traditional (and better) to derive your own class from threading.Thread, and that's where you can store any additional attributes that each thread will need. I demonstrated something I figured was simpler, by making the item_thread() method part of a separate class. -- DaveA From tjreedy at udel.edu Tue Aug 13 11:51:13 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 13 Aug 2013 11:51:13 -0400 Subject: multithreading in python In-Reply-To: References: Message-ID: On 8/13/2013 4:06 AM, samaneh.yahyapour at gmail.com wrote: Aside from the other comments... > def item_thread(self): > imageAnalyzer=ctypes.CDLL("../so/image_process.so") > imageAnalyzer.aref_img_score_init("/opt/amniran/etc/face.xml", "/opt/amniran/etc/porn.xml") > for filename in os.listdir("../script/images/"): > if filename[-4:] == ".jpg" or filename[-4:] == ".png" or filename[-4:] == ".gif" or filename[-5:] == ".jpeg" : > > path = "../script/images/%s"%filename > > fo = file(path, "r") Use 'open' instead of the deprecated 'file' (removed in 3.x) and use it with a 'with' statement (this is now standard). This closes the file at the end of the block. Not doing so can cause problems on other implementations. with open(path, 'rb') as fo: > content = fo.read() > score = imageAnalyzer.score_image(content, len(content)) > print "%d : %s " %(score, path) > print "ENDDDDDDDDDDDDDDDDDDDDDDDDDDDD" Do you know how to use queue.Queue to spread work to multiple worker threads so each processes different files (and to collect results from multiple threads).? (If not, read doc.) -- Terry Jan Reedy From alec.taylor6 at gmail.com Tue Aug 13 04:20:20 2013 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Tue, 13 Aug 2013 01:20:20 -0700 (PDT) Subject: Open-source vs. closed-source for centralised social-networks? Message-ID: <5d518caf-df62-4e77-900c-fe00d3d84d81@googlegroups.com> Fear open-sourcing fledgling social-networks; as centralisation is easily losable. Open-sourcing social-networks when large, seems to work (e.g.: Reddit). Without centralisation it becomes difficult to establish community. An example of a "decentralised" open-source social-network is: Disapora. Personally, I am using Python to build a niche social-network. Is it more likely that open-sourcing a niche fledgling social-network would result in: many decentralised communities?or one [larger] centralised community? From ben+python at benfinney.id.au Tue Aug 13 19:08:29 2013 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 14 Aug 2013 09:08:29 +1000 Subject: Writing a =?utf-8?Q?=E2=80=9Csocial_network=E2=80=9D?= in Python (was: Open-source vs. closed-source for centralised social-networks?) References: <5d518caf-df62-4e77-900c-fe00d3d84d81@googlegroups.com> Message-ID: <7wk3jp89zm.fsf@benfinney.id.au> Alec Taylor writes: > Fear open-sourcing fledgling social-networks; as centralisation is > easily losable. Welcome! This forum is for discussing the practice of programming with Python. Your topic doesn't seem much to do with Python (and no, ?I'm writing it in Python? isn't much to do with Python unless your *question* is specific to Python). > Personally, I am using Python to build a niche social-network. Great! What Python-related question did you want to ask? -- \ ?Oh, I love your magazine. My favorite section is ?How To | `\ Increase Your Word Power?. That thing is really, really, | _o__) really... good.? ?Homer, _The Simpsons_ | Ben Finney From dwightdhutto at gmail.com Fri Aug 16 21:15:06 2013 From: dwightdhutto at gmail.com (David Hutto) Date: Fri, 16 Aug 2013 21:15:06 -0400 Subject: =?windows-1252?Q?Re=3A_Writing_a_=93social_network=94_in_Python_=28was=3A_O?= =?windows-1252?Q?pen=2Dsource_vs=2E_closed=2Dsource_for_centralised_social=2Dnetworks?= =?windows-1252?Q?=3F=29?= In-Reply-To: <7wk3jp89zm.fsf@benfinney.id.au> References: <5d518caf-df62-4e77-900c-fe00d3d84d81@googlegroups.com> <7wk3jp89zm.fsf@benfinney.id.au> Message-ID: Is this social network app based, or browser based. Either will need an updated server file, or a db field. On Tue, Aug 13, 2013 at 7:08 PM, Ben Finney wrote: > Alec Taylor writes: > > > Fear open-sourcing fledgling social-networks; as centralisation is > > easily losable. > > Welcome! This forum is for discussing the practice of programming with > Python. > > Your topic doesn't seem much to do with Python (and no, ?I'm writing it > in Python? isn't much to do with Python unless your *question* is > specific to Python). > > > Personally, I am using Python to build a niche social-network. > > Great! What Python-related question did you want to ask? > > -- > \ ?Oh, I love your magazine. My favorite section is ?How To | > `\ Increase Your Word Power?. That thing is really, really, | > _o__) really... good.? ?Homer, _The Simpsons_ | > Ben Finney > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Best Regards, David Hutto *CEO:* *http://www.hitwebdevelopment.com* -------------- next part -------------- An HTML attachment was scrubbed... URL: From papillion at gmail.com Tue Aug 13 08:12:05 2013 From: papillion at gmail.com (Anthony Papillion) Date: Tue, 13 Aug 2013 07:12:05 -0500 Subject: Question about function failing with large number Message-ID: <520A2295.5040605@gmail.com> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA512 So I'm using the function below to test a large (617 digit) number for primality. For some reason, when I execute the code, I get an error telling me: OverflowError: long int too large to convert to float The error is being thrown on this line: for x in range(3, int(n**0.5)+1, 2): The odd thing is that the error is not thrown every single time. I can run the function a few times, it will generate a large number (always the same length) and run it through the function. After I do this a few times, it fails with the error. I might get the error on the next few runs but then, all of a sudden, it functions again. Any ideas? The entire program, including the method, is below. #!/usr/bin/env python from random import getrandbits bits = 2048 # Test if the number is a prime def isprime(n): # make sure n is a positive integer n = abs(int(n)) # 0 and 1 are not primes if n < 2: return False # 2 is the only even prime number if n == 2: return True # all other even numbers are not primes if not n & 1: return False # range starts with 3 and only needs to go up the squareroot of n # for all odd numbers for x in range(3, int(n**0.5)+1, 2): if n % x == 0: return False return True a = getrandbits(bits) print "\nGenerated Number: ", a, "\n" print "Number of digits: ", len(str(a)) isNumberPrime = isprime(a) if isNumberPrime == True: print "\nThis number is a prime.\n" else: print "\nThis number is not a prime.\n" Thanks! Anthony - -- Anthony Papillion Phone: 1.918.533.9699 SIP: 17772098750 at in.callcentric.com XMPP: cypherpunk at patts.us www.cajuntechie.org -----BEGIN PGP SIGNATURE----- iQIcBAEBCgAGBQJSCiKNAAoJEKCKfnPhYIFFn7gP/A8CHOyTv71J/uVpMVYRcDRp KRwbL9A1gkzUpycQibN3Q90FwY6nsGGPCeOkZByfYZnMKQH0o4Kd7QQf0hEOkhzO BLiQjbkjzuUq7usE5TIqjTi0pJ6J7DcRm6U77yhAWrVt60MpfOucojLzX8ZolTD6 7Ha1gJ+9uEcwjosx1ynjVt7MQ/uGZwM7xS6WNfOpOwIYnoT5zBUzlKbw1HqSGYLu 6cWmAFTnnnXv6qymbGTxdZf0dxciODXy5xIMp5CzG6zIeHIOvjG03AbcY/+nI5FI b9fKqjbblE/Npnh9GPXOLpI+I05VZMoO1b0AJSlU+Iq1liZAZOA4s2kf7XCrSb7Y 8Zn6qMPMTuNBPZpRJykTJSrA8s+4RxA0BWoq9rnTNXJPVR6imt6USOtwY4UssPxw HIUrbSmfAEF9+/g08mcKHTVFstMyQCuAbUGx+LxoxkySnwZkfcwTRQ2vuoBDd7XP 92IJlkAdwepEa748P6NHNkSN4+OV3zAeTczHkzD0OL2KAeCuPY5tlqsI0MAngKOu TIZrG+w1rvkz1gU3TBILLySuQTk/ioHNoVAH46bvp6ARRJiHJ4Ub61NoyooMCOgX Lg+XtmcYz9VdUzdayg5uwNQAb/2/DboAyvmuuRDrA2Lzndv1JO3ye222/WCI9e7V aB1ghvNnNMtrOWA0ZVQX =BZUP -----END PGP SIGNATURE----- From rosuav at gmail.com Tue Aug 13 08:42:56 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 13 Aug 2013 13:42:56 +0100 Subject: Question about function failing with large number In-Reply-To: <520A2295.5040605@gmail.com> References: <520A2295.5040605@gmail.com> Message-ID: On Tue, Aug 13, 2013 at 1:12 PM, Anthony Papillion wrote: > So I'm using the function below to test a large (617 digit) number for > primality. For some reason, when I execute the code, I get an error > telling me: > > OverflowError: long int too large to convert to float > > The error is being thrown on this line: > > for x in range(3, int(n**0.5)+1, 2): Python's integers are unbounded, storing arbitrary precision. Python's floats are limited to the range of the underlying IEEE implementation. You'll have a certain cutoff above which your system bombs. >>> float(1<<1023) 8.98846567431158e+307 >>> float(1<<1024) Traceback (most recent call last): File "", line 1, in float(1<<1024) OverflowError: long int too large to convert to float (The exact cutoff may depend on how your Python is built, I think; that was running on a 32-bit Python on Windows.) Everything else in your code will work, so your only problem is this square rooting. So here's an alternative: for x in range(3, n, 2): div, mod = divmod(n, x) if mod == 0: return False if div < n: break Once the quotient is lower than the divisor, you've passed the square root. (Note the use of divmod to do a single division and return both quotient and remainder. You could alternatively use // and % but it'd divide twice.) By the way, the "== True" in your final condition is redundant. Just test "if isNumberPrime:", or even just "if isprime(a):". :) Though with something like this that simply returns boolean, I'd be inclined to make it return a bit more - for instance, it returns the first-seen factor. if n < 2: return 1 # Only way this will ever return a non-prime integer if n == 2: return None # Prime! if not n & 1: return 2 # and inside the loop: "return x" Rename the function to "iscomposite", because prime numbers return None (false) and composites return a positive integer (true), and you've just made the function that bit more useful. :) ChrisA From python at mrabarnett.plus.com Tue Aug 13 11:33:54 2013 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 13 Aug 2013 16:33:54 +0100 Subject: Question about function failing with large number In-Reply-To: References: <520A2295.5040605@gmail.com> Message-ID: <520A51E2.8020908@mrabarnett.plus.com> On 13/08/2013 13:42, Chris Angelico wrote: > On Tue, Aug 13, 2013 at 1:12 PM, Anthony Papillion wrote: >> So I'm using the function below to test a large (617 digit) number for >> primality. For some reason, when I execute the code, I get an error >> telling me: >> >> OverflowError: long int too large to convert to float >> >> The error is being thrown on this line: >> >> for x in range(3, int(n**0.5)+1, 2): > > Python's integers are unbounded, storing arbitrary precision. Python's > floats are limited to the range of the underlying IEEE implementation. > You'll have a certain cutoff above which your system bombs. > >>>> float(1<<1023) > 8.98846567431158e+307 >>>> float(1<<1024) > Traceback (most recent call last): > File "", line 1, in > float(1<<1024) > OverflowError: long int too large to convert to float > > (The exact cutoff may depend on how your Python is built, I think; > that was running on a 32-bit Python on Windows.) > [snip] Here's a way to calculate the integer square root: def int_sqrt(number): root = 1 while True: new_root = (root + number // root) // 2 if new_root == root: break root = new_root return root It's result is the largest integer whose square doesn't exceed the original number. From rosuav at gmail.com Tue Aug 13 13:01:39 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 13 Aug 2013 18:01:39 +0100 Subject: Question about function failing with large number In-Reply-To: <520A51E2.8020908@mrabarnett.plus.com> References: <520A2295.5040605@gmail.com> <520A51E2.8020908@mrabarnett.plus.com> Message-ID: On Tue, Aug 13, 2013 at 4:33 PM, MRAB wrote: > Here's a way to calculate the integer square root: Yes, but the actual value of the square root isn't needed. All that's needed is to stop the loop once the sqrt is reached. ChrisA From davea at davea.name Tue Aug 13 08:57:12 2013 From: davea at davea.name (Dave Angel) Date: Tue, 13 Aug 2013 12:57:12 +0000 (UTC) Subject: Question about function failing with large number References: <520A2295.5040605@gmail.com> Message-ID: Anthony Papillion wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA512 > > So I'm using the function below to test a large (617 digit) number for > primality. For some reason, when I execute the code, I get an error > telling me: > > OverflowError: long int too large to convert to float In general, you should quote the entire traceback. it's no trouble in this case, as the problem is clearly in the n**0.5 portion. You should also specify the Python version. But it's a safe bet that you're using 2.x, since your print statements are illegal in 3.x > > The error is being thrown on this line: > > for x in range(3, int(n**0.5)+1, 2): > > The odd thing is that the error is not thrown every single time. I can > run the function a few times, it will generate a large number (always > the same length) and run it through the function. After I do this a > few times, it fails with the error. I might get the error on the next > few runs but then, all of a sudden, it functions again. > > Any ideas? The entire program, including the method, is below. > > #!/usr/bin/env python > > from random import getrandbits > > bits = 2048 > > # Test if the number is a prime > def isprime(n): > > # make sure n is a positive integer > n = abs(int(n)) > # 0 and 1 are not primes > if n < 2: > return False > # 2 is the only even prime number > if n == 2: > return True > # all other even numbers are not primes > if not n & 1: > return False > # range starts with 3 and only needs to go up the squareroot of n > # for all odd numbers > for x in range(3, int(n**0.5)+1, 2): > if n % x == 0: > return False > return True > > a = getrandbits(bits) > print "\nGenerated Number: ", a, "\n" > print "Number of digits: ", len(str(a)) > isNumberPrime = isprime(a) > if isNumberPrime == True: > print "\nThis number is a prime.\n" > else: > print "\nThis number is not a prime.\n" > > Thanks! > Anthony > The operator ** works by first converting its arguments to float. if the long value is too large for a Python float (which is a C double), then you get this error. You'll get the same one with math.sqrt(). Nothing you can do about that unless you want to write your own square root function. However, you can change your algorithm. You'll need to anyway, since a range that large would take a ridiculous amount of RAM, if it even was willing to start. The cure is to use a while loop, and test x*x agains the limit while x*x < n: if n%x == 0: return False n+=2 BTW, I can't believe that getrandbits(2048) is going to be small enough to 'work" for any noticeable probability. So when you find it working, perhaps you're using a smaller value for bits. BTW, when you get this all done, you'll find that testing a prime of 600 digits is going to take an effectively infiinite time. You need a faster method. -- DaveA From rlkling at gmail.com Tue Aug 13 08:25:34 2013 From: rlkling at gmail.com (rlkling at gmail.com) Date: Tue, 13 Aug 2013 05:25:34 -0700 (PDT) Subject: python and displaying on 10 bit monitors Message-ID: <415cd70d-72e8-4a2b-9e2e-81420b146682@googlegroups.com> Are there any python libraries that read and display High Dynamic Range images like this matlab code? http://www.mathworks.com/matlabcentral/fileexchange/27115-hdr-image-viewer-for-deep-color-monitor Or, asking another way, are there any python libraries that display images to 10 bit monitors as 10 bit images, and not scaled to 8 bit? From tjreedy at udel.edu Tue Aug 13 12:31:16 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 13 Aug 2013 12:31:16 -0400 Subject: python and displaying on 10 bit monitors In-Reply-To: <415cd70d-72e8-4a2b-9e2e-81420b146682@googlegroups.com> References: <415cd70d-72e8-4a2b-9e2e-81420b146682@googlegroups.com> Message-ID: On 8/13/2013 8:25 AM, rlkling at gmail.com wrote: > Are there any python libraries that read and display High Dynamic Range images like this matlab code? > > http://www.mathworks.com/matlabcentral/fileexchange/27115-hdr-image-viewer-for-deep-color-monitor > > Or, asking another way, are there any python libraries that display images to 10 bit monitors as 10 bit images, and not scaled to 8 bit? > I think that would depend on whether any of them wrap a C library that can do that. My guess is 'no'. -- Terry Jan Reedy From ian.g.kelly at gmail.com Tue Aug 13 14:35:31 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 13 Aug 2013 12:35:31 -0600 Subject: python and displaying on 10 bit monitors In-Reply-To: <415cd70d-72e8-4a2b-9e2e-81420b146682@googlegroups.com> References: <415cd70d-72e8-4a2b-9e2e-81420b146682@googlegroups.com> Message-ID: On Tue, Aug 13, 2013 at 6:25 AM, wrote: > Are there any python libraries that read and display High Dynamic Range images like this matlab code? > > http://www.mathworks.com/matlabcentral/fileexchange/27115-hdr-image-viewer-for-deep-color-monitor > > Or, asking another way, are there any python libraries that display images to 10 bit monitors as 10 bit images, and not scaled to 8 bit? A Google search turns up this blog post: http://hoganhdr.blogspot.com/2009/10/h2-image-viewer-python.html No idea whether it works or not, and you'll have to fix the indentation since the poster doesn't seem to know how to format Python scripts for the web. From rlkling at gmail.com Tue Aug 13 14:51:07 2013 From: rlkling at gmail.com (rlkling at gmail.com) Date: Tue, 13 Aug 2013 11:51:07 -0700 (PDT) Subject: python and displaying on 10 bit monitors In-Reply-To: References: <415cd70d-72e8-4a2b-9e2e-81420b146682@googlegroups.com> Message-ID: <3846728a-9051-4f65-a1d6-8e685bcffc3b@googlegroups.com> Yea, that is not quite what I am looking for. He uses Python Image Library and as far as I can tell it does not display the images as 10 bits on 10 bit displays. So far the only app that I can find that does this is photoshop. From nobody at nowhere.com Wed Aug 14 03:07:25 2013 From: nobody at nowhere.com (Nobody) Date: Wed, 14 Aug 2013 08:07:25 +0100 Subject: python and displaying on 10 bit monitors References: <415cd70d-72e8-4a2b-9e2e-81420b146682@googlegroups.com> Message-ID: On Tue, 13 Aug 2013 05:25:34 -0700, rlkling wrote: > Or, asking another way, are there any python libraries that display images > to 10 bit monitors as 10 bit images, and not scaled to 8 bit? This should be possible using PyOpenGL and GLUT, with: glutInitDisplayString("red=10 green=10 blue=10") That's based upon a quick look at: http://www.amd.com/us/Documents/10-Bit.pdf http://www.nvidia.com/docs/IO/40049/TB-04701-001_v02_new.pdf I don't have a 30-bpp monitor to test it with. You may be able to do the same thing using e.g. QGLWidget or wxGLCanvas, but I don't think that you can get 30-bpp with GDI, so you're limited to solutions involving OpenGL (or DirectX, if you can figure out the necessary pieces). From EWilliams at diversant.com Tue Aug 13 10:52:24 2013 From: EWilliams at diversant.com (Williams, Elisabeth S) Date: Tue, 13 Aug 2013 10:52:24 -0400 Subject: Python-List Message-ID: <72A7BED84C28954B90E688EC6A6F273B238DF6D6@SONIC.infotechnologiesinc.com> We have an immediate need for a Python Developer and Development Technical Lead (proficient in Python) in Charlotte, NC. Please reply with you r resume for consideration. Thanks, Beth Elisabeth Williams Technical Recruiter 704-227-3142 ewilliams at diversant.com 212 South Tryon Street 15th Floor, Suite 1500 Charlotte, NC 28281 www.diversant.com -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: image001.png Type: image/png Size: 11243 bytes Desc: image001.png URL: From ben+python at benfinney.id.au Tue Aug 13 19:18:22 2013 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 14 Aug 2013 09:18:22 +1000 Subject: Python-List References: <72A7BED84C28954B90E688EC6A6F273B238DF6D6@SONIC.infotechnologiesinc.com> Message-ID: <7wfvud89j5.fsf@benfinney.id.au> "Williams, Elisabeth S" writes: > We have an immediate need for a Python Developer and Development > Technical Lead Please use the Python Job Board to recruit Python programmers , not this forum. Thanks! -- \ ?I call him Governor Bush because that's the only political | `\ office he's ever held legally.? ?George Carlin, 2008 | _o__) | Ben Finney From englishkevin110 at gmail.com Tue Aug 13 18:51:26 2013 From: englishkevin110 at gmail.com (englishkevin110 at gmail.com) Date: Tue, 13 Aug 2013 15:51:26 -0700 (PDT) Subject: Getting a value that follows string.find() Message-ID: <40816fed-38d4-4baa-92cc-c80cd8febd82@googlegroups.com> I know the title doesn't make much sense, but I didnt know how to explain my problem. Anywho, I've opened a page's source in URLLIB starturlsource = starturlopen.read() string.find(starturlsource, ' I want to store what comes after ?id= in a variable. > Can someone help me with this? > -- > http://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com From englishkevin110 at gmail.com Tue Aug 13 19:03:46 2013 From: englishkevin110 at gmail.com (englishkevin110 at gmail.com) Date: Tue, 13 Aug 2013 16:03:46 -0700 (PDT) Subject: Getting a value that follows string.find() In-Reply-To: References: <40816fed-38d4-4baa-92cc-c80cd8febd82@googlegroups.com> Message-ID: <5b73c6fe-a282-4d28-ab29-2e1dfdd09290@googlegroups.com> On Tuesday, August 13, 2013 5:58:07 PM UTC-5, Joel Goldstick wrote: > lookup urlparse for you answer > > > > On Tue, Aug 13, 2013 at 6:51 PM, <> wrote: > > > I know the title doesn't make much sense, but I didnt know how to explain my problem. > > > > > > Anywho, I've opened a page's source in URLLIB > > > starturlsource = starturlopen.read() > > > string.find(starturlsource, '> >> > I want to store what comes after ?id= in a variable. >> >> > Can someone help me with this? >> >> > -- >> >> > http://mail.python.org/mailman/listinfo/python-list >> >> >> >> >> >> >> >> -- >> >> Joel Goldstick >> >> http://joelgoldstick.com > > I dont want to do any kind of HTML parsing. Aside from the fact that I really want a pony, and you seem to want your work done for you, look here: http://stackoverflow.com/questions/11600681/parse-query-part-from-url > -- > http://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com From joel.goldstick at gmail.com Tue Aug 13 19:40:22 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 13 Aug 2013 19:40:22 -0400 Subject: Getting a value that follows string.find() In-Reply-To: References: <40816fed-38d4-4baa-92cc-c80cd8febd82@googlegroups.com> <5b73c6fe-a282-4d28-ab29-2e1dfdd09290@googlegroups.com> Message-ID: On Tue, Aug 13, 2013 at 7:18 PM, Joel Goldstick wrote: > On Tue, Aug 13, 2013 at 7:03 PM, wrote: >> On Tuesday, August 13, 2013 5:58:07 PM UTC-5, Joel Goldstick wrote: >>> lookup urlparse for you answer >>> >>> >>> >>> On Tue, Aug 13, 2013 at 6:51 PM, <> wrote: >>> >>> > I know the title doesn't make much sense, but I didnt know how to explain my problem. >>> >>> > >>> >>> > Anywho, I've opened a page's source in URLLIB >>> >>> > starturlsource = starturlopen.read() >>> >>> > string.find(starturlsource, '> >> > I want to store what comes after ?id= in a variable. >> >> > Can someone help me with this? >> lookup urlparse for you answer > I dont want to do any kind of HTML parsing. What you are doing *is* HTML parsing, or at least a half-baked, fragile, likely to go wrong form of parsing. But if you insist, the algorithm is simple: after calling find(), you have the offset to the search string. You know the length of the search string. Therefore you can calculate the index of the first character that follows the search string: text = "blah blah blah blah spam spam... blah blah blah blah..." needle = "spam spam" # what we search for i = text.find(needle) if i == -1: print("not found") else: print(text[i+len(needle):]) Of course, the problem is, you need to know not just the *start* offset of the bit that follows, but the *ending* offset as well. Which brings you into the realm of half-arsed parsing. -- Steven From davea at davea.name Tue Aug 13 21:31:58 2013 From: davea at davea.name (Dave Angel) Date: Wed, 14 Aug 2013 01:31:58 +0000 (UTC) Subject: Getting a value that follows string.find() References: <40816fed-38d4-4baa-92cc-c80cd8febd82@googlegroups.com> Message-ID: englishkevin110 at gmail.com wrote: > I know the title doesn't make much sense, but I didnt know how to explain my problem. > > Anywho, I've opened a page's source in URLLIB > starturlsource = starturlopen.read() > string.find(starturlsource, '>> import string >>> help(string.find) Traceback (most recent call last): File "", line 1, in AttributeError: 'module' object has no attribute 'find' There is no find function in the string module [1]. But assuming starturlsource is a str, you could do: pattern = ' I want to store what comes after ?id= in a variable. > Can someone help me with this? starturlsource = starturlopen.read() match_string = ' Can anyone suggest a way to get a pair of file descriptor numbers such that data written to one can be read from the other and vice versa? Is there anything like os.pipe() where you can read/write both ends? Thanks! From rhodri at wildebst.demon.co.uk Tue Aug 13 20:17:40 2013 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Wed, 14 Aug 2013 01:17:40 +0100 Subject: Pair of filenos read/write each other? References: Message-ID: On Wed, 14 Aug 2013 00:10:41 +0100, Jack Bates wrote: > Can anyone suggest a way to get a pair of file descriptor numbers such > that data written to one can be read from the other and vice versa? > > Is there anything like os.pipe() where you can read/write both ends? Sockets? It depends a bit on what you're trying to do, exactly. If you give us a bit more context, we might be able to give you better advice. -- Rhodri James *-* Wildebeest Herder to the Masses From rosuav at gmail.com Tue Aug 13 20:55:38 2013 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 14 Aug 2013 01:55:38 +0100 Subject: Pair of filenos read/write each other? In-Reply-To: References: Message-ID: On Wed, Aug 14, 2013 at 1:17 AM, Rhodri James wrote: > On Wed, 14 Aug 2013 00:10:41 +0100, Jack Bates > wrote: > >> Can anyone suggest a way to get a pair of file descriptor numbers such >> that data written to one can be read from the other and vice versa? >> >> Is there anything like os.pipe() where you can read/write both ends? > > > Sockets? It depends a bit on what you're trying to do, exactly. If you > give us a bit more context, we might be able to give you better advice. Specific questions: 1) Do you need different processes to do the reading/writing? 2) Do you need to separate individual writes (message mode)? ChrisA From tdhfwh at nottheoilrig.com Thu Aug 15 11:59:29 2013 From: tdhfwh at nottheoilrig.com (Jack Bates) Date: Thu, 15 Aug 2013 08:59:29 -0700 Subject: Pair of filenos read/write each other? In-Reply-To: References: Message-ID: <20130815155928.GB28005@kolibri> On Wed, Aug 14, 2013 at 01:55:38AM +0100, Chris Angelico wrote: > On Wed, Aug 14, 2013 at 1:17 AM, Rhodri James > wrote: > > On Wed, 14 Aug 2013 00:10:41 +0100, Jack Bates > > wrote: > > > >> Can anyone suggest a way to get a pair of file descriptor numbers such > >> that data written to one can be read from the other and vice versa? > >> > >> Is there anything like os.pipe() where you can read/write both ends? > > > > > > Sockets? It depends a bit on what you're trying to do, exactly. If you > > give us a bit more context, we might be able to give you better advice. > > Specific questions: > > 1) Do you need different processes to do the reading/writing? No, only one process is involved. > 2) Do you need to separate individual writes (message mode)? No, data can be split into smaller writes or merged into a bigger write, so long as it arrives in order. > ChrisA Thank you for your help! From tdhfwh at nottheoilrig.com Thu Aug 15 11:56:10 2013 From: tdhfwh at nottheoilrig.com (Jack Bates) Date: Thu, 15 Aug 2013 08:56:10 -0700 Subject: Pair of filenos read/write each other? In-Reply-To: References: Message-ID: <20130815155601.GA28005@kolibri> On Wed, Aug 14, 2013 at 01:17:40AM +0100, Rhodri James wrote: > On Wed, 14 Aug 2013 00:10:41 +0100, Jack Bates > wrote: > > > Can anyone suggest a way to get a pair of file descriptor numbers such > > that data written to one can be read from the other and vice versa? > > > > Is there anything like os.pipe() where you can read/write both ends? > > Sockets? It depends a bit on what you're trying to do, exactly. If you > give us a bit more context, we might be able to give you better advice. Thanks, I am writing a fixture to test an app. The app normally uses the Python wrapper for the GnuTLS library to handshake with a service that I want to mock up in my fixture. The app passes a file descriptor number to gnutls_transport_set_ptr() When I test the app, the app and fixture run in the same process. I want a file descriptor number I can foist on the app, and in the fixture access what is read/written to it by the GnuTLS wrapper. From roy at panix.com Tue Aug 13 21:37:55 2013 From: roy at panix.com (Roy Smith) Date: Tue, 13 Aug 2013 21:37:55 -0400 Subject: Pair of filenos read/write each other? References: Message-ID: In article , Jack Bates wrote: > Can anyone suggest a way to get a pair of file descriptor numbers such > that data written to one can be read from the other and vice versa? > > Is there anything like os.pipe() where you can read/write both ends? > > Thanks! I think you're looking for socket.socketpair() From nobody at nowhere.com Wed Aug 14 03:21:36 2013 From: nobody at nowhere.com (Nobody) Date: Wed, 14 Aug 2013 08:21:36 +0100 Subject: Pair of filenos read/write each other? References: Message-ID: On Tue, 13 Aug 2013 16:10:41 -0700, Jack Bates wrote: > Is there anything like os.pipe() where you can read/write both ends? There's socket.socketpair(), but it's only available on Unix. Windows doesn't have AF_UNIX sockets, and anonymous pipes (like the ones created by os.pipe()) aren't bidirectional. Named pipes are bidirectional, but you would need to choose a name, create one, connect, and accept (like with sockets); there's no convenience function like socketpair(). Also, you need to consider the security implications, as other processes can (try to) connect to a named pipe. From solipsis at pitrou.net Wed Aug 14 04:34:36 2013 From: solipsis at pitrou.net (Antoine Pitrou) Date: Wed, 14 Aug 2013 08:34:36 +0000 (UTC) Subject: Pair of filenos read/write each other? References: Message-ID: Nobody nowhere.com> writes: > > On Tue, 13 Aug 2013 16:10:41 -0700, Jack Bates wrote: > > > Is there anything like os.pipe() where you can read/write both ends? > > There's socket.socketpair(), but it's only available on Unix. > > Windows doesn't have AF_UNIX sockets, and anonymous pipes (like the ones > created by os.pipe()) aren't bidirectional. I'm not sure I understand the problem: you can just create two pair of pipes using os.pipe(). If that's too low-level, you can wrap the fds using BufferedRWPair: http://docs.python.org/3.3/library/io.html#io.BufferedRWPair (actual incantation would be: r1, w1 = os.pipe() r2, w2 = os.pipe() end1 = io.BufferedRWPair(io.FileIO(r1, 'r'), io.FileIO(w2, 'w')) end2 = io.BufferedRWPair(io.FileIO(r2, 'r'), io.FileIO(w1, 'w')) end1.write(b"foo") end1.flush() end2.read(3) # -> return b"foo" ) An alternative is to use multiprocessing.Pipe(): http://docs.python.org/3.3/library/multiprocessing.html#multiprocessing.Pipe In any case, Python doesn't lack facilities for doing what you want. Regards Antoine. From tdhfwh at nottheoilrig.com Thu Aug 15 12:19:52 2013 From: tdhfwh at nottheoilrig.com (Jack Bates) Date: Thu, 15 Aug 2013 09:19:52 -0700 Subject: Pair of filenos read/write each other? In-Reply-To: References: Message-ID: <20130815161937.GA6494@kolibri> On Wed, Aug 14, 2013 at 08:34:36AM +0000, Antoine Pitrou wrote: > Nobody nowhere.com> writes: > > On Tue, 13 Aug 2013 16:10:41 -0700, Jack Bates wrote: > > > Is there anything like os.pipe() where you can read/write both ends? > > > > There's socket.socketpair(), but it's only available on Unix. > > > > Windows doesn't have AF_UNIX sockets, and anonymous pipes (like the ones > > created by os.pipe()) aren't bidirectional. > > I'm not sure I understand the problem: you can just create two pair of pipes > using os.pipe(). > If that's too low-level, you can wrap the fds using BufferedRWPair: > http://docs.python.org/3.3/library/io.html#io.BufferedRWPair > > (actual incantation would be: > r1, w1 = os.pipe() > r2, w2 = os.pipe() > > end1 = io.BufferedRWPair(io.FileIO(r1, 'r'), io.FileIO(w2, 'w')) > end2 = io.BufferedRWPair(io.FileIO(r2, 'r'), io.FileIO(w1, 'w')) > > end1.write(b"foo") > end1.flush() > end2.read(3) # -> return b"foo" > ) > > An alternative is to use multiprocessing.Pipe(): > http://docs.python.org/3.3/library/multiprocessing.html#multiprocessing.Pipe > > In any case, Python doesn't lack facilities for doing what you want. Thank you for your help, I need to satisfy an interface that requires a single file descriptor number that can be both read from and written to. Is it possible with any of the solutions you pointed out to get a single file descriptor number for each end? From solipsis at pitrou.net Thu Aug 15 14:24:05 2013 From: solipsis at pitrou.net (Antoine Pitrou) Date: Thu, 15 Aug 2013 18:24:05 +0000 (UTC) Subject: Pair of filenos read/write each other? References: <20130815161937.GA6494@kolibri> Message-ID: Jack Bates nottheoilrig.com> writes: > > > > An alternative is to use multiprocessing.Pipe(): > > http://docs.python.org/3.3/library/multiprocessing.html#multiprocessing.Pipe > > > > In any case, Python doesn't lack facilities for doing what you want. > > Thank you for your help, I need to satisfy an interface that requires a single > file descriptor number that can be both read from and written to. Is it > possible with any of the solutions you pointed out to get a single file > descriptor number for each end? Yes, it is what multiprocessing.Pipe() provides: http://docs.python.org/3.3/library/multiprocessing.html#multiprocessing.Connection.fileno Regards Antoine. From veritatem.ignotam at gmail.com Tue Aug 13 20:38:35 2013 From: veritatem.ignotam at gmail.com (Veritatem Ignotam) Date: Tue, 13 Aug 2013 20:38:35 -0400 Subject: Subprocess embedded in curses window Message-ID: <520AD18B.1070505@gmail.com> Hey All, I'm fairly new to python and even newer to curses. Does any one have a good solution for how to embed the output of a subprocess (ex. subprocess.Popen("htop", stdout=subprocess.PIPE)) into an ncurses window? So for example, the terminal window is broken up into quadrants and the top right has htop running inside. I'd imagine this would involve some kind of terminal emulation as the dimensions of the window would need to be queried by htop. If anyone could please point me in the right direction, I'd be very grateful Thanks, Ignotus From eschneider92 at comcast.net Wed Aug 14 00:51:24 2013 From: eschneider92 at comcast.net (eschneider92 at comcast.net) Date: Tue, 13 Aug 2013 21:51:24 -0700 (PDT) Subject: .split() Qeustion Message-ID: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> How can I use the '.split()' method (am I right in calling it a method?) without instead of writing each comma between words in the pie list in the following code? Also, is there a way to use .split instead of typing the apostrophes? Thank you. import random pie=['keylime', 'peach', 'apple', 'cherry', 'pecan'] print(random.choice(pie)) Eric From gary.herron at islandtraining.com Wed Aug 14 01:12:56 2013 From: gary.herron at islandtraining.com (Gary Herron) Date: Tue, 13 Aug 2013 22:12:56 -0700 Subject: .split() Qeustion In-Reply-To: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> Message-ID: <520B11D8.8090801@islandtraining.com> On 08/13/2013 09:51 PM, eschneider92 at comcast.net wrote: > How can I use the '.split()' method (am I right in calling it a method?) without instead of writing each comma between words in the pie list in the following code? Also, is there a way to use .split instead of typing the apostrophes? Thank you. > > import random > pie=['keylime', 'peach', 'apple', 'cherry', 'pecan'] > print(random.choice(pie)) > > Eric I think you are referring to this: pie = 'keylime peach apple cherry pecan'.split() While it's easier to type, and does save a few characters, I think the original list is clearer to a reader of your program. Gary Herron From davea at davea.name Wed Aug 14 01:35:40 2013 From: davea at davea.name (Dave Angel) Date: Wed, 14 Aug 2013 05:35:40 +0000 (UTC) Subject: .split() Qeustion References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> Message-ID: eschneider92 at comcast.net wrote: > How can I use the '.split()' method (am I right in calling it a method?) without instead of writing each comma between words in the pie list in the following code? Also, is there a way to use .split instead of typing the apostrophes? Thank you. > > import random > pie=['keylime', 'peach', 'apple', 'cherry', 'pecan'] > print(random.choice(pie)) > I can't make any sense out of the first sentence. But maybe I can guess what you're looking for. The split() method is indeed a method of the str class. It takes an optional argument for the separator character. By default it uses whitespace. So if you're trying to specify a series of items, none of which contain any whitespace, you can readily use split to build your list: pie = "keylime peach apple cherry pecan".split() However, there'd be no way to specify an item called "chocolate marshmallow". If you need to include whitespace in any item, then you'd have to use some other separator, like a comma: pie = "keylime,chocolate marshmallow,peach,apple,cherry, pecan".split(",") -- Signature file not found From i.am.songoku at gmail.com Wed Aug 14 01:37:51 2013 From: i.am.songoku at gmail.com (Krishnan Shankar) Date: Tue, 13 Aug 2013 22:37:51 -0700 Subject: .split() Qeustion In-Reply-To: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> Message-ID: Hi, >How can I use the '.split()' method (am I right in calling it a method?) The .split() is a method in Python which comes as in built method for String objects in Python. Any string defined in python will have the ability to call this function. >>> var = 'Hello how r u?' >>> dir(var) ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'replace', 'rfind', 'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] >>> var.split() ['Hello', 'how', 'r', 'u?'] >>> >writing each comma between words in the pie list in the following code? Also, is there >a way to use .split instead of typing the apostrophes? Thank you. >import random >pie=['keylime', 'peach', 'apple', 'cherry', 'pecan'] >print(random.choice(pie)) If you are talking about having predefined list pie with limited elements like above it is ok to code them straightaway with apostrophes and others will know that it is a predefined list. Suppose if the elements in list come as a line in a file or is a string, it will be better to use split() method and form a list. I hope Gary has provided the example for the same. pie = 'keylime peach apple cherry pecan'.split() I hope this clarifies your doubt. Regards, Krishnan On Tue, Aug 13, 2013 at 9:51 PM, wrote: > How can I use the '.split()' method (am I right in calling it a method?) > without instead of writing each comma between words in the pie list in the > following code? Also, is there a way to use .split instead of typing the > apostrophes? Thank you. > > import random > pie=['keylime', 'peach', 'apple', 'cherry', 'pecan'] > print(random.choice(pie)) > > Eric > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From eschneider92 at comcast.net Wed Aug 14 01:44:47 2013 From: eschneider92 at comcast.net (eschneider92 at comcast.net) Date: Tue, 13 Aug 2013 22:44:47 -0700 (PDT) Subject: .split() Qeustion In-Reply-To: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> Message-ID: <5198daa0-fe17-44cc-bd6b-8ba4dddd1333@googlegroups.com> It's obvious that the word 'without' in my first sentence was meant to be ommited, and it's a simple question. Thank Gary! From alister.ware at ntlworld.com Wed Aug 14 04:30:17 2013 From: alister.ware at ntlworld.com (Alister) Date: Wed, 14 Aug 2013 08:30:17 GMT Subject: .split() Qeustion References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> Message-ID: On Tue, 13 Aug 2013 22:12:56 -0700, Gary Herron wrote: > On 08/13/2013 09:51 PM, eschneider92 at comcast.net wrote: >> How can I use the '.split()' method (am I right in calling it a >> method?) without instead of writing each comma between words in the pie >> list in the following code? Also, is there a way to use .split instead >> of typing the apostrophes? Thank you. >> >> import random pie=['keylime', 'peach', 'apple', 'cherry', 'pecan'] >> print(random.choice(pie)) >> >> Eric > > I think you are referring to this: > pie = 'keylime peach apple cherry pecan'.split() > > While it's easier to type, and does save a few characters, I think the > original list is clearer to a reader of your program. > > Gary Herron I would agree with the last statement. Please write list definitions as lists rather than taking a short-cut to save a few key presses -- Accuracy, n.: The vice of being right From joshua at landau.ws Wed Aug 14 06:31:01 2013 From: joshua at landau.ws (Joshua Landau) Date: Wed, 14 Aug 2013 11:31:01 +0100 Subject: .split() Qeustion In-Reply-To: References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> Message-ID: On 14 August 2013 09:30, Alister wrote: > On Tue, 13 Aug 2013 22:12:56 -0700, Gary Herron wrote: > >> On 08/13/2013 09:51 PM, eschneider92 at comcast.net wrote: >>> How can I use the '.split()' method (am I right in calling it a >>> method?) without instead of writing each comma between words in the pie >>> list in the following code? Also, is there a way to use .split instead >>> of typing the apostrophes? Thank you. >>> >>> import random pie=['keylime', 'peach', 'apple', 'cherry', 'pecan'] >>> print(random.choice(pie)) >>> >>> Eric >> >> I think you are referring to this: >> pie = 'keylime peach apple cherry pecan'.split() >> >> While it's easier to type, and does save a few characters, I think the >> original list is clearer to a reader of your program. >> >> Gary Herron > > I would agree with the last statement. > Please write list definitions as lists rather than taking a short-cut to > save a few key presses That's true with this example, but is: lines = [ "Developments in high-speed rail, and high-speed", "transport more generally, have historically been", "impeded by the difficulties in managing friction", "and air resistance, both of which become", "substantial when vehicles approach high speeds.", "The vactrain concept eliminates these obstacles", "by employing magnetically levitating trains in", "tubes kept at a complete vacuum, allowing for", "heoretical speeds of thousands of miles per", "hour. The high cost of constructing such a system,", "however, and the difficulty of maintaining a", "vacuum over large distances, has prevented this", "type of system from ever being built. The", "Hyperloop can be viewed as a modified vactrain,", "employing more cost-effective solutions to the", "same problems the latter was designed to solve." ] really more readable than: lines = """\ Developments in high-speed rail, and high-speed transport more generally, have historically been impeded by the difficulties in managing friction and air resistance, both of which become substantial when vehicles approach high speeds. The vactrain concept eliminates these obstacles by employing magnetically levitating trains in tubes kept at a complete vacuum, allowing for heoretical speeds of thousands of miles per hour. The high cost of constructing such a system, however, and the difficulty of maintaining a vacuum over large distances, has prevented this type of system from ever being built. The Hyperloop can be viewed as a modified vactrain, employing more cost-effective solutions to the same problems the latter was designed to solve. """[1:-1].split("\n") ? Additionally,namedtuple has already set the precedence for this kind of thing. Finally, a simple extension or a decent editor should make it trivial to convert between the forms, so you can write the shorter way and convert on-the-fly. From __peter__ at web.de Wed Aug 14 07:45:19 2013 From: __peter__ at web.de (Peter Otten) Date: Wed, 14 Aug 2013 13:45:19 +0200 Subject: .split() Qeustion References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> Message-ID: Joshua Landau wrote: > On 14 August 2013 09:30, Alister wrote: >> On Tue, 13 Aug 2013 22:12:56 -0700, Gary Herron wrote: >> >>> On 08/13/2013 09:51 PM, eschneider92 at comcast.net wrote: >>>> How can I use the '.split()' method (am I right in calling it a >>>> method?) without instead of writing each comma between words in the pie >>>> list in the following code? Also, is there a way to use .split instead >>>> of typing the apostrophes? Thank you. >>>> >>>> import random pie=['keylime', 'peach', 'apple', 'cherry', 'pecan'] >>>> print(random.choice(pie)) >>>> >>>> Eric >>> >>> I think you are referring to this: >>> pie = 'keylime peach apple cherry pecan'.split() >>> >>> While it's easier to type, and does save a few characters, I think the >>> original list is clearer to a reader of your program. >>> >>> Gary Herron >> >> I would agree with the last statement. >> Please write list definitions as lists rather than taking a short-cut to >> save a few key presses > > That's true with this example, but is: > > lines = [ > "Developments in high-speed rail, and high-speed", ... > "same problems the latter was designed to solve." > ] > > really more readable than: > > lines = """\ > Developments in high-speed rail, and high-speed ... > same problems the latter was designed to solve. > """[1:-1].split("\n") > > ? It's definitely more correct -- unless you meant to strip the "D" from the first line ;) I would use lines = """\ Developments in high-speed rail, and high-speed ... same problems the latter was designed to solve. """.splitlines() From joshua at landau.ws Wed Aug 14 07:55:23 2013 From: joshua at landau.ws (Joshua Landau) Date: Wed, 14 Aug 2013 12:55:23 +0100 Subject: .split() Qeustion In-Reply-To: References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> Message-ID: On 14 August 2013 12:45, Peter Otten <__peter__ at web.de> wrote: > Joshua Landau wrote: >> On 14 August 2013 09:30, Alister wrote: >>> I would agree with the last statement. >>> Please write list definitions as lists rather than taking a short-cut to >>> save a few key presses >> >> That's true with this example, but is: >> >> lines = [ >> "Developments in high-speed rail, and high-speed", > ... >> "same problems the latter was designed to solve." >> ] >> >> really more readable than: >> >> lines = """\ >> Developments in high-speed rail, and high-speed > ... >> same problems the latter was designed to solve. >> """[1:-1].split("\n") >> >> ? > > It's definitely more correct -- unless you meant to strip the "D" from the > first line ;) > > I would use > > lines = """\ > Developments in high-speed rail, and high-speed > ... > same problems the latter was designed to solve. > """.splitlines() Thanks, I didn't actually know about .splitlines()! From wxjmfauth at gmail.com Wed Aug 14 10:32:03 2013 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Wed, 14 Aug 2013 07:32:03 -0700 (PDT) Subject: .split() Qeustion In-Reply-To: References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> Message-ID: Le mercredi 14 ao?t 2013 13:55:23 UTC+2, Joshua Landau a ?crit?: > On 14 August 2013 12:45, Peter Otten <__peter__ at web.de> wrote: > > > Joshua Landau wrote: > > >> On 14 August 2013 09:30, Alister wrote: > > >>> I would agree with the last statement. > > >>> Please write list definitions as lists rather than taking a short-cut to > > >>> save a few key presses > > >> > > >> That's true with this example, but is: > > >> > > >> lines = [ > > >> "Developments in high-speed rail, and high-speed", > > > ... > > >> "same problems the latter was designed to solve." > > >> ] > > >> > > >> really more readable than: > > >> > > >> lines = """\ > > >> Developments in high-speed rail, and high-speed > > > ... > > >> same problems the latter was designed to solve. > > >> """[1:-1].split("\n") > > >> > > >> ? > > > > > > It's definitely more correct -- unless you meant to strip the "D" from the > > > first line ;) > > > > > > I would use > > > > > > lines = """\ > > > Developments in high-speed rail, and high-speed > > > ... > > > same problems the latter was designed to solve. > > > """.splitlines() > > > > Thanks, I didn't actually know about .splitlines()! a = ['==\r**', '==\n**', '==\r\n**', '==\u0085**', '==\u000b**', '==\u000c**', '==\u2028**', '==\u2029**'] for e in a: print(e.splitlines()) ['==', '**'] ['==', '**'] ['==', '**'] ['==', '**'] ['==', '**'] ['==', '**'] ['==', '**'] ['==', '**'] Do not confuse these NLF's (new line functions) in the Unicode terminology, with the end of line *symbols* (pilcrow, \u2424, ...) I'm always and still be suprised by the number of hard coded '\n' one can find in Python code when the portable (here win) >>> os.linesep '\r\n' exists. jmf From random832 at fastmail.us Wed Aug 14 13:05:50 2013 From: random832 at fastmail.us (random832 at fastmail.us) Date: Wed, 14 Aug 2013 13:05:50 -0400 Subject: .split() Qeustion In-Reply-To: References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> Message-ID: <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> On Wed, Aug 14, 2013, at 10:32, wxjmfauth at gmail.com wrote: > I'm always and still be suprised by the number of hard coded > '\n' one can find in Python code when the portable (here > win) > > >>> os.linesep > '\r\n' > > exists. Because high-level code isn't supposed to use the os module directly. Text-mode streams automatically convert newlines you write to them. From rosuav at gmail.com Wed Aug 14 13:14:59 2013 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 14 Aug 2013 18:14:59 +0100 Subject: .split() Qeustion In-Reply-To: <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> Message-ID: On Wed, Aug 14, 2013 at 6:05 PM, wrote: > On Wed, Aug 14, 2013, at 10:32, wxjmfauth at gmail.com wrote: >> I'm always and still be suprised by the number of hard coded >> '\n' one can find in Python code when the portable (here >> win) >> >> >>> os.linesep >> '\r\n' >> >> exists. > > Because high-level code isn't supposed to use the os module directly. > Text-mode streams automatically convert newlines you write to them. I'm always, and will still be, surprised by the number of hard coded decimal integers one can find in Python code, when the portable way to do it is to use ctypes and figure out whether your literals should be big-endian or little-endian, 32-bit or 64-bit, etc. Yet people continue to just put decimal literals in their code! It can't be portable. ChrisA From python.list at tim.thechases.com Wed Aug 14 13:29:56 2013 From: python.list at tim.thechases.com (Tim Chase) Date: Wed, 14 Aug 2013 12:29:56 -0500 Subject: .split() Qeustion In-Reply-To: References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> Message-ID: <20130814122956.5779e621@bigbox.christie.dr> On 2013-08-14 18:14, Chris Angelico wrote: > On Wed, Aug 14, 2013 at 6:05 PM, wrote: > > On Wed, Aug 14, 2013, at 10:32, wxjmfauth at gmail.com wrote: > >> I'm always and still be suprised by the number of hard coded > >> '\n' one can find in Python code when the portable (here > >> win) > >> > >> >>> os.linesep > >> '\r\n' > >> > >> exists. > > > > Because high-level code isn't supposed to use the os module > > directly. Text-mode streams automatically convert newlines you > > write to them. > > I'm always, and will still be, surprised by the number of hard coded > decimal integers one can find in Python code, when the portable way > to do it is to use ctypes and figure out whether your literals > should be big-endian or little-endian, 32-bit or 64-bit, etc. Yet > people continue to just put decimal literals in their code! It > can't be portable. No, no, no...you want from sys.platform.integers import 0, 1, 2, 3, 14, 42 to be portable against endian'ness and bit-width. Granted, one might confuse them with regular numeric literals, so it would be best to clarify them by namespace: import sys answer_to_life = sys.platform.integers.42 print(sum(range(sys.platform.integers.0, sys.platform.integers.14))) That way you ensure platform independence, and *much* clearer! ;-) -tkc From rosuav at gmail.com Wed Aug 14 13:46:07 2013 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 14 Aug 2013 18:46:07 +0100 Subject: .split() Qeustion In-Reply-To: <20130814122956.5779e621@bigbox.christie.dr> References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <20130814122956.5779e621@bigbox.christie.dr> Message-ID: On Wed, Aug 14, 2013 at 6:29 PM, Tim Chase wrote: > On 2013-08-14 18:14, Chris Angelico wrote: >> On Wed, Aug 14, 2013 at 6:05 PM, wrote: >> > On Wed, Aug 14, 2013, at 10:32, wxjmfauth at gmail.com wrote: >> >> I'm always and still be suprised by the number of hard coded >> >> '\n' one can find in Python code when the portable (here >> >> win) >> >> >> >> >>> os.linesep >> >> '\r\n' >> >> >> >> exists. >> > >> > Because high-level code isn't supposed to use the os module >> > directly. Text-mode streams automatically convert newlines you >> > write to them. >> >> I'm always, and will still be, surprised by the number of hard coded >> decimal integers one can find in Python code, when the portable way >> to do it is to use ctypes and figure out whether your literals >> should be big-endian or little-endian, 32-bit or 64-bit, etc. Yet >> people continue to just put decimal literals in their code! It >> can't be portable. > > No, no, no...you want > > from sys.platform.integers import 0, 1, 2, 3, 14, 42 > > to be portable against endian'ness and bit-width. Oh! I didn't know about sys.platform.integers. All this time I've been doing it manually, usually copying and pasting a block of integer definitions from the re module. (I used to copy them from adamant.princess.ida but some of them were buggy. 2+2 made 5, or 3, or 7, or 25, depending on need.) ChrisA From skip at pobox.com Wed Aug 14 13:38:07 2013 From: skip at pobox.com (Skip Montanaro) Date: Wed, 14 Aug 2013 12:38:07 -0500 Subject: .split() Qeustion In-Reply-To: <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> Message-ID: On Wed, Aug 14, 2013 at 12:05 PM, wrote: > Because high-level code isn't supposed to use the os module directly. That seems a bit extreme. One would hope that Guido and the rest of the crew created the os module so people would use it instead of resorting to other lower level hacks. A quick find/grep of my own code suggests that I import os more than sys. I use it mostly for os.path.* and os.environ. I'm not sure there's a higher level way to access them without putting more layers between your code and those objects, which code would obviously have to call them anyway. Did I just misread your comment? Skip From tjreedy at udel.edu Wed Aug 14 15:45:10 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 14 Aug 2013 15:45:10 -0400 Subject: .split() Qeustion In-Reply-To: <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> Message-ID: On 8/14/2013 1:05 PM, random832 at fastmail.us wrote: > On Wed, Aug 14, 2013, at 10:32, wxjmfauth at gmail.com wrote: >> I'm always and still be suprised by the number of hard coded >> '\n' one can find in Python code when the portable (here >> win) >> >>>>> os.linesep >> '\r\n' >> >> exists. > Because high-level code isn't supposed to use the os module directly. This is a bit extreme, but definitely true for os.linesep and *much* of os other than os.path and maybe os.environ. > Text-mode streams automatically convert newlines you write to them. By default, to \n when reading files;, \n to os.linesep when writing. Windows is the only major OS for which os.linesep is not \n. The full details, from the builtin 'open' entry: " newline controls how universal newlines mode works (it only applies to text mode). It can be None, '', '\n', '\r', and '\r\n'. It works as follows: When reading input from the stream, if newline is None, universal newlines mode is enabled. Lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n' before being returned to the caller. If it is '', universal newlines mode is enabled, but line endings are returned to the caller untranslated. If it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated. When writing output to the stream, if newline is None, any '\n' characters written are translated to the system default line separator, os.linesep. If newline is '' or '\n', no translation takes place. If newline is any of the other legal values, any '\n' characters written are translated to the given string. " -- Terry Jan Reedy From wxjmfauth at gmail.com Thu Aug 15 03:46:14 2013 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Thu, 15 Aug 2013 00:46:14 -0700 (PDT) Subject: .split() Qeustion In-Reply-To: References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> Message-ID: <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> Le mercredi 14 ao?t 2013 19:14:59 UTC+2, Chris Angelico a ?crit?: > On Wed, Aug 14, 2013 at 6:05 PM, wrote: > > > On Wed, Aug 14, 2013, at 10:32, wxjmfauth at gmail.com wrote: > > >> I'm always and still be suprised by the number of hard coded > > >> '\n' one can find in Python code when the portable (here > > >> win) > > >> > > >> >>> os.linesep > > >> '\r\n' > > >> > > >> exists. > > > > > > Because high-level code isn't supposed to use the os module directly. > > > Text-mode streams automatically convert newlines you write to them. > > > > I'm always, and will still be, surprised by the number of hard coded > > decimal integers one can find in Python code, when the portable way to > > do it is to use ctypes and figure out whether your literals should be > > big-endian or little-endian, 32-bit or 64-bit, etc. Yet people > > continue to just put decimal literals in their code! It can't be > > portable. > > > > ChrisA ------ As a stupid scientist, I have the habbit to compare things of the same nature with the same units. This *string* containing one *character* >>> sys.getsizeof('a') 26 consumes 26 *bytes*. This *string* containing one *character* >>> sys.getsizeof('\u2023') 40 consumes 40 *bytes*. and the difference is 40 [bytes] - 26 [bytes] = 14 [bytes] . ????? Python seems to consider os.linesep as a str. >>> isinstance(os.linesep, str) True ????? PS A "mole" is not a number. jmf From lele at metapensiero.it Thu Aug 15 10:38:07 2013 From: lele at metapensiero.it (Lele Gaifax) Date: Thu, 15 Aug 2013 16:38:07 +0200 Subject: .split() Qeustion References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> Message-ID: <87siybuii8.fsf@nautilus.nautilus> wxjmfauth at gmail.com writes: > As a stupid scientist, I have the habbit to compare > things of the same nature with the same units. > > This *string* containing one *character* > >>>> sys.getsizeof('a') > 26 > > consumes 26 *bytes*. I'm not an expert in stupid science, and I fail to see the "common" nature of the stuff you are comparing. Strings are not characters, and neither the latter are bytes. Anyway, trying to apply the same stupid science, I notice a much more amazing fact: >>> sys.getsizeof(True) 24 Does Python really needs twentyfour bytes to store a *single* bit of information?? Wow, since by definition a byte contains eight bits, there's a factor of 192... what a shame! :-) > ????? > > Python seems to consider os.linesep as a > str. > >>>> isinstance(os.linesep, str) > True Yes, I bet in stupid languages that would be either a single character, or a tuple of two or more characters, much more usable and compact. > ????? > > PS A "mole" is not a number. Oh, nice to know. And OOC, what is a "mole" in your stupid science? OTOH, WTF does that matter in current thread and with Python in general? ciao, lele. -- nickname: Lele Gaifax | Quando vivr? di quello che ho pensato ieri real: Emanuele Gaifas | comincer? ad aver paura di chi mi copia. lele at metapensiero.it | -- Fortunato Depero, 1929. From python at mrabarnett.plus.com Thu Aug 15 10:54:14 2013 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 15 Aug 2013 15:54:14 +0100 Subject: .split() Qeustion In-Reply-To: <87siybuii8.fsf@nautilus.nautilus> References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> <87siybuii8.fsf@nautilus.nautilus> Message-ID: <520CEB96.2050902@mrabarnett.plus.com> On 15/08/2013 15:38, Lele Gaifax wrote: > wxjmfauth at gmail.com writes: > >> As a stupid scientist, I have the habbit to compare >> things of the same nature with the same units. >> >> This *string* containing one *character* >> >>>>> sys.getsizeof('a') >> 26 >> >> consumes 26 *bytes*. > > I'm not an expert in stupid science, and I fail to see the "common" > nature of the stuff you are comparing. Strings are not characters, and > neither the latter are bytes. > > Anyway, trying to apply the same stupid science, I notice a much more > amazing fact: > >>>> sys.getsizeof(True) > 24 > > Does Python really needs twentyfour bytes to store a *single* bit of > information?? Wow, since by definition a byte contains eight bits, > there's a factor of 192... what a shame! > > :-) > >> ????? >> >> Python seems to consider os.linesep as a >> str. >> >>>>> isinstance(os.linesep, str) >> True > > Yes, I bet in stupid languages that would be either a single character, > or a tuple of two or more characters, much more usable and compact. > >> ????? >> >> PS A "mole" is not a number. > > Oh, nice to know. And OOC, what is a "mole" in your stupid science? > OTOH, WTF does that matter in current thread and with Python in general? > A "mole" is a term from chemistry: http://en.wikipedia.org/wiki/Mole_%28unit%29 but I have no idea how it relates to Python or even to computers in general. From lele at metapensiero.it Thu Aug 15 11:30:45 2013 From: lele at metapensiero.it (Lele Gaifax) Date: Thu, 15 Aug 2013 17:30:45 +0200 Subject: .split() Qeustion References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> <87siybuii8.fsf@nautilus.nautilus> <520CEB96.2050902@mrabarnett.plus.com> Message-ID: <87mwojug2i.fsf@nautilus.nautilus> MRAB writes: > On 15/08/2013 15:38, Lele Gaifax wrote: >> wxjmfauth at gmail.com writes: >>> PS A "mole" is not a number. >> >> Oh, nice to know. And OOC, what is a "mole" in your stupid science? >> OTOH, WTF does that matter in current thread and with Python in general? >> > A "mole" is a term from chemistry: > > http://en.wikipedia.org/wiki/Mole_%28unit%29 Yes, I know that, but AFAIU, there is a closer correlation between "a mole" and "a number" than between "a string" and "the number of bytes an arbitrary computer [language] needs to store it". It did not come out as funny as I meant :) ciao, lele. -- nickname: Lele Gaifax | Quando vivr? di quello che ho pensato ieri real: Emanuele Gaifas | comincer? ad aver paura di chi mi copia. lele at metapensiero.it | -- Fortunato Depero, 1929. From rosuav at gmail.com Thu Aug 15 11:43:41 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 15 Aug 2013 16:43:41 +0100 Subject: .split() Qeustion In-Reply-To: <87mwojug2i.fsf@nautilus.nautilus> References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> <87siybuii8.fsf@nautilus.nautilus> <520CEB96.2050902@mrabarnett.plus.com> <87mwojug2i.fsf@nautilus.nautilus> Message-ID: On Thu, Aug 15, 2013 at 4:30 PM, Lele Gaifax wrote: > MRAB writes: > >> On 15/08/2013 15:38, Lele Gaifax wrote: >>> wxjmfauth at gmail.com writes: >>>> PS A "mole" is not a number. >>> >>> Oh, nice to know. And OOC, what is a "mole" in your stupid science? >>> OTOH, WTF does that matter in current thread and with Python in general? >>> >> A "mole" is a term from chemistry: >> >> http://en.wikipedia.org/wiki/Mole_%28unit%29 > > Yes, I know that, but AFAIU, there is a closer correlation between "a > mole" and "a number" than between "a string" and "the number of bytes an > arbitrary computer [language] needs to store it". It did not come out as > funny as I meant :) A mole is as much a number (6e23) as the light year is a number (9.5e15). ChrisA From joshua at landau.ws Thu Aug 15 12:54:57 2013 From: joshua at landau.ws (Joshua Landau) Date: Thu, 15 Aug 2013 17:54:57 +0100 Subject: .split() Qeustion In-Reply-To: References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> <87siybuii8.fsf@nautilus.nautilus> <520CEB96.2050902@mrabarnett.plus.com> <87mwojug2i.fsf@nautilus.nautilus> Message-ID: On 15 August 2013 16:43, Chris Angelico wrote: > A mole is as much a number (6e23) as the light year is a number (9.5e15). A mole is a number. A light year is a unit. From rosuav at gmail.com Thu Aug 15 14:28:46 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 15 Aug 2013 19:28:46 +0100 Subject: .split() Qeustion In-Reply-To: References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> <87siybuii8.fsf@nautilus.nautilus> <520CEB96.2050902@mrabarnett.plus.com> <87mwojug2i.fsf@nautilus.nautilus> Message-ID: On Thu, Aug 15, 2013 at 5:54 PM, Joshua Landau wrote: > On 15 August 2013 16:43, Chris Angelico wrote: >> A mole is as much a number (6e23) as the light year is a number (9.5e15). > > A mole is a number. A light year is a unit. A mole is an amount of something. Avogadro's Number is a number, which is what I was hinting at :) ChrisA From joshua at landau.ws Thu Aug 15 14:40:35 2013 From: joshua at landau.ws (Joshua Landau) Date: Thu, 15 Aug 2013 19:40:35 +0100 Subject: .split() Qeustion In-Reply-To: References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> <87siybuii8.fsf@nautilus.nautilus> <520CEB96.2050902@mrabarnett.plus.com> <87mwojug2i.fsf@nautilus.nautilus> Message-ID: On 15 August 2013 19:28, Chris Angelico wrote: > On Thu, Aug 15, 2013 at 5:54 PM, Joshua Landau wrote: >> On 15 August 2013 16:43, Chris Angelico wrote: >>> A mole is as much a number (6e23) as the light year is a number (9.5e15). >> >> A mole is a number. A light year is a unit. > > A mole is an amount of something. Avogadro's Number is a number, which > is what I was hinting at :) I stand corrected. From tjreedy at udel.edu Thu Aug 15 17:40:43 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 15 Aug 2013 17:40:43 -0400 Subject: .split() Qeustion In-Reply-To: References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> <87siybuii8.fsf@nautilus.nautilus> <520CEB96.2050902@mrabarnett.plus.com> <87mwojug2i.fsf@nautilus.nautilus> Message-ID: On 8/15/2013 2:28 PM, Chris Angelico wrote: > On Thu, Aug 15, 2013 at 5:54 PM, Joshua Landau wrote: >> On 15 August 2013 16:43, Chris Angelico wrote: >>> A mole is as much a number (6e23) as the light year is a number (9.5e15). >> >> A mole is a number. A light year is a unit. > > A mole is an amount of something. Avogadro's Number is a number, which > is what I was hinting at :) The unit for 'mole' is 'ion', 'atom', or 'molecule', as appropriate for the 'something'. In other words, the units are the reacting input units and resulting output units in a particular chemical reaction. -- Terry Jan Reedy From davea at davea.name Thu Aug 15 18:56:57 2013 From: davea at davea.name (Dave Angel) Date: Thu, 15 Aug 2013 22:56:57 +0000 (UTC) Subject: .split() Qeustion References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> <87siybuii8.fsf@nautilus.nautilus> <520CEB96.2050902@mrabarnett.plus.com> <87mwojug2i.fsf@nautilus.nautilus> Message-ID: Terry Reedy wrote: > On 8/15/2013 2:28 PM, Chris Angelico wrote: >> On Thu, Aug 15, 2013 at 5:54 PM, Joshua Landau wrote: >>> On 15 August 2013 16:43, Chris Angelico wrote: >>>> A mole is as much a number (6e23) as the light year is a number (9.5e15). >>> >>> A mole is a number. A light year is a unit. >> >> A mole is an amount of something. Avogadro's Number is a number, which >> is what I was hinting at :) > > The unit for 'mole' is 'ion', 'atom', or 'molecule', as appropriate for > the 'something'. In other words, the units are the reacting input units > and resulting output units in a particular chemical reaction. > To expand a little on that, the unit of "amount of something" is a "gram mole", which is 6.2 **23 grams times the molecular (or atomic) weight. My dad (research chemist) used to have to order supplies for his lab in "ton moles", and he used some very small multipliers, since he usually needed a kilogram or less in his lab. -- DaveA From steve+comp.lang.python at pearwood.info Fri Aug 16 00:39:16 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 Aug 2013 04:39:16 GMT Subject: .split() Qeustion References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> <87siybuii8.fsf@nautilus.nautilus> <520CEB96.2050902@mrabarnett.plus.com> <87mwojug2i.fsf@nautilus.nautilus> Message-ID: <520dacf3$0$30000$c3e8da3$5496439d@news.astraweb.com> On Thu, 15 Aug 2013 22:56:57 +0000, Dave Angel wrote: > To expand a little on that, the unit of "amount of something" is a "gram > mole", which is 6.2 **23 grams times the molecular (or atomic) weight. The unit of amount of substance is mole. Gram-mole is an unfortunate synonym for mole. Unfortunate, because it looks like it should have dimensions of Mass, but it is actually a dimensionless number, and exactly equal to mole. As usual, we can blame the damn engineers and their sloppy, ad-hoc thinking for abominations like this: http://web.utk.edu/~dad/mole.html Also, you're quoting Avogadro's Constant incorrectly: it's 6.02e23, or if you prefer, 6.02*10**23, not 6.2**23, which is a factor of about 358581 too small. > My dad (research chemist) used to have to order supplies for his lab in > "ton moles", and he used some very small multipliers, since he usually > needed a kilogram or less in his lab. That's just sad. The supplier won't be counting out individual molecules, they'll be putting it on a scale and weighing it. So your dad had to convert the desired weight into a ridiculously impractical unit to place the order, and the supplier no doubt had to convert that unit back into mass in order to weigh it out and supply it. If you want a kilogram of X, why not order a kilogram of X, instead of converting it into megamol? Sigh, I know the answer to that question. "We've always done it this way." -- Steven From steve+comp.lang.python at pearwood.info Fri Aug 16 00:41:55 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 Aug 2013 04:41:55 GMT Subject: .split() Qeustion References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> <87siybuii8.fsf@nautilus.nautilus> <520CEB96.2050902@mrabarnett.plus.com> <87mwojug2i.fsf@nautilus.nautilus> <520dacf3$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: <520dad93$0$30000$c3e8da3$5496439d@news.astraweb.com> On Fri, 16 Aug 2013 04:39:16 +0000, Steven D'Aprano wrote: > On Thu, 15 Aug 2013 22:56:57 +0000, Dave Angel wrote: > >> To expand a little on that, the unit of "amount of something" is a >> "gram mole", which is 6.2 **23 grams times the molecular (or atomic) >> weight. > > The unit of amount of substance is mole. Gram-mole is an unfortunate > synonym for mole. Oops, dropped a word there... The *SI unit* of amount of substance. -- Steven From steve+comp.lang.python at pearwood.info Fri Aug 16 00:22:47 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 Aug 2013 04:22:47 GMT Subject: .split() Qeustion References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> <87siybuii8.fsf@nautilus.nautilus> <520CEB96.2050902@mrabarnett.plus.com> <87mwojug2i.fsf@nautilus.nautilus> Message-ID: <520da917$0$30000$c3e8da3$5496439d@news.astraweb.com> On Thu, 15 Aug 2013 17:40:43 -0400, Terry Reedy wrote: > On 8/15/2013 2:28 PM, Chris Angelico wrote: >> On Thu, Aug 15, 2013 at 5:54 PM, Joshua Landau >> wrote: >>> On 15 August 2013 16:43, Chris Angelico wrote: >>>> A mole is as much a number (6e23) as the light year is a number >>>> (9.5e15). >>> >>> A mole is a number. A light year is a unit. >> >> A mole is an amount of something. Avogadro's Number is a number, which >> is what I was hinting at :) > > The unit for 'mole' is 'ion', 'atom', or 'molecule', as appropriate for > the 'something'. In other words, the units are the reacting input units > and resulting output units in a particular chemical reaction. Careful about the use of the word "unit", you're likely to confuse people into thinking "atom" is a unit of measurement like inches, seconds, grams or ohms. Naturally when dealing with moles of substance you have to take into account the kind of substance. In much the same way it makes a difference whether you are catering for a dozen people, a dozen couples, or a dozen football teams, a mole of oxygen molecules is not the same as a mole of oxygen atoms. But it's still the same number of things in each case, only the thing differs. -- Steven From steve+comp.lang.python at pearwood.info Fri Aug 16 00:17:14 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 Aug 2013 04:17:14 GMT Subject: .split() Qeustion References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> <87siybuii8.fsf@nautilus.nautilus> <520CEB96.2050902@mrabarnett.plus.com> <87mwojug2i.fsf@nautilus.nautilus> Message-ID: <520da7c9$0$30000$c3e8da3$5496439d@news.astraweb.com> On Thu, 15 Aug 2013 19:28:46 +0100, Chris Angelico wrote: > On Thu, Aug 15, 2013 at 5:54 PM, Joshua Landau wrote: >> On 15 August 2013 16:43, Chris Angelico wrote: >>> A mole is as much a number (6e23) as the light year is a number >>> (9.5e15). >> >> A mole is a number. A light year is a unit. > > A mole is an amount of something. Avogadro's Number is a number, which > is what I was hinting at :) Would you consider "a dozen" to be a number? Normally we use dozen only in reference to a dozen of something, not as an abstract pure number, but it's still a number in a way that "light-year" (or "mile", or "gram", or "second") is not. Mole is like dozen. Light-year is like mile. And Avagadro's Number is like "twelve", only a bit bigger :-) -- Steven From steve+comp.lang.python at pearwood.info Fri Aug 16 00:13:05 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 Aug 2013 04:13:05 GMT Subject: .split() Qeustion References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> <87siybuii8.fsf@nautilus.nautilus> <520CEB96.2050902@mrabarnett.plus.com> <87mwojug2i.fsf@nautilus.nautilus> Message-ID: <520da6d1$0$30000$c3e8da3$5496439d@news.astraweb.com> On Thu, 15 Aug 2013 16:43:41 +0100, Chris Angelico wrote: > A mole is as much a number (6e23) as the light year is a number > (9.5e15). Not quite. A mole (abbreviation: mol) is a name for a specific number, like couple (2) or dozen (12) or gross (144), only much bigger: 6.02e23. And I can't believe I still remember that value :-) It's normally used only for atoms, ions or molecules, but in principle you could talk about anything. E.g. the population of the world is a mere 1.2e-14 mol. A light-year, on the other hand, is a dimensional quantity. Whereas mole is dimensionless, light-year has dimensions of Length, and therefore the value depends on the units you measure in: 1 light-year: = 3.724697e+17 inches = 0.30660139 parsec = 9.4607305e+12 kilometres etc. -- Steven From roy at panix.com Fri Aug 16 00:29:39 2013 From: roy at panix.com (Roy Smith) Date: Fri, 16 Aug 2013 00:29:39 -0400 Subject: .split() Qeustion References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> <87siybuii8.fsf@nautilus.nautilus> <520CEB96.2050902@mrabarnett.plus.com> <87mwojug2i.fsf@nautilus.nautilus> <520da6d1$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <520da6d1$0$30000$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > On Thu, 15 Aug 2013 16:43:41 +0100, Chris Angelico wrote: > > > A mole is as much a number (6e23) as the light year is a number > > (9.5e15). > > Not quite. A mole (abbreviation: mol) is a name for a specific number, > like couple (2) or dozen (12) or gross (144), only much bigger: 6.02e23. > And I can't believe I still remember that value :-) I remember it as 6.022e23 :-) In my high school chemistry class, there was a wooden cube, about 1/2 meter on a side, sitting on the lecture desk in the front of the room. The only writing on it was "6.022 x 10^23". It sat there all year. The volume of the cube was that of 1 mole of an ideal gas at STP. > A light-year, on the other hand, is a dimensional quantity. Whereas mole > is dimensionless, light-year has dimensions of Length, and therefore the > value depends on the units you measure in: > > 1 light-year: > > = 3.724697e+17 inches > = 0.30660139 parsec > = 9.4607305e+12 kilometres Hold your hands out in front of you, palms facing towards each other, one shoulder-width apart. That distance is about one light-nanosecond. From davea at davea.name Fri Aug 16 01:27:49 2013 From: davea at davea.name (Dave Angel) Date: Fri, 16 Aug 2013 05:27:49 +0000 (UTC) Subject: .split() Qeustion References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> <87siybuii8.fsf@nautilus.nautilus> <520CEB96.2050902@mrabarnett.plus.com> <87mwojug2i.fsf@nautilus.nautilus> <520da6d1$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: Roy Smith wrote: > In article <520da6d1$0$30000$c3e8da3$5496439d at news.astraweb.com>, > Steven D'Aprano wrote: > >> On Thu, 15 Aug 2013 16:43:41 +0100, Chris Angelico wrote: >> >> > A mole is as much a number (6e23) as the light year is a number >> > (9.5e15). >> >> Not quite. A mole (abbreviation: mol) is a name for a specific number, >> like couple (2) or dozen (12) or gross (144), only much bigger: 6.02e23. >> And I can't believe I still remember that value :-) > > I remember it as 6.022e23 :-) > > In my high school chemistry class, there was a wooden cube, about 1/2 > meter on a side, sitting on the lecture desk in the front of the room. > The only writing on it was "6.022 x 10^23". It sat there all year. > > The volume of the cube was that of 1 mole of an ideal gas at STP. > >> A light-year, on the other hand, is a dimensional quantity. Whereas mole >> is dimensionless, light-year has dimensions of Length, and therefore the >> value depends on the units you measure in: >> >> 1 light-year: >> >> = 3.724697e+17 inches >> = 0.30660139 parsec >> = 9.4607305e+12 kilometres > > Hold your hands out in front of you, palms facing towards each other, > one shoulder-width apart. That distance is about one light-nanosecond. Narrow shoulders. I figure it just under a foot. I once attended a lecture by Grace Hopper where she handed out "nanoseconds," pieces of wire about a foot long. She said that the beaurocrats were always asking how much is a nanosecond, and couldn't imagine what a billionth was like. So she gave them something physical. -- Signature file not found From gheskett at wdtv.com Fri Aug 16 10:30:28 2013 From: gheskett at wdtv.com (Gene Heskett) Date: Fri, 16 Aug 2013 10:30:28 -0400 Subject: .split() Qeustion In-Reply-To: References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> Message-ID: <201308161030.28080.gheskett@wdtv.com> On Friday 16 August 2013 10:27:36 Dave Angel did opine: > Roy Smith wrote: > > In article <520da6d1$0$30000$c3e8da3$5496439d at news.astraweb.com>, > > > > Steven D'Aprano wrote: > >> On Thu, 15 Aug 2013 16:43:41 +0100, Chris Angelico wrote: > >> > A mole is as much a number (6e23) as the light year is a number > >> > (9.5e15). > >> > >> Not quite. A mole (abbreviation: mol) is a name for a specific > >> number, like couple (2) or dozen (12) or gross (144), only much > >> bigger: 6.02e23. And I can't believe I still remember that value :-) > > > > I remember it as 6.022e23 :-) > > > > In my high school chemistry class, there was a wooden cube, about 1/2 > > meter on a side, sitting on the lecture desk in the front of the room. > > The only writing on it was "6.022 x 10^23". It sat there all year. > > > > The volume of the cube was that of 1 mole of an ideal gas at STP. > > > >> A light-year, on the other hand, is a dimensional quantity. Whereas > >> mole is dimensionless, light-year has dimensions of Length, and > >> therefore the value depends on the units you measure in: > >> > >> 1 light-year: > >> > >> = 3.724697e+17 inches > >> = 0.30660139 parsec > >> = 9.4607305e+12 kilometres > > > > Hold your hands out in front of you, palms facing towards each other, > > one shoulder-width apart. That distance is about one > > light-nanosecond. > > Narrow shoulders. > > I figure it just under a foot. I once attended a lecture by Grace > Hopper where she handed out "nanoseconds," pieces of wire about a foot > long. She said that the beaurocrats were always asking how much is a > nanosecond, and couldn't imagine what a billionth was like. So she gave > them something physical. Chuckle. I always figured Grace Hopper had to have been a stand up comic in some previous life. She sure could translate the problem into something the paper pushers could grok. Cheers, Gene -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author) My web page: is up! My views Nachman's Rule: When it comes to foreign food, the less authentic the better. -- Gerald Nachman A pen in the hand of this president is far more dangerous than 200 million guns in the hands of law-abiding citizens. From gheskett at wdtv.com Fri Aug 16 10:24:42 2013 From: gheskett at wdtv.com (Gene Heskett) Date: Fri, 16 Aug 2013 10:24:42 -0400 Subject: .split() Qeustion In-Reply-To: References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <520da6d1$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: <201308161024.42105.gheskett@wdtv.com> On Friday 16 August 2013 10:07:12 Roy Smith did opine: > In article <520da6d1$0$30000$c3e8da3$5496439d at news.astraweb.com>, > > Steven D'Aprano wrote: > > On Thu, 15 Aug 2013 16:43:41 +0100, Chris Angelico wrote: > > > A mole is as much a number (6e23) as the light year is a number > > > (9.5e15). > > > > Not quite. A mole (abbreviation: mol) is a name for a specific number, > > like couple (2) or dozen (12) or gross (144), only much bigger: > > 6.02e23. And I can't believe I still remember that value :-) > > I remember it as 6.022e23 :-) > > In my high school chemistry class, there was a wooden cube, about 1/2 > meter on a side, sitting on the lecture desk in the front of the room. > The only writing on it was "6.022 x 10^23". It sat there all year. > > The volume of the cube was that of 1 mole of an ideal gas at STP. > > > A light-year, on the other hand, is a dimensional quantity. Whereas > > mole is dimensionless, light-year has dimensions of Length, and > > therefore the value depends on the units you measure in: > > > > 1 light-year: > > > > = 3.724697e+17 inches > > = 0.30660139 parsec > > = 9.4607305e+12 kilometres > > Hold your hands out in front of you, palms facing towards each other, > one shoulder-width apart. That distance is about one light-nanosecond. Or a quite noticeable color shift when you are cutting coax cables for color phase matching, which we often had to do in an analog NTSC broadcast facility. Where a 1 degree shift, may or may not have been noticeable, was the cable equivalent of 7.7601420788892939683e-10 seconds, which was for the small foam cored cables used for such, with a Propagation Velocity of 0.78*C, only a very short length of cable. I'd have figured how much but I got lost pushing buttons in kcalc just now and came up with something I'd have to use a micrometer to measure. Its been close to 30 years since I had to do such calcs on a near daily basis. Your trivia factoid for the day, and I now return you to the regularly scheduled discussion going no where specifically. :-) Cheers, Gene -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author) My web page: is up! My views I suppose that in a few hours I will sober up. That's such a sad thought. I think I'll have a few more drinks to prepare myself. A pen in the hand of this president is far more dangerous than 200 million guns in the hands of law-abiding citizens. From greg.ewing at canterbury.ac.nz Fri Aug 16 19:16:49 2013 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Sat, 17 Aug 2013 11:16:49 +1200 Subject: .split() Qeustion In-Reply-To: References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <520da6d1$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: Gene Heskett wrote: > Where a 1 degree shift, may or may not have been noticeable, was > the cable equivalent of 7.7601420788892939683e-10 seconds, which was for > the small foam cored cables used for such, with a Propagation Velocity of > 0.78*C, only a very short length of cable. I'd have figured how much but I > got lost pushing buttons in kcalc Python says about 180mm. -- Greg From steve+comp.lang.python at pearwood.info Fri Aug 16 22:38:30 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 Aug 2013 02:38:30 GMT Subject: .split() Qeustion References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> <87siybuii8.fsf@nautilus.nautilus> <520CEB96.2050902@mrabarnett.plus.com> <87mwojug2i.fsf@nautilus.nautilus> <520da6d1$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: <520ee226$0$30000$c3e8da3$5496439d@news.astraweb.com> On Fri, 16 Aug 2013 05:27:49 +0000, Dave Angel wrote: > I figure it just under a foot. I once attended a lecture by Grace > Hopper where she handed out "nanoseconds," pieces of wire about a foot > long. Is that based on the speed of light in a vacuum, speed of light in copper, speed of electron drift in copper, speed of sound in copper? Or perhaps it was aluminium wire? :-) > She said that the beaurocrats were always asking how much is a > nanosecond, and couldn't imagine what a billionth was like. So she gave > them something physical. Hmmm, given when Grace Hopper was active in the navy, I would have thought that the simplest way to imagine a billionth would be "one dollar is a billionth of the US National Debt" or "one person is a billionth of the world's population". None of which really helps the typical person visualise a billionth, since the typical person can't really visualise a billion people or a billion dollars. I think a simple analogy that works is: the width of a single human hair is a billionth of 100 metres (or yards, for Americans). People can visualise 100 metres, and they can visualise a hair. No need to relate things to the speed of light, which most people cannot visualise, or the circumference of the earth, or the distance from New York to Tokyo, or from Venus to Mars at aphelion :-) -- Steven From rosuav at gmail.com Fri Aug 16 22:45:40 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 17 Aug 2013 03:45:40 +0100 Subject: .split() Qeustion In-Reply-To: <520ee226$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> <87siybuii8.fsf@nautilus.nautilus> <520CEB96.2050902@mrabarnett.plus.com> <87mwojug2i.fsf@nautilus.nautilus> <520da6d1$0$30000$c3e8da3$5496439d@news.astraweb.com> <520ee226$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, Aug 17, 2013 at 3:38 AM, Steven D'Aprano wrote: > On Fri, 16 Aug 2013 05:27:49 +0000, Dave Angel wrote: > >> I figure it just under a foot. I once attended a lecture by Grace >> Hopper where she handed out "nanoseconds," pieces of wire about a foot >> long. > > Is that based on the speed of light in a vacuum, speed of light in > copper, speed of electron drift in copper, speed of sound in copper? Or > perhaps it was aluminium wire? :-) > >> She said that the beaurocrats were always asking how much is a >> nanosecond, and couldn't imagine what a billionth was like. So she gave >> them something physical. > > I think a simple analogy that works is: the width of a single human hair > is a billionth of 100 metres (or yards, for Americans). People can > visualise 100 metres, and they can visualise a hair. Further information here: http://en.wikipedia.org/wiki/Grace_Hopper#Anecdotes It was specifically looking at satellite comms, and it's the speed of light through a vacuum (the ideal maximum speed of satellite communication signals). Check the Wiki page's footnotes for reliable references. ChrisA From ben+python at benfinney.id.au Fri Aug 16 01:59:24 2013 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 16 Aug 2013 15:59:24 +1000 Subject: .split() Qeustion References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> <87siybuii8.fsf@nautilus.nautilus> <520CEB96.2050902@mrabarnett.plus.com> <87mwojug2i.fsf@nautilus.nautilus> <520da6d1$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: <7wioz6kwg3.fsf@benfinney.id.au> Steven D'Aprano writes: > Not quite. A mole (abbreviation: mol) is a name for a specific number, > like couple (2) or dozen (12) or gross (144), only much bigger: 6.02e23. > And I can't believe I still remember that value :-) Avogadro's Number is worth remembering, for mocking the pseudo-science of homeopathy . By knowing the scale of Avogadro's Limit ? the maximum dilution of matter before one can confidently say the solution contains none of the original substance ? one can look at the claims of homeopathy and confidently say that it can't contain anything effective. working-to-laugh-alt-med-out-of-the-world-ly yrs, -- \ ?I'm having amnesia and d?j? vu at the same time. I feel like | `\ I've forgotten this before sometime.? ?Steven Wright | _o__) | Ben Finney From roy at panix.com Fri Aug 16 07:14:41 2013 From: roy at panix.com (Roy Smith) Date: Fri, 16 Aug 2013 07:14:41 -0400 Subject: .split() Qeustion References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> <87siybuii8.fsf@nautilus.nautilus> <520CEB96.2050902@mrabarnett.plus.com> <87mwojug2i.fsf@nautilus.nautilus> <520da6d1$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article , Ben Finney wrote: > Avogadro's Number is worth remembering, for mocking the pseudo-science > of homeopathy . You have obviously never argued science with a homeopath if you believe that knowing Avogadro's number will in any way shake their faith. From wxjmfauth at gmail.com Fri Aug 16 09:14:08 2013 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Fri, 16 Aug 2013 06:14:08 -0700 (PDT) Subject: .split() Qeustion In-Reply-To: References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> <87siybuii8.fsf@nautilus.nautilus> <520CEB96.2050902@mrabarnett.plus.com> <87mwojug2i.fsf@nautilus.nautilus> <520da6d1$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: <2d88bc0f-fdcb-4685-87ed-c17998dd3137@googlegroups.com> ----- A mole is an amount of matter measured in [kg] . The Avogadro's number can only be a dimensionless number, [1] . The Avogadro's constant is the Avogadro's number (of "pieces" or "objects") per mol, [1 / mol]. A chemist has to work and is always working in mole; as his balance can only measure a mass, the calculation mole <-> mass is always mandatory. Why did I put this "mole" in the post scriptum? Because there is a tendency on this list to be extremely confused and to compare what is not comparable (eg. comparaison of mole and Python large number, number and radian, byte and unicode transformation unit). jmf From roy at panix.com Fri Aug 16 09:23:37 2013 From: roy at panix.com (Roy Smith) Date: Fri, 16 Aug 2013 09:23:37 -0400 Subject: .split() Qeustion References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> <87siybuii8.fsf@nautilus.nautilus> <520CEB96.2050902@mrabarnett.plus.com> <87mwojug2i.fsf@nautilus.nautilus> <520da6d1$0$30000$c3e8da3$5496439d@news.astraweb.com> <2d88bc0f-fdcb-4685-87ed-c17998dd3137@googlegroups.com> Message-ID: In article <2d88bc0f-fdcb-4685-87ed-c17998dd3137 at googlegroups.com>, wxjmfauth at gmail.com wrote: > A chemist has to work and is always working in mole; as his > balance can only measure a mass, the calculation mole <-> mass > is always mandatory. That's because chemists are lazy. The recipe says, "Add one mole of carbon atoms". So, does the chemist follow the recipe and count out 6.022 x 10^23 atoms like he's supposed to? No. He says, "I don't have time for that. I'll just weigh out 12 grams. Good enough for government work." Sheesh. From wxjmfauth at gmail.com Sat Aug 17 04:09:32 2013 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Sat, 17 Aug 2013 01:09:32 -0700 (PDT) Subject: .split() Qeustion In-Reply-To: References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> <87siybuii8.fsf@nautilus.nautilus> <520CEB96.2050902@mrabarnett.plus.com> <87mwojug2i.fsf@nautilus.nautilus> <520da6d1$0$30000$c3e8da3$5496439d@news.astraweb.com> <2d88bc0f-fdcb-4685-87ed-c17998dd3137@googlegroups.com> Message-ID: <8584caa1-1593-41ac-8e0c-4ac4639913d6@googlegroups.com> Le vendredi 16 ao?t 2013 15:23:37 UTC+2, Roy Smith a ?crit?: > In article <2d88bc0f-fdcb-4685-87ed-c17998dd3137 at googlegroups.com>, > > wxjmfauth at gmail.com wrote: > > > > > A chemist has to work and is always working in mole; as his > > > balance can only measure a mass, the calculation mole <-> mass > > > is always mandatory. > > > > That's because chemists are lazy. > > > > The recipe says, "Add one mole of carbon atoms". So, does the chemist > > follow the recipe and count out 6.022 x 10^23 atoms like he's supposed > > to? No. He says, "I don't have time for that. I'll just weigh out 12 > > grams. Good enough for government work." Sheesh. -------- You don't understand the concept of "mole". In this formal reaction Na + Cl --> NaCl the chemist combines *one mole* of sodium and *one mole* of chlorine to get *one mole* of sodium chloride (cooking salt). It's independent of the number of "particles" in a mole. It's not a question of laziness, the chemist can only weight 22.98 g of sodium to work with one mole of sodium, because the nature is like this. The work with relative quantities has a name: stoichiometry. jmf From roy at panix.com Sat Aug 17 07:55:56 2013 From: roy at panix.com (Roy Smith) Date: Sat, 17 Aug 2013 07:55:56 -0400 Subject: .split() Qeustion References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> <87siybuii8.fsf@nautilus.nautilus> <520CEB96.2050902@mrabarnett.plus.com> <87mwojug2i.fsf@nautilus.nautilus> <520da6d1$0$30000$c3e8da3$5496439d@news.astraweb.com> <2d88bc0f-fdcb-4685-87ed-c17998dd3137@googlegroups.com> <8584caa1-1593-41ac-8e0c-4ac4639913d6@googlegroups.com> Message-ID: I wrote: >> The recipe says, "Add one mole of carbon atoms". So, does the >> chemist follow the recipe and count out 6.022 x 10^23 atoms like >> he's supposed to? No. He says, "I don't have time for that. I'll >> just weigh out 12 grams. Good enough for government work." Sheesh. In article <8584caa1-1593-41ac-8e0c-4ac4639913d6 at googlegroups.com>, wxjmfauth at gmail.com wrote: > You don't understand the concept of "mole". You don't understand the concept of "joke" :-) From greg.ewing at canterbury.ac.nz Sat Aug 17 19:30:14 2013 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Sun, 18 Aug 2013 11:30:14 +1200 Subject: .split() Qeustion In-Reply-To: <8584caa1-1593-41ac-8e0c-4ac4639913d6@googlegroups.com> References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> <87siybuii8.fsf@nautilus.nautilus> <520CEB96.2050902@mrabarnett.plus.com> <87mwojug2i.fsf@nautilus.nautilus> <520da6d1$0$30000$c3e8da3$5496439d@news.astraweb.com> <2d88bc0f-fdcb-4685-87ed-c17998dd3137@googlegroups.com> <8584caa1-1593-41ac-8e0c-4ac4639913d6@googlegroups.com> Message-ID: wxjmfauth at gmail.com wrote: > Na + Cl --> NaCl > > the chemist combines *one mole* of sodium and *one > mole* of chlorine to get *one mole* of sodium chloride > > It's independent of the number of "particles" in a mole. The actual number chosen for the unit is arbitrary, but number of particles is still the central issue. The important thing is to have the *same* number of particles of Na and Cl. Weight only comes into it because it's totally impractical to count particles. And the particular number 6.02e23 is chosen because it happens to give a convenient relationship between number of particles and grams. If chemists had decided to use ounces instead, the number would be different. -- Greg From wxjmfauth at gmail.com Sun Aug 18 03:17:32 2013 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Sun, 18 Aug 2013 00:17:32 -0700 (PDT) Subject: .split() Qeustion In-Reply-To: References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> <87siybuii8.fsf@nautilus.nautilus> <520CEB96.2050902@mrabarnett.plus.com> <87mwojug2i.fsf@nautilus.nautilus> <520da6d1$0$30000$c3e8da3$5496439d@news.astraweb.com> <2d88bc0f-fdcb-4685-87ed-c17998dd3137@googlegroups.com> <8584caa1-1593-41ac-8e0c-4ac4639913d6@googlegroups.com> Message-ID: <3b6a574a-61a9-4541-a8d4-5dc433d282dd@googlegroups.com> Le dimanche 18 ao?t 2013 01:30:14 UTC+2, Gregory Ewing a ?crit?: > wxjmfauth at gmail.com wrote: > > > Na + Cl --> NaCl > > > > > > the chemist combines *one mole* of sodium and *one > > > mole* of chlorine to get *one mole* of sodium chloride > > > > > > It's independent of the number of "particles" in a mole. > > > > The actual number chosen for the unit is arbitrary, but > > number of particles is still the central issue. The > > important thing is to have the *same* number of particles > > of Na and Cl. > > > > Weight only comes into it because it's totally impractical > > to count particles. And the particular number 6.02e23 is > > chosen because it happens to give a convenient relationship > > between number of particles and grams. If chemists had > > decided to use ounces instead, the number would be different. > > > > -- > > Greg -------- "The actual number chosen for the unit is arbitrary, but number of particles is still the central issue." No. "The important thing is to have the *same* number of particles of Na and Cl." Yes. And it is precisely for that reason, a chemist works in "mole arithetic". Same reaction as above Na + Cl --> NaCl in pseudo math, with n = number of elements in a mole. n * Na + n * Cl --> n * NaCl <==> n * (Na + Cl) --> n * NaCl <==> division by n Na + Cl --> NaCl for any n. ---- The determination of n, the number of elements in a mole, is an indipendent and separate problem. (BTW, a very complicate task). jmf From invalid at invalid.invalid Fri Aug 16 09:59:25 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Fri, 16 Aug 2013 13:59:25 +0000 (UTC) Subject: .split() Qeustion References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <1376499950.3355.9802231.6A03F40D@webmail.messagingengine.com> <98e36bd1-1115-4f1d-b535-171f47408a62@googlegroups.com> <87siybuii8.fsf@nautilus.nautilus> <520CEB96.2050902@mrabarnett.plus.com> <87mwojug2i.fsf@nautilus.nautilus> <520da6d1$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2013-08-16, Roy Smith wrote: > In article , > Ben Finney wrote: > >> Avogadro's Number is worth remembering, for mocking the pseudo-science >> of homeopathy . > > You have obviously never argued science with a homeopath if you believe > that knowing Avogadro's number will in any way shake their faith. He wasn't talking about arguing with a homeopath -- he was talking about mocking homeopathy. If you accept the premise of homeopathy having to do with the transferring of the essence or memory or whatever it's called, the fact that the final "solution" contains none of the original solute is irrelevant, and they will freely admit that none of the solute is actually present. It's all a complete and utter scam, of course. -- Grant Edwards grant.b.edwards Yow! I'm a nuclear at submarine under the gmail.com polar ice cap and I need a Kleenex! From steve at pearwood.info Thu Aug 15 03:17:26 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 15 Aug 2013 07:17:26 GMT Subject: .split() Qeustion References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> Message-ID: <520c8086$0$29885$c3e8da3$5496439d@news.astraweb.com> On Wed, 14 Aug 2013 13:05:50 -0400, random832 wrote: > On Wed, Aug 14, 2013, at 10:32, wxjmfauth at gmail.com wrote: >> I'm always and still be suprised by the number of hard coded '\n' one >> can find in Python code when the portable (here win) >> >> >>> os.linesep >> '\r\n' >> >> exists. > > Because high-level code isn't supposed to use the os module directly. Say what? My brain hurts. The os module is full of lots of platform independent goodies. It is the right way to split and combine pathnames, test environment variables, manipulate file permissions, and other useful tasks. > Text-mode streams automatically convert newlines you write to them. Maybe they do, maybe they don't. It depends on whether Python is built with universal newline support, and what sort of text-mode stream you are using, and a bunch of other rules that make it a little more complicated than just "automatically convert". -- Steven From alister.ware at ntlworld.com Wed Aug 14 09:29:52 2013 From: alister.ware at ntlworld.com (Alister) Date: Wed, 14 Aug 2013 13:29:52 GMT Subject: .split() Qeustion References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> Message-ID: On Wed, 14 Aug 2013 11:31:01 +0100, Joshua Landau wrote: > On 14 August 2013 09:30, Alister wrote: >> On Tue, 13 Aug 2013 22:12:56 -0700, Gary Herron wrote: >> >>> On 08/13/2013 09:51 PM, eschneider92 at comcast.net wrote: >>>> How can I use the '.split()' method (am I right in calling it a >>>> method?) without instead of writing each comma between words in the >>>> pie list in the following code? Also, is there a way to use .split >>>> instead of typing the apostrophes? Thank you. >>>> >>>> import random pie=['keylime', 'peach', 'apple', 'cherry', 'pecan'] >>>> print(random.choice(pie)) >>>> >>>> Eric >>> >>> I think you are referring to this: >>> pie = 'keylime peach apple cherry pecan'.split() >>> >>> While it's easier to type, and does save a few characters, I think the >>> original list is clearer to a reader of your program. >>> >>> Gary Herron >> >> I would agree with the last statement. >> Please write list definitions as lists rather than taking a short-cut >> to save a few key presses > > That's true with this example, but is: > > lines = [ > "Developments in high-speed rail, and high-speed", "transport more > generally, have historically been", "impeded by the difficulties in > managing friction", "and air resistance, both of which become", > "substantial when vehicles approach high speeds.", "The vactrain > concept eliminates these obstacles", "by employing magnetically > levitating trains in", "tubes kept at a complete vacuum, allowing > for", "heoretical speeds of thousands of miles per", "hour. The high > cost of constructing such a system,", > "however, and the difficulty of maintaining a", "vacuum over large > distances, has prevented this", "type of system from ever being > built. The", "Hyperloop can be viewed as a modified vactrain,", > "employing more cost-effective solutions to the", "same problems the > latter was designed to solve." > ] > > really more readable than: > > lines = """\ > Developments in high-speed rail, and high-speed transport more > generally, have historically been impeded by the difficulties in > managing friction and air resistance, both of which become substantial > when vehicles approach high speeds. > The vactrain concept eliminates these obstacles by employing > magnetically levitating trains in tubes kept at a complete vacuum, > allowing for heoretical speeds of thousands of miles per hour. The high > cost of constructing such a system, > however, and the difficulty of maintaining a vacuum over large > distances, has prevented this type of system from ever being built. The > Hyperloop can be viewed as a modified vactrain, > employing more cost-effective solutions to the same problems the latter > was designed to solve. > """[1:-1].split("\n") > > ? Yes, because I can see at the start that a list is being created & can skip over the data top the next line of code. I could easily miss the .split() at the end of the string deffinition. -- "It ain't over until it's over." -- Casey Stengel From duncan.booth at invalid.invalid Thu Aug 15 05:15:56 2013 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 15 Aug 2013 09:15:56 GMT Subject: .split() Qeustion References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> Message-ID: Joshua Landau wrote: > That's true with this example, but is: > > lines = [ > "Developments in high-speed rail, and high-speed", > "transport more generally, have historically been", > "impeded by the difficulties in managing friction", > "and air resistance, both of which become", > "substantial when vehicles approach high speeds.", > "The vactrain concept eliminates these obstacles", > "by employing magnetically levitating trains in", > "tubes kept at a complete vacuum, allowing for", > "heoretical speeds of thousands of miles per", > "hour. The high cost of constructing such a system,", > "however, and the difficulty of maintaining a", > "vacuum over large distances, has prevented this", > "type of system from ever being built. The", > "Hyperloop can be viewed as a modified vactrain,", > "employing more cost-effective solutions to the", > "same problems the latter was designed to solve." > ] > > really more readable than: > > lines = """\ > Developments in high-speed rail, and high-speed > transport more generally, have historically been > impeded by the difficulties in managing friction > and air resistance, both of which become > substantial when vehicles approach high speeds. > The vactrain concept eliminates these obstacles > by employing magnetically levitating trains in > tubes kept at a complete vacuum, allowing for > heoretical speeds of thousands of miles per > hour. The high cost of constructing such a system, > however, and the difficulty of maintaining a > vacuum over large distances, has prevented this > type of system from ever being built. The > Hyperloop can be viewed as a modified vactrain, > employing more cost-effective solutions to the > same problems the latter was designed to solve. > """[1:-1].split("\n") > > ? I suppose the question really is whether the author of the second example really meant to start with the word 'evelopments'? If that was a mistake, then the first one is demonstrably less error prone. If it was intentional then the second one is definitely less readable. Either way I think you've proved that the first way of writing it is more readable. -- Duncan Booth From wxjmfauth at gmail.com Thu Aug 15 05:46:20 2013 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Thu, 15 Aug 2013 02:46:20 -0700 (PDT) Subject: .split() Qeustion In-Reply-To: References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> Message-ID: A technical ascpect of triple quoted strings is that the "end of lines" are not respected. >>> import zzz >>> zzz.__doc__ 'abc\ndef\n' >>> with open('zzz.py', 'rb') as fo: ... r = fo.read() ... >>> r b'"""abc\r\ndef\r\n"""\r\n' Now, one can argue... jmf From rosuav at gmail.com Thu Aug 15 05:54:10 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 15 Aug 2013 10:54:10 +0100 Subject: .split() Qeustion In-Reply-To: References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> Message-ID: On Thu, Aug 15, 2013 at 10:46 AM, wrote: > A technical ascpect of triple quoted strings is > that the "end of lines" are not respected. > >>>> import zzz >>>> zzz.__doc__ > 'abc\ndef\n' >>>> with open('zzz.py', 'rb') as fo: > ... r = fo.read() > ... >>>> r > b'"""abc\r\ndef\r\n"""\r\n' > > Now, one can argue... Actually, they are respected. Triple-quoted strings are parsed after the file is read in, and newline handling is dealt with at an earlier level, same as encodings are. You wouldn't expect that string to retain information about the source file's encoding, and nor do you expect it to retain the source file's newline style. A newline is represented in the file on disk as \r\n or \n (or something else, even), and in the string as \n. It's that simple. ChrisA From steve+comp.lang.python at pearwood.info Thu Aug 15 07:22:56 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 15 Aug 2013 11:22:56 GMT Subject: .split() Qeustion References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> Message-ID: <520cba0f$0$30000$c3e8da3$5496439d@news.astraweb.com> On Thu, 15 Aug 2013 02:46:20 -0700, wxjmfauth wrote: > A technical ascpect of triple quoted strings is that the "end of lines" > are not respected. > >>>> import zzz >>>> zzz.__doc__ > 'abc\ndef\n' You are misinterpreting what you are seeing. You are not reading lines of text from a file. You are importing a module, and they accessing its __doc__ attribute. The relationship between the module object and text from a file is tenuous, at best: - the module's file could use \n line endings, or \r, or \r\n, or even something else, depending on the platform; - the module might be a compiled .pyc file, and there are no lines of text to read, just byte code; - or a .dll or .so library, again, no lines of text, just compiled code; - or there might not even be a file, like the sys module, which is entirely built into the interpreter; - or it might not even be a module object, you can put anything into sys.module. It might be an instance with docstrings computed on the fly. So you can't conclude *anything* about text files from the fact that module docstrings typically contain only \n rather than \r\n line endings. Modules are not necessarily text files, and even when they are, once you import them, what you get is *not text*, but Python objects. >>>> with open('zzz.py', 'rb') as fo: > ... r = fo.read() > ... >>>> r > b'"""abc\r\ndef\r\n"""\r\n' And again, you are misinterpreting what you are seeing. By opening the file in binary mode, you are instructing Python to treat it as binary bytes, and return *exactly* what is stored on disk. If you opened the file in text mode, you would (likely, but not necessarily) get a very different result: the string would contain only \n line endings. Python is not a low-level language like C. If you expect it to behave like a low-level language like C, you will be confused and upset. But to prove that you are mistaken, we can do this: py> s = """Triple-quote string\r ... containing carriage-return+newline line\r ... endings.""" py> s 'Triple-quote string\r\ncontaining carriage-return+newline line\r \nendings.' -- Steven From wxjmfauth at gmail.com Thu Aug 15 09:58:15 2013 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Thu, 15 Aug 2013 06:58:15 -0700 (PDT) Subject: .split() Qeustion In-Reply-To: <520cba0f$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> <520cba0f$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: -------- I perfectly knows what Python does. I missinterpreting nothing. I opened my example in binary mode just to show the real endings. It still remains the """...""" has its owns EOL and one has to be aware of it. No more, no less. ("""...""" and tokenize.py is funny) jmf From alfonsoandalon at gmail.com Sat Aug 17 00:31:25 2013 From: alfonsoandalon at gmail.com (Alfonso Andalon Jr.) Date: Fri, 16 Aug 2013 21:31:25 -0700 (PDT) Subject: .split() Qeustion In-Reply-To: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> Message-ID: From chandan_psr at yahoo.co.in Wed Aug 14 06:12:11 2013 From: chandan_psr at yahoo.co.in (chandan kumar) Date: Wed, 14 Aug 2013 18:12:11 +0800 (SGT) Subject: Verifying Variable value Message-ID: <1376475131.60391.YahooMailNeo@web190502.mail.sg3.yahoo.com> Hi , Is there a way to validate variable values while debugging any python code.Run below example ?in debugging mode and i would like to know the value of c (I know print is an option) with any other option other than printing. In C# or some other tools we can verify each statement and values.?Is there way to check each statement in python code like in c#. Ex: def method() ? a = 20 ? b =30 ? c = ?a + b Best Regards, Chanadn -------------- next part -------------- An HTML attachment was scrubbed... URL: From i.am.songoku at gmail.com Wed Aug 14 07:29:04 2013 From: i.am.songoku at gmail.com (Krishnan Shankar) Date: Wed, 14 Aug 2013 04:29:04 -0700 Subject: Verifying Variable value In-Reply-To: <1376475131.60391.YahooMailNeo@web190502.mail.sg3.yahoo.com> References: <1376475131.60391.YahooMailNeo@web190502.mail.sg3.yahoo.com> Message-ID: Hi Chandan, Python has built-in module called pdb which can be used for debugging. Importing it in the right place will be like setting break point in code and will change the execution to debugging mode. We can use different debugging commands ((n)ext, (c)ontinue, (s)tep etc) to evaluate through the code. Below is the link to module, http://docs.python.org/2/library/pdb.html We can run this code in the following way "python -m pdb filename.py" to run it in pdb debugging mode. Else import pdb and set the trace at right place like below. For example ========= def method() import pdb;pdb.set_trace() <<<---------- import and set_trace() a = 20 b =30 c = a + b method() While running you will get pdb prompt to debug the code line by line. The execution will be in user's hands. Like below, > c:\users\krishnan\desktop\test.py(3)method() -> a = 20 (Pdb) n > c:\users\krishnan\desktop\test.py(4)method() -> b =30 (Pdb) n > c:\users\krishnan\desktop\test.py(5)method() -> c = a + b (Pdb) n --Return-- > c:\users\krishnan\desktop\test.py(5)method()->None -> c = a + b (Pdb) c You can explore the module and find it useful for debugging. I hope this helps Regards, Krishnan On Wed, Aug 14, 2013 at 3:12 AM, chandan kumar wrote: Hi , Is there a way to validate variable values while debugging any python code.Run below example in debugging mode and i would like to know the value of c (I know print is an option) with any other option other than printing. In C# or some other tools we can verify each statement and values. Is there way to check each statement in python code like in c#. Ex: def method() a = 20 b =30 c = a + b Best Regards, Chanadn -- http://mail.python.org/mailman/listinfo/python-list -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Wed Aug 14 08:01:58 2013 From: davea at davea.name (Dave Angel) Date: Wed, 14 Aug 2013 12:01:58 +0000 (UTC) Subject: Verifying Variable value References: <1376475131.60391.YahooMailNeo@web190502.mail.sg3.yahoo.com> Message-ID: chandan kumar wrote: > Hi , > > Is there a way to validate variable values while debugging any python code.Run below example ?in debugging mode and i would like to know the value of c (I know print is an option) with any other option other than printing. > In C# or some other tools we can verify each statement and values.?Is there way to check each statement in python code like in c#. > > Ex: > def method() > ? a = 20 > ? b =30 > ? c = ?a + b I haven't used the language called C# since before it was renamed. But if they were at all consistent to its forbears, you must be talking about assert(). assert() works pretty much the same in Python, though you don't need the parens. assert c==50 will throw an AssertionError if c is unequal to 50 > >
Hi ,

Is there a way to validate variable values while debugging any python code.Run below example  in debugging mode and i would like to know the value of c (I know print is an option) with any other option other than printing.
References: <4de52359-d352-4e35-afed-73df672c945c@googlegroups.com> Message-ID: On Thursday, August 15, 2013 1:34:38 AM UTC+3, Dennis Lee Bieber wrote: > On Wed, 14 Aug 2013 06:18:08 -0700 (PDT), Guy Tamir > > declaimed the following: > > > > >Hi all, > > > > > >I have a Ubuntu server running NGINX that logs data for me. > > > > Is the log coming from NGINX or (since you mention Django below) coming > > solely from the Django application. > > > > If the logging is from the Django application only, you should be able > > to have it connect to the database and write directly to it. > > -- > > Wulfraed Dennis Lee Bieber AF6VN > > wlfraed at ix.netcom.com HTTP://wlfraed.home.netcom.com/ the log is from NGINX.. From guytamir1 at gmail.com Thu Aug 15 03:23:57 2013 From: guytamir1 at gmail.com (Guy Tamir) Date: Thu, 15 Aug 2013 00:23:57 -0700 (PDT) Subject: Reading log and saving data to DB In-Reply-To: References: <4de52359-d352-4e35-afed-73df672c945c@googlegroups.com> Message-ID: On Wednesday, August 14, 2013 4:46:09 PM UTC+3, mar... at python.net wrote: > On Wed, Aug 14, 2013, at 09:18 AM, Guy Tamir wrote: > > > Hi all, > > > > > > I have a Ubuntu server running NGINX that logs data for me. > > > I want to write a python script that reads my customized logs and after > > > a little rearrangement save the new data into my DB (postgresql). > > > > > > The process should run about every 5 minutes and i'm expecting large > > > chunks of data on several 5 minute windows.. > > > > > > My plan for achieving this is to install python on the server, write a > > > script and add it to cron. > > > > > > My question is what the simplest way to do this? > > > should i use any python frameworks? > > > > Rarely do I put "framework" and "simplest way" in the same set. > > > > I would do 1 of 2 things: > > > > * Write a simple script that reads lines from stdin, and writes to the > > db. Make sure it gets run in init before nginx does and tail -F -n 0 to > > that script. Don't worry about the 5-minute cron. > > > > * Similar to above but if you want to use cron also store in the db the > > offset of the last byte read in the file, then when the cron job kicks > > off again seek to that position + 1 and begin reading, at EOF write the > > offset again. > > > > This is irrespective of any log rotating that is going on behind the > > scenes, of course. Not sure i understood the first options and what it means to run before the nginx. The second options sound more like what i had in mind. Aren't there any components like this written that i can use? since the log fills up a lot i'm having trouble reading so much data and writing it all to the DB in a reasonable amount of time. The table receiving the new data is somewhat complex.. the table's purpose is to save data regarding ads shown from my app, the fields are - (ad_id,user_source_site,user_location,day_date,specific_hour,views,clicks) each row is distinct by the first 5 fields since i need to show different types of stats.. because each new line created may or may not be in the DB i have to run a upsert command (update or insert) on each row.. This leads to very poor performance.. Do have any ideas about how i can make this script more efficient? From climb65 at laposte.net Wed Aug 14 10:16:31 2013 From: climb65 at laposte.net (climb65) Date: 14 Aug 2013 14:16:31 GMT Subject: many constructors in a class? Message-ID: <520b913f$0$13995$426a74cc@news.free.fr> Hello, here is a small basic question : Is it possible to have more than one constructor (__init__ function) in a class? For instance, to create an object with 2 different ways? If my memory is good, I think that with C++ it is possible. Thanks for your answer. From beth.mcnany at gmail.com Wed Aug 14 10:40:03 2013 From: beth.mcnany at gmail.com (Beth McNany) Date: Wed, 14 Aug 2013 10:40:03 -0400 Subject: many constructors in a class? In-Reply-To: <520b913f$0$13995$426a74cc@news.free.fr> References: <520b913f$0$13995$426a74cc@news.free.fr> Message-ID: On Wed, Aug 14, 2013 at 10:16 AM, climb65 wrote: > Hello, > > here is a small basic question : > > Is it possible to have more than one constructor (__init__ function) in a > class? For instance, to create an object with 2 different ways? If my > memory is good, I think that with C++ it is possible. > > Thanks for your answer. > > No, Python does not allow method overloading: >>> class Test: ... def __init__(self): ... print "first init" ... def __init__(self, arg): ... print "init with arg" ... >>> a = Test() Traceback (most recent call last): File "", line 1, in TypeError: __init__() takes exactly 2 arguments (1 given) No error on actually writing the class, but only the last __init__ is kept. You could, however, emulate that behavior with optional arguments, or something more sophisticated as the need may be. This stackoverflow question covers a few alternatives: http://stackoverflow.com/questions/6434482/python-function-overloading -------------- next part -------------- An HTML attachment was scrubbed... URL: From phil.le.bienheureux at gmail.com Wed Aug 14 10:46:33 2013 From: phil.le.bienheureux at gmail.com (Phil Le Bienheureux) Date: Wed, 14 Aug 2013 16:46:33 +0200 Subject: many constructors in a class? In-Reply-To: <520b913f$0$13995$426a74cc@news.free.fr> References: <520b913f$0$13995$426a74cc@news.free.fr> Message-ID: 2013/8/14 climb65 > Hello, > > here is a small basic question : > > Is it possible to have more than one constructor (__init__ function) in a > class? For instance, to create an object with 2 different ways? If my > memory is good, I think that with C++ it is possible. > > Thanks for your answer. > -- > http://mail.python.org/mailman/listinfo/python-list > Hello, You have to use default values in __init__ function, like : def __init__( self, name = None ): self.name_ = name and afterwards in your code, test variable : if self.name_: do something... Regards, Phil. -------------- next part -------------- An HTML attachment was scrubbed... URL: From buzzard at invalid.invalid Wed Aug 14 11:07:33 2013 From: buzzard at invalid.invalid (duncan smith) Date: Wed, 14 Aug 2013 16:07:33 +0100 Subject: many constructors in a class? In-Reply-To: <520b913f$0$13995$426a74cc@news.free.fr> References: <520b913f$0$13995$426a74cc@news.free.fr> Message-ID: <520b9d3d$0$7394$862e30e2@ngroups.net> On 14/08/13 15:16, climb65 wrote: > Hello, > > here is a small basic question : > > Is it possible to have more than one constructor (__init__ function) in a > class? For instance, to create an object with 2 different ways? If my > memory is good, I think that with C++ it is possible. > > Thanks for your answer. > http://stackoverflow.com/questions/5738470/whats-an-example-use-case-for-a-python-classmethod Duncan From steve at pearwood.info Thu Aug 15 03:23:34 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 15 Aug 2013 07:23:34 GMT Subject: many constructors in a class? References: <520b913f$0$13995$426a74cc@news.free.fr> Message-ID: <520c81f6$0$29885$c3e8da3$5496439d@news.astraweb.com> On Wed, 14 Aug 2013 14:16:31 +0000, climb65 wrote: > Hello, > > here is a small basic question : > > Is it possible to have more than one constructor (__init__ function) in > a class? For instance, to create an object with 2 different ways? If my > memory is good, I think that with C++ it is possible. > > Thanks for your answer. Yes it is. The built-in type dict is a good example, there is the regular default constructor[1] that you can call like this: dict([('a', 100), ('b', 200)], spam=1, ham=2, eggs=3) Plus there is an alternative constructor that you can call like this: dict.fromkeys(['a', 'b', 'spam', 'ham', 'eggs']) The way to create an alternative constructor is to use a class method: def MyDict(dict): @classmethod def fromkeys(cls, keys): ... If you need further details, please ask. [1] The constructor is __new__, not __init__. __init__ is called to initialise the instance after __new__ constructs it. -- Steven From fabiosantosart at gmail.com Thu Aug 15 06:09:57 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Thu, 15 Aug 2013 11:09:57 +0100 Subject: many constructors in a class? In-Reply-To: <520c81f6$0$29885$c3e8da3$5496439d@news.astraweb.com> References: <520b913f$0$13995$426a74cc@news.free.fr> <520c81f6$0$29885$c3e8da3$5496439d@news.astraweb.com> Message-ID: I agree with Steven here. classmethod is the best practise, most practical, readable, future-proof, one obvious way to do it. On 15 Aug 2013 08:29, "Steven D'Aprano" wrote: > On Wed, 14 Aug 2013 14:16:31 +0000, climb65 wrote: > > > Hello, > > > > here is a small basic question : > > > > Is it possible to have more than one constructor (__init__ function) in > > a class? For instance, to create an object with 2 different ways? If my > > memory is good, I think that with C++ it is possible. > > > > Thanks for your answer. > > Yes it is. The built-in type dict is a good example, there is the regular > default constructor[1] that you can call like this: > > dict([('a', 100), ('b', 200)], spam=1, ham=2, eggs=3) > > > Plus there is an alternative constructor that you can call like this: > > dict.fromkeys(['a', 'b', 'spam', 'ham', 'eggs']) > > > The way to create an alternative constructor is to use a class method: > > > def MyDict(dict): > @classmethod > def fromkeys(cls, keys): > ... > > > If you need further details, please ask. > > > > > [1] The constructor is __new__, not __init__. __init__ is called to > initialise the instance after __new__ constructs it. > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From roy at panix.com Thu Aug 15 09:00:27 2013 From: roy at panix.com (Roy Smith) Date: Thu, 15 Aug 2013 09:00:27 -0400 Subject: many constructors in a class? References: <520b913f$0$13995$426a74cc@news.free.fr> <520c81f6$0$29885$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <520c81f6$0$29885$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > [1] The constructor is __new__, not __init__. __init__ is called to > initialise the instance after __new__ constructs it. True, but be warned that writing your own __new__() is quite rare and probably falls into the realm of dark magic. If you're just starting out, learn about __init__(), and don't worry about __new__() at all. For those of you coming from a C++ background, Python's __init__() is like C++'s constructor, and __new__() is more like operator new. That's not a perfect analogy from a functional standpoint, but it's a good guide for how often you'll want to write each one. And then, of course, there's __enter__() and __exit__(), which are like C++'s constructor and destructor, but that's another story :-) From irmen.NOSPAM at xs4all.nl Wed Aug 14 11:16:51 2013 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Wed, 14 Aug 2013 17:16:51 +0200 Subject: ANN: Pyrolite 1.12 - native pickle and Pyro client library for java and .net Message-ID: <520b9f65$0$16008$e4fe514c@news.xs4all.nl> Hi, I'd like to announce Pyrolite 1.12, a tiny (~60k) pickle and Pyro client library for Java and .NET. Question: "what is a java/.net library doing in this newsgroup?" Answer 1: This library is meant to connect a Java or .NET program to Python in a very simple way, using the Pyro protocol. Pyro is my remote object library or Python. Answer 2: Pyrolite contains a native implementation of Python's pickle protocol. This can be useful by itself to read/write pickles from Java or .NET programs. Recently, I've implemented memo-support in the pickler, which made it feature complete. You can now pickle recursive object graphs, something which produced a stack overflow error earlier. Notice that the unpickler has been able to deal with these for a long time already. Pyrolite project page: https://github.com/irmen/Pyrolite Compiled binaries are here: http://irmen.home.xs4all.nl/pyrolite/ Enjoy, Irmen de Jong From jougraw at gmail.com Thu Aug 15 05:33:33 2013 From: jougraw at gmail.com (Joug Raw) Date: Thu, 15 Aug 2013 02:33:33 -0700 (PDT) Subject: Define a class containing methods for reading a file and then store lines in external variable Message-ID: Hi, I am really new in python programming and I want to build a class that perform various functions to the content(or lines) of any given file. I want to first include the opening and reading file function into that class, then add other methods. Here is what I wrote for opening file and reading the lines: class FileOpration: def __init__(self,name): self.name = name self.filename = self.name def readAllline(self): open(self.name).readlines() file1 = FileOpration("myfile.txt") print file1.filename allines = file1.readAllline() print allines I define self.name variable because I want to store the specific file that I read. This works fine by the test code of print file1.filename. I saw "myfile.txt" on the terminal output. However, the readAllline() method dose not work. The code only give me "None" as the output on terminal What was wrong with this code? Thank for the help. From rosuav at gmail.com Thu Aug 15 05:38:08 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 15 Aug 2013 10:38:08 +0100 Subject: Define a class containing methods for reading a file and then store lines in external variable In-Reply-To: References: Message-ID: On Thu, Aug 15, 2013 at 10:33 AM, Joug Raw wrote: > class FileOpration: > def __init__(self,name): > self.name = name > self.filename = self.name > def readAllline(self): > open(self.name).readlines() > > file1 = FileOpration("myfile.txt") > print file1.filename > allines = file1.readAllline() You probably want to return that value: def readAllline(self): return open(self.name).readlines() Then it'll do what you expect. But why wrap this up in a class? Why not simply call the underlying functions directly? ChrisA From premiaskumar29 at gmail.com Thu Aug 15 10:21:43 2013 From: premiaskumar29 at gmail.com (prem kumar) Date: Thu, 15 Aug 2013 07:21:43 -0700 (PDT) Subject: Want to learn Python Message-ID: Hi All, Presently Iam working with QTP(VBscript)..Now planning to learn PYTHON..Could you please suggest me like is ti good to learn what is the present condition for Python in IT Companies.. Iam not thinking abt only testing purpose even Iam interested to shift to development side on python.. Also if anyone knows abt good coaching institute for the same in Bangalore can share with me... Eagerly waiting for response from the group.. Thanks, Premkumar From joel.goldstick at gmail.com Thu Aug 15 13:23:06 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Thu, 15 Aug 2013 13:23:06 -0400 Subject: Want to learn Python In-Reply-To: References: Message-ID: On Thu, Aug 15, 2013 at 10:21 AM, prem kumar wrote: > Hi All, > > Presently Iam working with QTP(VBscript)..Now planning to learn PYTHON..Could you please suggest me like is ti good to learn what is the present condition for Python in IT Companies.. I don't really understand the previous sentence. But guessing a bit, If you are concerned about whether python is used in business check out http://wiki.python.org/moin/OrganizationsUsingPython As I live in the US I have no idea what the situation is like where you live. > Iam not thinking abt only testing purpose even Iam interested to shift to development side on python.. > Also if anyone knows abt good coaching institute for the same in Bangalore can share with me... > Eagerly waiting for response from the group.. > > Thanks, > Premkumar > -- > http://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com From kender.jr at gmail.com Thu Aug 15 14:03:33 2013 From: kender.jr at gmail.com (Jordi Riera) Date: Thu, 15 Aug 2013 11:03:33 -0700 (PDT) Subject: Want to learn Python In-Reply-To: References: Message-ID: On Thursday, August 15, 2013 4:21:43 PM UTC+2, prem kumar wrote: > Hi All, > > > > Presently Iam working with QTP(VBscript)..Now planning to learn PYTHON..Could you please suggest me like is ti good to learn what is the present condition for Python in IT Companies.. > > Iam not thinking abt only testing purpose even Iam interested to shift to development side on python.. > > Also if anyone knows abt good coaching institute for the same in Bangalore can share with me... > > Eagerly waiting for response from the group.. > > > > Thanks, > > Premkumar you should give a try to codecademy.com. The approach of teaching programming language is new and really efficient. Regards, JOrdi From nikhil389 at gmail.com Fri Aug 16 03:06:10 2013 From: nikhil389 at gmail.com (Nikhil Bansode) Date: Fri, 16 Aug 2013 00:06:10 -0700 (PDT) Subject: Want to learn Python In-Reply-To: References: Message-ID: On Thursday, August 15, 2013 7:51:43 PM UTC+5:30, prem kumar wrote: > Hi All, > > > > Presently Iam working with QTP(VBscript)..Now planning to learn PYTHON..Could you please suggest me like is ti good to learn what is the present condition for Python in IT Companies.. > > Iam not thinking abt only testing purpose even Iam interested to shift to development side on python.. > > Also if anyone knows abt good coaching institute for the same in Bangalore can share with me... > > Eagerly waiting for response from the group.. > > > > Thanks, > > Premkumar This web page has all the information you need to learn python. http://net.tutsplus.com/tutorials/the-best-way-to-learn-python/ From jtim.arnold at gmail.com Thu Aug 15 12:58:00 2013 From: jtim.arnold at gmail.com (Tim) Date: Thu, 15 Aug 2013 09:58:00 -0700 (PDT) Subject: nose config help? Message-ID: hi, I'm using nose to generate and run some tests not for python code, but for an html repository. I know this isn't the typical way to use nose, and so I'm asking here if the following code smells wrong. I pass some args with the testconfig plugin and run a class setup method one time to get some information from a database. Then the tests get generated and run. During the run, a database is updated with the result. That is an intentional side-effect of running the test....is that okay or something that should never be done? I've shortened the actual code to make it readable, but the main thing is the setup (setting class vars), the test generator, and the actual test (plus the database update): class HtmlTester(object): ''' Run this setup method once before tests begin. Reads from tconfig args to get document from database. ''' @classmethod def setup_class(cls): name = tconfig['name'] language = tconfig.get('language', 'en') report = db.conn({'name':name, 'language':language, 'format':'html}) @attr('images') def test_images(self): for image in self.report['images']: yield (self.check_image, image) def check_image(self, image): ''' True if image can be read, otherwise False ''' r = False if get_imagesize(image): p = ImageFile.Parser() try: p.feed(open(image).read()) except IOError: r = False else: r = True self.report.update({'image':image, 'result':r, 'name': self.name, 'language': self.language}) assert_true(r) if __name__ == '__main__': args = sys.argv[:1] + ['--tc=name:mybook', '--tc=language:en' ] + sys.argv[1:] nose.run(argv=args) I've used nose before in simpler situations, but not an expert. This is complex enough that I wonder if the above approach is correct. thanks, --Tim From ron.eggler at gmail.com Thu Aug 15 14:12:58 2013 From: ron.eggler at gmail.com (cerr) Date: Thu, 15 Aug 2013 11:12:58 -0700 (PDT) Subject: question about posting data using MultipartPostHandler Message-ID: <3c256548-5fed-446a-8683-b66abc0e301f@googlegroups.com> Hi, I want to use http post to upload data to a webserver but I want to pass multiple arguments within the post i.e. I know that you can load one item (data)in there like this: data = {"data":open(filename,"rb")} response = opener.open(url, data, timeout=TIMEOUT) but now I want multiple so I tried this: multipart = ({"data":data}, {"fname":fname}, {"f":f}) response = opener.open(url, multipart, timeout=TIMEOUT) but I get an error saying "'tuple' object has no attribute 'items'"... how do I do this correctly? Thank you! Ron From rosuav at gmail.com Thu Aug 15 20:45:03 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 16 Aug 2013 01:45:03 +0100 Subject: question about posting data using MultipartPostHandler In-Reply-To: <3c256548-5fed-446a-8683-b66abc0e301f@googlegroups.com> References: <3c256548-5fed-446a-8683-b66abc0e301f@googlegroups.com> Message-ID: On Thu, Aug 15, 2013 at 7:12 PM, cerr wrote: > multipart = ({"data":data}, {"fname":fname}, {"f":f}) > > but I get an error saying "'tuple' object has no attribute 'items'"... how do I do this correctly? You're no longer providing a dictionary, but a tuple of dictionaries. What you want to do is use a single dictionary: multipart = {"data":data, "fname":fname, "f":f} That should achieve what you want. ChrisA From hansyin at gmail.com Thu Aug 15 15:53:36 2013 From: hansyin at gmail.com (Hans) Date: Thu, 15 Aug 2013 12:53:36 -0700 (PDT) Subject: How can I redirect or launch a html file in wsgi coding? Message-ID: <6fa2b093-953f-4ffa-8fbb-e01581d18d2f@googlegroups.com> Hi, I have code like this: root at lin-ser-1:~# cat /usr/local/www/wsgi-scripts/myapp.py def application(environ, start_response): import sys ....... status = '200 OK' req_method=environ['REQUEST_METHOD'] if req_method == 'POST' : json_received = environ['wsgi.input'].read() resp=lib_json_http.process_json(json_received) output = simplejson.dumps(resp) elif req_method == 'GET' : webbrowser.open('http://autotestnet.sourceforge.net/') return >>>>>This code does not work!!!! else : output = "invalid request method" response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))] start_response(status, response_headers) return [output] POST works OK, for GET, I hope I can redirect it to a url link, or launch a local html file, how can I do it? thanks!!! From ron.eggler at gmail.com Thu Aug 15 16:27:47 2013 From: ron.eggler at gmail.com (cerr) Date: Thu, 15 Aug 2013 13:27:47 -0700 (PDT) Subject: http post goes into $_REQUEST instead into $_FILES Message-ID: <8e345e7a-71bd-4368-84f8-3a9cc3dc40e1@googlegroups.com> Hi, I have a Python script that is executing an http POST to transfer a file from the client to the server. I have achieved this with below code: from binascii import hexlify, unhexlify from httplib import HTTPConnection, HTTPException import os import hashlib import socket import urllib2 import time import subprocess import MultipartPostHandler def post_file(filename, data): try: wakeup() socket.setdefaulttimeout(TIMEOUT) #create multipartpost handler opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler) #set POST arguments host = HOST func = "post_file" fname = filename #assemble post URL url = "http://{0}{1}".format(host, URI) print "POSTING "+fname #assemble multipart header data = {"data":data,"f":func,"fname":filename} #execute POST response = opener.open(url, data, timeout=TIMEOUT) #reads server return value retval = response.read() print retval if "SUCCESS" in retval: return 0 else: print "RETVAL: "+retval return 99 except Exception as e: print "EXCEPTION time "+str(time.time())+" - "+str(e) return 99 but my problem is, the data gets posted to the sever but arrives in the `$_REQUEST` array and I'm expected to post stuff so that it arrives in the `$_FILES` array (this is a LAMP server). How do I need to modify my code to deliver it correctly? Thank you, From piet at vanoostrum.org Fri Aug 16 16:51:36 2013 From: piet at vanoostrum.org (Piet van Oostrum) Date: Fri, 16 Aug 2013 16:51:36 -0400 Subject: http post goes into $_REQUEST instead into $_FILES References: <8e345e7a-71bd-4368-84f8-3a9cc3dc40e1@googlegroups.com> Message-ID: cerr writes: > Hi, > > I have a Python script that is executing an http POST to transfer a file from the client to the server. I have achieved this with below code: > but my problem is, the data gets posted to the sever but arrives in > the `$_REQUEST` array and I'm expected to post stuff so that it > arrives in the `$_FILES` array (this is a LAMP server). How do I need > to modify my code to deliver it correctly? MultipartPostHandler only works like that if you give it a real file. Like: myfile = open('test.txt') data = {'file': myfile} response = opener.open(url, data) 'file' will be the key in the $_FILES array. If you want to 'fake' a file by supplying your own contents and fake filename, you can better use the code from Doug Hellmann: http://pymotw.com/2/urllib2/#uploading-files If you don't want to use his StringIO trick you could even add a new method add_fake_file like this: def add_fake_file(self, fieldname, filename, body, mimetype='text/plain'): """Add a fake file to be uploaded.""" self.files.append((fieldname, filename, mimetype, body)) return and then use it like: form.add_fake_file('file', 'test1.txt', 'This is a test.') -- Piet van Oostrum WWW: http://pietvanoostrum.com/ PGP key: [8DAE142BE17999C4] From ganesh1pal at gmail.com Fri Aug 16 02:21:32 2013 From: ganesh1pal at gmail.com (Ganesh Pal) Date: Fri, 16 Aug 2013 11:51:32 +0530 Subject: How to I do this in Python ? Message-ID: Hello Friends , Iam a newbie to python , Iam writing a small script that would generate various kinds of files in the specified path . Iam using sub process module to achieve this , I have stuck with few basic problems , any help on this would be great Case (a) : The below code creates the only one spare file named sp1 , # Creating sparse files in the sparse path sparse_path = os.path.join(path,'sparsefiles') os.makedirs(sparse_path) os.chdir(sparse_path) sparsefiles = "dd if=/dev/zero of=sp1 count=0 bs=1 seek=10G" process_0 = subprocess.Popen(sparsefiles, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) Current output : Sp1, How do I loop my script to create 100 of files like sp1 , sp2 ,sp3,.. sp100 .. using the same syntax " sparsefiles = "dd if=/dev/zero of=sp1 count=0 bs=1 seek=10G " Case (2) : Is there a better way to create the files in Python other than using sub process module and running dd command as shown below .. Example : # creating sparse File sparse_path = os.path.join(path,'sparsefiles') os.makedirs(sparse_path) os.chdir(sparse_path) sparsefiles = "dd if=/dev/zero of=sp1 count=0 bs=1 seek=10G" process_0 = subprocess.Popen(sparsefiles, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) # Creating Regular files Regular_path = os.path.join(path,'regularfiles') os.makedirs(Regular_path) os.chdir(Regular_path) regularfiles = " dd if=/dev/urandom of=file1 count=0 bs=1 seek=10" process_1 = subprocess.Popen(regularfiles, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) My goal is to create various kinds of files like sparse, regular ,directories, hard and symlinks etc what would be the best way to do achieve this ? Regards, Ganesh -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Fri Aug 16 06:59:25 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 Aug 2013 10:59:25 GMT Subject: How to I do this in Python ? References: Message-ID: <520e060d$0$30000$c3e8da3$5496439d@news.astraweb.com> Hi Ganesh, and welcome! Unfortunately, you ask your questions in reverse order. The most general (and important) question comes last, and the least important first, so I'm going to slice-and-dice your post and answer from most general to least. On Fri, 16 Aug 2013 11:51:32 +0530, Ganesh Pal wrote: > My goal is to create various kinds of files like sparse, regular > ,directories, hard and symlinks etc > what would be the best way to do achieve this ? Use your shell, such as bash or csh or equivalent. For simple tasks like that, it will solve the problem much more simply than Python. There are three good reasons for doing this in Python: - "This is only a small part of a larger Python application." - "I'm doing this to learn how to use Python." - "I really hate my shell." But of you just want to get the job done, and don't care what language you use, use the shell. Now, having said that, I'm going to assume you have a good reason to use Python: > Case (2) : > > Is there a better way to create the files in Python other than using > sub process module and running dd command as shown below .. > > Example : > > # creating sparse File > sparse_path = os.path.join(path,'sparsefiles') > os.makedirs(sparse_path) > os.chdir(sparse_path) > sparsefiles = "dd if=/dev/zero of=sp1 count=0 bs=1 seek=10G" > process_0 = subprocess.Popen(sparsefiles, stdout=subprocess.PIPE, > stderr=subprocess.PIPE, shell=True) > > # Creating Regular files > Regular_path = os.path.join(path,'regularfiles') > os.makedirs(Regular_path) > os.chdir(Regular_path) > regularfiles = " dd if=/dev/urandom of=file1 count=0 bs=1 seek=10" > process_1 = subprocess.Popen(regularfiles, stdout=subprocess.PIPE, > stderr=subprocess.PIPE, shell=True) What do you mean by "better"? There's probably nothing that will be faster at rapidly copying bytes from one file to another than dd. But not with a blocksize of 1 byte at a time. It's more usual to set bs=512 or bs=1024. Oh, I see you're not actually writing anything to the file (count=0). In that case, instead of using dd, you should use touch. I'm not sure that shell=True is a good idea. In Python, to create a new empty file, setting its contents to empty if it already exists: open("filename", "w").close() That will open the file, creating it if it doesn't exist, emptying it if it does, then close it. To touch a file without emptying it: open("filename", "a").close() To make a sparse file, assuming your file system supports it, I believe you actually have to write at least one byte to the file: f = open('foo', 'w') f.seek(10000) f.write('\0') f.close() [...] > How do I loop my script to create 100 of files like sp1 , sp2 ,sp3,.. > sp100 .. using the same syntax To generate the various file names, you need to loop over a counter from 1 to 100, and stick the count into a string. Use a for-loop and the range function: for i in range(1, 101): filename = "sp" + str(i) open(filename, "a").close() If you are a C programmer, you might prefer this style: filename = "sp%d" % i Or a more object-oriented style: filename = "sp{:d}".format(i) -- Steven From ganesh1pal at gmail.com Sun Aug 18 13:06:01 2013 From: ganesh1pal at gmail.com (Ganesh Pal) Date: Sun, 18 Aug 2013 22:36:01 +0530 Subject: How to I do this in Python ? In-Reply-To: <520e060d$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <520e060d$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: Hi Steven , Firstly thanks for responding to the question and also guiding me on how to post the question in the right order ( general to least important order ) Please find the comments >>> inline On Fri, 16 Aug 2013 11:51:32 +0530, Ganesh Pal wrote: > My goal is to create various kinds of files like sparse, regular > ,directories, hard and symlinks etc > what would be the best way to do achieve this ? Use your shell, such as bash or csh or equivalent. For simple tasks like that, it will solve the problem much more simply than Python. There are three good reasons for doing this in Python: - "This is only a small part of a larger Python application." - "I'm doing this to learn how to use Python." - "I really hate my shell." But of you just want to get the job done, and don't care what language you use, use the shell. Now, having said that, I'm going to assume you have a good reason to use Python: >>> You have hit the nail on the head - The main goal there was to learn python and avoid using shell commands as much as possible , I want to avoid shell and use Python even if its costly. Example : I don' want to create sparse files using dd command ( #dd if=/dev/zero of=sparse-file bs=1 count=1 seek=1024k ) If the same can be done fseek . But I guess we might have to d > Case (2) : > > Is there a better way to create the files in Python other than using > sub process module and running dd command as shown below .. > > Example : > > # creating sparse File > sparse_path = os.path.join(path,'sparsefiles') > os.makedirs(sparse_path) > os.chdir(sparse_path) > sparsefiles = "dd if=/dev/zero of=sp1 count=0 bs=1 seek=10G" > process_0 = subprocess.Popen(sparsefiles, stdout=subprocess.PIPE, > stderr=subprocess.PIPE, shell=True) > > # Creating Regular files > Regular_path = os.path.join(path,'regularfiles') > os.makedirs(Regular_path) > os.chdir(Regular_path) > regularfiles = " dd if=/dev/urandom of=file1 count=0 bs=1 seek=10" > process_1 = subprocess.Popen(regularfiles, stdout=subprocess.PIPE, > stderr=subprocess.PIPE, shell=True) What do you mean by "better"? There's probably nothing that will be faster at rapidly copying bytes from one file to another than dd. But not with a blocksize of 1 byte at a time. It's more usual to set bs=512 or bs=1024. Oh, I see you're not actually writing anything to the file (count=0). In that case, instead of using dd, you should use touch. I'm not sure that shell=True is a good idea. In Python, to create a new empty file, setting its contents to empty if it already exists: open("filename", "w").close() That will open the file, creating it if it doesn't exist, emptying it if it does, then close it. To touch a file without emptying it: open("filename", "a").close() To make a sparse file, assuming your file system supports it, I believe you actually have to write at least one byte to the file: f = open('foo', 'w') f.seek(10000) f.write('\0') f.close() >>> Thanks for the suggestion on creating empty files and they worked fine . I was today playing with the temp-file module to create temporary files and directories , Iam yet to explore it completely, but in current context , I had a quick question using temp-file module is its possible to create empty temporary files and save them on disk in the user-defined path ? If " yes " then can this also be an alternative and does this have any drawback ? On Fri, Aug 16, 2013 at 4:29 PM, Steven D'Aprano < steve+comp.lang.python at pearwood.info> wrote: > Hi Ganesh, and welcome! > > Unfortunately, you ask your questions in reverse order. The most general > (and important) question comes last, and the least important first, so > I'm going to slice-and-dice your post and answer from most general to > least. > > > On Fri, 16 Aug 2013 11:51:32 +0530, Ganesh Pal wrote: > > > My goal is to create various kinds of files like sparse, regular > > ,directories, hard and symlinks etc > > what would be the best way to do achieve this ? > > Use your shell, such as bash or csh or equivalent. For simple tasks like > that, it will solve the problem much more simply than Python. > > There are three good reasons for doing this in Python: > > - "This is only a small part of a larger Python application." > > - "I'm doing this to learn how to use Python." > > - "I really hate my shell." > > But of you just want to get the job done, and don't care what language > you use, use the shell. > > > Now, having said that, I'm going to assume you have a good reason to use > Python: > > > > Case (2) : > > > > Is there a better way to create the files in Python other than using > > sub process module and running dd command as shown below .. > > > > Example : > > > > # creating sparse File > > sparse_path = os.path.join(path,'sparsefiles') > > os.makedirs(sparse_path) > > os.chdir(sparse_path) > > sparsefiles = "dd if=/dev/zero of=sp1 count=0 bs=1 seek=10G" > > process_0 = subprocess.Popen(sparsefiles, stdout=subprocess.PIPE, > > stderr=subprocess.PIPE, shell=True) > > > > # Creating Regular files > > Regular_path = os.path.join(path,'regularfiles') > > os.makedirs(Regular_path) > > os.chdir(Regular_path) > > regularfiles = " dd if=/dev/urandom of=file1 count=0 bs=1 seek=10" > > process_1 = subprocess.Popen(regularfiles, stdout=subprocess.PIPE, > > stderr=subprocess.PIPE, shell=True) > > What do you mean by "better"? There's probably nothing that will be > faster at rapidly copying bytes from one file to another than dd. But not > with a blocksize of 1 byte at a time. It's more usual to set bs=512 or > bs=1024. > > Oh, I see you're not actually writing anything to the file (count=0). In > that case, instead of using dd, you should use touch. > > I'm not sure that shell=True is a good idea. > > In Python, to create a new empty file, setting its contents to empty if > it already exists: > > open("filename", "w").close() > > > That will open the file, creating it if it doesn't exist, emptying it if > it does, then close it. > > To touch a file without emptying it: > > open("filename", "a").close() > > > To make a sparse file, assuming your file system supports it, I believe > you actually have to write at least one byte to the file: > > f = open('foo', 'w') > f.seek(10000) > f.write('\0') > f.close() > > > > [...] > > How do I loop my script to create 100 of files like sp1 , sp2 ,sp3,.. > > sp100 .. using the same syntax > > To generate the various file names, you need to loop over a counter from > 1 to 100, and stick the count into a string. Use a for-loop and the range > function: > > for i in range(1, 101): > filename = "sp" + str(i) > open(filename, "a").close() > > > If you are a C programmer, you might prefer this style: > > filename = "sp%d" % i > > Or a more object-oriented style: > > filename = "sp{:d}".format(i) > > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Mon Aug 19 03:27:50 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 19 Aug 2013 07:27:50 GMT Subject: How to I do this in Python ? References: <520e060d$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5211c8f5$0$29885$c3e8da3$5496439d@news.astraweb.com> On Sun, 18 Aug 2013 22:36:01 +0530, Ganesh Pal wrote: > Please find the comments >>> inline Please don't do that! "Arrows" > are used for quoting in emails. If you prefix your *new* comments using >>> it looks like they were quoted *three messages back*. You should be able to configure your email or news client to prefix quoted text with a >, and then you just type your own comments with no prefix, like I'm doing here. Even Gmail can do that. You seem to have copied-and-pasted my response into a new email, and then added your comments. Am I right? The normal way to reply to an email is to use Reply or Reply All. [...] >>>> Thanks for the suggestion on creating empty files and they worked > fine . I was today playing with the temp-file module to create > temporary files and directories , > Iam yet to explore it completely, but in current context , I > had a > quick question using temp-file module is its possible to create empty > temporary files and save them on disk in the user-defined path ? You shouldn't normally care about where temporary files are stored, since they're temporary and will disappear as soon as you are done with them. But yes, tempfile has the ability to control where the files are stored. Both tempfile.NamedTemporaryFile and tempfile.TemporaryFile take an optional directory argument. To read the documentation, run these two commands at the interactive prompt: import tempfile help(tempfile) or read it on the web: http://docs.python.org/2/library/tempfile.html http://docs.python.org/3/library/tempfile.html > If " yes " then can this also be an alternative and does this > have any drawback ? Alternative to what? If you mean, alternative to *not* storing it in the user's directory, then yes, it is :-) Drawbacks -- yes. I hate it when applications dump temporary files in my home directory. -- Steven From ganesh1pal at gmail.com Wed Aug 28 00:40:59 2013 From: ganesh1pal at gmail.com (Ganesh Pal) Date: Wed, 28 Aug 2013 10:10:59 +0530 Subject: How to I do this in Python ? In-Reply-To: <5211c8f5$0$29885$c3e8da3$5496439d@news.astraweb.com> References: <520e060d$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211c8f5$0$29885$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Aug 19, 2013 at 12:57 PM, Steven D'Aprano wrote: > On Sun, 18 Aug 2013 22:36:01 +0530, Ganesh Pal wrote: > > > Please find the comments >>> inline > > Please don't do that! > > "Arrows" > are used for quoting in emails. If you prefix your *new* > comments using >>> it looks like they were quoted *three messages back*. > > You should be able to configure your email or news client to prefix > quoted text with a >, and then you just type your own comments with no > prefix, like I'm doing here. Even Gmail can do that. > > You seem to have copied-and-pasted my response into a new email, and then > added your comments. Am I right? The normal way to reply to an email is > to use Reply or Reply All. > > sure , hence forth will take of the same > > > You shouldn't normally care about where temporary files are stored, since > they're temporary and will disappear as soon as you are done with them. > But yes, tempfile has the ability to control where the files are stored. > Both tempfile.NamedTemporaryFile and tempfile.TemporaryFile take an > optional directory argument. > > To read the documentation, run these two commands at the interactive > prompt: > > > import tempfile > help(tempfile) > > > or read it on the web: > > http://docs.python.org/2/library/tempfile.html > http://docs.python.org/3/library/tempfile.html > > > > > If " yes " then can this also be an alternative and does this > > have any drawback ? > > Alternative to what? If you mean, alternative to *not* storing it in the > user's directory, then yes, it is :-) > > Drawbacks -- yes. I hate it when applications dump temporary files in my > home directory. > > > Thanks for the links and information on temporary files -------------- next part -------------- An HTML attachment was scrubbed... URL: From roy at panix.com Fri Aug 16 07:47:08 2013 From: roy at panix.com (Roy Smith) Date: Fri, 16 Aug 2013 07:47:08 -0400 Subject: How to I do this in Python ? References: Message-ID: In article , Ganesh Pal wrote: > # Creating sparse files in the sparse path > sparse_path = os.path.join(path,'sparsefiles') > os.makedirs(sparse_path) > os.chdir(sparse_path) > sparsefiles = "dd if=/dev/zero of=sp1 count=0 bs=1 seek=10G" > process_0 = subprocess.Popen(sparsefiles, stdout=subprocess.PIPE, > stderr=subprocess.PIPE, shell=True) There is no need to shell out to dd just to do this. All dd is doing for you is seeking to the offset you specify and closing the file. You can do that entirely in Python code. http://docs.python.org/2.7/library/stdtypes.html#file.seek From random832 at fastmail.us Fri Aug 16 14:17:04 2013 From: random832 at fastmail.us (random832 at fastmail.us) Date: Fri, 16 Aug 2013 14:17:04 -0400 Subject: How to I do this in Python ? In-Reply-To: References: Message-ID: <1376677024.18062.10646033.5BC79B5E@webmail.messagingengine.com> On Fri, Aug 16, 2013, at 7:47, Roy Smith wrote: > There is no need to shell out to dd just to do this. All dd is doing > for you is seeking to the offset you specify and closing the file. You > can do that entirely in Python code. > > http://docs.python.org/2.7/library/stdtypes.html#file.seek *ahem* http://docs.python.org/2.7/library/stdtypes.html#file.truncate The dd recipe was written some time over a decade ago as the easiest way to achieve this at a shell prompt (before the truncate tool was added to coreutils), and no-one's ever thought twice about it. From helmut_blass at web.de Fri Aug 16 06:28:16 2013 From: helmut_blass at web.de (helmut_blass at web.de) Date: Fri, 16 Aug 2013 03:28:16 -0700 (PDT) Subject: Options ExecCGI is off in this directory: Message-ID: Hello, I am desperately trying to get my python script running, but I alway get a 403-Error. apache logfile says: Options ExecCGI is off in this directory:/home/user12/cgi-bin/showblogs.py - apache configuration: AllowOverride None AddType application/python .py DirectoryIndex index.html index.htm index.php index.py Options +ExecCGI Indexes FollowSymLinks MultiViews AddHandler cgi-script .cgi .pl .py AllowOverride All Order allow,deny allow from all So what is missing or wrong here? thanx for your help, Helmut From jarabal at gmail.com Fri Aug 16 11:41:56 2013 From: jarabal at gmail.com (Xavi) Date: Fri, 16 Aug 2013 17:41:56 +0200 Subject: Options ExecCGI is off in this directory: In-Reply-To: References: Message-ID: <520E4844.2000202@gmail.com> You do not say the version of apache. If it's the 2.4 must change "allow from all" to "Require all granted". HTH -- Xavi El 16/08/2013 12:28, helmut_blass at web.de escribi?: > Hello, > I am desperately trying to get my python script running, but I alway get a 403-Error. > apache logfile says: > Options ExecCGI is off in this directory:/home/user12/cgi-bin/showblogs.py > - > apache configuration: > > > AllowOverride None > AddType application/python .py > DirectoryIndex index.html index.htm index.php index.py > Options +ExecCGI Indexes FollowSymLinks MultiViews > AddHandler cgi-script .cgi .pl .py > AllowOverride All > Order allow,deny > allow from all > > > So what is missing or wrong here? > thanx for your help, Helmut > From helmut_blass at web.de Fri Aug 16 12:57:48 2013 From: helmut_blass at web.de (helmut_blass at web.de) Date: Fri, 16 Aug 2013 09:57:48 -0700 (PDT) Subject: Options ExecCGI is off in this directory: In-Reply-To: References: Message-ID: <05652499-f83b-435c-aaae-9667e4e85baf@googlegroups.com> On Friday, August 16, 2013 5:41:56 PM UTC+2, Xavi wrote: > You do not say the version of apache. If it's the 2.4 > > must change "allow from all" to "Require all granted". it is apache2.2.14, so that's not the point. From andrew at invalid.invalid Fri Aug 16 10:02:08 2013 From: andrew at invalid.invalid (Andrew) Date: Fri, 16 Aug 2013 10:02:08 -0400 Subject: Proper use of the codecs module. Message-ID: <1efhl8i0dmr9b.15q8opn6p0cj3.dlg@40tude.net> I have a mixed binary/text file[0], and the text portions use a radically nonstandard character set. I want to read them easily given information about the character encoding and an offset for the beginning of a string. The descriptions of the codecs module and codecs.register() in particular seem to suggest that this is already supported in the standard library. However, I can't find any examples of its proper use. Most people who use the module seem to want to read utf files in python 2.x.[1] I would like to know how to correctly set up a new codec for reading files that have nonstandard encodings. I have two other related questions: How does seek() work on a file opened in text mode? Does it seek to a character offset or to a byte offset? I need the latter behavior. If I can't get it I will have to find a different approach. The files I'm working with use a nonstandard end-of-string character in the same fashion as C null-terminated strings. Is there a builtin function that will read a file "from seek position until seeing EOS character X"? The methods I see for this online seem to amount to reading one character at a time and checking manually, which seems nonoptimal to me. [0] The file is an SNES ROM dump, but I don't think that matters. [1] I'm using Python 3, if it's relevant. -- Andrew From steve+comp.lang.python at pearwood.info Fri Aug 16 15:12:02 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 Aug 2013 19:12:02 GMT Subject: Proper use of the codecs module. References: <1efhl8i0dmr9b.15q8opn6p0cj3.dlg@40tude.net> Message-ID: <520e7982$0$30000$c3e8da3$5496439d@news.astraweb.com> On Fri, 16 Aug 2013 10:02:08 -0400, Andrew wrote: > I have a mixed binary/text file[0], and the text portions use a > radically nonstandard character set. I want to read them easily given > information about the character encoding and an offset for the beginning > of a string. "Mixed binary/text" is not a helpful model to use. You are better off thinking of the file as "binary", where some of the fields happen to contain text encoded with some custom codec. If you try opening the file in text mode, you'll very likely break the binary parts (e.g. converting the two bytes 0x0D0A to a single byte 0x0A). So best to stick to binary only, extract the "text" portions of the file, then explicitly decode them. > The descriptions of the codecs module and codecs.register() in > particular seem to suggest that this is already supported in the > standard library. However, I can't find any examples of its proper use. > Most people who use the module seem to want to read utf files in python > 2.x.[1] I would like to know how to correctly set up a new codec for > reading files that have nonstandard encodings. I suggest you look at the source code for the dozens of codecs in the standard library. E.g. /usr/local/lib/python3.3/encodings/palmos.py (Adjust for your installation location as required.) > I have two other related questions: > > How does seek() work on a file opened in text mode? Does it seek to a > character offset or to a byte offset? I need the latter behavior. If I > can't get it I will have to find a different approach. For text files, seek() is only legal for offsets that tell() can return, but this is not enforced, so you can get nasty rubbish like this: py> f = open('/tmp/t', 'w', encoding='utf-32') py> f.write('hello world') 11 py> f.close() py> f = open('/tmp/t', 'r', encoding='utf-32') py> f.read(1) 'h' py> f.tell() 8 py> f.seek(3) 3 py> f.read(1) '?' So I prefer not to seek in text files if I can help it. > The files I'm working with use a nonstandard end-of-string character in > the same fashion as C null-terminated strings. Is there a builtin > function that will read a file "from seek position until seeing EOS > character X"? The methods I see for this online seem to amount to > reading one character at a time and checking manually, which seems > nonoptimal to me. How do you think such a built-in function would work, if not inspect each character until the EOS character is seen? :-) There is no such built-in function though. By default, Python files are buffered, so it won't literally read one character from disk at a time. The actual disk IO will read a bunch of bytes into a memory buffer, and then read from the buffer. -- Steven From andrew at invalid.invalid Fri Aug 16 16:16:31 2013 From: andrew at invalid.invalid (Andrew) Date: Fri, 16 Aug 2013 16:16:31 -0400 Subject: Proper use of the codecs module. References: <1efhl8i0dmr9b.15q8opn6p0cj3.dlg@40tude.net> <520e7982$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 16 Aug 2013 19:12:02 GMT, Steven D'Aprano wrote: > If you try opening the file in text mode, you'll very likely break the > binary parts (e.g. converting the two bytes 0x0D0A to a single byte > 0x0A). So best to stick to binary only, extract the "text" portions of > the file, then explicitly decode them. Okay, I'll do that. Given what you said about seek() and text mode below, I have no choice anyway. >> I would like to know how to correctly set up a new codec for >> reading files that have nonstandard encodings. > > I suggest you look at the source code for the dozens of codecs in the > standard library. E.g. /usr/local/lib/python3.3/encodings/palmos.py I'll do that too. My thanks for the pointer. >> How does seek() work on a file opened in text mode? Does it seek to a >> character offset or to a byte offset? I need the latter behavior. If I >> can't get it I will have to find a different approach. > > For text files, seek() is only legal for offsets that tell() can return, > but this is not enforced, so you can get nasty rubbish like this: > > > > So I prefer not to seek in text files if I can help it. If I'm understanding the above right, it seeks to a byte offset but the behavior is undocumented, not guaranteed, shouldn't be used, etc. That would actually work for me in theory (because I have exact byte offsets to work with) but I think I'll avoid it anyway, on the grounds that relying on undocumented behavior is bad. >> The files I'm working with use a nonstandard end-of-string character in >> the same fashion as C null-terminated strings. Is there a builtin >> function that will read a file "from seek position until seeing EOS >> character X"? The methods I see for this online seem to amount to >> reading one character at a time and checking manually, which seems >> nonoptimal to me. > > How do you think such a built-in function would work, if not inspect each > character until the EOS character is seen? :-) I don't know, but I'm assuming it wouldn't involve a function call to file.read(1) for each character, and that's what Google keeps handing me. Such an approach fills me with horror. :-) I suppose there's nothing stopping me from reading some educated guess at the length of the string and then stepping through the result. Or I'll look at the readline() source and see how it does its thing. > There is no such built-in function though. By default, Python files are > buffered, so it won't literally read one character from disk at a time. > The actual disk IO will read a bunch of bytes into a memory buffer, and > then read from the buffer. I'd guessed as much, but assumed there was still ridiculous function call overhead involved in the repeated read(1) method above. Of course, trying to avoid said overhead is premature optimization; my interest in doing so is more aesthetic than anything else. Thanks for the help. -- Andrew From rosuav at gmail.com Fri Aug 16 18:14:20 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 16 Aug 2013 23:14:20 +0100 Subject: Proper use of the codecs module. In-Reply-To: <1efhl8i0dmr9b.15q8opn6p0cj3.dlg@40tude.net> References: <1efhl8i0dmr9b.15q8opn6p0cj3.dlg@40tude.net> Message-ID: On Fri, Aug 16, 2013 at 3:02 PM, Andrew wrote: > I have a mixed binary/text file[0], and the text portions use a radically > nonstandard character set. I want to read them easily given information > about the character encoding and an offset for the beginning of a string. To add to all the information already given: Is the file small enough to comfortably fit into memory? If so, you'll find it a LOT easier to play with strings in RAM than files on disk. Even if not, you may find a lot of tasks simplified by just reading a kay or a meg in and then working within that. That spares you the fiddliness of read(1) all the time, at the expense of potentially reading more than you need. ChrisA From thrinaxodon.fan.club512 at gmail.com Fri Aug 16 10:25:26 2013 From: thrinaxodon.fan.club512 at gmail.com (THRINA.XODON) Date: Fri, 16 Aug 2013 10:25:26 -0400 Subject: THRINAXODON ON RAGE. Message-ID: ======================================================== >THRINAXODON GETS REVENGE AGAINST SMITHSONIAN ASS HOLES. ======================================================== > TODAY; THE SMITHSONIAN BURNED THRINAXODON'S HOUSE DOWN; AS HE WENT TO GET GROCERY'S FROM MARK'S. > THE ARSONISTS WERE COMPOSED OF: a) David Iain Greig, whose email is dgreig at ediacara.org b) SkyEyes, whose email is skyeyes9 at cox.net/skyeyes9 at yahoo.com c) Peter Nyikos, whose email is nyikos2 at bellsouth.net d) Paul J Gans, whose email is ganso at panix.com > THEY WERE ARRESTED AT CHRISTMAN EVE. > HOWEVER, THEY ESCAPED. CONTACT THRINAXODON @ mailto:biolord9 at spamgmail.com > ======================================== > MAN AS OLD AS FROGS: > http://thrinaxodon.wordpress.com/faq > THRINAXODON NOW ON FACEBOOK. -- Thrinaxodon, The Ultimate Defender of USENET From ms2597 at cornell.edu Fri Aug 16 13:15:01 2013 From: ms2597 at cornell.edu (cutems93) Date: Fri, 16 Aug 2013 10:15:01 -0700 (PDT) Subject: Lettuce vs Behave Message-ID: I found that BDD is a very good philosophy for coding and checking my program, and I decided to use either of these two software. However, it seems these two are very similar in the way they function. As professionals, what do you prefer and why? From ben+python at benfinney.id.au Thu Aug 29 02:12:59 2013 From: ben+python at benfinney.id.au (Ben Finney) Date: Thu, 29 Aug 2013 16:12:59 +1000 Subject: Lettuce vs Behave References: Message-ID: <7wd2oxuis4.fsf@benfinney.id.au> cutems93 writes: > However, it seems these two [Lettuce and Behave] are very similar in > the way they function. As professionals, what do you prefer and why? I haven't used both, and have only begun using Behave. The Behave documentation compares it with Lettuce here . -- \ ?Odious ideas are not entitled to hide from criticism behind | `\ the human shield of their believers' feelings.? ?Richard | _o__) Stallman | Ben Finney From jumpmanlane at myopera.com Fri Aug 30 10:23:32 2013 From: jumpmanlane at myopera.com (jumpmanlane at myopera.com) Date: Fri, 30 Aug 2013 07:23:32 -0700 (PDT) Subject: Lettuce vs Behave In-Reply-To: References: Message-ID: <0c4e6d65-334d-4de7-8c96-87ba4e5f14bc@googlegroups.com> On Friday, August 16, 2013 1:15:01 PM UTC-4, cutems93 wrote: > I found that BDD is a very good philosophy for coding and checking my program, and I decided to use either of these two software. However, it seems these two are very similar in the way they function. As professionals, what do you prefer and why? +1 for Behave -J From ben+python at benfinney.id.au Fri Aug 30 18:58:07 2013 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 31 Aug 2013 08:58:07 +1000 Subject: Lettuce vs Behave References: <0c4e6d65-334d-4de7-8c96-87ba4e5f14bc@googlegroups.com> Message-ID: <7wzjry9474.fsf@benfinney.id.au> jumpmanlane at myopera.com writes: > On Friday, August 16, 2013 1:15:01 PM UTC-4, cutems93 wrote: > > As professionals, what do you prefer and why? > > +1 for Behave And why? -- \ ?In the long run, the utility of all non-Free software | `\ approaches zero. All non-Free software is a dead end.? ?Mark | _o__) Pilgrim, 2006 | Ben Finney From dr.mtarver at gmail.com Fri Aug 16 13:37:44 2013 From: dr.mtarver at gmail.com (Mark Tarver) Date: Fri, 16 Aug 2013 10:37:44 -0700 (PDT) Subject: Shen video appeal - generating type secure Python Message-ID: Shen is a hypermodern functional programming language based on a core that is essentially a Lisp, but portable to many major language platforms. One of these platforms is Python. I am asking for support for the Shen project in this video appeal www.shenlanguage.org/appeal.html The video explains the important work taking place in the Shen group and what we hope to achieve. Essentially we will be able to generate type-secure Python programs which can be run independently within the Python environment. We're asking Python programmers to support us in the production of this technology. with thanks Mark Tarver www.shenlanguage.org From orgnut at yahoo.com Fri Aug 16 22:55:38 2013 From: orgnut at yahoo.com (Larry Hudson) Date: Fri, 16 Aug 2013 19:55:38 -0700 Subject: Rock, Paper, Scissors game Message-ID: Some time ago there was a post asking for help on a rock/paper/scissors game. I read that thread at the time it was posted, but since it received several answers I didn't pay too much attention to it. But I can't find that thread again right now. However, the subject stuck (loosely) in my mind, and it finally fermented enough that I wanted to try writing a version myself. My approach is very different from the original. It is based on the fact that there are only nine possible combinations. I calculate a value based on the combination (it's effectively calculating the value of a ternary number) and use this value as an index into a tuple of Results strings and a tuple of win/lose/draw codes. Probably the least obvious and most confusing aspect is how I piece together these output strings, using the new-style print formatting syntax. The rest of the program should be pretty straight-forward. I think it's fairly well commented. Of course, I don't claim this to be optimum, or even necessarily a good approach -- but it works. And I thought some people might find it interesting look at and try. (An irrelevant side-note: I wrote it on a Raspberry Pi.) =========== ========== #!/usr/bin/env python3 # rps.py -- Rock, Paper, Scissors game from random import choice WIN = 0 LOSE = 1 DRAW = 2 def get_result(h, c): """Determine who wins this round, return appropriate string and win/lose/draw code. h: Human's selection (r, p or s) c: Computer's selection (r, p or s) """ def val(c): """Convert charcter r, p or s to values 0, 1 or 2""" return 'rps'.index(c) # Strings used in results rp = 'Paper covers Rock. {}' rs = 'Rock smashes scissors. {}' ps = 'Scissors cuts paper. {}' ti = 'We both have {}. {}' # Win/lose/draw codes win = (DRAW, WIN, LOSE, LOSE, DRAW, WIN, WIN, LOSE, DRAW) # Win/lose/draw strings wins = ('You win', 'I win', "It's a draw") # Results strings res = ( ti.format('rocks', wins[DRAW]), # r-r rp.format(wins[WIN]), # r-p rs.format(wins[LOSE]), # r-s rp.format(wins[LOSE]), # p-r ti.format('paper', wins[DRAW]), # p-p ps.format(wins[WIN]), # p-s rs.format(wins[WIN]), # s-r ps.format(wins[LOSE]), # s-p ti.format('scissors', wins[DRAW]) # s-s ) score = val(h) + 3 * val(c) # Calculate score return res[score], win[score] # Result string & win code def get_rps(): """Get Rock/Paper/Scissor choice.""" while True: select = input('\nDo you choose Rock, Paper or Scissors ').lower() # Check for empty input or quit command if not select or select in ['q', 'quit']: return 'q' # Return quit code # Check for valid input if select in ['r', 'rock', 'p', 'paper', 's', 'scissors']: return select[0] # Return first character print('What did you say?? Try again please') #================= Main Program starts here ============== # Keep track of results: # scores[0] = number of human wins # scores[1] = number of computer wins # scores[2] = number of draws scores = [0] * 3 things = {'r':'a rock', 'p':'paper', 's':'scissors'} print("Let's play a game or Rock, Paper, Scissors.\n") print('Enter "r", "p", "s", "rock", "paper", or "scissors" for your choice.') print('Use empty input, "q" or "quit" to end the game.') while True: computer = choice('rps') # Computer selects human = get_rps() # Human selects if human == 'q': break print('You have {}, I have {}. '.format( things[human], things[computer]), end='') res, scr = get_result(human, computer) scores[scr] += 1 # Count win/lose/draw print(res) # And show results # Show final scores print('\nTotal scores:') print('\tYou won {} games'.format(scores[WIN])) print('\tComputer won {} games'.format(scores[LOSE])) print('\tThere were {} tie games'.format(scores[DRAW])) print('\nThanks for playing with me. Bye now.') ========== ========== -=- Larry -=- From fsaldan1 at gmail.com Sat Aug 17 08:26:32 2013 From: fsaldan1 at gmail.com (fsaldan1 at gmail.com) Date: Sat, 17 Aug 2013 05:26:32 -0700 (PDT) Subject: Encapsulation unpythonic? Message-ID: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> I am new to Python, with experience in Java, C++ and R. As I understand encapsulation is not a big thing in the Python world. I read that you can put two underscores before the name of a variable within a class declaration but in the many examples of code I looked at this is not widely used. I also read that encapsulation is "unpythonic." Questions: 1) Is there a good text where I can read about the language philosophy? What practices are "pythonic" or "unpythonic"? 2) If it is in fact true that encapsulation is rarely used, how do I deal with the fact that other programmers can easily alter the values of members of my classes? Thanks for any insights. FS From skip at pobox.com Sat Aug 17 08:54:39 2013 From: skip at pobox.com (Skip Montanaro) Date: Sat, 17 Aug 2013 07:54:39 -0500 Subject: Encapsulation unpythonic? In-Reply-To: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> References: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> Message-ID: In Guido's own words: "We're all consenting adults here." http://importthis.tumblr.com/post/6719643315/public-private-attributes Skip From bouncingcats at gmail.com Sat Aug 17 09:05:52 2013 From: bouncingcats at gmail.com (David) Date: Sat, 17 Aug 2013 23:05:52 +1000 Subject: Encapsulation unpythonic? In-Reply-To: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> References: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> Message-ID: On 17 August 2013 22:26, wrote: > > 1) Is there a good text where I can read about the language philosophy? What practices are "pythonic" or "unpythonic"? > > 2) If it is in fact true that encapsulation is rarely used, how do I deal with the fact that other programmers can easily alter the values of members of my classes? Try this: http://dirtsimple.org/2004/12/python-is-not-java.html From rosuav at gmail.com Sat Aug 17 11:50:06 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 17 Aug 2013 16:50:06 +0100 Subject: Encapsulation unpythonic? In-Reply-To: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> References: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> Message-ID: On Sat, Aug 17, 2013 at 1:26 PM, wrote: > 2) If it is in fact true that encapsulation is rarely used, how do I deal with the fact that other programmers can easily alter the values of members of my classes? Very simply: You accept it. Let them! It's their responsibility. Python scripts are generally assumed to start at the beginning, go on till they reach the end, then stop (like the White King's advice to Alice). The author of the application is assumed to be in command of everything. If s/he chooses to monkey-patch something, so be it. If that monkey-patch breaks in the next version, it's the app author's problem. As a module or class author, you just need to make sure you don't make crazy changes to the environment, and all will be well. If you have invariants that you want to maintain, you can simply document the one official way to mutate your objects ("use the .foo() method, don't tinker with the members"), and most people will respect that. But most of the time, that's not even an issue - all you have to do is tell yourself "It's fine for them to change stuff", and (a) you save the hassle of preventing them, and (b) you save the hassle of writing tons of boilerplate to grant specific access. class Point def __init__(self,x,y): self.x,self.y=x,y def distance(self,other): return math.sqrt((self.x-other.x)**2+(self.y-other.y)**2) foo = Point(0,0) while True: foo.x+=deltax; foo.y+=deltay if foo.distance(bar)>50: break Easy! No getter/setter needed. ChrisA From tjreedy at udel.edu Sat Aug 17 17:22:38 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 17 Aug 2013 17:22:38 -0400 Subject: Encapsulation unpythonic? In-Reply-To: References: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> Message-ID: On 8/17/2013 11:50 AM, Chris Angelico wrote: > On Sat, Aug 17, 2013 at 1:26 PM, wrote: >> 2) If it is in fact true that encapsulation is rarely used, how do I deal with the fact that other programmers can easily alter the values of members of my classes? > > > Very simply: You accept it. Let them! It's their responsibility. When a project has multiple programmers, there is a possibility that module C could monkeypatch module A in a way that breaks existing user module B. But it is still the collective responsibility of the respective users or project manager to assign responsibility for fixing the conflict. -- Terry Jan Reedy From rosuav at gmail.com Sat Aug 17 20:59:10 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 18 Aug 2013 01:59:10 +0100 Subject: Encapsulation unpythonic? In-Reply-To: References: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> Message-ID: On Sat, Aug 17, 2013 at 10:22 PM, Terry Reedy wrote: > On 8/17/2013 11:50 AM, Chris Angelico wrote: >> >> On Sat, Aug 17, 2013 at 1:26 PM, wrote: >>> >>> 2) If it is in fact true that encapsulation is rarely used, how do I deal >>> with the fact that other programmers can easily alter the values of members >>> of my classes? >> >> Very simply: You accept it. Let them! It's their responsibility. > > > When a project has multiple programmers, there is a possibility that module > C could monkeypatch module A in a way that breaks existing user module B. > But it is still the collective responsibility of the respective users or > project manager to assign responsibility for fixing the conflict. Yep. I would say there that the responsibility is with module C's programmers; they are the ones breaching encapsulation, ergo it's primarily their responsibility to both test this (against current usage) and thoroughly document it (against future changes). Personally, I would like to see a comment in module A that says something like "NOTE: This {function|attribute|spam} is replaced externally under [some circumstance]. Changes to its purpose may affect Module B." to make it clear what's going on. At that point, of course, it stops being monkeypatching and becomes documented and official behaviour, so maybe that's not really an argument in this example. ChrisA From steve+comp.lang.python at pearwood.info Sat Aug 17 12:17:40 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 Aug 2013 16:17:40 GMT Subject: Encapsulation unpythonic? References: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> Message-ID: <520fa223$0$30000$c3e8da3$5496439d@news.astraweb.com> On Sat, 17 Aug 2013 05:26:32 -0700, fsaldan1 wrote: > I am new to Python, with experience in Java, C++ and R. > > As I understand encapsulation is not a big thing in the Python world. Utter nonsense. Whoever told you this doesn't understand what encapsulation is. Python encapsulates related code into objects. It encapsulates related objects into modules. It encapsulates related modules into packages. > I > read that you can put two underscores before the name of a variable > within a class declaration but in the many examples of code I looked at > this is not widely used. I also read that encapsulation is "unpythonic." That's *data hiding*, not encapsulation. Very few languages -- possibly none at all -- can hide data from a sufficiently motivated programmer. Python doesn't even try. "We're all adults here" is the philosophy, and data hiding is by convention, not enforced by the compiler. Single leading underscores are "private". Don't touch them. If you do, and code breaks, nobody will give you sympathy. You have nobody to blame but yourself. Double leading underscores are "private", and also have name-mangling to try to avoid certain inheritance-related issues. In general, it's more of a nuisance than anything else, so most people don't bother. Consider double underscore __names to be for advanced OOP usage, 98% of the time a single underscore is enough. Double leading and trailing __names__ are reserved for Python. They're not necessarily private, but if you're calling them directly, you're probably doing something wrong. Again, consider them to be advanced usage. > Questions: > > 1) Is there a good text where I can read about the language philosophy? > What practices are "pythonic" or "unpythonic"? Good question! Start at the interactive interpreter: import this This is a reasonable description of what it means to be Pythonic: http://blog.startifact.com/posts/older/what-is-pythonic.html This is a good pair of resources, comparing the Java and Python philosophies, and the strengths of each: http://dirtsimple.org/2004/12/python-is-not-java.html http://dirtsimple.org/2004/12/java-is-not-python-either.html Also, it helps to understand that Python is named after Monty Python, not the snake. It's not necessary to like anarchic British humour, but it helps to get some of the references. We'll talk about "spam, ham, eggs" rather than "foo, bar, baz", and the Cheeseshop, and the Spanish Inquisition, and Norwegian Blue parrots. But ultimately, writing Pythonic code doesn't come from reading a list of rules. It comes from becoming comfortable with the language, from understanding its strengths and weaknesses, from reading lots of people's code, and writing lots of code, and learning the idioms. > 2) If it is in fact true that encapsulation is rarely used, Not true. It is true that data hiding is really used though, at least in pure-Python code, except by convention. (C extensions are much more strict about data hiding, since you can crash the compiler if you muck about with C-level internals. Exceptions are a good thing. Segfaults are not.) > how do I > deal with the fact that other programmers can easily alter the values of > members of my classes? Embrace it! That's a good thing! In Java or C++ or other languages, other programmers are going to alter your classes' members anyway. The only difference is that they will spend hours or days fighting the compiler in order to do so, and eventually end up with horrible, fragile, non-portable code. Besides, while it's nearly always a Bad Thing to mess with private attributes, sometimes it is a really, really Useful Thing to *inspect* private attributes, for debugging. Python makes that easy. Treat other programmers as adults, and they in turn will treat you the same way. If they insist on messing with your private single-underscore _attributes, you can't stop them, but that's okay, you don't have to be sympathetic when they shoot their foot off. Just slap them with a large halibut[1] and laugh. [1] Another Monty Python reference. -- Steven From joshua at landau.ws Sun Aug 18 13:15:10 2013 From: joshua at landau.ws (Joshua Landau) Date: Sun, 18 Aug 2013 18:15:10 +0100 Subject: Encapsulation unpythonic? In-Reply-To: <520fa223$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> <520fa223$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 17 August 2013 17:17, Steven D'Aprano wrote: > On Sat, 17 Aug 2013 05:26:32 -0700, fsaldan1 wrote: >> how do I >> deal with the fact that other programmers can easily alter the values of >> members of my classes? > ... > If they insist on messing with your private single-underscore > _attributes, you can't stop them, but that's okay, you don't have to be > sympathetic when they shoot their foot off. Just slap them with a large > halibut[1] and laugh. I know I've cropped your points but I just want to mention here that the only reason to monkey-patch code in these ways where you'd want to stop them is when the alternative is *worse*. It's like removing railings from a cliff to stop people hitting the bars. From steve at pearwood.info Mon Aug 19 03:05:48 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 19 Aug 2013 07:05:48 GMT Subject: Encapsulation unpythonic? References: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> <520fa223$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5211c3cc$0$29885$c3e8da3$5496439d@news.astraweb.com> On Sun, 18 Aug 2013 18:15:10 +0100, Joshua Landau wrote: > On 17 August 2013 17:17, Steven D'Aprano > wrote: >> On Sat, 17 Aug 2013 05:26:32 -0700, fsaldan1 wrote: >>> how do I >>> deal with the fact that other programmers can easily alter the values >>> of members of my classes? >> ... >> If they insist on messing with your private single-underscore >> _attributes, you can't stop them, but that's okay, you don't have to be >> sympathetic when they shoot their foot off. Just slap them with a large >> halibut[1] and laugh. > > I know I've cropped your points but I just want to mention here that the > only reason to monkey-patch code in these ways where you'd want to stop > them is when the alternative is *worse*. It's like removing railings > from a cliff to stop people hitting the bars. I'm not actually talking about monkey-patching. I'm talking about just normal inheritance of classes. E.g. If a module has this class: class Parrot: def __init__(self): self._name = "Polly" def talk(self): print "%s wants a cracker!" % self._name I might be tempted to do this: class MyParrot: def __init__(self): super(MyParrot, self).__init__() self._name = "George" No monkey-patching involved. But, if the author of Parrot class changes his implementation and gets rid of "_name", or even makes it public "name", my subclass will stop working. Sucks to be me. In this toy example, both parties are at fault: the author of Parrot for unnecessary data-hiding of something which is so obviously a useful piece of information and should be part of the public interface, and me for nevertheless ignoring that warning and using the private attribute in my own code. More realistic examples may be different. -- Steven From random832 at fastmail.us Wed Aug 21 12:52:06 2013 From: random832 at fastmail.us (random832 at fastmail.us) Date: Wed, 21 Aug 2013 12:52:06 -0400 Subject: Encapsulation unpythonic? In-Reply-To: <5211c3cc$0$29885$c3e8da3$5496439d@news.astraweb.com> References: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> <520fa223$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211c3cc$0$29885$c3e8da3$5496439d@news.astraweb.com> Message-ID: <1377103926.2140.12517705.035CBD5C@webmail.messagingengine.com> On Mon, Aug 19, 2013, at 3:05, Steven D'Aprano wrote: > In this toy example, both parties are at fault: the author of Parrot for > unnecessary data-hiding of something which is so obviously a useful piece > of information and should be part of the public interface, It may wish to be notified when its name changes, and so have a name property. The subclass may want to use a variable called _name for some other purpose. (maybe "name" isn't the best example). Examples often look pathological when you simplify out the bit that makes them make sense. -- Random832 From ian.g.kelly at gmail.com Wed Aug 21 18:02:57 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 21 Aug 2013 16:02:57 -0600 Subject: Encapsulation unpythonic? In-Reply-To: <1377103926.2140.12517705.035CBD5C@webmail.messagingengine.com> References: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> <520fa223$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211c3cc$0$29885$c3e8da3$5496439d@news.astraweb.com> <1377103926.2140.12517705.035CBD5C@webmail.messagingengine.com> Message-ID: On Aug 21, 2013 10:53 AM, wrote: > > On Mon, Aug 19, 2013, at 3:05, Steven D'Aprano wrote: > > In this toy example, both parties are at fault: the author of Parrot for > > unnecessary data-hiding of something which is so obviously a useful piece > > of information and should be part of the public interface, > > It may wish to be notified when its name changes, and so have a name > property. The example as given has no such property, and regardless of whether it is a property or an attribute the public API should just be called "name". > The subclass may want to use a variable called _name for some > other purpose. (maybe "name" isn't the best example). Probably not a good idea for multiple reasons if the base class already has something called "name". On Aug 21, 2013 10:53 AM, wrote: > On Mon, Aug 19, 2013, at 3:05, Steven D'Aprano wrote: > > In this toy example, both parties are at fault: the author of Parrot for > > unnecessary data-hiding of something which is so obviously a useful piece > > of information and should be part of the public interface, > > It may wish to be notified when its name changes, and so have a name > property. The subclass may want to use a variable called _name for some > other purpose. (maybe "name" isn't the best example). > > Examples often look pathological when you simplify out the bit that > makes them make sense. > > -- > Random832 > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Wed Aug 21 22:06:24 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 22 Aug 2013 02:06:24 GMT Subject: Encapsulation unpythonic? References: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> <520fa223$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211c3cc$0$29885$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5215721f$0$29986$c3e8da3$5496439d@news.astraweb.com> On Wed, 21 Aug 2013 12:52:06 -0400, random832 wrote: > On Mon, Aug 19, 2013, at 3:05, Steven D'Aprano wrote: >> In this toy example, both parties are at fault: the author of Parrot >> for unnecessary data-hiding of something which is so obviously a useful >> piece of information and should be part of the public interface, > > It may wish to be notified when its name changes, and so have a name > property. The subclass may want to use a variable called _name for some > other purpose. (maybe "name" isn't the best example). Such a "name" property would be a public interface, and so a Good Thing. However, my toy example was of a case where something *obviously useful* was being left out of the public interface. If it were a public property, it wouldn't be the case that it were left out, would it? > Examples often look pathological when you simplify out the bit that > makes them make sense. Naturally :-) I did call this a toy example. Nevertheless, in the Real World, data hiding can sometimes be a bit of a religion. Not all cases where people try various tricks and hacks to gain access to "private" and "protected" members are specious. The whole Java getter and setter philosophy is based on the idea that everything, even the most innocuous data attribute, ought to be private, with a computed getter and setter Just In Case some day in the future you want to wrap access in code. In Python, you can turn an attribute into a computed property with no change to the public interface. In Java, you can't. -- Steven From gary.herron at islandtraining.com Sat Aug 17 16:16:32 2013 From: gary.herron at islandtraining.com (Gary Herron) Date: Sat, 17 Aug 2013 13:16:32 -0700 Subject: Encapsulation unpythonic? In-Reply-To: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> References: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> Message-ID: <520FDA20.6050400@islandtraining.com> On 08/17/2013 05:26 AM, fsaldan1 at gmail.com wrote: > I am new to Python, with experience in Java, C++ and R. > > As I understand encapsulation is not a big thing in the Python world. I read that you can put two underscores before the name of a variable within a class declaration but in the many examples of code I looked at this is not widely used. I also read that encapsulation is "unpythonic." > > Questions: > > 1) Is there a good text where I can read about the language philosophy? What practices are "pythonic" or "unpythonic"? > > 2) If it is in fact true that encapsulation is rarely used, how do I deal with the fact that other programmers can easily alter the values of members of my classes? > > Thanks for any insights. > > FS You are confusing encapsulation with data hiding! Encapsulation is very much a part of Python. Every class, module, indeed every object, encapsulates some kind of behavior. However, *hiding* the members of a class is not considered Pythonic. There is no private/public as in C++, however, there are way to achieve that effect. Gary Herron From chaz2cry at gmail.com Thu Aug 22 22:12:19 2013 From: chaz2cry at gmail.com (chaz2cry at gmail.com) Date: Thu, 22 Aug 2013 19:12:19 -0700 (PDT) Subject: Encapsulation unpythonic? In-Reply-To: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> References: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> Message-ID: On Saturday, August 17, 2013 8:26:32 AM UTC-4, Fernando Saldanha wrote: > I am new to Python, with experience in Java, C++ and R. > > > > As I understand encapsulation is not a big thing in the Python world. I read that you can put two underscores before the name of a variable within a class declaration but in the many examples of code I looked at this is not widely used. I also read that encapsulation is "unpythonic." > > > > Questions: > > > > 1) Is there a good text where I can read about the language philosophy? What practices are "pythonic" or "unpythonic"? > > > > 2) If it is in fact true that encapsulation is rarely used, how do I deal with the fact that other programmers can easily alter the values of members of my classes? > > > > Thanks for any insights. > > > > FS Hi FS, I'm taking the Python Cert series w/ O'Reilly School of Technology, which I recommend if you've got a good handle on OO programming. In any event, according to what I've learned, "encapsulation is the idea that the only way to access or change the data inside an object is by calling its methods. This idea has never really gained much ground in the Python world, and it is normally considered acceptable to both read and set an object's attributes from anywhere in a program." Not being an expert OO programmer, I take this at face value. There are ways to protect class attributes from having their values reset from outside. That is, they can be made "internal use only" and an AttributeError raised when someone tries to change the attribute(s). This involves __setattr__ and checking if the key of the attribute is in a/the list of attributes you've chose to protect. If so, raise AttributeError. Hope that helps in some small measure. In the interest of full disclosure, answering a Python question is part of my homework for the O'Reilly Python 4 class I'm taking. Good luck! From fp2161 at gmail.com Fri Aug 30 13:43:28 2013 From: fp2161 at gmail.com (Fabrice Pombet) Date: Fri, 30 Aug 2013 10:43:28 -0700 (PDT) Subject: Encapsulation unpythonic? In-Reply-To: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> References: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> Message-ID: <8c7c4854-70e1-46e7-a3ff-a3206c4c5c27@googlegroups.com> On Saturday, August 17, 2013 2:26:32 PM UTC+2, Fernando Saldanha wrote: > I am new to Python, with experience in Java, C++ and R. > > > > As I understand encapsulation is not a big thing in the Python world. I read that you can put two underscores before the name of a variable within a class declaration but in the many examples of code I looked at this is not widely used. I also read that encapsulation is "unpythonic." > > > > Questions: > > > 2) If it is in fact true that encapsulation is rarely used, how do I deal with the fact that other programmers can easily alter the values of members of my classes? > Fernando, it is widely accepted that Python pays very little attention to encapsulation as a principle set in stone. Chaz's definition of encapsulation is also mine. Now you need to consider that taking this principle off the hostel of OOP does not mean that you can do whatever you fancy and you can't make anything unsettable. There are plenty of techniques within Python that allow you to protect your arguments (in particular, decorators) inside a Class. Now, lets get to the pretentious philosophical discussion: I guess encapsulation is quite the opposite of, say, dynamic typing, which is arguably core in Python. In practice this allows Python to be less verbose: at the end of the day, if you look back at your previous languages, don't you find that some of their compulsory features are usually more of a pain than something useful in practice? And after all, whither encapsulation? Can't we just have objects whose arguments are determined externally if we want to? And that is the ballgame: as my old tutor says: "the claptrap of setters and getters does not need to be here if it is unnecessary". I would add: "so long as you can have them when you deem it necessary", and Python allows that. From steve+comp.lang.python at pearwood.info Fri Aug 30 22:35:39 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 31 Aug 2013 02:35:39 GMT Subject: Encapsulation unpythonic? References: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> <8c7c4854-70e1-46e7-a3ff-a3206c4c5c27@googlegroups.com> Message-ID: <5221567b$0$6599$c3e8da3$5496439d@news.astraweb.com> On Fri, 30 Aug 2013 10:43:28 -0700, Fabrice Pombet wrote: > On Saturday, August 17, 2013 2:26:32 PM UTC+2, Fernando Saldanha wrote: >> 2) If it is in fact true that encapsulation is rarely used, how do I >> deal with the fact that other programmers can easily alter the values >> of members of my classes? >> > Fernando, it is widely accepted that Python pays very little attention > to encapsulation as a principle set in stone. Widely accepted by whom? Python code is *full* of encapsulation. Functions, methods, classes, modules, packages, even local variables, are all mechanisms for encapsulating code and data. Those who say that Python has little or no encapsulation are talking rubbish. > Chaz's definition of > encapsulation is also mine. Who is Chaz, and what definition does he have? > Now you need to consider that taking this > principle off the hostel of OOP does not mean that you can do whatever > you fancy and you can't make anything unsettable. > > There are plenty of techniques within Python that allow you to protect > your arguments (in particular, decorators) inside a Class. And now you are talking about information hiding and protection, which is not the same of encapsulation, no matter what the academics think. Sometimes the air gets a bit too thin to breathe way up at the top of those ivory towers... Encapsulation is about grouping code that needs to be together together. In contract, you have programming languages that give you little, or nothing, in the way of grouping -- everything is one big chunk of code, with GOTO or GOSUB to jump from place to place. Functions and procedures are the first, most simple, form of encapsulation. Classes allow you to encapsulate multiple functions ("methods") together with the data they need to operate on in one chunk. Even in C++ or Java, you can have classes that provide no information hiding at all -- just declare everything "public". On the other hand, non-OOP languages like C can implement information hiding. In C, you can hide information from other files by declaring them as "static". Variables declared inside a brace-delimited block only exist within that block: local variables are hidden. For example: int foo; static int bar; bar is hidden from other files. Likewise, in this function: int func(void) { int baz; ... } baz is local to func, and invisible to any other function. So you can have information hiding without classes, and classes without information hiding. The two concepts are obviously independent, but as usual, the academics who are in love with OOP like to pretend that anything that is of any interest whatsoever in computing was invented by Java and C++. There are even languages with functions, but no local variables. For instance, older versions of Forth let you define functions, what Forth calls "words", but all functions operate on the same global stack. Python has excellent encapsulation: we can combine code that ought to be together into a function, related functions into a class, related classes into a module, and related modules into a package. > Now, lets get to the pretentious philosophical discussion: I guess > encapsulation is quite the opposite of, say, dynamic typing, which is > arguably core in Python. They are utterly unrelated. Dynamic typing has nothing to do with whether or not you can encapsulate code into chunks (subroutines, functions, modules, classes...) or whether you have to write one big amorphous unstructured program where every chunk of code can reach inside other chunks of code. Nor does dynamic type have to do with information hiding. You can have a private member of a class regardless of whether that member has a single fixed type enforced at compile-time, or a dynamically typed value enforced at run-time. -- Steven From fp2161 at gmail.com Sat Aug 31 02:07:47 2013 From: fp2161 at gmail.com (Fabrice Pombet) Date: Fri, 30 Aug 2013 23:07:47 -0700 (PDT) Subject: Encapsulation unpythonic? In-Reply-To: <5221567b$0$6599$c3e8da3$5496439d@news.astraweb.com> References: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> <8c7c4854-70e1-46e7-a3ff-a3206c4c5c27@googlegroups.com> <5221567b$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Saturday, August 31, 2013 4:35:39 AM UTC+2, Steven D'Aprano wrote: > On Fri, 30 Aug 2013 10:43:28 -0700, Fabrice Pombet wrote: > > > > > On Saturday, August 17, 2013 2:26:32 PM UTC+2, Fernando Saldanha wrote: > > > > >> 2) If it is in fact true that encapsulation is rarely used, how do I > > >> deal with the fact that other programmers can easily alter the values > > >> of members of my classes? > > >> > > > Fernando, it is widely accepted that Python pays very little attention > > > to encapsulation as a principle set in stone. > > > > Widely accepted by whom? > most people(except you, apparently, but I fear that you do not really accept in general) > > > Python code is *full* of encapsulation. Functions, methods, classes, > > modules, packages, even local variables, are all mechanisms for > > encapsulating code and data. Those who say that Python has little or no > > encapsulation are talking rubbish. > > Chaz's definition of > > > encapsulation is also mine. > > > > Who is Chaz, and what definition does he have? See above me chaz... at gmail.com, the definition of encapsulation from his OST course is fine by y standards, quoting him: "I'm taking the Python Cert series w/ O'Reilly School of Technology, which I recommend if you've got a good handle on OO programming. In any event, according to what I've learned, "encapsulation is the idea that the only way to access or change the data inside an object is by calling its methods. This idea has never really gained much ground in the Python world, and it is normally considered acceptable to both read and set an object's attributes from anywhere in a program." Keep in mind that we are talking about Encapsulation(a general/philosophical principle) as opposed to encapsulating (i.e. setting an argument so that it can only be read/written from within its class/object) this is a key conceptual point. I agree with you that Python allows you to enforce the encapsulation principle within your code, whenever you want it. But not as a principle that you NEED to respect(as in Java for instance). It is, in my opinion, much better this way. > And now you are talking about information hiding and protection, which is > > not the same of encapsulation, no matter what the academics think. > > Sometimes the air gets a bit too thin to breathe way up at the top of > > those ivory towers... > I am no academic, and I think that's right. > > > Encapsulation is about grouping code that needs to be together together. > > In contract, you have programming languages that give you little, or > > nothing, in the way of grouping -- everything is one big chunk of code, > > with GOTO or GOSUB to jump from place to place. > > I think that I prefer chaz' definition (it is, how could I put it... A tad easier to understand) > > Functions and procedures are the first, most simple, form of > > encapsulation. Classes allow you to encapsulate multiple functions > > ("methods") together with the data they need to operate on in one chunk. > > Even in C++ or Java, you can have classes that provide no information > > hiding at all -- just declare everything "public". > > > > On the other hand, non-OOP languages like C can implement information > > hiding. In C, you can hide information from other files by declaring them > > as "static". Variables declared inside a brace-delimited block only exist > > within that block: local variables are hidden. For example: > > > > int foo; > > static int bar; > > > > > > bar is hidden from other files. Likewise, in this function: > > > > > > int func(void) { > > int baz; > > ... > > } > > > > > > baz is local to func, and invisible to any other function. > > > > So you can have information hiding without classes, and classes without > > information hiding. The two concepts are obviously independent, but as > > usual, the academics who are in love with OOP like to pretend that > > anything that is of any interest whatsoever in computing was invented by > > Java and C++. > > > > There are even languages with functions, but no local variables. For > > instance, older versions of Forth let you define functions, what Forth > > calls "words", but all functions operate on the same global stack. > > > > Python has excellent encapsulation: we can combine code that ought to be > > together into a function, related functions into a class, related classes > > into a module, and related modules into a package. > > > > > > > Now, lets get to the pretentious philosophical discussion: I guess > > > encapsulation is quite the opposite of, say, dynamic typing, which is > > > arguably core in Python. > > > > They are utterly unrelated. Dynamic typing has nothing to do with whether > > or not you can encapsulate code into chunks (subroutines, functions, > > modules, classes...) or whether you have to write one big amorphous > > unstructured program where every chunk of code can reach inside other > > chunks of code. Nor does dynamic type have to do with information hiding. > > You can have a private member of a class regardless of whether that > > member has a single fixed type enforced at compile-time, or a dynamically > > typed value enforced at run-time. > > Steven well, look at that: a=(1,2) a=2+3 ->a is an object and I have changed its type and value from outside. As far as I am concerned this is one hell of an encapsulation violation... Could you do this -strictly speaking- in Java or C++? From marco.buttu at gmail.com Sat Aug 31 02:49:45 2013 From: marco.buttu at gmail.com (Marco Buttu) Date: Sat, 31 Aug 2013 08:49:45 +0200 Subject: Encapsulation unpythonic? References: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> <8c7c4854-70e1-46e7-a3ff-a3206c4c5c27@googlegroups.com> <5221567b$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <52219209.5030806@gmail.com> On 08/31/2013 08:07 AM, Fabrice Pombet wrote: > well, look at that: > > a=(1,2) > a=2+3 ->a is an object and I have changed its type and value from outside. No, `a` is not an object, so you did not change the type of any object. `a` is just a name (a label), that initially refers to the tuple (1, 2): >>> a = (1, 2) >>> id(a) 140377464514968 ad after, to another object, of type int: >>> a = 2 + 3 >>> id(a) 8752608 The bytecode: >>> dis.dis('a = (1, 2); a = 2 + 3;') 1 0 LOAD_CONST 4 ((1, 2)) 3 STORE_NAME 0 (a) 6 LOAD_CONST 5 (5) 9 STORE_NAME 0 (a) 12 LOAD_CONST 3 (None) 15 RETURN_VALUE Regards, M. -- Marco Buttu From gherron at digipen.edu Sat Aug 31 03:03:58 2013 From: gherron at digipen.edu (Gary Herron) Date: Sat, 31 Aug 2013 00:03:58 -0700 Subject: Encapsulation unpythonic? In-Reply-To: References: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> <8c7c4854-70e1-46e7-a3ff-a3206c4c5c27@googlegroups.com> <5221567b$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5221955E.7010700@digipen.edu> On 08/30/2013 11:07 PM, Fabrice Pombet wrote: > ... long discussion elided ... > well, look at that: > > a=(1,2) > a=2+3 ->a is an object and I have changed its type and value from outside. As far as I am concerned this is one hell of an encapsulation violation... Could you do this -strictly speaking- in Java or C++? Yes, in fact you can do that in C++ and java: Obj1 a = ...some object...; { // new scope... Obj2 a = ...another object...; } On one line, the name 'a' is bound to one object, and later it is bound to another object. Your Python code is similar, binding the name 'a' to object (1,2) on one line and the object 5 on the next line. Granted, Python seems a little freer because, with it's dynamic typing, one doesn't need to create a new scope to rebind a name, but all languages with variable names allow some control over binding/rebinding names. But this has *nothing* at all to do with objects and encapsulation. Please don't confuse: the binding of names to objects and the existence of objects and their encapsulated behavior They are very different things. -- Dr. Gary Herron Department of Computer Science DigiPen Institute of Technology (425) 895-4418 -------------- next part -------------- An HTML attachment was scrubbed... URL: From fp2161 at gmail.com Sat Aug 31 03:42:55 2013 From: fp2161 at gmail.com (Fabrice Pombet) Date: Sat, 31 Aug 2013 00:42:55 -0700 (PDT) Subject: Encapsulation unpythonic? In-Reply-To: References: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> <8c7c4854-70e1-46e7-a3ff-a3206c4c5c27@googlegroups.com> <5221567b$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <95e00c44-9e65-47d6-823e-6211fa4d597d@googlegroups.com> On Saturday, August 31, 2013 9:03:58 AM UTC+2, Gary Herron wrote: > On 08/30/2013 11:07 PM, Fabrice Pombet > wrote: > > > ... long discussion elided ... > > well, look at that: > > a=(1,2) > a=2+3 ->a is an object and I have changed its type and value from outside. As far as I am concerned this is one hell of an encapsulation violation... Could you do this -strictly speaking- in Java or C++? > > > > > Yes, in fact you can do that in C++ and java: > > > > Obj1 a = ...some object...; > > { // new scope... > > ?? Obj2 a = ...another object...; > > } > > > > On one line, the name 'a' is bound to one object, and later it is > bound to another object.?? Your Python code is similar, binding the > name 'a' to object (1,2) on one line and the object 5 on the next > line.? Granted, Python seems a little freer because, with it's > dynamic typing,? one doesn't need to create a new scope to rebind a > name, but all languages with variable names allow some control over > binding/rebinding names. > > > > But this has *nothing* at all to do with objects and encapsulation. > > > > Please don't confuse: > > the binding of names to objects and > > > > the existence of objects and their encapsulated behavior > > > They are very different things. > > > > -- > Dr. Gary Herron > Department of Computer Science > DigiPen Institute of Technology > (425) 895-4418 That's interesting, can you do this in C++ or java: class X(): From fp2161 at gmail.com Sat Aug 31 04:00:28 2013 From: fp2161 at gmail.com (Fabrice Pombet) Date: Sat, 31 Aug 2013 01:00:28 -0700 (PDT) Subject: Encapsulation unpythonic? In-Reply-To: <95e00c44-9e65-47d6-823e-6211fa4d597d@googlegroups.com> References: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> <8c7c4854-70e1-46e7-a3ff-a3206c4c5c27@googlegroups.com> <5221567b$0$6599$c3e8da3$5496439d@news.astraweb.com> <95e00c44-9e65-47d6-823e-6211fa4d597d@googlegroups.com> Message-ID: On Saturday, August 31, 2013 9:42:55 AM UTC+2, Fabrice Pombet wrote: > On Saturday, August 31, 2013 9:03:58 AM UTC+2, Gary Herron wrote: > > > On 08/30/2013 11:07 PM, Fabrice Pombet > > > wrote: > > > > > > > > > ... long discussion elided ... > > > > > > well, look at that: > > > > > > a=(1,2) > > > a=2+3 ->a is an object and I have changed its type and value from outside. As far as I am concerned this is one hell of an encapsulation violation... Could you do this -strictly speaking- in Java or C++? > > > > > > > > > > > > > > > Yes, in fact you can do that in C++ and java: > > > > > > > > > > > > Obj1 a = ...some object...; > > > > > > { // new scope... > > > > > > ?? Obj2 a = ...another object...; > > > > > > } > > > > > > > > > > > > On one line, the name 'a' is bound to one object, and later it is > > > bound to another object.?? Your Python code is similar, binding the > > > name 'a' to object (1,2) on one line and the object 5 on the next > > > line.? Granted, Python seems a little freer because, with it's > > > dynamic typing,? one doesn't need to create a new scope to rebind a > > > name, but all languages with variable names allow some control over > > > binding/rebinding names. > > > > > > > > > > > > But this has *nothing* at all to do with objects and encapsulation. > > > > > > > > > > > > Please don't confuse: > > > > > > the binding of names to objects and > > > > > > > > > > > > the existence of objects and their encapsulated behavior > > > > > > > > > They are very different things. > > > > > > > > > > > > -- > > > Dr. Gary Herron > > > Department of Computer Science > > > DigiPen Institute of Technology > > > (425) 895-4418 > > > > That's interesting, can you do this in C++ or java: > > > > class X(): def __init__(self, *arg): for x in arg: self.x=x and then: a=X("x","y","z") and then: a.w="w" ? I guess my point was dynamic typing and encapsulation go a little in opposite directions in terms of philosophy, and it is therefore clear that Python privileges "dynamic typing" kind of thinking over encapsulation as a philosophical stance. Am I the only one thinking like this? From steve+comp.lang.python at pearwood.info Sat Aug 31 07:46:52 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 31 Aug 2013 11:46:52 GMT Subject: Encapsulation unpythonic? References: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> <8c7c4854-70e1-46e7-a3ff-a3206c4c5c27@googlegroups.com> <5221567b$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5221d7ab$0$6599$c3e8da3$5496439d@news.astraweb.com> On Fri, 30 Aug 2013 23:07:47 -0700, Fabrice Pombet wrote: > well, look at that: > > a=(1,2) > a=2+3 ->a is an object and I have changed its type and value from > outside. Incorrect. You have not changed the type or value of any object. "a" is not an object, it is a *name*, and while you can change the object bound to the name, the objects remain unchanged. When you do this: x = 23 x = 42 the *object* 23 does not change, only the name binding changes. To do otherwise would cause all sorts of surprises: # THIS DOES NOT HAPPEN IN PYTHON # or any other language, as far as I am aware x = 23 y = x # y now has the value 23 x = 42 # change the value of the object ### NOT SO! ### print y => prints 42 Name binding (assignment) does not change objects. It changes the link between a name and the object, but the object remains untouched (unless it is unbound, and garbage collected). Assignment is not mutation. Assigning to a name does not modify the object that was previously bound. But even if you were right about changing the type and value of objects in place -- Python allows you to mutate lists and dicts in place, and even mutate the type of some objects, although not built-ins -- your understanding is still confused. Re-assignment (re-binding) of local variables is hardly a violation of encapsulation. But if it was, then Java and C++ have no encapsulation either, because you can re-assign to local variables inside a function too. If you want to see a language without encapsulation, you need to look at something like 1970s-style BASIC, a language where all variables are global, where there is no support for grouping related code into separate modules or files, where the closest thing to a function call is GOTO or GOSUB. Of course, languages can have *more* or *less* encapsulation than other languages. C lets you encapsulate related functions into a file, but it has no way of grouping data and functions together except loosely, in a file. C++ adds classes, which has more encapsulation since you can group functions and their data together. > As far as I am concerned this is one hell of an encapsulation > violation... Could you do this -strictly speaking- in Java or C++? Of course you could. All you need is a way to tell the compiler not to type-check the variable "a". Or more practically, some way to declare variable "a" as a reference to an untyped or generic value. C# has the dynamic keyword for this functionality. I don't know about Java or C++, but the JVM is certainly capable of implementing dynamic typing as there are various dynamically-typed languages built on top of the JVM, such as OpenXION and Cobra. -- Steven From ned at nedbatchelder.com Sat Aug 31 08:41:54 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Sat, 31 Aug 2013 08:41:54 -0400 Subject: Encapsulation unpythonic? In-Reply-To: <5221d7ab$0$6599$c3e8da3$5496439d@news.astraweb.com> References: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> <8c7c4854-70e1-46e7-a3ff-a3206c4c5c27@googlegroups.com> <5221567b$0$6599$c3e8da3$5496439d@news.astraweb.com> <5221d7ab$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5221E492.4080605@nedbatchelder.com> On 8/31/13 7:46 AM, Steven D'Aprano wrote: > On Fri, 30 Aug 2013 23:07:47 -0700, Fabrice Pombet wrote: > >> well, look at that: >> >> a=(1,2) >> a=2+3 ->a is an object and I have changed its type and value from >> outside. > Incorrect. You have not changed the type or value of any object. "a" is > not an object, it is a *name*, and while you can change the object bound > to the name, the objects remain unchanged. > > When you do this: > > x = 23 > x = 42 > > the *object* 23 does not change, only the name binding changes. To do > otherwise would cause all sorts of surprises: > > # THIS DOES NOT HAPPEN IN PYTHON > # or any other language, as far as I am aware > x = 23 > y = x # y now has the value 23 > x = 42 # change the value of the object ### NOT SO! ### > print y > => prints 42 > > Name binding (assignment) does not change objects. It changes the link > between a name and the object, but the object remains untouched (unless > it is unbound, and garbage collected). Assignment is not mutation. > Assigning to a name does not modify the object that was previously bound. > I wrote a piece about names and values that might help clarify these points: Facts and Myths about Names and Values in Python: http://nedbatchelder.com/text/names.html --Ned. From fp2161 at gmail.com Sat Aug 31 08:49:07 2013 From: fp2161 at gmail.com (Fabrice Pombet) Date: Sat, 31 Aug 2013 05:49:07 -0700 (PDT) Subject: Encapsulation unpythonic? In-Reply-To: References: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> <8c7c4854-70e1-46e7-a3ff-a3206c4c5c27@googlegroups.com> <5221567b$0$6599$c3e8da3$5496439d@news.astraweb.com> <5221d7ab$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5d651434-5d46-4f51-8951-a71cf0344c76@googlegroups.com> > > http://nedbatchelder.com/text/names.html > > > > --Ned. This is an excellent explanation, thank you. It is mostly of theoretical interest though, and in practice, I still contend that the consequences towards the syntax are (or seem, if you prefer) analogous to those of the lack of encapsulation. From fp2161 at gmail.com Sat Aug 31 08:57:47 2013 From: fp2161 at gmail.com (Fabrice Pombet) Date: Sat, 31 Aug 2013 05:57:47 -0700 (PDT) Subject: Encapsulation unpythonic? In-Reply-To: <5221d7ab$0$6599$c3e8da3$5496439d@news.astraweb.com> References: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> <8c7c4854-70e1-46e7-a3ff-a3206c4c5c27@googlegroups.com> <5221567b$0$6599$c3e8da3$5496439d@news.astraweb.com> <5221d7ab$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Saturday, August 31, 2013 1:46:52 PM UTC+2, Steven D'Aprano wrote: > On Fri, 30 Aug 2013 23:07:47 -0700, Fabrice Pombet wrote: > > > > > well, look at that: > > > > > > a=(1,2) > > > a=2+3 ->a is an object and I have changed its type and value from > > > outside. > > > > Incorrect. You have not changed the type or value of any object. "a" is > > not an object, it is a *name*, and while you can change the object bound > > to the name, the objects remain unchanged. > > > > When you do this: > > > > x = 23 > > x = 42 > > > > the *object* 23 does not change, only the name binding changes. To do > > otherwise would cause all sorts of surprises: > > > > # THIS DOES NOT HAPPEN IN PYTHON > > # or any other language, as far as I am aware > > x = 23 > > y = x # y now has the value 23 > > x = 42 # change the value of the object ### NOT SO! ### > > print y > > => prints 42 > > > > Name binding (assignment) does not change objects. It changes the link > > between a name and the object, but the object remains untouched (unless > > it is unbound, and garbage collected). Assignment is not mutation. > > Assigning to a name does not modify the object that was previously bound. > > > > > > But even if you were right about changing the type and value of objects > > in place -- Python allows you to mutate lists and dicts in place, and > > even mutate the type of some objects, although not built-ins -- your > > understanding is still confused. Re-assignment (re-binding) of local > > variables is hardly a violation of encapsulation. But if it was, then > > Java and C++ have no encapsulation either, because you can re-assign to > > local variables inside a function too. > > > > If you want to see a language without encapsulation, you need to look at > > something like 1970s-style BASIC, a language where all variables are > > global, where there is no support for grouping related code into separate > > modules or files, where the closest thing to a function call is GOTO or > > GOSUB. > > > > Of course, languages can have *more* or *less* encapsulation than other > > languages. C lets you encapsulate related functions into a file, but it > > has no way of grouping data and functions together except loosely, in a > > file. C++ adds classes, which has more encapsulation since you can group > > functions and their data together. > > > > > > > > > As far as I am concerned this is one hell of an encapsulation > > > violation... Could you do this -strictly speaking- in Java or C++? > > > > Of course you could. All you need is a way to tell the compiler not to > > type-check the variable "a". Or more practically, some way to declare > > variable "a" as a reference to an untyped or generic value. C# has the > > dynamic keyword for this functionality. I don't know about Java or C++, > > but the JVM is certainly capable of implementing dynamic typing as there > > are various dynamically-typed languages built on top of the JVM, such as > > OpenXION and Cobra. > -- > > Steven Steve, I think that your definition of encapsulation is too wide to give a reasonable answer to the question at hand. If I understand you correctly, you are taking encapsulation as a language characteristic, rather than a principle. Plus, you seem to forget that encapsulation is an OOP principle, and, forgive me if I am wrong, does not apply normally to functions or languages like C. Please read Steve Holden's (in chaz') definition, and tell us whether you think that Python enforces strongly this principle, I think that would be a good basis for an agreement. My answer is no, it doesn't, but it allows you to abide by it if you want to. Unlike Java or C++ who would tend to do exactly the contrary (enforces it strictly, and (possibly?) allow you to discard it at times with a few jiffies (or not? I don't know)) From timothy.c.delaney at gmail.com Sat Aug 31 13:47:22 2013 From: timothy.c.delaney at gmail.com (Tim Delaney) Date: Sun, 1 Sep 2013 03:47:22 +1000 Subject: Encapsulation unpythonic? In-Reply-To: References: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> <8c7c4854-70e1-46e7-a3ff-a3206c4c5c27@googlegroups.com> <5221567b$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 1 September 2013 03:31, Dennis Lee Bieber wrote: > On Fri, 30 Aug 2013 23:07:47 -0700 (PDT), Fabrice Pombet > > declaimed the following: > > >well, look at that: > > > >a=(1,2) > >a=2+3 ->a is an object and I have changed its type and value from > outside. As far as I am concerned this is one hell of an encapsulation > violation... Could you do this -strictly speaking- in Java or C++? > > There is where your major misunderstanding is... > > "a" is a NAME attached (bound) to an object. In the first statement, the > object is the tuple (1,2). That object was not changed when you execute the > second statement -- which is taking two integer objects and creating a new > integer object having a value of '5', and then attaches the NAME "a" to the > new object. If no other names are bound to the (1,2) object, it will be > garbage collected. > I'll try another way to explain it, using Java terminology(since Fabrice appears to be familiar with Java). Object a = Arrays.asList(1, 2); // a is a reference to the List returned by Arrays.asList a = Integer.valueOf(2 + 3); // a is now a reference to the Integer returned by Integer.valueOf You have not changed the type of 'a' in any way - you have simply changed what the name 'a' refers to. This is functionally identical to your Python code above,except that in Python you do not have to downcast the Object reference 'a' or use reflection to call methods on it or access it's members (think of it as Python does reflection automatically for you). Tim Delaney -------------- next part -------------- An HTML attachment was scrubbed... URL: From fsaldan1 at gmail.com Sat Aug 17 08:34:54 2013 From: fsaldan1 at gmail.com (Fernando Saldanha) Date: Sat, 17 Aug 2013 05:34:54 -0700 (PDT) Subject: Check for the type of arguments Message-ID: <185a0a88-9515-43e6-ae65-73d86b0299e7@googlegroups.com> I am new to Python, with some experience in Java, C++ and R. Writing in other languages I usually check the type and values of function arguments. In the Python code examples I have seen this is rarely done. Questions: 1) Is this because it would be "unpythonic" or just because the examples are not really production code? 2) If I still want to check the type of my arguments, do I a) use type() or is instance() to check for type? b) use assert (I guess not), raise a ValueError, or sys.exit()? (I noticed that raising a ValueError does not stop execution when I am running the Interactive Interpreter under PTVS, which I find inconvenient, but it does stop execution when running the code non-interactively.) Thanks. FS From skip at pobox.com Sat Aug 17 08:55:58 2013 From: skip at pobox.com (Skip Montanaro) Date: Sat, 17 Aug 2013 07:55:58 -0500 Subject: Check for the type of arguments In-Reply-To: <185a0a88-9515-43e6-ae65-73d86b0299e7@googlegroups.com> References: <185a0a88-9515-43e6-ae65-73d86b0299e7@googlegroups.com> Message-ID: Read up on "duck typing." Skip From rosuav at gmail.com Sat Aug 17 09:31:05 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 17 Aug 2013 14:31:05 +0100 Subject: Check for the type of arguments In-Reply-To: <185a0a88-9515-43e6-ae65-73d86b0299e7@googlegroups.com> References: <185a0a88-9515-43e6-ae65-73d86b0299e7@googlegroups.com> Message-ID: On Sat, Aug 17, 2013 at 1:34 PM, Fernando Saldanha wrote: > Writing in other languages I usually check the type and values of function arguments. In the Python code examples I have seen this is rarely done. > > Questions: > > 1) Is this because it would be "unpythonic" or just because the examples are not really production code? It's unpythonic. The normal thing to do is simply use the object you're given, and if something goes wrong, the exception will have a full traceback. And if you don't notice that it isn't what you expect, well, it probably didn't matter! ChrisA From joshua at landau.ws Sat Aug 17 10:38:17 2013 From: joshua at landau.ws (Joshua Landau) Date: Sat, 17 Aug 2013 15:38:17 +0100 Subject: Check for the type of arguments In-Reply-To: <185a0a88-9515-43e6-ae65-73d86b0299e7@googlegroups.com> References: <185a0a88-9515-43e6-ae65-73d86b0299e7@googlegroups.com> Message-ID: On 17 August 2013 13:34, Fernando Saldanha wrote: > I am new to Python, with some experience in Java, C++ and R. > > Writing in other languages I usually check the type and values of function arguments. In the Python code examples I have seen this is rarely done. > > Questions: > > 1) Is this because it would be "unpythonic" or just because the examples are not really production code? Unpythonic. Python duck-types so we tend to take things as long as the seem like they work. This is really helpful if you want to provide a custom type or data object to a function (or anything really). If it looks like a duck and quacks like a duck... you can probably make a duck sandwich. > 2) If I still want to check the type of my arguments, do I > > a) use type() or is instance() to check for type? You'd want to travel down this stack, choosing the first reasonable one: ? Don't check at all ? Check that it can do what you need it to do, such as by calling "iter(input)" to check that it's iterable. ? Check using an ABC (http://docs.python.org/3/library/abc.html) with isinstance ? Check that it has the methods you need using hasattr ? Check using "isinstance(...)" against a type ? Check using "type(...) is" The choices higher up are better than the choices lower down. > b) use assert (I guess not), "assert" is for things that *can't* be wrong (yet still sometimes are). Don't normally assert user input, I'd say. >raise a ValueError, Sounds right. >or sys.exit()? No. You should never be throwing a SystemExit except at top-level. > (I noticed that raising a ValueError does not stop execution when I am running the Interactive Interpreter under PTVS, which I find inconvenient, but it does stop execution when running the code non-interactively.) From steve+comp.lang.python at pearwood.info Sat Aug 17 11:46:35 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 Aug 2013 15:46:35 GMT Subject: Check for the type of arguments References: <185a0a88-9515-43e6-ae65-73d86b0299e7@googlegroups.com> Message-ID: <520f9ada$0$30000$c3e8da3$5496439d@news.astraweb.com> On Sat, 17 Aug 2013 05:34:54 -0700, Fernando Saldanha wrote: > I am new to Python, with some experience in Java, C++ and R. > > Writing in other languages I usually check the type and values of > function arguments. Surely not in Java and C++. Being static-typed languages, the compiler checks them for you. > In the Python code examples I have seen this is rarely done. Correct. Rarely, but not never. > Questions: > > 1) Is this because it would be "unpythonic" or just because the examples > are not really production code? Both. Production code does tend to have a little more type-checking than basic examples or throw-away code, but not excessively so. But even so, Python has a philosophy of "duck-typing". In other words, if an object quacks like a duck, and swims like a duck, then it's close enough to a duck. The result is that Python code is nearly always naturally polymorphic with no extra effort, unless you deliberately break duck-typing by performing unnecessary type-checks. For example, you might have a function that expects a list, and then iterates over the list: # Obviously this is a toy example. def example(L): for item in L: if item == 1: print("found one") You might be tempted to insist on a list, and put in type-checking, but that goes against Python's philosophy. Your code above works perfectly with list subclasses, and objects which delegate to lists, and tuples, and arrays, and sets, and iterators, and even dicts. Why insist on an actual duck when all you really need is a bird that swims? Any iterable object will work, not just lists. And if the caller passes a non-iterable object? You get a nice, clean exception which can optionally be caught, not a segfault, kernel panic, or BSOD. Really, the only difference is you get a runtime exception instead of a compile-time error. This is more flexible, and tends to lead to more rapid development, but with a higher reliance on test-driven development, and less opportunity for code correctness proofs. So you win a bit, and lose a bit, but in my opinion you win more than you lose. A related concept in Python is that we prefer to "ask for forgiveness rather than permission". Since all error checking happens at runtime, it often doesn't matter exactly when the error occurs. So if you need to sort a list: def example(L): L.sort() ... rather than checking up-front that L has a sort method, or that L is an actual list, just try to sort it. If L is a tuple, say, you will get a runtime exception. If you can recover from the exception, do so: def example(L): try: L.sort() except AttributeError: # no sort method L = list(L) L.sort() ... but usually you will just let the exception propagate up to the caller, who can either catch it, or treat it as a fatal error. > 2) If I still want to check the type of my arguments, do I > > a) use type() or is instance() to check for type? Depends what you want to do, but 99.99% of the time you will use isinstance. "type(obj) is list" checks that obj is an actual list. Subclasses of list will be rejected. This is nearly always NOT what you want. "isinstance(obj, list)" is better, since it accepts list subclasses. But still, very restrictive -- what's wrong with other sequences? import collections isinstance(obj, collections.Sequence) is better still, since it accepts anything that is sequence-like, but it will still reject objects which would have worked perfectly well, such as iterators. > b) use assert (I guess not), raise a ValueError, or sys.exit()? None of the above. Do not use assert for testing arguments. Two reasons: 1) Assertions are *optional*. If you run Python with the -O (optimize) flag, asserts are ignored. 2) It's the wrong error. (Same with ValueError, or UnicodeDecodeError, or ZeroDivisionError, etc.) Assertions are for checking internal code logic. "The caller passed an int when I need a list" is not a failure of your internal code logic, it is the wrong type of argument. So the exception you raise should be TypeError. When in doubt, see what Python built-ins do: py> sorted(24) # expect a list, or other sequence Traceback (most recent call last): File "", line 1, in TypeError: 'int' object is not iterable TypeError. Not AssertionError. And certainly not sys.exit(). That's both annoying and unnecessary: - It's annoying when trying to debug code. Instead of getting a nice traceback, which shows you exactly what went wrong, the program just exits. Often coders give pathetically feeble error messages like: "An error occurred" but even if the error message is useful, you still throw away all the other information in the traceback. Consequently, there's no hint as to *where* the error was, only *what* it was. - And it's unnecessary. If your aim is to exit the program, *any* uncaught exception will exit the program. > (I noticed that raising a ValueError does not stop execution when I am > running the Interactive Interpreter under PTVS, which I find > inconvenient, but it does stop execution when running the code > non-interactively.) What's PTVS? Of course exceptions stop execution of the current code. What they don't do is kill the interactive interpreter. That would be foolish. The whole point of the interactive interpreter is that it is supposed to be interactive and keep going even if you make a mistake. You're not supposed to use it for running live production code. -- Steven From fsaldan1 at gmail.com Sat Aug 17 12:00:14 2013 From: fsaldan1 at gmail.com (Fernando Saldanha) Date: Sat, 17 Aug 2013 09:00:14 -0700 (PDT) Subject: Check for the type of arguments In-Reply-To: <185a0a88-9515-43e6-ae65-73d86b0299e7@googlegroups.com> References: <185a0a88-9515-43e6-ae65-73d86b0299e7@googlegroups.com> Message-ID: <3e81d904-0883-4ba5-b427-e055c558382d@googlegroups.com> Thanks for the insightful answers. PTVS is Python Tools for Visual Studio. From tmellman at googlemail.com Sat Aug 17 10:25:43 2013 From: tmellman at googlemail.com (tmellman at googlemail.com) Date: Sat, 17 Aug 2013 07:25:43 -0700 (PDT) Subject: Importing variables non-deterministic? Message-ID: I have this file that has this: from struct_global import y from struct_global import x and this usage (a few lines later in a class method definition in the same file): if y == x: If I stop there in pdb, I can observe this: (Pdb) p x 66 (Pdb) p y -1 (Pdb) from struct_global import y (Pdb) p y 62 How did my first import get hidden? Is there any way to see where a variable resolves to? From steve+comp.lang.python at pearwood.info Sat Aug 17 11:01:41 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 Aug 2013 15:01:41 GMT Subject: Importing variables non-deterministic? References: Message-ID: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> On Sat, 17 Aug 2013 07:25:43 -0700, tmellman wrote: > I have this file that has this: > > from struct_global import y > from struct_global import x The import of x appears to be irrelevant, so I'll ignore it. Here you import y from the struct_global module. This creates a new name y in the current module. y is bound to the same object that struct_global.y happens to be at the moment of the import, but y is *not* a reference to the *name* struct_global.y. This means that the two names, "y in current module" and "y in struct_global module" point to the same object, but they aren't two aliases for the same variable. They are different variables that just happen to share the same value (an object). If the object mutates (which ints cannot do), then both "y"s will see the change (naturally, since both refer to the same object). But if either name is rebound to a different object, the other name is unaffected. We can see the same behaviour locally, without an import, by doing this: py> x = [1] py> y = x # y is bound to the same object as x py> print(y) [1] py> x.append(2) # mutate x in place py> print(y) [1, 2] py> x = [1, 2, 3] # re-bind x to a new object py> print(y) # y still bound to the first object [1, 2] "from struct_globals import y" is a binding operation, no different from "y = something". > and this usage (a few lines later in a class method definition in the > same file): > > if y == x: > > If I stop there in pdb, I can observe this: > > (Pdb) p x > 66 > (Pdb) p y > -1 > (Pdb) from struct_global import y > (Pdb) p y > 62 And here you re-import the name "y" from struct_global. That rebinds the current module's "y" with whatever value struct_global.y has *now*, rather than a second (or a minute, or an hour) earlier when the first import took place. Obviously at some point between the first import and the second import, struct_global.y must have been reassigned from -1 to 62. This goes to show why global variables are considered harmful, and why clean, modern program design tries to reduce the use of them as much as possible. Global variables are too easily modified by, well, *anything*. The sort of behaviour you are seeing is sometimes called "action at a distance" -- something, anything, anywhere in your program, possibly buried deep, deep down inside some function you might never suspect, is changing the global variable. > How did my first import get hidden? You reassigned to it. "from struct_global import y" is practically syntactic sugar for this: import struct_global y = struct_global.y > Is there any way to see where a > variable resolves to? You just did. You inspected the variable "y", and you saw that it is assigned the value 62. -- Steven From antoon.pardon at rece.vub.ac.be Mon Aug 19 03:14:05 2013 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Mon, 19 Aug 2013 09:14:05 +0200 Subject: Importing variables non-deterministic? In-Reply-To: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5211C5BD.5040209@rece.vub.ac.be> Op 17-08-13 17:01, Steven D'Aprano schreef: > > And here you re-import the name "y" from struct_global. That rebinds the > current module's "y" with whatever value struct_global.y has *now*, > rather than a second (or a minute, or an hour) earlier when the first > import took place. Obviously at some point between the first import and > the second import, struct_global.y must have been reassigned from -1 to > 62. > > This goes to show why global variables are considered harmful, and why > clean, modern program design tries to reduce the use of them as much as > possible. Global variables are too easily modified by, well, *anything*. > The sort of behaviour you are seeing is sometimes called "action at a > distance" -- something, anything, anywhere in your program, possibly > buried deep, deep down inside some function you might never suspect, is > changing the global variable. I think you are overstating your case. Classes and functions are variables too and in general nobody seems to have a problem with them being global. -- Antoon Pardon From davea at davea.name Mon Aug 19 03:45:00 2013 From: davea at davea.name (Dave Angel) Date: Mon, 19 Aug 2013 07:45:00 +0000 (UTC) Subject: Importing variables non-deterministic? References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211C5BD.5040209@rece.vub.ac.be> Message-ID: Antoon Pardon wrote: > Op 17-08-13 17:01, Steven D'Aprano schreef: >> >> And here you re-import the name "y" from struct_global. That rebinds the >> current module's "y" with whatever value struct_global.y has *now*, >> rather than a second (or a minute, or an hour) earlier when the first >> import took place. Obviously at some point between the first import and >> the second import, struct_global.y must have been reassigned from -1 to >> 62. >> >> This goes to show why global variables are considered harmful, and why >> clean, modern program design tries to reduce the use of them as much as >> possible. Global variables are too easily modified by, well, *anything*. >> The sort of behaviour you are seeing is sometimes called "action at a >> distance" -- something, anything, anywhere in your program, possibly >> buried deep, deep down inside some function you might never suspect, is >> changing the global variable. > > I think you are overstating your case. Classes and functions are > variables too and in general nobody seems to have a problem with them > being global. > It's global *variables* that are to be avoided. constants like clsases and functions are fine. On the other hand, class attributes can be variable, and thus are to be avoided when reasonable. There *are* places where global variables make sense, such as for caching, or counting. But those are typically hidden, so they are global in lifetime, but not in scope. -- DaveA From antoon.pardon at rece.vub.ac.be Mon Aug 19 04:16:36 2013 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Mon, 19 Aug 2013 10:16:36 +0200 Subject: Importing variables non-deterministic? In-Reply-To: References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211C5BD.5040209@rece.vub.ac.be> Message-ID: <5211D464.5090303@rece.vub.ac.be> Op 19-08-13 09:45, Dave Angel schreef: > Antoon Pardon wrote: > >> Op 17-08-13 17:01, Steven D'Aprano schreef: >>> >>> And here you re-import the name "y" from struct_global. That rebinds the >>> current module's "y" with whatever value struct_global.y has *now*, >>> rather than a second (or a minute, or an hour) earlier when the first >>> import took place. Obviously at some point between the first import and >>> the second import, struct_global.y must have been reassigned from -1 to >>> 62. >>> >>> This goes to show why global variables are considered harmful, and why >>> clean, modern program design tries to reduce the use of them as much as >>> possible. Global variables are too easily modified by, well, *anything*. >>> The sort of behaviour you are seeing is sometimes called "action at a >>> distance" -- something, anything, anywhere in your program, possibly >>> buried deep, deep down inside some function you might never suspect, is >>> changing the global variable. >> >> I think you are overstating your case. Classes and functions are >> variables too and in general nobody seems to have a problem with them >> being global. >> > > It's global *variables* that are to be avoided. constants like clsases > and functions are fine. On the other hand, class attributes can be > variable, and thus are to be avoided when reasonable. Python has no constants. Classes and functions can be changed just like any other variable. I agree that classes and function are generally meant to be constant, but often enought so are global int variables. And some of those that do change, only do so in the initialisation phase and should be considered constant for the rest of the program. My point was that Steven has no way of knowing what exactly is going on here and so shouldn't be making such a sweeping statement. -- Antoon Pardon From rosuav at gmail.com Mon Aug 19 04:32:53 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 19 Aug 2013 09:32:53 +0100 Subject: Importing variables non-deterministic? In-Reply-To: <5211D464.5090303@rece.vub.ac.be> References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211C5BD.5040209@rece.vub.ac.be> <5211D464.5090303@rece.vub.ac.be> Message-ID: On Mon, Aug 19, 2013 at 9:16 AM, Antoon Pardon wrote: > Op 19-08-13 09:45, Dave Angel schreef: >> Antoon Pardon wrote: >>> I think you are overstating your case. Classes and functions are >>> variables too and in general nobody seems to have a problem with them >>> being global. >>> >> >> It's global *variables* that are to be avoided. constants like clsases >> and functions are fine. On the other hand, class attributes can be >> variable, and thus are to be avoided when reasonable. > > Python has no constants. Classes and functions can be changed just like > any other variable. I agree that classes and function are generally > meant to be constant, but often enought so are global int variables. # telnet.py IAC = 0xFF GA = 0xF9 WILL = 0xFB WONT = 0xFC DO = 0xFD DONT = 0xFE # connection.py from telnet import IAC,DO,DONT To be sure, Python won't stop me from changing the value of DONT. But it's a constant, and its value is defined elsewhere (RFC 854). (In this instance, an enum would probably be the better option; but this is an example of a more general case.) In connection.py, I don't care that I've taken a "copy" of the integer 0xFE. It's never going to change; it is a constant in the best tradition of "named number". I could hard-code 0xFE everywhere and the code would work *just fine*, but it'd be less readable, so I don't. Python does have constants. It just doesn't have interpreter support for them. Same as private members, in fact. ChrisA From antoon.pardon at rece.vub.ac.be Mon Aug 19 05:03:40 2013 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Mon, 19 Aug 2013 11:03:40 +0200 Subject: Importing variables non-deterministic? In-Reply-To: References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211C5BD.5040209@rece.vub.ac.be> <5211D464.5090303@rece.vub.ac.be> Message-ID: <5211DF6C.6030603@rece.vub.ac.be> Op 19-08-13 10:32, Chris Angelico schreef: > On Mon, Aug 19, 2013 at 9:16 AM, Antoon Pardon > wrote: >> Op 19-08-13 09:45, Dave Angel schreef: >>> Antoon Pardon wrote: >>>> I think you are overstating your case. Classes and functions are >>>> variables too and in general nobody seems to have a problem with them >>>> being global. >>>> >>> >>> It's global *variables* that are to be avoided. constants like clsases >>> and functions are fine. On the other hand, class attributes can be >>> variable, and thus are to be avoided when reasonable. >> >> Python has no constants. Classes and functions can be changed just like >> any other variable. I agree that classes and function are generally >> meant to be constant, but often enought so are global int variables. > > # telnet.py > IAC = 0xFF > GA = 0xF9 > WILL = 0xFB > WONT = 0xFC > DO = 0xFD > DONT = 0xFE > > > # connection.py > from telnet import IAC,DO,DONT > > > To be sure, Python won't stop me from changing the value of DONT. But > it's a constant, and its value is defined elsewhere (RFC 854). (In > this instance, an enum would probably be the better option; but this > is an example of a more general case.) This is irrelevant. That some context defines a constant, and that you can use a variable with the same name as a constant in python, doesn't contradict the statement that python (as a language) doesn't has constants. There is nothing in the language that would prevent buggy code from changing any of those variables. So from a python point of views these are just global variables. Just as the struct_global.y was in the original contribution. -- Antoon Pardon From rosuav at gmail.com Mon Aug 19 05:18:47 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 19 Aug 2013 10:18:47 +0100 Subject: Importing variables non-deterministic? In-Reply-To: <5211DF6C.6030603@rece.vub.ac.be> References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211C5BD.5040209@rece.vub.ac.be> <5211D464.5090303@rece.vub.ac.be> <5211DF6C.6030603@rece.vub.ac.be> Message-ID: On Mon, Aug 19, 2013 at 10:03 AM, Antoon Pardon wrote: > This is irrelevant. That some context defines a constant, and that you > can use a variable with the same name as a constant in python, doesn't > contradict the statement that python (as a language) doesn't has > constants. There is nothing in the language that would prevent buggy > code from changing any of those variables. So from a python point of > views these are just global variables. Just as the struct_global.y was > in the original contribution. And there's nothing preventing a program from using ctypes to overwrite an object's refcount, thus causing a segfault. So? The issue was regarding imports, and it's perfectly safe to import a constant, even if the interpreter doesn't protect you from then being a total idiot and changing it. ChrisA From __peter__ at web.de Mon Aug 19 05:49:48 2013 From: __peter__ at web.de (Peter Otten) Date: Mon, 19 Aug 2013 11:49:48 +0200 Subject: Importing variables non-deterministic? References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211C5BD.5040209@rece.vub.ac.be> <5211D464.5090303@rece.vub.ac.be> <5211DF6C.6030603@rece.vub.ac.be> Message-ID: Chris Angelico wrote: > On Mon, Aug 19, 2013 at 10:03 AM, Antoon Pardon > wrote: >> This is irrelevant. That some context defines a constant, and that you >> can use a variable with the same name as a constant in python, doesn't >> contradict the statement that python (as a language) doesn't has >> constants. There is nothing in the language that would prevent buggy >> code from changing any of those variables. So from a python point of >> views these are just global variables. Just as the struct_global.y was >> in the original contribution. > > And there's nothing preventing a program from using ctypes to > overwrite an object's refcount, thus causing a segfault. So? The issue > was regarding imports, and it's perfectly safe to import a constant, > even if the interpreter doesn't protect you from then being a total > idiot and changing it. Come on, breaking a "gentlemen's agreement" by rebinding a name and using ctypes to manipulate the internal state of the cpython imlementation are very different things. If that proves anything I'll best you with a Python script that controls a robot which in turn destroys the computer running the script with a hammer ;) If Antoon's point is that global "constants" in Python are only constants because the programmer thinks of them that way I'd say that's an important addition to put Steve's "global variables are considered harmful" into perspective. From antoon.pardon at rece.vub.ac.be Mon Aug 19 07:54:43 2013 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Mon, 19 Aug 2013 13:54:43 +0200 Subject: Importing variables non-deterministic? In-Reply-To: References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211C5BD.5040209@rece.vub.ac.be> <5211D464.5090303@rece.vub.ac.be> <5211DF6C.6030603@rece.vub.ac.be> Message-ID: <52120783.8040008@rece.vub.ac.be> Op 19-08-13 11:18, Chris Angelico schreef: > On Mon, Aug 19, 2013 at 10:03 AM, Antoon Pardon > wrote: >> This is irrelevant. That some context defines a constant, and that you >> can use a variable with the same name as a constant in python, doesn't >> contradict the statement that python (as a language) doesn't has >> constants. There is nothing in the language that would prevent buggy >> code from changing any of those variables. So from a python point of >> views these are just global variables. Just as the struct_global.y was >> in the original contribution. > > And there's nothing preventing a program from using ctypes to > overwrite an object's refcount, thus causing a segfault. So? Yes so? What is your point? Since Cpython caches small integers there is probably nothing preventing a program from using ctypes to make the number three behave as the number five. That doesn't mean that the language define three and five to be equal. A language having constants, does mean that the language can prevent a name from being reassigned. > The issue > was regarding imports, and it's perfectly safe to import a constant, > even if the interpreter doesn't protect you from then being a total > idiot and changing it. Python doesn't have constants, so you statement about importing a constant doesn't make sense. The point is that python doesn't provide the mechanism for protecting names against reassignments. So you don't know whether the variable you think of as a constant is so in reality. And this from a pure language definition point of view. That you can use tools that make the interpreter no longer behave as the language should, doesn't negate that. -- Antoon Pardon. From davea at davea.name Mon Aug 19 08:33:32 2013 From: davea at davea.name (Dave Angel) Date: Mon, 19 Aug 2013 12:33:32 +0000 (UTC) Subject: Importing variables non-deterministic? References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211C5BD.5040209@rece.vub.ac.be> <5211D464.5090303@rece.vub.ac.be> <5211DF6C.6030603@rece.vub.ac.be> <52120783.8040008@rece.vub.ac.be> Message-ID: Antoon Pardon wrote: > Op 19-08-13 11:18, Chris Angelico schreef: >> The issue >> was regarding imports, and it's perfectly safe to import a constant, >> even if the interpreter doesn't protect you from then being a total >> idiot and changing it. > > Python doesn't have constants, so you statement about importing a > constant doesn't make sense. The point is that python doesn't provide > the mechanism for protecting names against reassignments. So you > don't know whether the variable you think of as a constant is so > in reality. And this from a pure language definition point of view. > That you can use tools that make the interpreter no longer behave > as the language should, doesn't negate that. > Who cares what the language "protects?" I don't know any language whose protections can't be at least partially bypassed by clever foot-shooters. In any case, we all know that Python doesn't protect constants, so we're free to use the word in a friendlier way. A Python constant is what I use as a constant. I follow Pep-8 and make it all caps. So in any library I write struct_global.y would be a bug or a design flaw. (And with a single character name like that, it wouldn't be global in any case. Single character names are reserved for play code and for short loops) And if I subsequently change it in my calling code, it isn't a constant any more. If I rebind the name, it's not even the same variable any more. That's a bug, not an exception to the rule "don't use global variables." I also accept as a constant those values which are initialized sufficiently early in the code that most places will only ever see the final value. Those may be global without worry. -- DaveA From antoon.pardon at rece.vub.ac.be Mon Aug 19 10:55:46 2013 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Mon, 19 Aug 2013 16:55:46 +0200 Subject: Importing variables non-deterministic? In-Reply-To: References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211C5BD.5040209@rece.vub.ac.be> <5211D464.5090303@rece.vub.ac.be> <5211DF6C.6030603@rece.vub.ac.be> <52120783.8040008@rece.vub.ac.be> Message-ID: <521231F2.6070407@rece.vub.ac.be> Op 19-08-13 14:33, Dave Angel schreef: > Antoon Pardon wrote: > >> Op 19-08-13 11:18, Chris Angelico schreef: > >>> The issue >>> was regarding imports, and it's perfectly safe to import a constant, >>> even if the interpreter doesn't protect you from then being a total >>> idiot and changing it. >> >> Python doesn't have constants, so you statement about importing a >> constant doesn't make sense. The point is that python doesn't provide >> the mechanism for protecting names against reassignments. So you >> don't know whether the variable you think of as a constant is so >> in reality. And this from a pure language definition point of view. >> That you can use tools that make the interpreter no longer behave >> as the language should, doesn't negate that. >> > > Who cares what the language "protects?" I don't know any language > whose protections can't be at least partially bypassed by clever > foot-shooters. So? A number of language designers seem to think it is still worth while. And even as it may be possible to circumvent it, when we are talking about whether a language has constants or not, we are talking about this language feature. Whether you think this feature is worth having or not, doesn't change that this is what is meant when people talk about a language having constants or not. > In any case, we all know that Python doesn't protect > constants, so we're free to use the word in a friendlier way. > > A Python constant is what I use as a constant. No it is not. It can be a project constant, but it is not a python constant. As it turns out, python does have some constants if the version is high enough, being True, False and None. So for those values the python designers thought it valuable enough to protect them against reassignment. Pity enough they chose a way that didn't allow programmers to protect names they thought important enough to do so too. > I follow Pep-8 and make > it all caps. So in any library I write struct_global.y would be a bug > or a design flaw. No it is not. People are not obligated to follow pep-8 in their projects and not following pep-8 doesn't imply a design flaw. > (And with a single character name like that, it > wouldn't be global in any case. Single character names are reserved > for play code and for short loops) So? What if it is play code. You still don't know whether this person meant this variable in his play code to be used as a constant or not. -- Antoon Pardon From rosuav at gmail.com Mon Aug 19 11:04:08 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 19 Aug 2013 16:04:08 +0100 Subject: Importing variables non-deterministic? In-Reply-To: <521231F2.6070407@rece.vub.ac.be> References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211C5BD.5040209@rece.vub.ac.be> <5211D464.5090303@rece.vub.ac.be> <5211DF6C.6030603@rece.vub.ac.be> <52120783.8040008@rece.vub.ac.be> <521231F2.6070407@rece.vub.ac.be> Message-ID: On Mon, Aug 19, 2013 at 3:55 PM, Antoon Pardon wrote: > Pity enough they chose a > way that didn't allow programmers to protect names they thought > important enough to do so too. As of Python 3, we can redefine something that used to be a keyword, which is even stronger than a constant. def print(*args,print=print,**kw): print("##",*args,**kw) What easier way to tag all your print calls? And this is why it helps to have nothing technically constant. You should be able to do the same with anything, without having to go and edit someone else's code to remove the 'const' keyword. ChrisA From antoon.pardon at rece.vub.ac.be Mon Aug 19 13:25:45 2013 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Mon, 19 Aug 2013 19:25:45 +0200 Subject: Importing variables non-deterministic? In-Reply-To: References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211C5BD.5040209@rece.vub.ac.be> <5211D464.5090303@rece.vub.ac.be> <5211DF6C.6030603@rece.vub.ac.be> <52120783.8040008@rece.vub.ac.be> <521231F2.6070407@rece.vub.ac.be> Message-ID: <52125519.2080301@rece.vub.ac.be> Op 19-08-13 17:04, Chris Angelico schreef: > On Mon, Aug 19, 2013 at 3:55 PM, Antoon Pardon > wrote: >> Pity enough they chose a >> way that didn't allow programmers to protect names they thought >> important enough to do so too. > > As of Python 3, we can redefine something that used to be a keyword, > which is even stronger than a constant. > > def print(*args,print=print,**kw): > print("##",*args,**kw) > > What easier way to tag all your print calls? And this is why it helps > to have nothing technically constant. You should be able to do the > same with anything, without having to go and edit someone else's code > to remove the 'const' keyword. But what you are showing here is shadowing, not redefintion and you can do that even if the shadowed name would be a constant. -- Antoon Pardon From steve+comp.lang.python at pearwood.info Mon Aug 19 13:05:53 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 19 Aug 2013 17:05:53 GMT Subject: Importing variables non-deterministic? References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211C5BD.5040209@rece.vub.ac.be> <5211D464.5090303@rece.vub.ac.be> Message-ID: <52125071$0$29986$c3e8da3$5496439d@news.astraweb.com> On Mon, 19 Aug 2013 09:32:53 +0100, Chris Angelico wrote: > Python does have constants. It just doesn't have interpreter support for > them. Same as private members, in fact. "Constant by convention". I wish Python had stronger support for enforcing constantness, to whit, some way to say "you can't rebind or delete this name once it is bound". You can do it with attributes, by use of property, or in C extensions, but you cannot do it with top-level name bindings. It makes me terribly sad that you can do this: import math math.pi = 3.0 although I can't decide whether I am less sad or more sad to see that the behaviour of math.sin and friends doesn't depend on math.pi. Now, of course, and I don't expect any such "constants" to be proof against a dedicated attacker. One can work around read-only properties, and I expect that any future "no-rebind" constants will also be capable of being worked around. This is Python, not Haskell or Ada. But, naming convention or no naming convention, it is still valuable to get an exception if you accidentally rebind something that you shouldn't rebind. -- Steven From antoon.pardon at rece.vub.ac.be Mon Aug 19 16:34:00 2013 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Mon, 19 Aug 2013 22:34:00 +0200 Subject: Importing variables non-deterministic? In-Reply-To: <52125071$0$29986$c3e8da3$5496439d@news.astraweb.com> References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211C5BD.5040209@rece.vub.ac.be> <5211D464.5090303@rece.vub.ac.be> <52125071$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: <52128138.6080609@rece.vub.ac.be> Op 19-08-13 19:05, Steven D'Aprano schreef: > > I wish Python had stronger support for enforcing constantness, to whit, > some way to say "you can't rebind or delete this name once it is bound". > You can do it with attributes, by use of property, or in C extensions, > but you cannot do it with top-level name bindings. It makes me terribly > sad that you can do this: > > import math > math.pi = 3.0 > > > although I can't decide whether I am less sad or more sad to see that the > behaviour of math.sin and friends doesn't depend on math.pi. Why should you expect math.sin and friends be dependant on math.pi? AfAIR the numerical algorithms for calulating sin and friends don't depend on (the value of) pi. So there is no reason to suspect that altering math.pi would have any effect on the results of these functions. -- Antoon Pardon From steve at pearwood.info Tue Aug 20 01:48:37 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 20 Aug 2013 05:48:37 GMT Subject: Importing variables non-deterministic? References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211C5BD.5040209@rece.vub.ac.be> <5211D464.5090303@rece.vub.ac.be> <52125071$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: <52130335$0$29885$c3e8da3$5496439d@news.astraweb.com> On Mon, 19 Aug 2013 22:34:00 +0200, Antoon Pardon wrote: > Op 19-08-13 19:05, Steven D'Aprano schreef: > > >> I wish Python had stronger support for enforcing constantness, to whit, >> some way to say "you can't rebind or delete this name once it is >> bound". You can do it with attributes, by use of property, or in C >> extensions, but you cannot do it with top-level name bindings. It makes >> me terribly sad that you can do this: >> >> import math >> math.pi = 3.0 >> >> >> although I can't decide whether I am less sad or more sad to see that >> the behaviour of math.sin and friends doesn't depend on math.pi. > > Why should you expect math.sin and friends be dependant on math.pi? > AfAIR the numerical algorithms for calulating sin and friends don't > depend on (the value of) pi. So there is no reason to suspect that > altering math.pi would have any effect on the results of these > functions. Of course they depend on pi. Or rather, they depend on the geometric properties of circles, which are related to pi. If the ratio of the circumference of a circle to its diameter was exactly 3, instead of 3.1415..., then sine and cosine functions would be periodic with period 6 rather than ? = 2?. If you consider the implementation of sin and cos functions, they usually reduce the argument modulo ? to something in the first quadrant, and then use symmetry to adjust the value. So changing the value of pi could, in principle, change the implementation of sin, cos and tan. -- Steven From wxjmfauth at gmail.com Tue Aug 20 02:40:40 2013 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Mon, 19 Aug 2013 23:40:40 -0700 (PDT) Subject: Importing variables non-deterministic? In-Reply-To: <52130335$0$29885$c3e8da3$5496439d@news.astraweb.com> References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211C5BD.5040209@rece.vub.ac.be> <5211D464.5090303@rece.vub.ac.be> <52125071$0$29986$c3e8da3$5496439d@news.astraweb.com> <52130335$0$29885$c3e8da3$5496439d@news.astraweb.com> Message-ID: <697cde04-0ec2-44d6-85b8-3e95bce3f7f4@googlegroups.com> Le mardi 20 ao?t 2013 07:48:37 UTC+2, Steven D'Aprano a ?crit?: > On Mon, 19 Aug 2013 22:34:00 +0200, Antoon Pardon wrote: > > > > > Op 19-08-13 19:05, Steven D'Aprano schreef: > > > > > > > > >> I wish Python had stronger support for enforcing constantness, to whit, > > >> some way to say "you can't rebind or delete this name once it is > > >> bound". You can do it with attributes, by use of property, or in C > > >> extensions, but you cannot do it with top-level name bindings. It makes > > >> me terribly sad that you can do this: > > >> > > >> import math > > >> math.pi = 3.0 > > >> > > >> > > >> although I can't decide whether I am less sad or more sad to see that > > >> the behaviour of math.sin and friends doesn't depend on math.pi. > > > > > > Why should you expect math.sin and friends be dependant on math.pi? > > > AfAIR the numerical algorithms for calulating sin and friends don't > > > depend on (the value of) pi. So there is no reason to suspect that > > > altering math.pi would have any effect on the results of these > > > functions. > > > > > > Of course they depend on pi. Or rather, they depend on the geometric > > properties of circles, which are related to pi. If the ratio of the > > circumference of a circle to its diameter was exactly 3, instead of > > 3.1415..., then sine and cosine functions would be periodic with period 6 > > rather than ? = 2?. > > > > If you consider the implementation of sin and cos functions, they usually > > reduce the argument modulo ? to something in the first quadrant, and then > > use symmetry to adjust the value. So changing the value of pi could, in > > principle, change the implementation of sin, cos and tan. > > > > > > -- > > Steven --------- Never heard about series, Taylor, Maclaurin, ... ? jmf From antoon.pardon at rece.vub.ac.be Tue Aug 20 02:55:18 2013 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Tue, 20 Aug 2013 08:55:18 +0200 Subject: Importing variables non-deterministic? In-Reply-To: <52130335$0$29885$c3e8da3$5496439d@news.astraweb.com> References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211C5BD.5040209@rece.vub.ac.be> <5211D464.5090303@rece.vub.ac.be> <52125071$0$29986$c3e8da3$5496439d@news.astraweb.com> <52130335$0$29885$c3e8da3$5496439d@news.astraweb.com> Message-ID: <521312D6.4050602@rece.vub.ac.be> Op 20-08-13 07:48, Steven D'Aprano schreef: > On Mon, 19 Aug 2013 22:34:00 +0200, Antoon Pardon wrote: > >> Op 19-08-13 19:05, Steven D'Aprano schreef: >> >> >>> I wish Python had stronger support for enforcing constantness, to whit, >>> some way to say "you can't rebind or delete this name once it is >>> bound". You can do it with attributes, by use of property, or in C >>> extensions, but you cannot do it with top-level name bindings. It makes >>> me terribly sad that you can do this: >>> >>> import math >>> math.pi = 3.0 >>> >>> >>> although I can't decide whether I am less sad or more sad to see that >>> the behaviour of math.sin and friends doesn't depend on math.pi. >> >> Why should you expect math.sin and friends be dependant on math.pi? >> AfAIR the numerical algorithms for calulating sin and friends don't >> depend on (the value of) pi. So there is no reason to suspect that >> altering math.pi would have any effect on the results of these >> functions. > > > Of course they depend on pi. Or rather, they depend on the geometric > properties of circles, which are related to pi. If the ratio of the > circumference of a circle to its diameter was exactly 3, instead of > 3.1415..., then sine and cosine functions would be periodic with period 6 > rather than ? = 2?. Which is beside the point. The fact that n? are the zeropoints of the sin function doesn't imply in any way that you somehow need to work with the value of ? in order to calculate any result of the function. Just as the function x^2 - 2 has -?2 and ?2 as zeropoints, yet when given a numerical value, you dont need (the value of) ?2 in order to get the result. So setting a variable like sqrt2 to whatever value shouldn't lead us to suspect it would influence the zeropoints of that function. Likewise with ? and the calculation of sin. > If you consider the implementation of sin and cos functions, they usually > reduce the argument modulo ? to something in the first quadrant, and then > use symmetry to adjust the value. So changing the value of pi could, in > principle, change the implementation of sin, cos and tan. Yes there is this aspect, which is a fair point. -- Antoon Pardon From wxjmfauth at gmail.com Tue Aug 20 03:31:32 2013 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Tue, 20 Aug 2013 00:31:32 -0700 (PDT) Subject: Importing variables non-deterministic? In-Reply-To: References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211C5BD.5040209@rece.vub.ac.be> <5211D464.5090303@rece.vub.ac.be> <52125071$0$29986$c3e8da3$5496439d@news.astraweb.com> <52130335$0$29885$c3e8da3$5496439d@news.astraweb.com> Message-ID: Le mardi 20 ao?t 2013 08:55:18 UTC+2, Antoon Pardon a ?crit?: > > > > > > > > > If you consider the implementation of sin and cos functions, they usually > > > reduce the argument modulo ? to something in the first quadrant, and then > > > use symmetry to adjust the value. So changing the value of pi could, in > > > principle, change the implementation of sin, cos and tan. > > > > Yes there is this aspect, which is a fair point. > > > > -- > > Antoon Pardon ----- Not really, see my previous post. This is only a geometric interpretation, useless for calculation. Consequence of an implementation: common trick to use and/or define pi: >>> const_pi = 4 * atan(1) >>> const_pi 3.141592653589793 jmf From antoon.pardon at rece.vub.ac.be Tue Aug 20 03:55:44 2013 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Tue, 20 Aug 2013 09:55:44 +0200 Subject: Importing variables non-deterministic? In-Reply-To: References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211C5BD.5040209@rece.vub.ac.be> <5211D464.5090303@rece.vub.ac.be> <52125071$0$29986$c3e8da3$5496439d@news.astraweb.com> <52130335$0$29885$c3e8da3$5496439d@news.astraweb.com> Message-ID: <52132100.9040808@rece.vub.ac.be> Op 20-08-13 09:31, wxjmfauth at gmail.com schreef: > Le mardi 20 ao?t 2013 08:55:18 UTC+2, Antoon Pardon a ?crit : >> >>> >> > >> >> >> >>> If you consider the implementation of sin and cos functions, they usually >> >>> reduce the argument modulo ? to something in the first quadrant, and then >> >>> use symmetry to adjust the value. So changing the value of pi could, in >> >>> principle, change the implementation of sin, cos and tan. >> >> >> >> Yes there is this aspect, which is a fair point. >> >> >> >> -- >> >> Antoon Pardon > > ----- > > Not really, see my previous post. This is only a geometric > interpretation, useless for calculation. No it is not. Steven is correct that if for example you want the value of sin(10), that in a typical implementation this will be reduced to calculating -sin(10 - 3?). This for two reasons. It is faster to first reduce the argument within the first kwadrant, do the series expansion and then correct for sign than to expand the series with the original argument and it is more acurate because first reducing asures that all terms will stay relatively small while using the original arguments can intrduce some large terms that will have to cancel each other but that will reduce acuracy. -- Antoon Pardon From wxjmfauth at gmail.com Tue Aug 20 05:15:03 2013 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Tue, 20 Aug 2013 02:15:03 -0700 (PDT) Subject: Importing variables non-deterministic? In-Reply-To: References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211C5BD.5040209@rece.vub.ac.be> <5211D464.5090303@rece.vub.ac.be> <52125071$0$29986$c3e8da3$5496439d@news.astraweb.com> <52130335$0$29885$c3e8da3$5496439d@news.astraweb.com> Message-ID: <3875e747-d33d-46ac-94ac-8f03ec5f9736@googlegroups.com> Le mardi 20 ao?t 2013 09:55:44 UTC+2, Antoon Pardon a ?crit?: > Op 20-08-13 09:31, wxjmfauth at gmail.com schreef: > > > Le mardi 20 ao?t 2013 08:55:18 UTC+2, Antoon Pardon a ?crit : > > >> > > >>> > > >> > > > > > >> > > >> > > >> > > >>> If you consider the implementation of sin and cos functions, they usually > > >> > > >>> reduce the argument modulo ? to something in the first quadrant, and then > > >> > > >>> use symmetry to adjust the value. So changing the value of pi could, in > > >> > > >>> principle, change the implementation of sin, cos and tan. > > >> > > >> > > >> > > >> Yes there is this aspect, which is a fair point. > > >> > > >> > > >> > > >> -- > > >> > > >> Antoon Pardon > > > > > > ----- > > > > > > Not really, see my previous post. This is only a geometric > > > interpretation, useless for calculation. > > > > No it is not. Steven is correct that if for example you > > want the value of sin(10), that in a typical implementation > > this will be reduced to calculating -sin(10 - 3?). > > > > This for two reasons. It is faster to first reduce the argument > > within the first kwadrant, do the series expansion and then > > correct for sign than to expand the series with the original > > argument and it is more acurate because first reducing asures > > that all terms will stay relatively small while using the > > original arguments can intrduce some large terms that will > > have to cancel each other but that will reduce acuracy. > > > > -- > > Antoon Pardon Ok. Fine. I was aware of the serie expansion, not about the reduction. jmf From ben+python at benfinney.id.au Mon Aug 19 21:14:49 2013 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 20 Aug 2013 11:14:49 +1000 Subject: Importing variables non-deterministic? References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211C5BD.5040209@rece.vub.ac.be> <5211D464.5090303@rece.vub.ac.be> Message-ID: <7w38q5noxi.fsf@benfinney.id.au> Antoon Pardon writes: > Op 19-08-13 09:45, Dave Angel schreef: > > Antoon Pardon wrote: > >> I think you are overstating your case. Classes and functions are > >> variables too and in general nobody seems to have a problem with > >> them being global. > > > > It's global *variables* that are to be avoided. constants like > > clsases and functions are fine. On the other hand, class attributes > > can be variable, and thus are to be avoided when reasonable. > > Python has no constants. Classes and functions can be changed just like > any other variable. I agree that classes and function are generally > meant to be constant, but often enought so are global int variables. I take this as yet another data point that speaking about ?variables? leads mostly to confusion. What Python has are bindings. Those bindings can change (what Antoon calls ?changed just like any other variable?). Many objects are immutable (what Dave calls ?constants like classes and functions?), even if the bindings can vary and even if they contain attributes which are themselves bindings that can change. So, you're both right. The terminology is leading you both astray and having you talk past each other. Now go forth and speak of variables no more :-) -- \ ?We demand rigidly defined areas of doubt and uncertainty!? | `\ ?Vroomfondel, _The Hitch-Hiker's Guide To The Galaxy_, Douglas | _o__) Adams | Ben Finney From steve+comp.lang.python at pearwood.info Mon Aug 19 12:57:37 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 19 Aug 2013 16:57:37 GMT Subject: Importing variables non-deterministic? References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211C5BD.5040209@rece.vub.ac.be> Message-ID: <52124e81$0$29986$c3e8da3$5496439d@news.astraweb.com> On Mon, 19 Aug 2013 10:16:36 +0200, Antoon Pardon wrote: > Op 19-08-13 09:45, Dave Angel schreef: >> Antoon Pardon wrote: >> >>> Op 17-08-13 17:01, Steven D'Aprano schreef: >>>> >>>> And here you re-import the name "y" from struct_global. That rebinds >>>> the current module's "y" with whatever value struct_global.y has >>>> *now*, rather than a second (or a minute, or an hour) earlier when >>>> the first import took place. Obviously at some point between the >>>> first import and the second import, struct_global.y must have been >>>> reassigned from -1 to 62. >>>> >>>> This goes to show why global variables are considered harmful, and >>>> why clean, modern program design tries to reduce the use of them as >>>> much as possible. Global variables are too easily modified by, well, >>>> *anything*. The sort of behaviour you are seeing is sometimes called >>>> "action at a distance" -- something, anything, anywhere in your >>>> program, possibly buried deep, deep down inside some function you >>>> might never suspect, is changing the global variable. >>> >>> I think you are overstating your case. Classes and functions are >>> variables too and in general nobody seems to have a problem with them >>> being global. >>> >>> >> It's global *variables* that are to be avoided. constants like clsases >> and functions are fine. On the other hand, class attributes can be >> variable, and thus are to be avoided when reasonable. > > Python has no constants. Classes and functions can be changed just like > any other variable. I agree that classes and function are generally > meant to be constant, but often enought so are global int variables. You are technically correct, but missing the point. If I wrote code that went around reassigning names from one function to another function, I would very likely soon work myself into a state of utter confusion: def func(x): ... # later save_func = func func = lambda x, y: do_stuff(x, 3*y)-4 result = something_that_calls_func() func = save_func Nasty, horrible code, yes? But it's nasty and horrible because "func" is bound to a function, it would be equally nasty and horrible if it was a data type (a string, a list, an int, a flag, ...) instead. Since classes and functions are First Class objects in Python, naturally if you treat them as global *variables* rather than global *constants* you can end up with problems. The problem is not the global part alone. Global constants, or pseudo-constant-by-convention-only, are fine. > And some of those that do change, only do so in the initialisation phase > and should be considered constant for the rest of the program. > > My point was that Steven has no way of knowing what exactly is going on > here and so shouldn't be making such a sweeping statement. On the contrary, I can read the Original Poster's description of the problem, and then use my ability to reason to deduce what the most likely explanation was. Now of course I might be wrong. I don't claim infallibility or omniscience. But I'm not an idiot, and if the OP says that a global variable has one value at one time, and then some time later has a different value, there are two likely possibilities: * The variable was changed by some other piece of code, i.e. it actually was being used as a *global variable*. * The OP is completely confused, and (inadvertently, we hope) reporting things which are actually not true. Much less likely: * Despite 15+ years of experience with Python, and reading the documentation, I have failed to notice that, yes, the OP is correct and importing variables is non-deterministic. * There's a bug in Python. Both of these are theoretically possible, but the chance of them is somewhere between Buckley's and None. Of course, if you have an alternate explanation for the issue the OP reported, I would love to hear it. Maybe I have completely misunderstood the situation. -- Steven From steve+comp.lang.python at pearwood.info Mon Aug 19 13:16:34 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 19 Aug 2013 17:16:34 GMT Subject: Importing variables non-deterministic? References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211C5BD.5040209@rece.vub.ac.be> <52124e81$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: <521252f2$0$29986$c3e8da3$5496439d@news.astraweb.com> On Mon, 19 Aug 2013 16:57:37 +0000, Steven D'Aprano wrote: > def func(x): > ... > > # later > save_func = func > func = lambda x, y: do_stuff(x, 3*y)-4 result = > something_that_calls_func() > func = save_func > > > Nasty, horrible code, yes? But it's nasty and horrible because "func" is > bound to a function, it would be equally nasty and horrible if it was a > data type (a string, a list, an int, a flag, ...) instead. Oops, I left out a word. It is NOT nasty and horrible specifically because "func" is bound to a function, it would be equally horrible if it were a data type. -- Steven From rosuav at gmail.com Mon Aug 19 13:25:17 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 19 Aug 2013 18:25:17 +0100 Subject: Importing variables non-deterministic? In-Reply-To: <521252f2$0$29986$c3e8da3$5496439d@news.astraweb.com> References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211C5BD.5040209@rece.vub.ac.be> <52124e81$0$29986$c3e8da3$5496439d@news.astraweb.com> <521252f2$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Aug 19, 2013 at 6:16 PM, Steven D'Aprano wrote: > On Mon, 19 Aug 2013 16:57:37 +0000, Steven D'Aprano wrote: > >> def func(x): >> ... >> >> # later >> save_func = func >> func = lambda x, y: do_stuff(x, 3*y)-4 result = >> something_that_calls_func() >> func = save_func >> >> >> Nasty, horrible code, yes? But it's nasty and horrible because "func" is >> bound to a function, it would be equally nasty and horrible if it was a >> data type (a string, a list, an int, a flag, ...) instead. > > Oops, I left out a word. It is NOT nasty and horrible specifically > because "func" is bound to a function, it would be equally horrible if it > were a data type. I was having a little trouble parsing that! Thank you for the Iolanthe-style[1] update. ChrisA [1] http://math.boisestate.edu/gas/iolanthe/web_op/iol23d.html From antoon.pardon at rece.vub.ac.be Mon Aug 19 13:40:49 2013 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Mon, 19 Aug 2013 19:40:49 +0200 Subject: Importing variables non-deterministic? In-Reply-To: <52124e81$0$29986$c3e8da3$5496439d@news.astraweb.com> References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211C5BD.5040209@rece.vub.ac.be> <52124e81$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: <521258A1.4060805@rece.vub.ac.be> Op 19-08-13 18:57, Steven D'Aprano schreef: > On Mon, 19 Aug 2013 10:16:36 +0200, Antoon Pardon wrote: > >> Op 19-08-13 09:45, Dave Angel schreef: >>> Antoon Pardon wrote: >>> >>>> Op 17-08-13 17:01, Steven D'Aprano schreef: >>>>> >>>>> And here you re-import the name "y" from struct_global. That rebinds >>>>> the current module's "y" with whatever value struct_global.y has >>>>> *now*, rather than a second (or a minute, or an hour) earlier when >>>>> the first import took place. Obviously at some point between the >>>>> first import and the second import, struct_global.y must have been >>>>> reassigned from -1 to 62. >>>>> >>>>> This goes to show why global variables are considered harmful, and >>>>> why clean, modern program design tries to reduce the use of them as >>>>> much as possible. Global variables are too easily modified by, well, >>>>> *anything*. The sort of behaviour you are seeing is sometimes called >>>>> "action at a distance" -- something, anything, anywhere in your >>>>> program, possibly buried deep, deep down inside some function you >>>>> might never suspect, is changing the global variable. >>>> >>>> I think you are overstating your case. Classes and functions are >>>> variables too and in general nobody seems to have a problem with them >>>> being global. >>>> >>>> >>> It's global *variables* that are to be avoided. constants like clsases >>> and functions are fine. On the other hand, class attributes can be >>> variable, and thus are to be avoided when reasonable. >> >> Python has no constants. Classes and functions can be changed just like >> any other variable. I agree that classes and function are generally >> meant to be constant, but often enought so are global int variables. > > You are technically correct, but missing the point. If I wrote code that > went around reassigning names from one function to another function, I > would very likely soon work myself into a state of utter confusion: > > def func(x): > ... > > # later > save_func = func > func = lambda x, y: do_stuff(x, 3*y)-4 > result = something_that_calls_func() > func = save_func > > > Nasty, horrible code, yes? But it's nasty and horrible because "func" is > bound to a function, it would be equally nasty and horrible if it was a > data type (a string, a list, an int, a flag, ...) instead. > > Since classes and functions are First Class objects in Python, naturally > if you treat them as global *variables* rather than global *constants* > you can end up with problems. The problem is not the global part alone. > Global constants, or pseudo-constant-by-convention-only, are fine. I don't think there is a big disagreement on this, I just thought you overstated your case as you originally worded it. >> And some of those that do change, only do so in the initialisation phase >> and should be considered constant for the rest of the program. >> >> My point was that Steven has no way of knowing what exactly is going on >> here and so shouldn't be making such a sweeping statement. > > On the contrary, I can read the Original Poster's description of the > problem, and then use my ability to reason to deduce what the most likely > explanation was. > > Now of course I might be wrong. I don't claim infallibility or > omniscience. But I'm not an idiot, and if the OP says that a global > variable has one value at one time, and then some time later has a > different value, there are two likely possibilities: > > * The variable was changed by some other piece of code, i.e. it > actually was being used as a *global variable*. Sure. But was that because it was intended to be used as a variable or was the intention to use it as a constant but because of a bug it was changed anyway? I don't think you have enough information to discriminate between these two cases and for the possibility of this being the latter case I found your disgression into the harm of global variable too strongly worded. -- Antoon Pardon. From fsaldan1 at gmail.com Sat Aug 17 12:53:07 2013 From: fsaldan1 at gmail.com (Fernando Saldanha) Date: Sat, 17 Aug 2013 09:53:07 -0700 (PDT) Subject: Python getters and setters Message-ID: <227bd47c-0e86-4d65-985f-e59aa4f25294@googlegroups.com> I am new to Python. I understand that it is "unpythonic" to write getters and setters, and that property() can be used if necessary. This deals with the case of attributes, but there are other kinds of information available within a class. Suppose my class contains an attribute called "data" that can potentially provide a lot of information that will be needed by class users. I have two options: 1) For each piece of information within data (e.g., length) I write a method that retrieves that information: def data_length(self): return len(self.data) 2) I do not create such a method. Users that are interested in that information will have to write len(obj.data), where obj is a previously instantiated object of my class. Which one of the two alternatives fits better with the Python philosophy? The first alternative is more work for me, creates a "heavier" class and may have slower performance, but makes things easier for the user and is more implementation independent. Thanks for the help. FS From python at mrabarnett.plus.com Sat Aug 17 13:10:25 2013 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 17 Aug 2013 18:10:25 +0100 Subject: Python getters and setters In-Reply-To: <227bd47c-0e86-4d65-985f-e59aa4f25294@googlegroups.com> References: <227bd47c-0e86-4d65-985f-e59aa4f25294@googlegroups.com> Message-ID: <520FAE81.9060002@mrabarnett.plus.com> On 17/08/2013 17:53, Fernando Saldanha wrote: > I am new to Python. > > I understand that it is "unpythonic" to write getters and setters, and that property() can be used if necessary. > > This deals with the case of attributes, but there are other kinds of information available within a class. > > Suppose my class contains an attribute called "data" that can potentially provide a lot of information that will be needed by class users. I have two options: > > 1) For each piece of information within data (e.g., length) I write a method that retrieves that information: > > def data_length(self): > return len(self.data) > > 2) I do not create such a method. Users that are interested in that information will have to write len(obj.data), where obj is a previously instantiated object of my class. > > Which one of the two alternatives fits better with the Python philosophy? The first alternative is more work for me, creates a "heavier" class and may have slower performance, but makes things easier for the user and is more implementation independent. > If the attribute is public, i.e. the user is expected to write obj.data, then len(obj.data) is the right way to get its length. From steve+comp.lang.python at pearwood.info Sat Aug 17 13:18:08 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 Aug 2013 17:18:08 GMT Subject: Python getters and setters References: <227bd47c-0e86-4d65-985f-e59aa4f25294@googlegroups.com> Message-ID: <520fb04f$0$30000$c3e8da3$5496439d@news.astraweb.com> On Sat, 17 Aug 2013 09:53:07 -0700, Fernando Saldanha wrote: > Suppose my class contains an attribute called "data" that can > potentially provide a lot of information that will be needed by class > users. I have two options: > > 1) For each piece of information within data (e.g., length) I write a > method that retrieves that information: > > def data_length(self): > return len(self.data) Certainly not. Python is not Java. > 2) I do not create such a method. Users that are interested in that > information will have to write len(obj.data), where obj is a previously > instantiated object of my class. This one. > Which one of the two alternatives fits better with the Python > philosophy? The first alternative is more work for me, creates a > "heavier" class and may have slower performance, but makes things easier > for the user and is more implementation independent. How is this: obj.data_length() easier for the user than this? len(obj.data) The second is shorter to type, and it is a standard idiom that works everywhere. You want to know the length of something? len(something), no matter what it is. You don't have to play a game of "Guess the method name" with every class you come across. # Yes, this is good, consistent design len(myrecord.field) len(obj.data) len(data.value) len(collection[key]) # No, this is crappy, inconsistent design myrecord.field_len() obj.data_length() data.get_length_of_value() collection.key_len(key) -- Steven From python.list at tim.thechases.com Sat Aug 17 13:31:01 2013 From: python.list at tim.thechases.com (Tim Chase) Date: Sat, 17 Aug 2013 12:31:01 -0500 Subject: Python getters and setters In-Reply-To: <520fb04f$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <227bd47c-0e86-4d65-985f-e59aa4f25294@googlegroups.com> <520fb04f$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: <20130817123101.21eb4d67@bigbox.christie.dr> On 2013-08-17 17:18, Steven D'Aprano wrote: > # Yes, this is good, consistent design > len(myrecord.field) > len(obj.data) > len(data.value) > len(collection[key]) I would also add that, if the primary goal of your class is to encapsulate the data, you can do class MyClass: def __init__(self, ...): self.data = [] def __len__(self): return len(self.data) which allows for the even clearer my_obj = MyClass(...) manipulate(my_obj) if len(my_obj) > 42: do_important_stuff() -tkc From irmen.NOSPAM at xs4all.nl Sat Aug 17 18:52:06 2013 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Sun, 18 Aug 2013 00:52:06 +0200 Subject: Python getters and setters In-Reply-To: <520fb04f$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <227bd47c-0e86-4d65-985f-e59aa4f25294@googlegroups.com> <520fb04f$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: <520ffe9a$0$15920$e4fe514c@news.xs4all.nl> On 17-8-2013 19:18, Steven D'Aprano wrote: > On Sat, 17 Aug 2013 09:53:07 -0700, Fernando Saldanha wrote: > >> Suppose my class contains an attribute called "data" that can >> potentially provide a lot of information that will be needed by class >> users. I have two options: >> >> 1) For each piece of information within data (e.g., length) I write a >> method that retrieves that information: >> >> def data_length(self): >> return len(self.data) > > Certainly not. Python is not Java. > > >> 2) I do not create such a method. Users that are interested in that >> information will have to write len(obj.data), where obj is a previously >> instantiated object of my class. > > This one. > > >> Which one of the two alternatives fits better with the Python >> philosophy? The first alternative is more work for me, creates a >> "heavier" class and may have slower performance, but makes things easier >> for the user and is more implementation independent. > > How is this: > > obj.data_length() > > easier for the user than this? > > len(obj.data) > It's not easier for the user perse, but it might be preferable from a design point of view. For the direct components of obj, it's probably alright to access them directly. There's something called the Law of Demeter aka principle of least knowledge (http://en.wikipedia.org/wiki/Law_of_Demeter) though. It basically argues against the use of "more than one dot". Doing that ties the use of the object more to the actual implementation/internal structure of the object. Irmen From steve+comp.lang.python at pearwood.info Sat Aug 17 19:40:30 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 Aug 2013 23:40:30 GMT Subject: Python getters and setters References: <227bd47c-0e86-4d65-985f-e59aa4f25294@googlegroups.com> <520fb04f$0$30000$c3e8da3$5496439d@news.astraweb.com> <520ffe9a$0$15920$e4fe514c@news.xs4all.nl> Message-ID: <521009ee$0$30000$c3e8da3$5496439d@news.astraweb.com> On Sun, 18 Aug 2013 00:52:06 +0200, Irmen de Jong wrote: > On 17-8-2013 19:18, Steven D'Aprano wrote: [...] >> How is this: >> >> obj.data_length() >> >> easier for the user than this? >> >> len(obj.data) >> >> > It's not easier for the user perse, but it might be preferable from a > design point of view. For the direct components of obj, it's probably > alright to access them directly. Yes, and my comments assume that obj.data is public in the first place. > There's something called the Law of > Demeter aka principle of least knowledge > (http://en.wikipedia.org/wiki/Law_of_Demeter) though. It basically > argues against the use of "more than one dot". Doing that ties the use > of the object more to the actual implementation/internal structure of > the object. A very good point! But not an easy one to understand. The Law of Demeter can be described as "if you want to walk the dog, talk to the dog, not the dog's legs". Another classic example is of the paperboy who reaches into your back pocket, pulls out your wallet, removes the money he is owed, then puts your wallet back: # No, this is bad design. class Paperboy: def take_payment(self, customer): payment_owed = 2 wallet = customer.backpocket.get_wallet() money = wallet.take(payment_owed) self.money += money customer.backpocket.put_wallet(wallet) It's bad design because it ties the paperboy to one specific implementation of customer. Perhaps the customer carries his wallet in a front pocket. Or she carries her purse in her handbag. Or she is the Queen of England, and doesn't carry money at all, but has a flunky who carries money for her. Better is: class Paperboy: def take_payment(self, customer): payment_owed = 2 self.money = customer.get_payment(payment_owed) and raise an error if the customer doesn't implement get_payment. The Law of Demeter is often characterised as "don't use more than one dot at a time", but of course that's wrong for two reasons: - Re-writing take_payment to avoid multiple dots is still wrong: def take_payment(self, customer): payment_owed = 2 pocket = customer.backpocket wallet = pocket.get_wallet() money = wallet.take(payment_owed) self.money += money pocket.put_wallet(wallet) - If the attribute is part of the public interface, then it is okay to use it no matter how deep (how many dots) it is. The Law of Demeter is really about being careful about what interface your classes provide. Customers should provide a get_payment method; dogs should provide a walk method. You shouldn't have to send individual step messages to the dog's legs. -- Steven From rosuav at gmail.com Sat Aug 17 21:07:16 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 18 Aug 2013 02:07:16 +0100 Subject: Python getters and setters In-Reply-To: <521009ee$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <227bd47c-0e86-4d65-985f-e59aa4f25294@googlegroups.com> <520fb04f$0$30000$c3e8da3$5496439d@news.astraweb.com> <520ffe9a$0$15920$e4fe514c@news.xs4all.nl> <521009ee$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Aug 18, 2013 at 12:40 AM, Steven D'Aprano wrote: > The Law of Demeter is really about being careful about what interface > your classes provide. Customers should provide a get_payment method; dogs > should provide a walk method. You shouldn't have to send individual step > messages to the dog's legs. And it has its limits, too. If you're walking a dog on a leash, you don't send a message to the leash to send a message to the dog to walk. You talk directly to the dog. Like most programming principles, it's something to read, to grok, and then to think about. I have yet to find any rule which, if slavishly followed, will not lead to bad code _somewhere_. ChrisA From fsaldan1 at gmail.com Sun Aug 18 01:26:32 2013 From: fsaldan1 at gmail.com (Fernando Saldanha) Date: Sat, 17 Aug 2013 22:26:32 -0700 (PDT) Subject: Python getters and setters In-Reply-To: <227bd47c-0e86-4d65-985f-e59aa4f25294@googlegroups.com> References: <227bd47c-0e86-4d65-985f-e59aa4f25294@googlegroups.com> Message-ID: <5e7fd63b-78b2-4afb-9a84-33afb19378e1@googlegroups.com> The debate got more interesting than I expected. Without taking sides, I would like to add that perhaps my "length" example was misleading: length is easy to calculate. The information could be hard to extract from the data, either because complex calculations are involved, or because it is not apparent which pieces of information have to be combined. Also, notice that I never stated that the information was in the shape of an attribute. The comparison below may seem clear cut if the information to be obtained is length but one could arrive at exactly the opposite conclusion by imagining that "length" is hard to calculate or extract. It may be easier and faster to look up a function in an API than figuring out how to do a complex calculation. Even in the simple case where the information is "length" there may be more than one reasonable definition. For example, when dealing with time series data in matrix form it makes sense to consider the number of rows as the length of the data, but it also makes sense to define length as the number of elements of the matrix. So the word "len" in the first half of the example below could hide different concepts: consistent but misleading. The different methods in the second half could have the virtue of clarifying which concept of length applies in each case. "# Yes, this is good, consistent design len(myrecord.field) len(obj.data) len(data.value) len(collection[key]) # No, this is crappy, inconsistent design myrecord.field_len() obj.data_length() data.get_length_of_value() collection.key_len(key)" From gouzounakis at hotmail.com Sat Aug 17 16:23:17 2013 From: gouzounakis at hotmail.com (D. Xenakis) Date: Sat, 17 Aug 2013 13:23:17 -0700 (PDT) Subject: log incoming ip/porrt connections Message-ID: Hi there. I have a script-service running on a remote server, listening on a specific port. What i need here is to make this also maintain a log file of ALL incoming connections. Could someone suggest to me a simple codefunction example to implement that on my main running service? THX!! From gouzounakis at hotmail.com Sat Aug 17 16:35:01 2013 From: gouzounakis at hotmail.com (D. Xenakis) Date: Sat, 17 Aug 2013 13:35:01 -0700 (PDT) Subject: log incoming ip/porrt connections In-Reply-To: References: Message-ID: <6afa0547-91c7-4a3c-aa45-346ff440f239@googlegroups.com> Or point me the right way in case this is not that simple to do. From irmen.NOSPAM at xs4all.nl Sat Aug 17 18:43:40 2013 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Sun, 18 Aug 2013 00:43:40 +0200 Subject: log incoming ip/porrt connections In-Reply-To: References: Message-ID: <520ffca0$0$15945$e4fe514c@news.xs4all.nl> On 17-8-2013 22:23, D. Xenakis wrote: > Hi there. I have a script-service running on a remote server, listening on a specific port. What i need here is to make this also maintain a log file of ALL incoming connections. > > Could someone suggest to me a simple codefunction example to implement that on my main running service? > > THX!! > One approach could be simply to log the client socket's peername to a logfile, something like this: import logging log=logging.getLogger("connections") ... if <>: clientsocket = ... log.debug("new connection from "+clientsocket.getpeername()) ... Irmen From gouzounakis at hotmail.com Sun Aug 18 16:41:32 2013 From: gouzounakis at hotmail.com (D. Xenakis) Date: Sun, 18 Aug 2013 13:41:32 -0700 (PDT) Subject: log incoming ip/porrt connections In-Reply-To: <520ffca0$0$15945$e4fe514c@news.xs4all.nl> References: <520ffca0$0$15945$e4fe514c@news.xs4all.nl> Message-ID: <746d9a4e-b643-447c-b4bb-a2aa4e8f2d95@googlegroups.com> This monitors also ip/port of incoming udp packets? Or just tcp after a connection has been enstablished? If i dont make any sense, plz correct me. Not much experience with networking here :) From w.w.milner at googlemail.com Sun Aug 18 05:41:27 2013 From: w.w.milner at googlemail.com (w.w.milner at googlemail.com) Date: Sun, 18 Aug 2013 02:41:27 -0700 (PDT) Subject: Local variable in a closure Message-ID: <107941d9-a981-4dd6-8460-336afc16f025@googlegroups.com> Is f local or not? http://pastebin.com/AKDJrbDs From rosuav at gmail.com Sun Aug 18 06:28:40 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 18 Aug 2013 11:28:40 +0100 Subject: Local variable in a closure In-Reply-To: <107941d9-a981-4dd6-8460-336afc16f025@googlegroups.com> References: <107941d9-a981-4dd6-8460-336afc16f025@googlegroups.com> Message-ID: On Sun, Aug 18, 2013 at 10:41 AM, wrote: > Is f local or not? > http://pastebin.com/AKDJrbDs With something that short, it'd be easier to simply paste it straight into your post, rather than having it off elsewhere. But to answer your question: It is its own kind of beast. You can play around with the dis.dis() function (start with "import dis", which is not just "import this" with an accent) in the interactive interpreter, as an effective way of finding out what actually happens. In my testing, the opcodes for retrieving and updating 'f' are LOAD_DEREF and STORE_DEREF, different from LOAD_FAST/STORE_FAST as used for locals, and LOAD_GLOBAL/STORE_GLOBAL for globals. In normal usage, nonlocal variables are most like local variables, but they happen to span one level of function nesting. So they're still basically locals, hence they appear in locals(). At least, that's my understanding of it. ChrisA From ian.g.kelly at gmail.com Sun Aug 18 06:40:10 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 18 Aug 2013 04:40:10 -0600 Subject: Local variable in a closure In-Reply-To: <107941d9-a981-4dd6-8460-336afc16f025@googlegroups.com> References: <107941d9-a981-4dd6-8460-336afc16f025@googlegroups.com> Message-ID: f is nonlocal to times(), local to multiplier(). As the docs for the locals() function say, "Free variables are returned by locals() when it is called in function blocks, but not in class blocks." On Sun, Aug 18, 2013 at 3:41 AM, wrote: > Is f local or not? > http://pastebin.com/AKDJrbDs > -- > http://mail.python.org/mailman/listinfo/python-list From davea at davea.name Sun Aug 18 06:44:06 2013 From: davea at davea.name (Dave Angel) Date: Sun, 18 Aug 2013 10:44:06 +0000 (UTC) Subject: Local variable in a closure References: <107941d9-a981-4dd6-8460-336afc16f025@googlegroups.com> Message-ID: w.w.milner at googlemail.com wrote: > Is f local or not? > http://pastebin.com/AKDJrbDs Please have a little respect, and include the source in your message. You managed quite nicely to keep it small, but you put it in an obscure place that some people won't be able to reach, and that might not survive for the archives. def multiplier(f): def times(n): # is f local? nonlocal f f=f+1 # if not, why is it here? print("Locals: ",locals()) return n*f return times times2 = multiplier(2) print(times2(4)) # 3X4=12 print(times2(4)) # 4X4=16 Inside function times, the variable 'f' is a free variable, not a local. You can prove that to yourself by adding a dis.dis(times) just before the "return times" statement. Here's how it begins: 7 0 LOAD_DEREF 0 (f) 3 LOAD_CONST 1 (1) 6 BINARY_ADD 7 STORE_DEREF 0 (f) In the dis.dis listing, the LOAD_DEREF and STORE_DEREF opcodes are referring to free variables, the LOAD_FAST is referring to a local, and the LOAD_GLOBAL is referring to a global. The locals() function is just over-simplifying. it's only a convenience function, not what I would consider part of the language, and it wasn't apparently deemed necessary to have a separate function for debugging free varaibles. -- DaveA From tjreedy at udel.edu Sun Aug 18 16:42:52 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 18 Aug 2013 16:42:52 -0400 Subject: Local variable in a closure In-Reply-To: References: <107941d9-a981-4dd6-8460-336afc16f025@googlegroups.com> Message-ID: On 8/18/2013 6:44 AM, Dave Angel wrote: > w.w.milner at googlemail.com wrote: > >> Is f local or not? >> http://pastebin.com/AKDJrbDs > > Please have a little respect, and include the source in your message. > You managed quite nicely to keep it small, but you put it in an obscure > place that some people won't be able to reach, and that might not > survive for the archives. > > def multiplier(f): > def times(n): > # is f local? > nonlocal f > f=f+1 > # if not, why is it here? > print("Locals: ",locals()) Because nonlocal names are not in the global dict and the devs wanted globals() + locals() to report all accessible names, rather than add nonlocals() or leave them invisible. > return n*f > return times > > times2 = multiplier(2) > print(times2(4)) # 3X4=12 > print(times2(4)) # 4X4=16 > > Inside function times, the variable 'f' is a free variable, not a local. > You can prove that to yourself by adding a dis.dis(times) just before > the "return times" statement. Here's how it begins: > > 7 0 LOAD_DEREF 0 (f) > 3 LOAD_CONST 1 (1) > 6 BINARY_ADD > 7 STORE_DEREF 0 (f) > > In the dis.dis listing, the LOAD_DEREF and STORE_DEREF opcodes are > referring to free variables, the LOAD_FAST is referring to a local, and > the LOAD_GLOBAL is referring to a global. > > The locals() function is just over-simplifying. it's only a convenience > function, not what I would consider part of the language, I think this is a good way to look at it. > and it wasn't apparently deemed necessary to have a separate function > for debugging free varaibles. One should think of 'locals' as meaning 'non_globals', which was exactly true when there were no non-global, non-local names. When closures were first added, such names were only readable. There was a long debate over what term to use for the keyword that would allow rebinding the names in outer functions. 'Nonlocal' is, at best, the least bad of the options considered. In standard usage, the terms 'free' and 'bound' are context dependent. https://en.wikipedia.org/wiki/Free_variable Within a function or class body, all global variables are non-local and free, just like 'nonlocals'. -- Terry Jan Reedy From badouglas at gmail.com Sun Aug 18 08:40:37 2013 From: badouglas at gmail.com (bruce) Date: Sun, 18 Aug 2013 08:40:37 -0400 Subject: crawling/parsing a webpage based on dynamic javascript Message-ID: Hi. Looking at using python/cerely/twisted to test in parsing a test site. Also looking at being able to parse a site created using dynamic javascript. I've got test apps to parse a site, but I'm interested in getting a better understanding of using multi-thread/multi-processing approaches to spin out as many fetch processes as possible. At the same time, I'm interested in understanding a bit better what's used for parsing the javascript pages in the py world. Also, rather than just point me to something like "scrapy", I'm actually interested in finding someone who's done this that I can talk to. Heck, for the right person, I'll even toss some cash your way!! Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From gheskett at wdtv.com Sun Aug 18 16:09:54 2013 From: gheskett at wdtv.com (Gene Heskett) Date: Sun, 18 Aug 2013 16:09:54 -0400 Subject: Need somebody schmardter than me Message-ID: <201308181609.54898.gheskett@wdtv.com> Greetings all; I am trying to make a machine vision kit of modules work in linuxcnc, which uses a lot of python for scripts and gui controls in the user interface. This is on a Ubu 10.04.4 LTS install, which because linuxcnc is intimately married to an RTAI equipt kernel version, has not been updated past 2.6.32-12x-RTAI. Those patches are very invasive but a 3.8.something version is being worked on as we speak. But in the meantime, in an effort to debug the failure of the camera application to work WITH linuxcnc, I ran the debug level up to maximum to see if I might find the first point of failure, and get this on the console when linuxcnc is launched from the cli: ================ Xlib.protocol.request.QueryExtension parse error? unable to instantiate [/dev/video0] ================ Then it reverts to the normal traceback reporting the same thing but quoting the line numbers of the two scripts that led to the failed call. /dev/video0 exists, is created when the usb cable from the camera is plugged in, and the program "cheese" can display this cameras output just fine using /dev/video0 as the src device. When cheese runs this camera, there is a several second delay while cheese is apparently talking to it to establish the correct output format before cheese starts displaying its output, so I am wondering if an initialization time delay might be the fix, but I haven't a clue about how to go about that in python despite my copying the list for what, a year or more now? So, where should I start? Heck, maybe I might learn a bit of python while running this one down. ;-) Thanks all. Cheers, Gene -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author) My web page: is up! My views From 0 to "what seems to be the problem officer" in 8.3 seconds. -- Ad for the new VW Corrado A pen in the hand of this president is far more dangerous than 200 million guns in the hands of law-abiding citizens. From roy at panix.com Sun Aug 18 16:19:55 2013 From: roy at panix.com (Roy Smith) Date: Sun, 18 Aug 2013 16:19:55 -0400 Subject: Need somebody schmardter than me References: Message-ID: In article , Gene Heskett wrote: [much that doesn't appear to have anything to do with Python elided] > When cheese runs this camera, there is a several second delay while cheese > is apparently talking to it to establish the correct output format before > cheese starts displaying its output, so I am wondering if an initialization > time delay might be the fix, In general, when you have some sort of synchronization problem, i.e., "this piece of code must not start running until that other thing is completed", sticking in a time delay is: 1) Almost always the wrong thing to do. 2) Very often the most simple solution. How it can simultaneously be both of these is left as an exercise for the reader :-) > but I haven't a clue about how to go about that in python import time time.sleep(1.5) will sleep for 1.5 seconds. From gheskett at wdtv.com Sun Aug 18 16:48:17 2013 From: gheskett at wdtv.com (Gene Heskett) Date: Sun, 18 Aug 2013 16:48:17 -0400 Subject: Need somebody schmardter than me In-Reply-To: References: Message-ID: <201308181648.17774.gheskett@wdtv.com> On Sunday 18 August 2013 16:38:52 Roy Smith did opine: > In article , > Gene Heskett wrote: > > [much that doesn't appear to have anything to do with Python elided] > > > When cheese runs this camera, there is a several second delay while > > cheese is apparently talking to it to establish the correct output > > format before cheese starts displaying its output, so I am wondering > > if an initialization time delay might be the fix, > > In general, when you have some sort of synchronization problem, i.e., > "this piece of code must not start running until that other thing is > completed", sticking in a time delay is: > > 1) Almost always the wrong thing to do. > > 2) Very often the most simple solution. > > How it can simultaneously be both of these is left as an exercise for > the reader :-) > Oh Loverly... > > but I haven't a clue about how to go about that in python > > import time That of course goes near the top of the script. > time.sleep(1.5) I'll see if I can see a sensible place to add this one. > will sleep for 1.5 seconds. Thanks. Its likely I'll be back because now that I think about it, that first line I quoted is quite likely from the function itself, and doesn't serve to establish the context of the call, hence the actual arguments to it. Its one of those cases where, if the function were to also echo the arguments it was handed, the troubleshooting would be a lot more straightforward because you could then see the error causer right up front. Cheers, Gene -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author) My web page: is up! My views Automobile, n.: A four-wheeled vehicle that runs up hills and down pedestrians. A pen in the hand of this president is far more dangerous than 200 million guns in the hands of law-abiding citizens. From rosuav at gmail.com Sun Aug 18 17:20:13 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 18 Aug 2013 22:20:13 +0100 Subject: Need somebody schmardter than me In-Reply-To: <201308181648.17774.gheskett@wdtv.com> References: <201308181648.17774.gheskett@wdtv.com> Message-ID: On Sun, Aug 18, 2013 at 9:48 PM, Gene Heskett wrote: >> import time > > That of course goes near the top of the script. > >> time.sleep(1.5) > > I'll see if I can see a sensible place to add this one. The import doesn't have to happen near the top of the script; it's an executable statement like any other. If you want to keep it near the time.sleep() call, that'll work fine. It's up to you to decide what goes where. ChrisA From gheskett at wdtv.com Sun Aug 18 19:33:52 2013 From: gheskett at wdtv.com (Gene Heskett) Date: Sun, 18 Aug 2013 19:33:52 -0400 Subject: Need somebody schmardter than me In-Reply-To: References: <201308181648.17774.gheskett@wdtv.com> Message-ID: <201308181933.52234.gheskett@wdtv.com> On Sunday 18 August 2013 19:30:43 Chris Angelico did opine: > On Sun, Aug 18, 2013 at 9:48 PM, Gene Heskett wrote: > >> import time > > > > That of course goes near the top of the script. > > > >> time.sleep(1.5) > > > > I'll see if I can see a sensible place to add this one. > > The import doesn't have to happen near the top of the script; it's an > executable statement like any other. If you want to keep it near the > time.sleep() call, that'll work fine. It's up to you to decide what > goes where. > > ChrisA Actually, I just found the authors names in the manpage. They wrote this thing back in Ubuntu Hardy 8.04 LTS days, so I have posted a note and a screen shot pix to them as I am now convinced its a Hardy to Lucid thing. Thank you for taking the time to try & sort me, I know most have better things to do. Cheers, Gene -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author) My web page: is up! My views All things are possible, except for skiing through a revolving door. A pen in the hand of this president is far more dangerous than 200 million guns in the hands of law-abiding citizens. From papillion at gmail.com Sun Aug 18 17:56:57 2013 From: papillion at gmail.com (Anthony Papillion) Date: Sun, 18 Aug 2013 16:56:57 -0500 Subject: Question about crypto Message-ID: <52114329.2030801@gmail.com> I've just started working with the Crypto library and I've already run into a wall even though I'm following a tutorial. Basically, I'm trying to encrypt a string using AES in CBC mode. Here is the code: from Crypto import AES import hashlib text_to_encrypt = 'This is a super secret encrypted message, yo!' key = '0123456789abcdef' mode = AES.MODE_CBC encryptor = AES.new(key, mode) ciphertext = encryptor.encrypt(text) When I run the code above, I am told that the IV must be 16 bytes long. I'm assuming that the IV (I know that means "Initialization Vector") is either the key OR something else I can set. But I don't know how or what to do. Does anyone see what is wrong with the code above and could suggest ways to make it work? I've spent the last 45 minutes googling around and nothing comes up specific to my problem. Thanks, Anthony From skip at pobox.com Sun Aug 18 18:29:37 2013 From: skip at pobox.com (Skip Montanaro) Date: Sun, 18 Aug 2013 17:29:37 -0500 Subject: Question about crypto In-Reply-To: <52114329.2030801@gmail.com> References: <52114329.2030801@gmail.com> Message-ID: > When I run the code above, I am told that the IV must be 16 bytes long. > I'm assuming that the IV (I know that means "Initialization Vector") is > either the key OR something else I can set. But I don't know how or what > to do. Does this Stack Overflow thread help? It looks to me like you aren't defining an initialization vector at all. http://stackoverflow.com/questions/14716338/pycrypto-how-does-the-initialization-vector-work Skip From papillion at gmail.com Mon Aug 19 08:32:34 2013 From: papillion at gmail.com (Anthony Papillion) Date: Mon, 19 Aug 2013 07:32:34 -0500 Subject: Question about crypto In-Reply-To: References: <52114329.2030801@gmail.com> Message-ID: <52121062.6000500@gmail.com> On 08/18/2013 05:29 PM, Skip Montanaro wrote: >> When I run the code above, I am told that the IV must be 16 bytes long. >> I'm assuming that the IV (I know that means "Initialization Vector") is >> either the key OR something else I can set. But I don't know how or what >> to do. > > Does this Stack Overflow thread help? It looks to me like you aren't > defining an initialization vector at all. > > http://stackoverflow.com/questions/14716338/pycrypto-how-does-the-initialization-vector-work Completely missed that Stack Overflow discussion. Thanks, Skip! That does help! Anthony From roy at panix.com Sun Aug 18 18:52:46 2013 From: roy at panix.com (Roy Smith) Date: Sun, 18 Aug 2013 18:52:46 -0400 Subject: Question about crypto References: Message-ID: In article , Anthony Papillion wrote: > I've just started working with the Crypto library and I've already run > into a wall even though I'm following a tutorial. Basically, I'm trying > to encrypt a string using AES in CBC mode. Here is the code: > > from Crypto import AES You don't say exactly what module you're using. I'm assuming https://www.dlitz.net/software/pycrypto/api/current/, yes? > import hashlib > > text_to_encrypt = 'This is a super secret encrypted message, yo!' > key = '0123456789abcdef' > mode = AES.MODE_CBC > encryptor = AES.new(key, mode) > > ciphertext = encryptor.encrypt(text) > There's a good explanation of CBC in Wikipedia: http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Cipher-block_ chaining_.28CBC.29 In a nutshell, AES-CBC works is to break the input up into 16-byte blocks. Each 16-byte block of input plaintext is encrypted to yield a 16-byte block of ciphertext. This 16-byte ciphertext block is also fed forward into the encryption step for the next block. The IV is just the feed-forward block used for the first step. So, it has to be the same size as the cipher block (i.e. 16 bytes). AES can have different length keys (128, 192, or 256 bits), but the size of the cipher block is always 256 bits. This can get confusing because most people just use a 256 bit key and incorrectly assume that the key size and block size are somehow related. They're not. When you say: > When I run the code above, I am told that the IV must be 16 bytes long. > I'm assuming that the IV (I know that means "Initialization Vector") is > either the key OR something else I can set. you're making that mistake. The key can't (in general) be used as the IV. In this particular case, you happen to have picked a key length which is the same as the block length, but that's just coincidence. What you need to do is pick a 16 byte (256 bit) IV. > Does anyone see what is wrong with the code above and could suggest > ways to make it work? I've spent the last 45 minutes googling around > and nothing comes up specific to my problem. 45 minutes is nothing when trying to understand crypto :-) I've never used this particular module, but from the docs, I think what you want to do is: iv = "SpamSpamSpamSpam" encryptor = AES.new(key, mode, iv) Keep in mind that the receiver will need the iv to decrypt your message. From papillion at gmail.com Mon Aug 19 08:35:22 2013 From: papillion at gmail.com (Anthony Papillion) Date: Mon, 19 Aug 2013 07:35:22 -0500 Subject: Question about crypto In-Reply-To: References: Message-ID: <5212110A.3050705@gmail.com> On 08/18/2013 05:52 PM, Roy Smith wrote: > In article , > Anthony Papillion wrote: > >> I've just started working with the Crypto library and I've already run >> into a wall even though I'm following a tutorial. Basically, I'm trying >> to encrypt a string using AES in CBC mode. Here is the code: >> >> from Crypto import AES > > You don't say exactly what module you're using. I'm assuming > https://www.dlitz.net/software/pycrypto/api/current/, yes? Thank you, Roy, this was very helpful. You're right, I was confusing key size with the IV and I was tying the two together in an inappropriate (wrong) way. Thanks again! Anthony From sudheer.joseph at yahoo.com Sun Aug 18 20:55:36 2013 From: sudheer.joseph at yahoo.com (Sudheer Joseph) Date: Mon, 19 Aug 2013 08:55:36 +0800 (SGT) Subject: refresing the edited python function Message-ID: <1376873736.85790.YahooMailNeo@web193402.mail.sg3.yahoo.com> Hi, I have been using ipython and ipython with qtconsole and working on a code with functions. Each time I make a modification in function? I have to quit IPTHON console (in both with and with out qt console ) and reload the function freshly. If I need to see the changed I made in the function. I tried below options del function name import the module again? by issuing "from xxx.py import yy" import xxx.py make changes reload(xxx.py) this works only if the the function in the code has same name as the code. But even this do not reflect the changes made by editing the code. So what is the standard way to update the function for further tests after an edit? with best regards, Sudheer ? *************************************************************** Sudheer Joseph Indian National Centre for Ocean Information Services Ministry of Earth Sciences, Govt. of India POST BOX NO: 21, IDA Jeedeemetla P.O. Via Pragathi Nagar,Kukatpally, Hyderabad; Pin:5000 55 Tel:+91-40-23886047(O),Fax:+91-40-23895011(O), Tel:+91-40-23044600(R),Tel:+91-40-9440832534(Mobile) E-mail:sjo.India at gmail.com;sudheer.joseph at yahoo.com Web- http://oppamthadathil.tripod.com *************************************************************** -------------- next part -------------- An HTML attachment was scrubbed... URL: From dieter at handshake.de Mon Aug 19 02:18:49 2013 From: dieter at handshake.de (dieter) Date: Mon, 19 Aug 2013 08:18:49 +0200 Subject: refresing the edited python function References: <1376873736.85790.YahooMailNeo@web193402.mail.sg3.yahoo.com> Message-ID: <87haemjj92.fsf@handshake.de> Sudheer Joseph writes: > I have been using ipython and ipython with qtconsole and working on a code with functions. Each time I make a modification in function? > > I have to quit IPTHON console (in both with and with out qt console ) and reload the function freshly. If I need to see the changed I made in the function. I tried below options > del function name > > import the module again? by issuing "from xxx.py import yy" > import xxx.py > make changes > reload(xxx.py) > this > works only if the the function in the code has same name as the code. > But even this do not reflect the changes made by editing the code. > So what is the standard way to update the function for further tests after an edit? Getting changes into a running application is difficult. Python has not been designed to make this easy. The "reload" above is one partial way to achieve something like this. The "reload" causes the module to be reloaded. If you have changed the modules code, these changes will be reflected *inside* the reloaded module. However, other modules may have imported objects from this module (as in your "from xxx.py import yy"). To see changes in those objects, they, too, must repeat the import (otherwise, they continue to use the old, unchanged object). There is an additional approach, used e.g. by "plone.reload". In this approach, the objects are modified "in place". All usage points of the modified object will see changes. However, there are (quite severe) limitations to what changes can be made "in place". Thus, this, too, does not give a complete solution. In simple cases, one of those approaches can avoid a restart after modifications. However, in general, a restart is required. From sudheer.joseph at yahoo.com Mon Aug 19 03:27:47 2013 From: sudheer.joseph at yahoo.com (Sudheer Joseph) Date: Mon, 19 Aug 2013 15:27:47 +0800 (SGT) Subject: refresing the edited python function In-Reply-To: <87haemjj92.fsf@handshake.de> References: <1376873736.85790.YahooMailNeo@web193402.mail.sg3.yahoo.com> <87haemjj92.fsf@handshake.de> Message-ID: <1376897267.58372.YahooMailNeo@web193402.mail.sg3.yahoo.com> Thank you Dieter, ???????????????????? I never thought it will be so difficult task, All I was thinking was that, I just do not know how it is done. I wonder how the code developers work in this case every time a function is modified one has to restart the console is a nightmare... Hope one day some solution will be evolved. with best regards, Sudheer >________________________________ > From: dieter >To: python-list at python.org >Sent: Monday, 19 August 2013 11:48 AM >Subject: Re: refresing the edited python function > > >Sudheer Joseph writes: > >> I have been using ipython and ipython with qtconsole and working on a code with functions. Each time I make a modification in function? >> >> I have to quit IPTHON console (in both with and with out qt console ) and reload the function freshly. If I need to see the changed I made in the function. I tried below options >> del function name >> >> import the module again? by issuing "from xxx.py import yy" >> import xxx.py >> make changes >> reload(xxx.py) >> this >>? works only if the the function in the code has same name as the code. >> But even this do not reflect the changes made by editing the code. >> So what is the standard way to update the function for further tests after an edit? > >Getting changes into a running application is difficult. >Python has not been designed to make this easy. > >The "reload" above is one partial way to achieve something like this. >The "reload" causes the module to be reloaded. If you have changed >the modules code, these changes will be reflected *inside* the reloaded >module. However, other modules may have imported objects from >this module (as? in your "from xxx.py import yy"). To see changes >in those objects, they, too, must repeat the import (otherwise, >they continue to use the old, unchanged object). > >There is an additional approach, used e.g. by "plone.reload". >In this approach, the objects are modified "in place". All usage >points of the modified object will see changes. >However, there are (quite severe) limitations to what changes >can be made "in place". Thus, this, too, does not give a complete >solution. > >In simple cases, one of those approaches can avoid a restart >after modifications. However, in general, a restart is required. > >-- >http://mail.python.org/mailman/listinfo/python-list > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Mon Aug 19 07:15:43 2013 From: davea at davea.name (Dave Angel) Date: Mon, 19 Aug 2013 11:15:43 +0000 (UTC) Subject: refresing the edited python function References: <1376873736.85790.YahooMailNeo@web193402.mail.sg3.yahoo.com> <87haemjj92.fsf@handshake.de> <1376897267.58372.YahooMailNeo@web193402.mail.sg3.yahoo.com> Message-ID: Sudheer Joseph wrote: > Thank you Dieter, > ???????????????????? I never thought it will be so difficult task, All I was thinking was that, I just do not know how it is done. I wonder how the code developers work in this case every time a function is modified one has to restart the console is a nightmare... Hope one day some solution will be evolved. > with best regards, > Sudheer > Please don't top-post, and please use text messages, rather than html mail, when posting on this list. Seems to me your problem is with ipython's IDE, not with Python. Python requires you to rerun your application when making most changes to code. But it doesn't say anything about restarting a "console," whatever that is in this context. I use Komodo IDE when i want an IDE functionality, and never restart Komodo, over hours of work. -- DaveA From sudheer.joseph at yahoo.com Mon Aug 19 07:21:29 2013 From: sudheer.joseph at yahoo.com (Sudheer Joseph) Date: Mon, 19 Aug 2013 19:21:29 +0800 (SGT) Subject: refresing the edited python function In-Reply-To: References: <1376873736.85790.YahooMailNeo@web193402.mail.sg3.yahoo.com> <87haemjj92.fsf@handshake.de> <1376897267.58372.YahooMailNeo@web193402.mail.sg3.yahoo.com> Message-ID: <1376911289.74236.YahooMailNeo@web193403.mail.sg3.yahoo.com> ----- Original Message ----- > From: Dave Angel > To: python-list at python.org > Cc: > Sent: Monday, 19 August 2013 4:45 PM > Subject: Re: refresing the edited python function > > Sudheer Joseph wrote: > >> Thank you Dieter, >> ???????????????????? I never thought it will be so difficult task, All I > was thinking was that, I just do not know how it is done. I wonder how the code > developers work in this case every time a function is modified one has to > restart the console is a nightmare... Hope one day some solution will be > evolved. >> with best regards, >> Sudheer >> > > Please don't top-post, and please use text messages, rather than html > mail, when posting on this list. > > Seems to me your problem is with ipython's IDE, not with Python.? Python > requires you to rerun your application when making most changes to code. > But it doesn't say anything about restarting a "console," whatever > that > is in this context.? I use Komodo IDE when i want an IDE functionality, > and never restart Komodo, over hours of work. > > Thank you Dave, I will make sure that when I post next time. with best regards, Sudheer From ben+python at benfinney.id.au Mon Aug 19 21:09:37 2013 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 20 Aug 2013 11:09:37 +1000 Subject: refresing the edited python function References: <1376873736.85790.YahooMailNeo@web193402.mail.sg3.yahoo.com> <87haemjj92.fsf@handshake.de> <1376897267.58372.YahooMailNeo@web193402.mail.sg3.yahoo.com> Message-ID: <7w7gfhnp66.fsf@benfinney.id.au> Sudheer Joseph writes: > I wonder how the code developers work in this case every time a > function is modified one has to restart the console is a nightmare... Developers use the interactive console for exploring, not developing. Use it to test, to confirm a suspicion about what a function call might do, to write code you know you will not run more than once. Any code you suspect might persist ? and if you don't know, expect that it might persist ? you should be writing in a module file and running as a program. > Hope one day some solution will be evolved. The time machine has brought you the solution: a text editor and the Python non-interactive session :-) -- \ ?Generally speaking, the errors in religion are dangerous; | `\ those in philosophy only ridiculous.? ?David Hume, _A Treatise | _o__) of Human Nature_, 1739 | Ben Finney From roy at panix.com Mon Aug 19 21:39:43 2013 From: roy at panix.com (Roy Smith) Date: Mon, 19 Aug 2013 21:39:43 -0400 Subject: refresing the edited python function References: <1376873736.85790.YahooMailNeo@web193402.mail.sg3.yahoo.com> <87haemjj92.fsf@handshake.de> <1376897267.58372.YahooMailNeo@web193402.mail.sg3.yahoo.com> Message-ID: In article , Ben Finney wrote: > Developers use the interactive console for exploring, not developing. I am reminded of a (very strange) guy I went to school with. He linked /dev/tty to /dev/tty.c. That allowed him to run "cc /dev/tty.c" and type in the text of a C program to compile. From dieter at handshake.de Tue Aug 20 02:30:22 2013 From: dieter at handshake.de (dieter) Date: Tue, 20 Aug 2013 08:30:22 +0200 Subject: refresing the edited python function References: <1376873736.85790.YahooMailNeo@web193402.mail.sg3.yahoo.com> <87haemjj92.fsf@handshake.de> <1376897267.58372.YahooMailNeo@web193402.mail.sg3.yahoo.com> Message-ID: <87ob8svpq9.fsf@handshake.de> Sudheer Joseph writes: > ... > ???????????????????? I wonder how the code developers work in this case every time a function is modified one has to restart the console is a nightmare... I use the "reload" approach a lot when developing isolated classes or functions and "plone.reload" for "Plone" related development. These approaches are not perfect but can avoid restarts in about 90 % of the cases. I also restart whenever something unexpected happens after a reload - as the reload approaches can have surprising effects. From piet at vanoostrum.org Wed Aug 21 12:41:59 2013 From: piet at vanoostrum.org (Piet van Oostrum) Date: Wed, 21 Aug 2013 12:41:59 -0400 Subject: refresing the edited python function References: <1376873736.85790.YahooMailNeo@web193402.mail.sg3.yahoo.com> <87haemjj92.fsf@handshake.de> <1376897267.58372.YahooMailNeo@web193402.mail.sg3.yahoo.com> Message-ID: Dave Angel writes: > > Seems to me your problem is with ipython's IDE, not with Python. Python > requires you to rerun your application when making most changes to code. > But it doesn't say anything about restarting a "console," whatever that > is in this context. I use Komodo IDE when i want an IDE functionality, > and never restart Komodo, over hours of work. > IPython's IDE just works the way alex23 described. -- Piet van Oostrum WWW: http://pietvanoostrum.com/ PGP key: [8DAE142BE17999C4] From wuwei23 at gmail.com Tue Aug 20 03:01:33 2013 From: wuwei23 at gmail.com (alex23) Date: Tue, 20 Aug 2013 17:01:33 +1000 Subject: refresing the edited python function In-Reply-To: References: Message-ID: On 19/08/2013 10:55 AM, Sudheer Joseph wrote: > I have been using ipython and ipython with qtconsole and working on a > code with functions. Each time I make a modification in function > I have to quit IPTHON console (in both with and with out qt console ) > and reload the function freshly. If I need to see the changed I made in > the function. I tried below options > del function name > import the module again by issuing "from xxx.py import yy" This doesn't re-import the module if xxx has already been imported. It simply rebinds xxx.yy to yy. > import xxx.py This also doesn't re-import the module if it has already been imported. When you import a module, or a function from a module, a module object is created and stored in sys.modules. Any subsequent 'import ' calls will return a reference to that module object, and won't reload from file at all. You can easily verify this by creating a test module 'foo' with a single line of `print('loading foo')` and then trying this from the console: In [1]: import foo loading foo In [2]: del foo In [3]: import foo In [4]: Note that you only see 'loading foo' the first time you import the module. In order to have the module loaded again rather than returning the existing reference, you would use `reload(foo)`: In [5]: reload(foo) loading foo So: in order to be able to use functions from a re-loaded module, you should always refer to them via the module object, and not import them directly: >>> import xxx >>> xxx.yy() # original code # ...modify function `yy` in your source file >>> reload(xxx) >>> xxx.yy() # new code Or: you can reload the module and then rebind the functions: >>> from xxx import yy >>> yy() # original code # ...modify function `yy` in your source file >>> reload(xxx) >>> from xxx import yy >>> yy() # new code Hope this helps. From ramit.prasad at jpmorgan.com.dmarc.invalid Tue Aug 20 11:08:48 2013 From: ramit.prasad at jpmorgan.com.dmarc.invalid (Prasad, Ramit) Date: Tue, 20 Aug 2013 15:08:48 +0000 Subject: refresing the edited python function In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF47418678D63@SCACMX008.exchad.jpmchase.net> alex23 > > On 19/08/2013 10:55 AM, Sudheer Joseph wrote: > > I have been using ipython and ipython with qtconsole and working on a > > code with functions. Each time I make a modification in function > > I have to quit IPTHON console (in both with and with out qt console ) > > and reload the function freshly. If I need to see the changed I made in > > the function. I tried below options > > > del function name > > import the module again by issuing "from xxx.py import yy" > > This doesn't re-import the module if xxx has already been imported. It > simply rebinds xxx.yy to yy. > > > import xxx.py > > This also doesn't re-import the module if it has already been imported. > > When you import a module, or a function from a module, a module object > is created and stored in sys.modules. Any subsequent 'import ' > calls will return a reference to that module object, and won't reload > from file at all. > > You can easily verify this by creating a test module 'foo' with a single > line of `print('loading foo')` and then trying this from the console: > > In [1]: import foo > loading foo > > In [2]: del foo > > In [3]: import foo > > In [4]: > > Note that you only see 'loading foo' the first time you import the > module. In order to have the module loaded again rather than returning > the existing reference, you would use `reload(foo)`: > > In [5]: reload(foo) > loading foo > > So: in order to be able to use functions from a re-loaded module, you > should always refer to them via the module object, and not import them > directly: > > >>> import xxx > >>> xxx.yy() # original code > # ...modify function `yy` in your source file > >>> reload(xxx) > >>> xxx.yy() # new code > > Or: you can reload the module and then rebind the functions: > > >>> from xxx import yy > >>> yy() # original code > # ...modify function `yy` in your source file > >>> reload(xxx) > >>> from xxx import yy > >>> yy() # new code > > Hope this helps. > > -- In Python 3 the reload built-in was moved to the imp module. So use imp.reload() instead. ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From jeanmichel at sequans.com Tue Aug 20 12:37:37 2013 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 20 Aug 2013 18:37:37 +0200 (CEST) Subject: refresing the edited python function In-Reply-To: <1376873736.85790.YahooMailNeo@web193402.mail.sg3.yahoo.com> Message-ID: <240094707.38267207.1377016657069.JavaMail.root@sequans.com> ----- Original Message ----- > Hi, > I have been using ipython and ipython with qtconsole and working on a > code with functions. Each time I make a modification in function > I have to quit IPTHON console (in both with and with out qt console ) > and reload the function freshly. If I need to see the changed I made > in the function. I tried below options > del function name > import the module again by issuing "from xxx.py import yy" > import xxx.py > make changes > reload(xxx.py) > this works only if the the function in the code has same name as the > code. But even this do not reflect the changes made by editing the > code. > So what is the standard way to update the function for further tests > after an edit? > with best regards, > Sudheer Hi, My "standard" way ;) : 1/ create a file 2/ edit the code 3/ run ipython (with %pdb on) 4/ within ipython "run myfile.py" 5/ check / introspect /debug 6/ change the code 7/ exit ipython 8/ reenter ipython 9/ using the ipython shell history, reexecute the file (2 key press) and go back to 5/ I used to reload my objects, it's been useful until one time when I lost a lot of time because of some nasty side effect. In the end it's not worth it. Always quit the shell, always. 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 sudheer.joseph at yahoo.com Wed Aug 21 02:26:54 2013 From: sudheer.joseph at yahoo.com (Sudheer Joseph) Date: Wed, 21 Aug 2013 14:26:54 +0800 (SGT) Subject: refresing the edited python function In-Reply-To: <240094707.38267207.1377016657069.JavaMail.root@sequans.com> References: <1376873736.85790.YahooMailNeo@web193402.mail.sg3.yahoo.com> <240094707.38267207.1377016657069.JavaMail.root@sequans.com> Message-ID: <1377066414.77885.YahooMailNeo@web193403.mail.sg3.yahoo.com> Thank you, ??????????????? But I wish if there was a foolproof reload with best regards, Sudheer ----- Original Message ----- > From: Jean-Michel Pichavant > To: Sudheer Joseph > Cc: python-list at python.org > Sent: Tuesday, 20 August 2013 10:07 PM > Subject: Re: refresing the edited python function > > > ----- Original Message ----- > >> Hi, >> I have been using ipython and ipython with qtconsole and working on a >> code with functions. Each time I make a modification in function > >> I have to quit IPTHON console (in both with and with out qt console ) >> and reload the function freshly. If I need to see the changed I made >> in the function. I tried below options >> del function name > >> import the module again by issuing "from xxx.py import yy" >> import xxx.py >> make changes >> reload(xxx.py) >> this works only if the the function in the code has same name as the >> code. But even this do not reflect the changes made by editing the >> code. >> So what is the standard way to update the function for further tests >> after an edit? >> with best regards, >> Sudheer > > Hi, > > My "standard" way ;) : > 1/ create a file > 2/ edit the code > 3/ run ipython (with %pdb on) > 4/ within ipython "run myfile.py" > 5/ check / introspect /debug > 6/ change the code > 7/ exit ipython > 8/ reenter ipython > 9/ using the ipython shell history, reexecute the file (2 key press) and go back > to 5/ > > I used to reload my objects, it's been useful until one time when I lost a > lot of time because of some nasty side effect. In the end it's not worth it. > Always quit the shell, always. > > 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 rosuav at gmail.com Wed Aug 21 14:23:56 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 22 Aug 2013 04:23:56 +1000 Subject: refresing the edited python function In-Reply-To: <1377066414.77885.YahooMailNeo@web193403.mail.sg3.yahoo.com> References: <1376873736.85790.YahooMailNeo@web193402.mail.sg3.yahoo.com> <240094707.38267207.1377016657069.JavaMail.root@sequans.com> <1377066414.77885.YahooMailNeo@web193403.mail.sg3.yahoo.com> Message-ID: On Wed, Aug 21, 2013 at 4:26 PM, Sudheer Joseph wrote: > > > Thank you, > But I wish if there was a foolproof reload > with best regards, > Sudheer There isn't, any more than there's a foolproof way to prevent top-posting. Some languages are designed to handle code reload; others simply aren't. Python is one of the latter. And even when you're using a language like Pike, you really have to design your application around code reload; so it's a feature of specific apps (like MUDs) that will be running for years on end without restarting, rather than something that you want all the time. Otherwise, you run the risk of having internal data structures and code out of sync - and if you're not keeping anything significant from the previous run, then why are you reloading code instead of restarting the app? ChrisA From josiah.carlson at gmail.com Sun Aug 18 23:30:15 2013 From: josiah.carlson at gmail.com (Josiah Carlson) Date: Sun, 18 Aug 2013 20:30:15 -0700 Subject: ANN: rom 0.18 - Redis object mapper for Python Message-ID: Hey everyone, As time progresses, so does my Redis object mapper. The "rom" package is a Redis object mapper for Python. It sports an interface similar to Django's ORM, SQLAlchemy + Elixir, or Appengine's datastore. The changelog for recent releases can be seen below my signature. You can find the package at: https://www.github.com/josiahcarlson/rom https://pypi.python.org/pypi/rom And docs can be found at: http://pythonhosted.org/rom/ Please CC me on any replies if you have any questions or comments. Thank you, - Josiah #----------------------------------- 0.18 ------------------------------------ [fixed] Thanks to a bug report by https://github.com/MickeyKim , was notified and received an interim patch for a bug that could cause deleted entities to be resurrected on session.commit() or session.flush() . This has now been fixed and a testcase has been added. #----------------------------------- 0.17 ------------------------------------ [added] LGPL 3 licensing option. #----------------------------------- 0.16 ------------------------------------ [added] Thanks to a feature request from https://github.com/jrsmith , Boolean column support is now available. [added] DateTime, Date, and Time column types because I was already adding Boolean columns. #----------------------------------- 0.15 ------------------------------------ [fixed] Thanks to https://github.com/MickeyKim who fixed Json columns so that they round-trip after multiple saves. -------------- next part -------------- An HTML attachment was scrubbed... URL: From to.mueller13 at gmail.com Mon Aug 19 05:19:36 2013 From: to.mueller13 at gmail.com (=?ISO-8859-1?Q?Tobias_M=FCller?=) Date: Mon, 19 Aug 2013 11:19:36 +0200 Subject: NodeTransformer: how to remove nodes? Message-ID: Hi I'm facing an issue with NodeTransformer, a tool used for Python AST manipulations. Last week I posted on stackoverflow.com, but there are no responses yet. Maybe someone reading the mailing list can have a look and leave me a response here or over there? http://stackoverflow.com/questions/18275662/python-nodetransformer-how-to-remove-nodes Thanks Tobias Mueller -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Mon Aug 19 07:04:27 2013 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 19 Aug 2013 12:04:27 +0100 Subject: NodeTransformer: how to remove nodes? In-Reply-To: References: Message-ID: On Mon, Aug 19, 2013 at 10:19 AM, Tobias M?ller wrote: > Hi > > I'm facing an issue with NodeTransformer, a tool used for Python AST > manipulations. > > Last week I posted on stackoverflow.com, but there are no responses yet. > Maybe someone reading the mailing list can have a look and leave me a > response here or over there? > > http://stackoverflow.com/questions/18275662/python-nodetransformer-how-to-remove-nodes I'm not familiar with NodeTransformer, but by the look of things, your visit_For is being called (indirectly) by your own iterate_children, which then ignores the return value. To apply changes like this, either remove your definition of generic_visit, or have it pass through to super(): def generic_visit(self, node): """ default behaviour """ print("visiting: "+node.__class__.__name__) return super().generic_visit(node) It's the code for NodeTransformer.generic_visit that does the replacements (check out Lib/ast.py - it's down the very bottom in the 3.3 that I have), so if you bypass that, no replacement happens. ChrisA From __peter__ at web.de Mon Aug 19 07:21:51 2013 From: __peter__ at web.de (Peter Otten) Date: Mon, 19 Aug 2013 13:21:51 +0200 Subject: NodeTransformer: how to remove nodes? References: Message-ID: Tobias M?ller wrote: > I'm facing an issue with NodeTransformer, a tool used for Python AST > manipulations. > > Last week I posted on stackoverflow.com, but there are no responses yet. > Maybe someone reading the mailing list can have a look and leave me a > response here or over there? > > http://stackoverflow.com/questions/18275662/python-nodetransformer-how-to- remove-nodes As Chris says, you are overriding too much of the generic behaviour. Here is a working demo: import ast class MyTransformer(ast.NodeTransformer): def visit_For(self, node): """ For nodes: replace with nothing """ print("removing a For node") return None source = """ l = [0, 1, 2, 3] total = 0 for i in l: total += i print(total) """ transformer = MyTransformer() module = ast.parse(source) transformer.visit(module) codeobj = compile(module, '', 'exec') exec(codeobj) From to.mueller13 at gmail.com Wed Aug 21 10:46:39 2013 From: to.mueller13 at gmail.com (=?ISO-8859-1?Q?Tobias_M=FCller?=) Date: Wed, 21 Aug 2013 16:46:39 +0200 Subject: NodeTransformer: how to remove nodes? In-Reply-To: References: Message-ID: Thanks Chris and Peter. I already went further along. No more issues so far. 2013/8/19 Peter Otten <__peter__ at web.de> > Tobias M?ller wrote: > > > I'm facing an issue with NodeTransformer, a tool used for Python AST > > manipulations. > > > > Last week I posted on stackoverflow.com, but there are no responses yet. > > Maybe someone reading the mailing list can have a look and leave me a > > response here or over there? > > > > > http://stackoverflow.com/questions/18275662/python-nodetransformer-how-to- > remove-nodes > > As Chris says, you are overriding too much of the generic behaviour. Here > is > a working demo: > > import ast > > class MyTransformer(ast.NodeTransformer): > def visit_For(self, node): > """ > For nodes: replace with nothing > """ > print("removing a For node") > return None > > > source = """ > l = [0, 1, 2, 3] > > total = 0 > > for i in l: > total += i > > print(total) > """ > > transformer = MyTransformer() > module = ast.parse(source) > transformer.visit(module) > codeobj = compile(module, '', 'exec') > exec(codeobj) > > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From th982a at googlemail.com Mon Aug 19 12:48:19 2013 From: th982a at googlemail.com (Tamer Higazi) Date: Mon, 19 Aug 2013 18:48:19 +0200 Subject: opposite of __init__.py Message-ID: <52124C53.1040401@googlemail.com> Hi people! I have asked myself a question, if there is a opposite of "__init__.py" like "__del__.py" ?! I want, that when the application ends, certain functions are executed. I know I could make a constructor and a destructor, but I simply want to know if there is a opposite.... Thanks Tamer From ian.g.kelly at gmail.com Mon Aug 19 13:06:49 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 19 Aug 2013 11:06:49 -0600 Subject: opposite of __init__.py In-Reply-To: <52124C53.1040401@googlemail.com> References: <52124C53.1040401@googlemail.com> Message-ID: Yes, see the atexit module: http://docs.python.org/3/library/atexit.html On Mon, Aug 19, 2013 at 10:48 AM, Tamer Higazi wrote: > Hi people! > > I have asked myself a question, if there is a opposite of "__init__.py" > like "__del__.py" ?! > > I want, that when the application ends, certain functions are executed. > I know I could make a constructor and a destructor, but I simply want to > know if there is a opposite.... > > > Thanks > > > > Tamer > -- > http://mail.python.org/mailman/listinfo/python-list From steve+comp.lang.python at pearwood.info Mon Aug 19 13:13:38 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 19 Aug 2013 17:13:38 GMT Subject: opposite of __init__.py References: Message-ID: <52125242$0$29986$c3e8da3$5496439d@news.astraweb.com> On Mon, 19 Aug 2013 18:48:19 +0200, Tamer Higazi wrote: > Hi people! > > I have asked myself a question, if there is a opposite of "__init__.py" > like "__del__.py" ?! No. If you want to run code when your application is shutting down, run it just before your code finishes: def main(): do_this() do_that() if __name__ == '__main__': main() shutdown() If you care about code running even if an exception takes place: if __name__ == '__main__': try: main() finally: shutdown() -- Steven From davea at davea.name Mon Aug 19 14:48:36 2013 From: davea at davea.name (Dave Angel) Date: Mon, 19 Aug 2013 18:48:36 +0000 (UTC) Subject: opposite of __init__.py References: <52124C53.1040401@googlemail.com> Message-ID: Tamer Higazi wrote: > Hi people! > > I have asked myself a question, if there is a opposite of "__init__.py" > like "__del__.py" ?! Others have answered your question, but I wanted to correct a misunderstanding: > > I want, that when the application ends, certain functions are executed. > I know I could make a constructor and a destructor, Not sure what you're talkiing about, but you cannot be sure that an object's destructor ( __del__() ) will ever be called. If you want to make sure that a particular object gets processed, one approach is the with- syntax. > but I simply want to > know if there is a opposite.... > -- DaveA From uwe.rangs at fernuni-hagen.de Mon Aug 19 16:14:04 2013 From: uwe.rangs at fernuni-hagen.de (Uwe Rangs) Date: Mon, 19 Aug 2013 22:14:04 +0200 Subject: default python os x Message-ID: Hello, how can I change the default version of python with os x 10.7. Thanks, Uwe From uwe.rangs at fernuni-hagen.de Mon Aug 19 16:24:46 2013 From: uwe.rangs at fernuni-hagen.de (Uwe Rangs) Date: Mon, 19 Aug 2013 22:24:46 +0200 Subject: default python os x References: Message-ID: My workflow at the moment is to set a link: mv python python_old ln -s /usr/local/bin/python3.2 python But is this a good idea? -- Uwe On 2013-08-19 20:14:04 +0000, Uwe Rangs said: > Hello, > > how can I change the default version of python with os x 10.7. > > Thanks, > Uwe From steve at pearwood.info Tue Aug 20 01:39:56 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 20 Aug 2013 05:39:56 GMT Subject: default python os x References: Message-ID: <5213012b$0$29885$c3e8da3$5496439d@news.astraweb.com> On Mon, 19 Aug 2013 22:24:46 +0200, Uwe Rangs wrote: > My workflow at the moment is to set a link: mv python python_old > ln -s /usr/local/bin/python3.2 python > > But is this a good idea? You should never change the system Python, since that runs the risk of breaking system tools that expect a particular version. If there are any system tools that run "python", they may break. Instead, set a personal alias. For example, in my ~/.bashrc file, I have the following: export PYTHONPATH="/home/steve/python/:/home/steve/python/utilities" export PYTHONSTARTUP=/home/steve/python/utilities/startup.py alias python=python2.7 alias python3=python3.3 alias python1.5='env -u PYTHONSTARTUP python1.5' alias python2.0='env -u PYTHONSTARTUP python2.0' alias python2.1='env -u PYTHONSTARTUP python2.1' alias python2.2='env -u PYTHONSTARTUP python2.2' alias python2.3='env -u PYTHONSTARTUP python2.3' alias python3.4="env -u PYTHONPATH ~/python/cpython/python" So, for me, and me alone, typing "python" refers to Python 2.7 instead of the system Python, which happens to be 2.4 on my server and 2.6 on my laptop. "python1.5" etc unsets the startup file environment variable, since my startup file assumes 2.4 or better. "python3.4" points to a local copy in my home directory. Otherwise, python3.3, python3.2, etc. work as expected. -- Steven From uwe.rangs at fernuni-hagen.de Tue Aug 20 03:01:06 2013 From: uwe.rangs at fernuni-hagen.de (Uwe Rangs) Date: Tue, 20 Aug 2013 09:01:06 +0200 Subject: default python os x References: <5213012b$0$29885$c3e8da3$5496439d@news.astraweb.com> Message-ID: Ah, I see. Thank you! On 2013-08-20 05:39:56 +0000, Steven D'Aprano said: > On Mon, 19 Aug 2013 22:24:46 +0200, Uwe Rangs wrote: > >> My workflow at the moment is to set a link: mv python python_old >> ln -s /usr/local/bin/python3.2 python >> >> But is this a good idea? > > > You should never change the system Python, since that runs the risk of > breaking system tools that expect a particular version. If there are any > system tools that run "python", they may break. > > Instead, set a personal alias. For example, in my ~/.bashrc > file, I have the following: > > > export PYTHONPATH="/home/steve/python/:/home/steve/python/utilities" > export PYTHONSTARTUP=/home/steve/python/utilities/startup.py > alias python=python2.7 > alias python3=python3.3 > alias python1.5='env -u PYTHONSTARTUP python1.5' > alias python2.0='env -u PYTHONSTARTUP python2.0' > alias python2.1='env -u PYTHONSTARTUP python2.1' > alias python2.2='env -u PYTHONSTARTUP python2.2' > alias python2.3='env -u PYTHONSTARTUP python2.3' > alias python3.4="env -u PYTHONPATH ~/python/cpython/python" > > > So, for me, and me alone, typing "python" refers to Python 2.7 instead of > the system Python, which happens to be 2.4 on my server and 2.6 on my > laptop. "python1.5" etc unsets the startup file environment variable, > since my startup file assumes 2.4 or better. "python3.4" points to a > local copy in my home directory. Otherwise, python3.3, python3.2, etc. > work as expected. From cs at zip.com.au Wed Aug 21 06:22:11 2013 From: cs at zip.com.au (Cameron Simpson) Date: Wed, 21 Aug 2013 20:22:11 +1000 Subject: default python os x In-Reply-To: References: Message-ID: <20130821102211.GA7732@cskk.homeip.net> On 20Aug2013 09:01, Uwe Rangs wrote: | Ah, I see. Thank you! Please don't top post. Thanks. | On 2013-08-20 05:39:56 +0000, Steven D'Aprano said: | >alias python1.5='env -u PYTHONSTARTUP python1.5' I should point out that -u is a GNU env feature. It is not portable, and in particular OSX "env" does not have it. A shell function can do the same though: py20() { ( unset PYTHONSTARTUP exec python2.0 ${1+"$@"} ) } I've said py20 instead of python2.0 primarily because bash is a bit... picky about function names and rejected the ".". Cheers, -- Cameron Simpson But pessimism IS realism! - D.L.Bahr From joel.goldstick at gmail.com Mon Aug 19 16:46:29 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 19 Aug 2013 16:46:29 -0400 Subject: default python os x In-Reply-To: References: Message-ID: On Mon, Aug 19, 2013 at 4:14 PM, Uwe Rangs wrote: > Hello, > > how can I change the default version of python with os x 10.7. > > Thanks, > Uwe > > -- > http://mail.python.org/mailman/listinfo/python-list You might want to look at this: http://stackoverflow.com/questions/5846167/how-to-change-default-python-version -- Joel Goldstick http://joelgoldstick.com From fabiosantosart at gmail.com Mon Aug 19 19:49:16 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Tue, 20 Aug 2013 00:49:16 +0100 Subject: Is this PEP-able? (syntax for functools.partial-like functionality) Message-ID: I had an idea for a handy syntax which I thought of while designing a language for fun. This would be a syntax for creating, from a function, a function which is just like it but with some parameters pre-filled. The usage is much like functools.partials, and my proposed syntax is like this: def spam(a, b, c): ... spam_with_defaults = spam{1, 2} Now calling spam_with_defaults is just like calling spam, but it only needs one argument since a and b were set to 1 and 2, respectively. spam_with_defaults(3) # same as calling spam(1, 2, 3) This would also work with keyword arguments, E.G. spam{c=3} would return a callable which would only need the arguments a and b. This is just the plain old functools.partial functionality, but of course I won't stop here. Still on our spam function: spam_require_b = spam{2, *, 3} spam_require_ab = spam{*, 3} spam_require_a(1) # same as spam(1, 2, 3) spam_require_ab(1, 2) # same as above The * sign means that the function takes positional arguments which will be added in place of the star. This is how we would do spam_require_b in pure python: def spam_require_b(*args, **kwargs): return spam(*([1] + args + [2]), **kwargs) Or, since we know it's only one argument, spam_require_b = lambda b: spam(1, b, 3) I also propose unpacking: spam_unpacking = spam{1, (*, *)} c = map(spam_unpacking, some_dict.items()) (Although this syntax isn't final), and receiving specific keyword arguments. spam_kw = spam{a, b, c=*} The use cases this is intended to serve are mostly iteration related. There is the case for being good plumbing for functions such as map, sorted, filter and itertools.takewhile. lines = filter(str.startswith{*, '#'}, open('file.cfg')) lines = filter(bool, map(str.strip, lines)) config = dict(map(str.split{*, '=', 1}, lines)) A secondary use case is the creation of aliases. def baz(self, callback): respond = callback{instance=self} ... What do you think? PS: yes, I realized that I am proposing the addition of braces to the language syntax. -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Tue Aug 20 02:16:48 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 20 Aug 2013 06:16:48 GMT Subject: Is this PEP-able? (syntax for functools.partial-like functionality) References: Message-ID: <521309d0$0$29885$c3e8da3$5496439d@news.astraweb.com> On Tue, 20 Aug 2013 00:49:16 +0100, F?bio Santos wrote: > I had an idea for a handy syntax which I thought of while designing a > language for fun. This would be a syntax for creating, from a function, > a function which is just like it but with some parameters pre-filled. > The usage is much like functools.partials, and my proposed syntax is > like this: > > def spam(a, b, c): > ... > > spam_with_defaults = spam{1, 2} Handy it may be, but why is this usage important enough to deserve new syntax? The barrier to entry for new syntax is very high. For example, Enums are a common, and standard, feature in many programming languages. Enums will be introduced to Python in 3.4, but even they don't get special syntax. -- Steven From fabiosantosart at gmail.com Tue Aug 20 04:28:29 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Tue, 20 Aug 2013 09:28:29 +0100 Subject: Is this PEP-able? (syntax for functools.partial-like functionality) In-Reply-To: <521309d0$0$29885$c3e8da3$5496439d@news.astraweb.com> References: <521309d0$0$29885$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 20 Aug 2013 07:22, "Steven D'Aprano" wrote: > > On Tue, 20 Aug 2013 00:49:16 +0100, F?bio Santos wrote: > > > I had an idea for a handy syntax which I thought of while designing a > > language for fun. This would be a syntax for creating, from a function, > > a function which is just like it but with some parameters pre-filled. > > The usage is much like functools.partials, and my proposed syntax is > > like this: > > > > def spam(a, b, c): > > ... > > > > spam_with_defaults = spam{1, 2} > > > Handy it may be, but why is this usage important enough to deserve new > syntax? The barrier to entry for new syntax is very high. For example, > Enums are a common, and standard, feature in many programming languages. > Enums will be introduced to Python in 3.4, but even they don't get > special syntax. > > > > -- > Steven I do realize that syntax in python is practically written in stone, but I have seen changes come by if they have good reasons. For example, keyword-only argument syntax was added. I suggested this because I thought it would be the most practical way to create partial functions. Lambda is too verbose and kind of ugly, and its purpose is not to create partials, functools.partial does not allow argument "insertion" (the star thing) nor unpacking. Maybe I saw this as common usage but it is really a special case. I see myself needing this kind of advanced partials _very_ often. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ian.g.kelly at gmail.com Tue Aug 20 05:11:41 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 20 Aug 2013 03:11:41 -0600 Subject: Is this PEP-able? (syntax for functools.partial-like functionality) In-Reply-To: References: <521309d0$0$29885$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, Aug 20, 2013 at 2:28 AM, F?bio Santos wrote: > I do realize that syntax in python is practically written in stone, but I > have seen changes come by if they have good reasons. For example, > keyword-only argument syntax was added. > > I suggested this because I thought it would be the most practical way to > create partial functions. Lambda is too verbose and kind of ugly, and its > purpose is not to create partials, functools.partial does not allow argument > "insertion" (the star thing) nor unpacking. I think that if you're doing argument insertion, then your partial application is overly complex in the way that it modifies the original argspec. You should be looking at it as a new function, not as a partial application that prevents you from easily supplying a doc string for it. If you want to specify an arbitrary combination of arguments in your partial application, you can already do that using keyword arguments. The exception would be if the function being partially applied takes a *args argument and you want to specify some of those arguments using partial without specifying earlier arguments. That seems like an abuse of partial application to me. Tuple unpacking in function signatures was a feature in Python 2, and it was intentionally removed in Python 3, so this is very unlikely to happen. See PEP 3113. Besides which, the syntax you suggest for this is unintuitive. spam_unpacking = spam{1, (*, *)} To me, this reads that spam_unpacking will take two positional arguments that will be packed into the second argument sent to spam (b), and that the third argument to spam (c) is not specified here. I don't think that's what you intended. From fabiosantosart at gmail.com Tue Aug 20 05:25:39 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Tue, 20 Aug 2013 10:25:39 +0100 Subject: Is this PEP-able? (syntax for functools.partial-like functionality) In-Reply-To: References: <521309d0$0$29885$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 20 Aug 2013 10:14, "Ian Kelly" wrote: > > On Tue, Aug 20, 2013 at 2:28 AM, F?bio Santos wrote: > > I do realize that syntax in python is practically written in stone, but I > > have seen changes come by if they have good reasons. For example, > > keyword-only argument syntax was added. > > > > I suggested this because I thought it would be the most practical way to > > create partial functions. Lambda is too verbose and kind of ugly, and its > > purpose is not to create partials, functools.partial does not allow argument > > "insertion" (the star thing) nor unpacking. > > I think that if you're doing argument insertion, then your partial > application is overly complex in the way that it modifies the original > argspec. You should be looking at it as a new function, not as a > partial application that prevents you from easily supplying a doc > string for it. If you want to specify an arbitrary combination of > arguments in your partial application, you can already do that using > keyword arguments. The exception would be if the function being > partially applied takes a *args argument and you want to specify some > of those arguments using partial without specifying earlier arguments. > That seems like an abuse of partial application to me. > > Tuple unpacking in function signatures was a feature in Python 2, and > it was intentionally removed in Python 3, so this is very unlikely to > happen. See PEP 3113. Besides which, the syntax you suggest for this > is unintuitive. > > spam_unpacking = spam{1, (*, *)} > > To me, this reads that spam_unpacking will take two positional > arguments that will be packed into the second argument sent to spam > (b), and that the third argument to spam (c) is not specified here. I > don't think that's what you intended. No, that syntax was meant to take a tuple and break it into two arguments. It does read terribly. Anyway, good points were made and I have seen the light. I've changed my mind. This is a bad idea. -------------- next part -------------- An HTML attachment was scrubbed... URL: From leafonsword at gmail.com Tue Aug 20 04:14:27 2013 From: leafonsword at gmail.com (leafonsword at gmail.com) Date: Tue, 20 Aug 2013 01:14:27 -0700 (PDT) Subject: Beginer of python,could I write a personal blog using cherrtpy? Message-ID: <67b5787f-71aa-4089-8f40-008322c35416@googlegroups.com> I'v seen ?byte of python??head first python?~ Could I read cherrypy's offical document and then I write a personal blog using cherrypy? From steve at pearwood.info Tue Aug 20 05:02:24 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 20 Aug 2013 09:02:24 GMT Subject: Beginer of python,could I write a personal blog using cherrtpy? References: <67b5787f-71aa-4089-8f40-008322c35416@googlegroups.com> Message-ID: <521330a0$0$29885$c3e8da3$5496439d@news.astraweb.com> On Tue, 20 Aug 2013 01:14:27 -0700, leafonsword wrote: > I'v seen ?byte of python??head first python?~ Could I read cherrypy's > offical document and then I write a personal blog using cherrypy? It is certainly possible to create a blog using CherryPy. http://docs.cherrypy.org/stable/appendix/success.html -- Steven From eschneider92 at comcast.net Tue Aug 20 04:35:05 2013 From: eschneider92 at comcast.net (eschneider92 at comcast.net) Date: Tue, 20 Aug 2013 01:35:05 -0700 (PDT) Subject: Replace blanks with letter Message-ID: <2bdbd16f-a676-4973-9866-db93b1b9cd9b@googlegroups.com> I'm trying to replace the blank(_) with the letter typed in by the user, in the appropriate blank(_) spot where the letter should be (where is in the letters list). letters='abcdefg' blanks='_ '*len(letters) print('type letter from a to g') print(blanks) input1=input() for i in range(len(letters)): if letters[i] in input1: blanks = blanks[:i] + letters[i] + blanks[i+1:] What am I doing wrong in this code? Thanks Eric From fabiosantosart at gmail.com Tue Aug 20 05:16:55 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Tue, 20 Aug 2013 10:16:55 +0100 Subject: Replace blanks with letter In-Reply-To: <2bdbd16f-a676-4973-9866-db93b1b9cd9b@googlegroups.com> References: <2bdbd16f-a676-4973-9866-db93b1b9cd9b@googlegroups.com> Message-ID: On 20 Aug 2013 09:42, wrote: > > I'm trying to replace the blank(_) with the letter typed in by the user, in the appropriate blank(_) spot where the letter should be (where is in the letters list). > > letters='abcdefg' > blanks='_ '*len(letters) > print('type letter from a to g') > print(blanks) > input1=input() > for i in range(len(letters)): > if letters[i] in input1: > blanks = blanks[:i] + letters[i] + blanks[i+1:] > > > What am I doing wrong in this code? > > Thanks > Eric First, don't use range(len(iterable)). It's bad practise, and you will have to use iterable[i] all the time. Try for i, letter in enumerate(letters): If you are modifying a string in-place, you could change it into a list, then back. blankslst = list(blanks) ... blankslst[i] = letters[i] # or 'letter' if you used enumerate() ... blanks = ''.join(blankslst) Now, why are you not printing the `blanks` string again? -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Tue Aug 20 05:19:31 2013 From: __peter__ at web.de (Peter Otten) Date: Tue, 20 Aug 2013 11:19:31 +0200 Subject: Replace blanks with letter References: <2bdbd16f-a676-4973-9866-db93b1b9cd9b@googlegroups.com> Message-ID: eschneider92 at comcast.net wrote: > I'm trying to replace the blank(_) with the letter typed in by the user, > in the appropriate blank(_) spot where the letter should be (where is in > the letters list). > > letters='abcdefg' > blanks='_ '*len(letters) > print('type letter from a to g') > print(blanks) > input1=input() > for i in range(len(letters)): > if letters[i] in input1: > blanks = blanks[:i] + letters[i] + blanks[i+1:] > > > What am I doing wrong in this code? `blanks` has two chars per letter in `letters`. If you change the initial value to blanks = "_" * len(letters) your code should work. If you think the output looks better with spaces you have to adjust the indices -- or you add spaces for printing only with print(" ".join(blanks)) From eschneider92 at comcast.net Tue Aug 20 16:45:31 2013 From: eschneider92 at comcast.net (eschneider92 at comcast.net) Date: Tue, 20 Aug 2013 13:45:31 -0700 (PDT) Subject: Replace blanks with letter In-Reply-To: <2bdbd16f-a676-4973-9866-db93b1b9cd9b@googlegroups.com> References: <2bdbd16f-a676-4973-9866-db93b1b9cd9b@googlegroups.com> Message-ID: <102afb35-1c8d-4957-9e9c-14b17429437b@googlegroups.com> Is there also a way to have the code remember what I typed and not stop after the first letter the user types? For example, if I typed 'a' once, thus returning 'a______', and then typed in 'b', I want the code to return 'ab_____' and so on. I wasn't clear about this part in my original post. Thanks for the help. Eric From davea at davea.name Tue Aug 20 17:54:11 2013 From: davea at davea.name (Dave Angel) Date: Tue, 20 Aug 2013 21:54:11 +0000 (UTC) Subject: Replace blanks with letter References: <2bdbd16f-a676-4973-9866-db93b1b9cd9b@googlegroups.com> <102afb35-1c8d-4957-9e9c-14b17429437b@googlegroups.com> Message-ID: eschneider92 at comcast.net wrote: > Is there also a way to have the code remember what I typed and not stop after the first letter the user types? For example, if I typed 'a' once, thus returning 'a______', and then typed in 'b', I want the code to return 'ab_____' and so on. I wasn't clear about this part in my original post. Thanks for the help. First you need some kind of loop. There's only one call to input() in your present code, so the question is meaningless. Roughly speaking, make sure the following line is NOT in your loop: blanks='_ '*len(letters) -- DaveA From eschneider92 at comcast.net Wed Aug 21 03:49:42 2013 From: eschneider92 at comcast.net (eschneider92 at comcast.net) Date: Wed, 21 Aug 2013 00:49:42 -0700 (PDT) Subject: Replace blanks with letter In-Reply-To: <2bdbd16f-a676-4973-9866-db93b1b9cd9b@googlegroups.com> References: <2bdbd16f-a676-4973-9866-db93b1b9cd9b@googlegroups.com> Message-ID: <89146bb1-fb60-4746-93e2-6cb59cfbc432@googlegroups.com> Thanks. I am running into a bunch of problems with the following code, all of which are clear when running the program import random letters='abcdefg' blanks='_'*len(letters) print('type letters from a to g') print(blanks) for i in range(len(letters)): if letters[i] in input(): blanks = blanks[:i] + letters[i] + blanks[i+1:] print(blanks) If anyone could post an example of how to correctly code this, I would appreciate it. I can't seem to figure it out. I'll definitely heed Fabio's advice for future reference, but I don't think it's related to the problems I'm currently experiencing. If it is, and I'm just not getting it (most likely the case), please post an example of how to implement his code advice in doing what I wish to accomplish here. From rosuav at gmail.com Wed Aug 21 05:24:32 2013 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 21 Aug 2013 19:24:32 +1000 Subject: Replace blanks with letter In-Reply-To: <89146bb1-fb60-4746-93e2-6cb59cfbc432@googlegroups.com> References: <2bdbd16f-a676-4973-9866-db93b1b9cd9b@googlegroups.com> <89146bb1-fb60-4746-93e2-6cb59cfbc432@googlegroups.com> Message-ID: On Wed, Aug 21, 2013 at 5:49 PM, wrote: > Thanks. I am running into a bunch of problems with the following code, all of which are clear when running the program Some of us don't have time to just execute arbitrary code in some safe environment, so we'd REALLY rather you paste in the exception traceback (if it's throwing one), or explain what it ought to be doing that it isn't doing, or in whatever other way show what the problems actually are. Remember, what's obvious to you isn't obvious to us; what you see as an obvious problem might actually be correct behaviour, so without knowing your expectations, we can't pinpoint the trouble. ChrisA From davea at davea.name Wed Aug 21 07:42:24 2013 From: davea at davea.name (Dave Angel) Date: Wed, 21 Aug 2013 11:42:24 +0000 (UTC) Subject: Replace blanks with letter References: <2bdbd16f-a676-4973-9866-db93b1b9cd9b@googlegroups.com> <89146bb1-fb60-4746-93e2-6cb59cfbc432@googlegroups.com> Message-ID: eschneider92 at comcast.net wrote: > Thanks. I am running into a bunch of problems with the following code, all of which are clear when running the program > > import random > letters='abcdefg' > blanks='_'*len(letters) > print('type letters from a to g') > print(blanks) > for i in range(len(letters)): > if letters[i] in input(): > blanks = blanks[:i] + letters[i] + blanks[i+1:] > print(blanks) > > If anyone could post an example of how to correctly code this, I would appreciate it. I can't seem to figure it out. > > I'll definitely heed Fabio's advice for future reference, but I don't think it's related to the problems I'm currently experiencing. If it is, and I'm just not getting it (most likely the case), please post an example of how to implement his code advice in doing what I wish to accomplish here. Nowhere have you told us just what the homework assignment was. Depending on the goal, this could be "fixed" in various ways. As it stands, you are asking the user 7 times to type in the letters from a to g. So long as he responds each time the same way, it'll gradually fill in the letters from left to right, and end up with all seven showing. In fact, it'll do that even if the user just types the particular single letter you're asking for. So in my last response below, I typed a string that didn't have all 7, but it did have a g, so that was good enough. davea at think2:~/temppython$ python3.3 eric.py type letters from a to g _______ abcdefg a______ abcdefg ab_____ abcdefg abc____ abcdefg abcd___ abcdefg abcde__ agcdbfe abcdef_ aggecca abcdefg davea at think2:~/temppython$ Maybe the problem is that you don't tell the user whether he has succeeded or not. To tell that, just stick a test at the end, outside the for-loop. if blanks == letters: print("Good job") else: print("You lose, run again, and guess what I wanted") -- DaveA From gordon at panix.com Wed Aug 21 10:35:40 2013 From: gordon at panix.com (John Gordon) Date: Wed, 21 Aug 2013 14:35:40 +0000 (UTC) Subject: Replace blanks with letter References: <2bdbd16f-a676-4973-9866-db93b1b9cd9b@googlegroups.com> <89146bb1-fb60-4746-93e2-6cb59cfbc432@googlegroups.com> Message-ID: In <89146bb1-fb60-4746-93e2-6cb59cfbc432 at googlegroups.com> eschneider92 at comcast.net writes: > Thanks. I am running into a bunch of problems with the following code, all > of which are clear when running the program No, they're not clear. We can see what the code does, obviously, but we don't know what it's *supposed* to do. -- 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 eschneider92 at comcast.net Thu Aug 22 18:42:35 2013 From: eschneider92 at comcast.net (eschneider92 at comcast.net) Date: Thu, 22 Aug 2013 15:42:35 -0700 (PDT) Subject: Replace blanks with letter In-Reply-To: <2bdbd16f-a676-4973-9866-db93b1b9cd9b@googlegroups.com> References: <2bdbd16f-a676-4973-9866-db93b1b9cd9b@googlegroups.com> Message-ID: <060f97db-188d-4078-9d87-c70230914ff1@googlegroups.com> I wanted the program to stop only after all the letters were typed; why in the world would I try to write a program with blanks for each letter that seem intended to be filled, only to have it stop if the last letter is typed, or have to type each letter so many times until its processed? If you ran the code, my problems, as well as the intended goal, should become obvious. Also, why wouldn't someone who's willing to help me not run the code (takes a few seconds btw) I'm having trouble with in order to diagnose its faults, yet you have the time to write how you won't run it? If I did have a teacher to help me, this would be the last place I'd come to for help. It should be easy to deduce what I intended this program to do. Please no one respond being as I am done here, I just had to vent, but instead report it if you want. From davea at davea.name Thu Aug 22 21:28:28 2013 From: davea at davea.name (Dave Angel) Date: Fri, 23 Aug 2013 01:28:28 +0000 (UTC) Subject: Replace blanks with letter References: <2bdbd16f-a676-4973-9866-db93b1b9cd9b@googlegroups.com> <060f97db-188d-4078-9d87-c70230914ff1@googlegroups.com> Message-ID: eschneider92 at comcast.net wrote: > I wanted the program to stop only after all the letters were typed; why in the world would I try to write a program with blanks for each letter that seem intended to be filled, only to have it stop if the last letter is typed, or have to type each letter so many times until its processed? If you ran the code, my problems, as well as the intended goal, should become obvious. Also, why wouldn't someone who's willing to help me not run the code (takes a few seconds btw) I'm having trouble with in order to diagnose its faults, yet you have the time to write how you won't run it? If I did have a teacher to help me, this would be the last place I'd come to for help. It should be easy to deduce what I intended this program to do. Please no one respond being as I am done here, I just had to vent, > but instead report it if you want. It would have been much fewer words to describe the goal of the program. I ran the code, and showed the results of running it. But I still have no spec for what your assignment was. Like maybe the user is supposed to type one character at each prompt, even though it tells him to type 7. And the program should refuse any attempt to type more than one letter. And the user can type the characters in any order, not just A to G, and it'll keep prompting him till all 7 of the original letters is found. And it'll score him based on how many tries before he finishes. Or about 40 other possibilities. If you do have a teacher, have him tell you about comments. -- DaveA From eschneider92 at comcast.net Thu Aug 22 18:44:29 2013 From: eschneider92 at comcast.net (eschneider92 at comcast.net) Date: Thu, 22 Aug 2013 15:44:29 -0700 (PDT) Subject: Replace blanks with letter In-Reply-To: <2bdbd16f-a676-4973-9866-db93b1b9cd9b@googlegroups.com> References: <2bdbd16f-a676-4973-9866-db93b1b9cd9b@googlegroups.com> Message-ID: I wanted the program to stop only after all the letters were typed; why in the world would I try to write a program with blanks for each letter that seem intended to be filled, only to have it stop if the last letter is typed, or have to type each letter so many times until its processed? If you ran the code, my problems, as well as the intended goal, should become obvious. Also, why would someone who's willing to help me not run the code (takes a few seconds btw) I'm having trouble with in order to diagnose its faults, yet you have the time to write how you won't run it? If I did have a teacher to help me, this would be the last place I'd come to for help. It should be easy to deduce what I intended this program to do. Please no one respond being as I am done here, I just had to vent, but instead report it if you want. From jasonguto at gmail.com Tue Aug 20 11:17:17 2013 From: jasonguto at gmail.com (jasonguto at gmail.com) Date: Tue, 20 Aug 2013 08:17:17 -0700 (PDT) Subject: monitor multicast traffic/IP Message-ID: Folks: I am a Network Engineer, but have been trying to teach myself Python since Cisco will be leverage it on new high end models, yet I am very new to programming and Python; however, I have a need to have the ability to monitor traffic, more specificity multicast packets from a few sources. Just need some direction or pointers as to what to look for, what to review or what can be leveraged to start developing this app. In addition, can someone direct me to resources for procedural style of coding in Python? I am not grasping OOP concepts very well. I have played around with C and BASIC back in High School, but that was many years ago. Thank you From elchino at cnn.cn Tue Aug 20 15:05:35 2013 From: elchino at cnn.cn (ElChino) Date: Tue, 20 Aug 2013 21:05:35 +0200 Subject: monitor multicast traffic/IP References: Message-ID: wrote: > I am a Network Engineer, but have been trying to teach myself Python since Cisco > will be leverage it on new high end models, yet I am very new to programming and > Python; however, I have a need to have the ability to monitor traffic, more > specificity multicast packets from a few sources. Take a look at ImPacket and Pcapy [1+2]. Using one slightly modified sample therein, I was able to sniff out all mcast traffic here with this command: python sniff.py %PCAP_DEVICE% "ip[16] >= 224" I'm on a quiet home LAN, so not much mcasts here: Ether: 1c:bd:b9:c0:63:c6 -> 01:00:5e:00:00:01 IP 10.0.0.10 -> 224.0.0.1 110a eef5 0000 0000 ........ Ether: 1c:bd:b9:c0:63:c6 -> 01:00:5e:00:00:01 IP 10.0.0.10 -> 224.0.0.1 110a eef5 0000 0000 ........ [1] http://corelabs.coresecurity.com/index.php?module=Wiki&action=view&type=tool&name=Impacket [2] http://corelabs.coresecurity.com/index.php?module=Wiki&action=view&type=tool&name=Pcapy --gv From asmbansal2 at gmail.com Tue Aug 20 13:14:45 2013 From: asmbansal2 at gmail.com (Aseem Bansal) Date: Tue, 20 Aug 2013 10:14:45 -0700 (PDT) Subject: Found a grammar error in PEP 5. How to suggest correction? Message-ID: In PEP 5 it is 4. Add an an optional warning mode to the parser that will inform There are 2 `an`s here. How to suggest a correction for this? From joel.goldstick at gmail.com Tue Aug 20 13:32:56 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 20 Aug 2013 13:32:56 -0400 Subject: Found a grammar error in PEP 5. How to suggest correction? In-Reply-To: References: Message-ID: On Tue, Aug 20, 2013 at 1:14 PM, Aseem Bansal wrote: > In PEP 5 it is > > 4. Add an an optional warning mode to the parser that will inform > > There are 2 `an`s here. How to suggest a correction for this? > -- > http://mail.python.org/mailman/listinfo/python-list You already asked this question. Here is your answer: http://docs.python.org/2/bugs.html#documentation-bugs shows this: f you find a bug in this documentation or would like to propose an improvement, please send an e-mail to docs at python.org describing the bug and where you found it. If you have a suggestion how to fix it, include that as well. docs at python.org is a mailing list run by volunteers; your request will be noticed, even if it takes a while to be processed. Of course, if you want a more persistent record of your issue, you can use the issue tracker for documentation bugs as well. By the way, I found this link by going to google and searching for "how to submit documentation bug to python.org" Come on Aseem. Pick up your game a little bit. You actually have the opportunity to get the documentation changed for the better, or you can just keep asking the same question. -- Joel Goldstick http://joelgoldstick.com From asmbansal2 at gmail.com Tue Aug 20 13:48:16 2013 From: asmbansal2 at gmail.com (Aseem Bansal) Date: Tue, 20 Aug 2013 10:48:16 -0700 (PDT) Subject: Found a grammar error in PEP 5. How to suggest correction? In-Reply-To: References: Message-ID: <1041bc5d-228f-4162-9266-d2201cfb2686@googlegroups.com> @Joel Goldstick That is the documentation and this is about the PEP. I didn't realize that the same works for both. I'll do that. From tjreedy at udel.edu Tue Aug 20 16:05:39 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 20 Aug 2013 16:05:39 -0400 Subject: Found a grammar error in PEP 5. How to suggest correction? In-Reply-To: References: Message-ID: On 8/20/2013 1:14 PM, Aseem Bansal wrote: > In PEP 5 it is > > 4. Add an an optional warning mode to the parser that will inform > > There are 2 `an`s here. Fixed. > How to suggest a correction for this? You just did. If this list does not work, or for non-trivial suggestions, go to bugs.python.org or email docs at python.org. See http://docs.python.org/3/bugs.html -- Terry Jan Reedy From joel.goldstick at gmail.com Tue Aug 20 16:33:08 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 20 Aug 2013 16:33:08 -0400 Subject: Found a grammar error in PEP 5. How to suggest correction? In-Reply-To: References: Message-ID: On Tue, Aug 20, 2013 at 4:05 PM, Terry Reedy wrote: > On 8/20/2013 1:14 PM, Aseem Bansal wrote: >> >> In PEP 5 it is >> >> 4. Add an an optional warning mode to the parser that will inform >> >> There are 2 `an`s here. > > > Fixed. > > >> How to suggest a correction for this? > > > You just did. If this list does not work, or for non-trivial suggestions, go > to bugs.python.org or email docs at python.org. See > http://docs.python.org/3/bugs.html > -- > Terry Jan Reedy > > -- > http://mail.python.org/mailman/listinfo/python-list Hey Terry, Aseem! that's great! -- Joel Goldstick http://joelgoldstick.com From asmbansal2 at gmail.com Wed Aug 21 01:08:25 2013 From: asmbansal2 at gmail.com (Aseem Bansal) Date: Tue, 20 Aug 2013 22:08:25 -0700 (PDT) Subject: Found a grammar error in PEP 5. How to suggest correction? In-Reply-To: References: Message-ID: <5c3fc9de-2302-4afa-8fdb-ae6e21386abd@googlegroups.com> @ Terry Jan Reedy, @Joel Goldstick The problem with that documentation link is that it says the bugs in that documentation should be mailed at that e-mail address. But the PEPs are not the part of the documentation. I am saying that PEPs are not its part because when I downloaded the documentation it did not contain the PEPs. So the information in the documentation is misleading because it doesn't include the PEPs in the downloaded version but the e-mail address is used to accept PEP bugs according to you people. peps at python dot org is the e-mail suggested in PEP1 for contacting the PEP editors. I e-mailed there and got a reply. From luca.cerone at gmail.com Tue Aug 20 18:24:13 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Tue, 20 Aug 2013 15:24:13 -0700 (PDT) Subject: How to keep cookies when making http requests (Python 2.7) Message-ID: <7e79a9b4-0bf9-4756-afd4-3bc127360b95@googlegroups.com> Hi everybody, I am trying to write a simple Python script to solve the "riddle" at: http://quiz.gambitresearch.com/ The quiz is quite easy to solve, one needs to evaluate the expression between the curly brackets (say that the expression has value ) and go to the web page: http://quiz.gambitresearch/job/ You have to be fast enough, because with the page there is an associated cookie that expires 1 sec after the first request, therefore you need to be quick to access the /job/ page. [I know that this is the correct solution because with a friend we wrote a small script in JavaScript and could access the page with the email address] As an exercise I have decided to try doing the same with Python. First I have tried with the following code: #START SCRIPT import re import urllib2 regex = re.compile(r'\{(.*)\}') base_address = "http://quiz.gambitresearch.com/" base_h = urllib2.urlopen(base_address) base_page = base_h.read() val = str(eval(regex.findall(base_page)[0])) job_address = base_address + "job/" + val job_h = urllib2.urlopen(job_address) job_page = job_h.read() print job_page #END SCRIPT job_page has the following content now: "WRONG! (Have you enabled cookies?)" Trying to solve the issues with the cookies I found the "requests" module that in theory should work. I therefore rewrote the above script to use request: #START SCRIPT: import re import requests regex = re.compile(r'\{(.*)\}') base_address = "http://quiz.gambitresearch.com/" s = requests.Session() base_h = s.get('http://quiz.gambitresearch.com/') base_page = base_h.text val = eval( regex.findall( base_page )[0] ) job_address = base_address + "job/" + str(val) job_h = s.get( job_address ) job_page = job_h.text print job_page #END SCRIPT # print job_page produces "Wrong!". According to the manual using Session() the cookies should be enabled and persistent for all the session. In fact the cookies in base_h.cookies and in job_h.cookies seem to be the same: base_h.cookies == job_h.cookies #returns True So, why does this script fail to access the job page? How can I change it so that I it works as intended and job_page prints the content of the page that displays the email address to use for the job applications? Thanks a lot in advance for the help! Best Wishes, Luca From dieter at handshake.de Wed Aug 21 02:07:35 2013 From: dieter at handshake.de (dieter) Date: Wed, 21 Aug 2013 08:07:35 +0200 Subject: How to keep cookies when making http requests (Python 2.7) References: <7e79a9b4-0bf9-4756-afd4-3bc127360b95@googlegroups.com> Message-ID: <871u5nk254.fsf@handshake.de> Luca Cerone writes: > ... Python has a module for cookie handling: "cookielib" ("cookiejar" in Python 3). "urllib2" has a standard way to integrate with this module. However, I do not know the details (check the documentation for the modules). I have used "cookielib" externally to "urllib2". It looks like this: from urllib2 import urlopen, Request from cookielib import CookieJar cookies = CookieJar() .... r = Request(...) cookies.add_cookie_header(r) # set the cookies R = urlopen(r, ...) # make the request cookies.extract_cookies(R, r) # remember the new cookies From luca.cerone at gmail.com Wed Aug 21 04:18:28 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Wed, 21 Aug 2013 01:18:28 -0700 (PDT) Subject: How to keep cookies when making http requests (Python 2.7) In-Reply-To: References: <7e79a9b4-0bf9-4756-afd4-3bc127360b95@googlegroups.com> Message-ID: <8266faf3-892b-4e49-9b38-87f0030250fc@googlegroups.com> > > I have used "cookielib" externally to "urllib2". It looks > > like this: > > from urllib2 import urlopen, Request > > from cookielib import CookieJar > cookies = CookieJar() > > .... > > r = Request(...) > > cookies.add_cookie_header(r) # set the cookies > > R = urlopen(r, ...) # make the request > > cookies.extract_cookies(R, r) # remember the new cookies Hi Dieter, thanks a lot for the help. I am sorry but your code is not very clear to me. It seems that you are setting some cookies, but I can't understand how you use the ones that the site sends to you when you perform the initial request. Have you tried this code to check if this work? If it works as intended can you explain a bit better what it does exactly? Thanks again! Luca From fabiosantosart at gmail.com Wed Aug 21 05:15:25 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Wed, 21 Aug 2013 10:15:25 +0100 Subject: How to keep cookies when making http requests (Python 2.7) In-Reply-To: <8266faf3-892b-4e49-9b38-87f0030250fc@googlegroups.com> References: <7e79a9b4-0bf9-4756-afd4-3bc127360b95@googlegroups.com> <8266faf3-892b-4e49-9b38-87f0030250fc@googlegroups.com> Message-ID: On 21 Aug 2013 09:22, "Luca Cerone" wrote: > > > > > I have used "cookielib" externally to "urllib2". It looks > > > > like this: > > > > from urllib2 import urlopen, Request > > > > from cookielib import CookieJar > > cookies = CookieJar() > > > > .... > > > > r = Request(...) > > > > cookies.add_cookie_header(r) # set the cookies > > > > R = urlopen(r, ...) # make the request > > > > cookies.extract_cookies(R, r) # remember the new cookies > > Hi Dieter, > thanks a lot for the help. > I am sorry but your code is not very clear to me. > It seems that you are setting some cookies, > but I can't understand how you use the ones that the site > sends to you when you perform the initial request. This example does both. The cookie jar adds the cookies to the http request to be sent to the server, and updates the cookies from the response, if any were sent. It seems pretty clear, seeing that it has a lot of comments. The cookies from the site are thus in the cookie jar object after the call to extract_cookies() extracts them from the response. > Have you tried this code to check if this work? > If it works as intended can you explain a bit better > what it does exactly? You should really test this yourself ;) > Thanks again! > Luca -------------- next part -------------- An HTML attachment was scrubbed... URL: From dieter at handshake.de Thu Aug 22 02:08:47 2013 From: dieter at handshake.de (dieter) Date: Thu, 22 Aug 2013 08:08:47 +0200 Subject: How to keep cookies when making http requests (Python 2.7) References: <7e79a9b4-0bf9-4756-afd4-3bc127360b95@googlegroups.com> <8266faf3-892b-4e49-9b38-87f0030250fc@googlegroups.com> Message-ID: <8761uyb6kw.fsf@handshake.de> Luca Cerone writes: >... > Have you tried this code to check if this work? Not this code, but code like this (as I have written). > If it works as intended can you explain a bit better > what it does exactly? Fabio already did the explanation. Let me make an additional remark however: you should not expect to get complete details in a list like this - but only hints towards a solution for your problem (i.e. there remains some work for you). Thus, I expect you to read the "cookielib/cookiejar" documentation (part of Python's standard documentation) in order to understand my example code - before I would be ready to provide further details. From luca.cerone at gmail.com Tue Aug 27 05:16:50 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Tue, 27 Aug 2013 02:16:50 -0700 (PDT) Subject: How to keep cookies when making http requests (Python 2.7) In-Reply-To: References: <7e79a9b4-0bf9-4756-afd4-3bc127360b95@googlegroups.com> <8266faf3-892b-4e49-9b38-87f0030250fc@googlegroups.com> Message-ID: <5970a1d3-ec1b-4892-b53a-907de332ecaa@googlegroups.com> Dear all, first of all thanks for the help. As for your remark, you are right, and I usually tend to post questions in a way that is detached from the particular problem I have to solve. In this case since I only have a limited knowledge of the cookies mechanism (in general, not only in Python), I preferred to ask for the specific case. I am sorry if I gave you the impression I didn't appreciate your answer, it was absolutely not my intention. Cheers, Luca > Let me make an additional remark however: you should > > not expect to get complete details in a list like this - but only > > hints towards a solution for your problem (i.e. > > there remains some work for you). > > Thus, I expect you to read the "cookielib/cookiejar" documentation > > (part of Python's standard documentation) in order to understand > > my example code - before I would be ready to provide further details. From luca.cerone at gmail.com Tue Aug 27 06:17:54 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Tue, 27 Aug 2013 03:17:54 -0700 (PDT) Subject: How to keep cookies when making http requests (Python 2.7) In-Reply-To: <5970a1d3-ec1b-4892-b53a-907de332ecaa@googlegroups.com> References: <7e79a9b4-0bf9-4756-afd4-3bc127360b95@googlegroups.com> <8266faf3-892b-4e49-9b38-87f0030250fc@googlegroups.com> <5970a1d3-ec1b-4892-b53a-907de332ecaa@googlegroups.com> Message-ID: <62a5fb2d-b4d0-4de4-a0d8-a7ee4dbb1b90@googlegroups.com> > > > Let me make an additional remark however: you should > > not expect to get complete details in a list like this - but only > > hints towards a solution for your problem (i.e. > > there remains some work for you). > > Thus, I expect you to read the "cookielib/cookiejar" documentation > > (part of Python's standard documentation) in order to understand > > my example code - before I would be ready to provide further details. Ok so after reading the documentation for urllib2 and cookielib I came up with the following code: #START from urllib2 import urlopen , Request from cookielib import CookieJar import re regex = re.compile(r'\{(.*)\}') base_url = "http://quiz.gambitresearch.com" job_url = base_url + "/job/" cookies = CookieJar() r = Request(base_url) #prepare the request object cookies.add_cookie_header(r) #allow to have cookies R = urlopen(r) #read the url cookies.extract_cookies(R,r) #take the cookies from the response R and adds #them to the request object #build the new url t = R.read() v = str(eval(regex.findall(t)[0])) job_url = job_url + v # Here I create a new request to the url containing the email address r2 = Request(job_url) cookies.add_cookie_header(r2) #I prepare the request for cookies adding the cookies that I extracted before. #perform the request and print the page R2 = urlopen(r2) t2 = R2.read() print job_url print t2 #END This still doesn't work, but I really can't understand why. As far as I have understood first I have to instantiate a Request object and allow it to receive and set cookies (I do this with r = Request() and cookies.add_cookie_header(r)) Next I perform the request (urlopen), save the cookies in the CookieJar (cookies.extract_cookies(R,r)). I evaluate the new address and I create a new Request for it (r2 = Request) I add the cookies stored in the cookiejar in my new request (cookies.add_cookie_header(r2)) Then I perform the request (R2 = urlopen(r2)) and read the page (t2 = R2.read()) What am I doing wrong? Do I misunderstand something in the process? Thanks again in advance for the help, Cheers, Luca From dieter at handshake.de Wed Aug 28 00:52:37 2013 From: dieter at handshake.de (dieter) Date: Wed, 28 Aug 2013 06:52:37 +0200 Subject: How to keep cookies when making http requests (Python 2.7) References: <7e79a9b4-0bf9-4756-afd4-3bc127360b95@googlegroups.com> <8266faf3-892b-4e49-9b38-87f0030250fc@googlegroups.com> <5970a1d3-ec1b-4892-b53a-907de332ecaa@googlegroups.com> <62a5fb2d-b4d0-4de4-a0d8-a7ee4dbb1b90@googlegroups.com> Message-ID: <87sixu1koa.fsf@handshake.de> Luca Cerone writes: > ... > Ok so after reading the documentation for urllib2 and cookielib I came up with the following code: > > #START > from urllib2 import urlopen , Request > from cookielib import CookieJar > import re > regex = re.compile(r'\{(.*)\}') > > base_url = "http://quiz.gambitresearch.com" > job_url = base_url + "/job/" > > cookies = CookieJar() > r = Request(base_url) #prepare the request object > cookies.add_cookie_header(r) #allow to have cookies > R = urlopen(r) #read the url > cookies.extract_cookies(R,r) #take the cookies from the response R and adds #them to the request object "adds them to the request object" should be "adds them to the cookie jar". > #build the new url > t = R.read() > v = str(eval(regex.findall(t)[0])) > job_url = job_url + v > > > # Here I create a new request to the url containing the email address > r2 = Request(job_url) > cookies.add_cookie_header(r2) #I prepare the request for cookies adding the cookies that I extracted before. > > #perform the request and print the page > R2 = urlopen(r2) > t2 = R2.read() > print job_url > print t2 > #END > > This still doesn't work, but I really can't understand why. > As far as I have understood first I have to instantiate a Request object > and allow it to receive and set cookies (I do this with r = Request() and cookies.add_cookie_header(r)) > Next I perform the request (urlopen), save the cookies in the CookieJar (cookies.extract_cookies(R,r)). > > I evaluate the new address and I create a new Request for it (r2 = Request) > I add the cookies stored in the cookiejar in my new request (cookies.add_cookie_header(r2)) > Then I perform the request (R2 = urlopen(r2)) and read the page (t2 = R2.read()) > > What am I doing wrong? With respect to cookie handling, you do everything right. There may be other problems with the (wider) process. Analysing the responses of your requests (reading the status codes, the response headers and the response bodies) may provide hints towards the problem. >Do I misunderstand something in the process? Not with respect to cookie handling. From luca.cerone at gmail.com Fri Aug 30 08:01:08 2013 From: luca.cerone at gmail.com (Luca Cerone) Date: Fri, 30 Aug 2013 05:01:08 -0700 (PDT) Subject: How to keep cookies when making http requests (Python 2.7) In-Reply-To: References: <7e79a9b4-0bf9-4756-afd4-3bc127360b95@googlegroups.com> <8266faf3-892b-4e49-9b38-87f0030250fc@googlegroups.com> <5970a1d3-ec1b-4892-b53a-907de332ecaa@googlegroups.com> <62a5fb2d-b4d0-4de4-a0d8-a7ee4dbb1b90@googlegroups.com> Message-ID: Thanks Dieter, > With respect to cookie handling, you do everything right. > > > > There may be other problems with the (wider) process. > > Analysing the responses of your requests (reading the status codes, > > the response headers and the response bodies) may provide hints > > towards the problem. > I will try to do that and try to see if I can figure out why. > > > >Do I misunderstand something in the process? > > > > Not with respect to cookie handling. From asmbansal2 at gmail.com Wed Aug 21 01:14:48 2013 From: asmbansal2 at gmail.com (Aseem Bansal) Date: Tue, 20 Aug 2013 22:14:48 -0700 (PDT) Subject: PEPs should be included with the documentation download Message-ID: <4bc4d258-1e12-4e1a-af9c-0019f099612f@googlegroups.com> Currently the documentation download includes a lot of things but PEPs are not its part. I wanted to suggest that PEPs should be included in the download. They are very much relevant to Python. From rosuav at gmail.com Wed Aug 21 13:32:53 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 22 Aug 2013 03:32:53 +1000 Subject: PEPs should be included with the documentation download In-Reply-To: <4bc4d258-1e12-4e1a-af9c-0019f099612f@googlegroups.com> References: <4bc4d258-1e12-4e1a-af9c-0019f099612f@googlegroups.com> Message-ID: On Wed, Aug 21, 2013 at 3:14 PM, Aseem Bansal wrote: > Currently the documentation download includes a lot of things but PEPs are not its part. I wanted to suggest that PEPs should be included in the download. They are very much relevant to Python. The PEPs are kinda like the specs that Python is built from, rather than being end-user documentation; certainly most, if not all, are unnecessary to most use of Python. There's really no point downloading a whole pile of rejected PEPs as part of the crucial user-facing docs. Also, how many people actually depend on the downloadable documentation, rather than simply reading things online? ChrisA From random832 at fastmail.us Wed Aug 21 13:55:44 2013 From: random832 at fastmail.us (random832 at fastmail.us) Date: Wed, 21 Aug 2013 13:55:44 -0400 Subject: PEPs should be included with the documentation download In-Reply-To: References: <4bc4d258-1e12-4e1a-af9c-0019f099612f@googlegroups.com> Message-ID: <1377107744.21484.12540441.3FF11C64@webmail.messagingengine.com> On Wed, Aug 21, 2013, at 13:32, Chris Angelico wrote: > On Wed, Aug 21, 2013 at 3:14 PM, Aseem Bansal > wrote: > > Currently the documentation download includes a lot of things but PEPs are not its part. I wanted to suggest that PEPs should be included in the download. They are very much relevant to Python. > > The PEPs are kinda like the specs that Python is built from, rather > than being end-user documentation; certainly most, if not all, are > unnecessary to most use of Python. There's really no point downloading > a whole pile of rejected PEPs as part of the crucial user-facing docs. > > Also, how many people actually depend on the downloadable > documentation, rather than simply reading things online? If you've taken your laptop to somewhere there's no wi-fi, it's nice to have offline help. I think, though, that if there's any useful information that can be obtained by reading accepted PEPs but not the documentation, or if things are explained less clearly than in the PEPs, that's a bug in the documentation, and should be remedied by adding to the documentation. From malaclypse2 at gmail.com Wed Aug 21 14:15:37 2013 From: malaclypse2 at gmail.com (Jerry Hill) Date: Wed, 21 Aug 2013 14:15:37 -0400 Subject: PEPs should be included with the documentation download In-Reply-To: <1377107744.21484.12540441.3FF11C64@webmail.messagingengine.com> References: <4bc4d258-1e12-4e1a-af9c-0019f099612f@googlegroups.com> <1377107744.21484.12540441.3FF11C64@webmail.messagingengine.com> Message-ID: On Wed, Aug 21, 2013 at 1:55 PM, wrote: > I think, though, that if there's any useful information that can be > obtained by reading accepted PEPs but not the documentation, or if > things are explained less clearly than in the PEPs, that's a bug in the > documentation, and should be remedied by adding to the documentation. Personally, the only PEPs I've used as reference material as PEP 8 (the Python Style Guide), and PEP 249 (the Python Database API Specification v2.0). If I recall correctly, one of the database adapters I used basically said that they were PEP 249 compliant, and didn't have much documentation beyond that. It seems to me that adding the PEPs to the compiled documentation would be a good thing. They are at least as useful as the Language Reference or the Embedding and Extending Python sections that are already included. -- Jerry From random832 at fastmail.us Wed Aug 21 14:28:16 2013 From: random832 at fastmail.us (random832 at fastmail.us) Date: Wed, 21 Aug 2013 14:28:16 -0400 Subject: PEPs should be included with the documentation download In-Reply-To: References: <4bc4d258-1e12-4e1a-af9c-0019f099612f@googlegroups.com> <1377107744.21484.12540441.3FF11C64@webmail.messagingengine.com> Message-ID: <1377109696.31206.12552657.56DCAF41@webmail.messagingengine.com> On Wed, Aug 21, 2013, at 14:15, Jerry Hill wrote: > Personally, the only PEPs I've used as reference material as PEP 8 > (the Python Style Guide), and PEP 249 (the Python Database API > Specification v2.0). If I recall correctly, one of the database > adapters I used basically said that they were PEP 249 compliant, and > didn't have much documentation beyond that. Maybe there should be documentation for PEP 249 and other such "API Specifications" in the main documentation tree, in the same way that .NET documents interfaces. From rosuav at gmail.com Wed Aug 21 14:50:25 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 22 Aug 2013 04:50:25 +1000 Subject: PEPs should be included with the documentation download In-Reply-To: References: <4bc4d258-1e12-4e1a-af9c-0019f099612f@googlegroups.com> <1377107744.21484.12540441.3FF11C64@webmail.messagingengine.com> Message-ID: On Thu, Aug 22, 2013 at 4:15 AM, Jerry Hill wrote: > On Wed, Aug 21, 2013 at 1:55 PM, wrote: >> I think, though, that if there's any useful information that can be >> obtained by reading accepted PEPs but not the documentation, or if >> things are explained less clearly than in the PEPs, that's a bug in the >> documentation, and should be remedied by adding to the documentation. > > Personally, the only PEPs I've used as reference material as PEP 8 > (the Python Style Guide), and PEP 249 (the Python Database API > Specification v2.0). If I recall correctly, one of the database > adapters I used basically said that they were PEP 249 compliant, and > didn't have much documentation beyond that. > > It seems to me that adding the PEPs to the compiled documentation > would be a good thing. They are at least as useful as the Language > Reference or the Embedding and Extending Python sections that are > already included. Ah, yes, there are a few that would be good. But I don't really see that all the internally bits (PEP 393, anyone?) and rejected proposals (PEP 315) need to be in the download. I wouldn't expect the full set of RFCs to be included with the docs for the socket module, so I equally don't expect the PEPs to be included. ChrisA From tjreedy at udel.edu Wed Aug 21 16:15:17 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 21 Aug 2013 16:15:17 -0400 Subject: PEPs should be included with the documentation download In-Reply-To: References: <4bc4d258-1e12-4e1a-af9c-0019f099612f@googlegroups.com> Message-ID: On 8/21/2013 1:32 PM, Chris Angelico wrote: > On Wed, Aug 21, 2013 at 3:14 PM, Aseem Bansal wrote: >> Currently the documentation download includes a lot of things but PEPs are not its part. I wanted to suggest that PEPs should be included in the download. They are very much relevant to Python. > > The PEPs are kinda like the specs that Python is built from, rather > than being end-user documentation; certainly most, if not all, are > unnecessary to most use of Python. There's really no point downloading > a whole pile of rejected PEPs as part of the crucial user-facing docs. The manuals are intended to document current reality. Accepted PEPs document plans, often minus details. They do not get updated to reflect the initial implementation, let alone subsequent changes. Thus even There are a few chapters in the manual that reference a PEP, either because the details are though to be too esoteric for the manual or becuase no one has yet gotten around to rewriting the material for the manual. (In the latter case, a patch should be welcome.) So there might be a reason to include a '(Highly) Selected PEPs' heading to the main page. PEP 8 might be a candidate, though it was originally intended as an internal style guide for the stdlib only. -- Terry Jan Reedy From cs at zip.com.au Wed Aug 21 23:25:04 2013 From: cs at zip.com.au (Cameron Simpson) Date: Thu, 22 Aug 2013 13:25:04 +1000 Subject: PEPs should be included with the documentation download In-Reply-To: References: Message-ID: <20130822032504.GA24431@cskk.homeip.net> On 22Aug2013 03:32, Chris Angelico wrote: | Also, how many people actually depend on the downloadable | documentation, rather than simply reading things online? I do. It is outstandingly faster, and works when one is offline; I always have a local copy of a 2.x and 3.x documentation set as desktop icons, ready for instant opening. I agree I rarely need the PEPs unless I want to look up something unusual. Cheers, -- Cameron Simpson I need your clothes, your boots, and your motorcycle. - Arnold Schwarzenegger, Terminator 2 From asmbansal2 at gmail.com Thu Aug 22 04:45:01 2013 From: asmbansal2 at gmail.com (Aseem Bansal) Date: Thu, 22 Aug 2013 01:45:01 -0700 (PDT) Subject: PEPs should be included with the documentation download In-Reply-To: References: <4bc4d258-1e12-4e1a-af9c-0019f099612f@googlegroups.com> Message-ID: On Wednesday, August 21, 2013 11:25:44 PM UTC+5:30, rand... at fastmail.us wrote: > I think, though, that if there's any useful information that can be > obtained by reading accepted PEPs but not the documentation, or if > things are explained less clearly than in the PEPs, that's a bug in the > documentation, and should be remedied by adding to the documentation. PEP8 is referenced a lot but only a very small portion is included in the documentation (in the tutorial). I am a Python newbie and there may be other PEPs usually referenced which I might not be aware about. Maybe add selected PEPs to the documentation? I agree that adding rejected PEPs is no good but there may be PEPs worthy of addition to the documentation. From ben+python at benfinney.id.au Thu Aug 22 21:47:44 2013 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 23 Aug 2013 11:47:44 +1000 Subject: PEPs should be included with the documentation download References: <4bc4d258-1e12-4e1a-af9c-0019f099612f@googlegroups.com> Message-ID: <7wppt5mb3z.fsf@benfinney.id.au> Chris Angelico writes: > Also, how many people actually depend on the downloadable > documentation, rather than simply reading things online? Many countries do not have infrastructure that allows reliable, fast, low-latency internet access 24-hours-a-day. Most countries's internet infrastructure, in fact, does not satisfy all of those. And without all of those being satisfied simultaneously, accessing programmer documentation online is frustratingly inconsistent. It is much more convenient and reliable, in those cases, to have the documentation downloaded and accessible on one's development workstation. -- \ ?We must find our way to a time when faith, without evidence, | `\ disgraces anyone who would claim it.? ?Sam Harris, _The End of | _o__) Faith_, 2004 | Ben Finney From rosuav at gmail.com Thu Aug 22 21:54:38 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 23 Aug 2013 11:54:38 +1000 Subject: PEPs should be included with the documentation download In-Reply-To: <7wppt5mb3z.fsf@benfinney.id.au> References: <4bc4d258-1e12-4e1a-af9c-0019f099612f@googlegroups.com> <7wppt5mb3z.fsf@benfinney.id.au> Message-ID: On Fri, Aug 23, 2013 at 11:47 AM, Ben Finney wrote: > Chris Angelico writes: > >> Also, how many people actually depend on the downloadable >> documentation, rather than simply reading things online? > > Many countries do not have infrastructure that allows reliable, fast, > low-latency internet access 24-hours-a-day. Most countries's internet > infrastructure, in fact, does not satisfy all of those. I'm aware of that. However, I'm also aware that many people still read things online, even with a less-than-reliable internet connection. Hence the question: How many people actually do use the downloaded docs? Maybe it'd turn out to be quite high, but it's not an unreasonable question. ChrisA From ben+python at benfinney.id.au Fri Aug 23 02:21:04 2013 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 23 Aug 2013 16:21:04 +1000 Subject: PEPs should be included with the documentation download References: <4bc4d258-1e12-4e1a-af9c-0019f099612f@googlegroups.com> <7wppt5mb3z.fsf@benfinney.id.au> Message-ID: <7wvc2xkjvz.fsf@benfinney.id.au> Chris Angelico writes: > Hence the question: How many people actually do use the downloaded > docs? Maybe it'd turn out to be quite high, but it's not an > unreasonable question. I think it's an unreasonable question. What would you accept as an answer? Who could possibly be autoritative at estimating such a number? How would you choose between competing authorities and estimates? It should be sufficient to realise that the reality of internet infrastructure in most countries makes it preferable ? at least some of the time, for some significant, even if small, number of users ? to read the documentation on local storage instead of on the internet. -- \ ?I took a course in speed waiting. Now I can wait an hour in | `\ only ten minutes.? ?Steven Wright | _o__) | Ben Finney From nad at acm.org Fri Aug 23 04:14:29 2013 From: nad at acm.org (Ned Deily) Date: Fri, 23 Aug 2013 01:14:29 -0700 Subject: PEPs should be included with the documentation download References: <4bc4d258-1e12-4e1a-af9c-0019f099612f@googlegroups.com> <7wppt5mb3z.fsf@benfinney.id.au> <7wvc2xkjvz.fsf@benfinney.id.au> Message-ID: In article <7wvc2xkjvz.fsf at benfinney.id.au>, Ben Finney wrote: > Chris Angelico writes: > > Hence the question: How many people actually do use the downloaded > > docs? Maybe it'd turn out to be quite high, but it's not an > > unreasonable question. > > I think it's an unreasonable question. What would you accept as an > answer? Who could possibly be autoritative at estimating such a number? > How would you choose between competing authorities and estimates? > > It should be sufficient to realise that the reality of internet > infrastructure in most countries makes it preferable ? at least some of > the time, for some significant, even if small, number of users ? to read > the documentation on local storage instead of on the internet. In any case if you want to see this happen, someone needs to open an issue and make a case for it on the Python bug tracker. -- Ned Deily, nad at acm.org From ben+python at benfinney.id.au Fri Aug 23 05:24:29 2013 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 23 Aug 2013 19:24:29 +1000 Subject: PEPs should be included with the documentation download References: <4bc4d258-1e12-4e1a-af9c-0019f099612f@googlegroups.com> <7wppt5mb3z.fsf@benfinney.id.au> <7wvc2xkjvz.fsf@benfinney.id.au> Message-ID: <7whaegu5de.fsf@benfinney.id.au> Ned Deily writes: > In article <7wvc2xkjvz.fsf at benfinney.id.au>, > Ben Finney wrote: > > Chris Angelico writes: > > > Hence the question: How many people actually do use the downloaded > > > docs? Maybe it'd turn out to be quite high, but it's not an > > > unreasonable question. > > > > I think it's an unreasonable question [in this context]. > In any case if you want to see this happen, someone needs to open an > issue and make a case for it on the Python bug tracker. Neither Chris nor I are proposing to have PEPs in the installed documentation :-) You might want to respond directly to the original post with that suggestion. -- \ ?Men never do evil so completely and cheerfully as when they do | `\ it from religious conviction.? ?Blaise Pascal (1623?1662), | _o__) Pens?es, #894. | Ben Finney From neilc at norwich.edu Fri Aug 23 09:19:51 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 23 Aug 2013 13:19:51 GMT Subject: PEPs should be included with the documentation download References: <4bc4d258-1e12-4e1a-af9c-0019f099612f@googlegroups.com> <7wppt5mb3z.fsf@benfinney.id.au> Message-ID: On 2013-08-23, Chris Angelico wrote: > I'm aware of that. However, I'm also aware that many people > still read things online, even with a less-than-reliable > internet connection. Hence the question: How many people > actually do use the downloaded docs? Maybe it'd turn out to be > quite high, but it's not an unreasonable question. I use the compiled html/windows help and the integrated with the interpreter html version of the Python docs, both downloaded and installed for easy access. -- Neil Cerutti From asmbansal2 at gmail.com Thu Aug 22 04:39:56 2013 From: asmbansal2 at gmail.com (Aseem Bansal) Date: Thu, 22 Aug 2013 01:39:56 -0700 (PDT) Subject: PEPs should be included with the documentation download In-Reply-To: References: <4bc4d258-1e12-4e1a-af9c-0019f099612f@googlegroups.com> Message-ID: <5a636f8b-1b4e-46d8-904e-f38cc8ec53b9@googlegroups.com> I do depend on offline documentation. I have both Python2 and 3's documentation offline. A lot of people have 24-hour access to internet but a lot of people don't have. And while moving around it isn't always possible to have internet then offline documentation is really helpful. From tjreedy at udel.edu Thu Aug 22 18:46:30 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 22 Aug 2013 18:46:30 -0400 Subject: PEPs should be included with the documentation download In-Reply-To: <5a636f8b-1b4e-46d8-904e-f38cc8ec53b9@googlegroups.com> References: <4bc4d258-1e12-4e1a-af9c-0019f099612f@googlegroups.com> <5a636f8b-1b4e-46d8-904e-f38cc8ec53b9@googlegroups.com> Message-ID: On 8/22/2013 4:39 AM, Aseem Bansal wrote: > I do depend on offline documentation. I have both Python2 and 3's documentation offline. A lot of people have 24-hour access to internet but a lot of people don't have. And while moving around it isn't always possible to have internet then offline documentation is really helpful. If you have mercurial installed, you can easily download a read-only clone of the peps repository (hg.python.org/peps, I believe). You can even pull updates whenever you feel like it. One can also clone the main repository and build the regular docs, including the html version thereof. Again, pull and rebuild when you expect to be offline. -- Terry Jan Reedy From fabrice.romand at gmail.com Wed Aug 21 02:30:43 2013 From: fabrice.romand at gmail.com (fabrice.romand at gmail.com) Date: Tue, 20 Aug 2013 23:30:43 -0700 (PDT) Subject: Any Django users in Reims (France) ? Message-ID: <7441659d-4e6c-470d-bd4f-c1a12c2d1c46@googlegroups.com> Bonjour, Je cherche des dev Django dans la r?gion de Reims (France) pour organiser des rencontres sympas et ?changer sur notre plateforme pr?f?r?e voire d?velopper ensemble nos excellentes id?es. A votre ?coute, Fabrice From chandan_psr at yahoo.co.in Wed Aug 21 02:50:20 2013 From: chandan_psr at yahoo.co.in (chandan kumar) Date: Wed, 21 Aug 2013 14:50:20 +0800 (SGT) Subject: Basic Python Query Message-ID: <1377067820.2408.YahooMailNeo@web190505.mail.sg3.yahoo.com> Hi all, Please see the below code. class Test(threading.Thread): ? ? ? ? ? def StartThread(self): ? ? ? ?Lock = threading.Lock() ? ? ? ? self.start() ?? class Test1(threading.Thread): ? ? def __init__(self): ? ? ? ? threading.Thread.__init__ ( self ) ? ? ? ? self.Lock = threading.Lock() self.start() ?? if __name__ == '__main__': ? ? ? ?instance = Test() ? ? ? ?instance.StartThread() ? ? ? ?instance1= Test1() ? ? ? ?instance1.start() Please clarify the questions below with respect to above code 1.Difference between??def StartThread(self) and def __init__(self): 2???instance = Test() ,when this is called ,the code flow goes inside the threading module ,But? ? ? ? instance1= Test1() ,the code flow goes inside the class Test1 ? ? ?When we call the both classes in same way ,How does the flow is different for both. 3. Lets say self is passed explicitly for all the methods Like ? ? def method1(self) ? ? ? ? ?method2() ? ?def ?method2(self): ? ? ? ? ?method3() ? ?def method(self) ? ? ? ? method4() ? ?def method4(self) ?What does self holds in method4 ,Is it self argument from method1? Sorry i'm confused with self argument.Please clarify if its valid question. Best Regards, Chandan -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Wed Aug 21 04:19:28 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 21 Aug 2013 08:19:28 GMT Subject: Basic Python Query References: Message-ID: <5214780f$0$29885$c3e8da3$5496439d@news.astraweb.com> On Wed, 21 Aug 2013 14:50:20 +0800, chandan kumar wrote: [...] > 1.Difference between??def StartThread(self) and def __init__(self): __init__ is a special method called automatically by Python when you create an instance. StartThread is a method that the author of the code (perhaps you?) wrote themselves. It has no special meaning in Python, it will do whatever it is programmed to do, but only if you call it. > 2???instance = Test() ,when this is called ,the code flow goes inside > the threading module , But? > ? ? instance1= Test1() ,the code flow goes inside the class Test1 > ? ? ?When we call the both classes in same way ,How does the flow is > different for both. When Test() is called, a new Test instance is created, and the __init__ method is called, but the thread is not started. When you call Test1(), the __init___ method automatically starts the thread, because it includes the line "self.start()". (Although, in the code you show, the indentation is wrong and the code will not work correctly. Please be more careful in the future about the indentation.) > 3. Lets say self is passed explicitly for all the methods Like > ? ? def method1(self) > ? ? ? ? ?method2() That won't work, you need to say self.method2() > ? ?def ?method2(self): > ? ? ? ? ?method3() > ? ?def method(self) > ? ? ? ? method4() > ? ?def method4(self) > >?What does self holds in method4 ,Is it self argument from method1? > Sorry i'm confused with self argument.Please clarify if its valid > question. Yes, it is the same "self" all the way through. If you have ordinary functions: def f1(x): f2(x) def f2(y): f3(y) def f3(z): print z and you call f1("something"), then the result will be to print "something". Even though the argument name is different, the same argument is passed from one function to the next. Methods are exactly the same, except that the "self" argument is nearly always called "self". When you have an instance, and you call one of its methods: instance = SomeClass() instance.method(extra, args) then Python automatically uses the instance as the "self" argument. This is equivalent to: SomeClass.method(instance, extra, args) # inside the method, self=instance except Python does it for you, instead of you needing to write it out in full like that. -- Steven From ulrich.eckhardt at dominolaser.com Wed Aug 21 05:11:18 2013 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Wed, 21 Aug 2013 11:11:18 +0200 Subject: Basic Python Query In-Reply-To: References: Message-ID: Am 21.08.2013 08:50, schrieb chandan kumar: > class Test(threading.Thread): > def StartThread(self): > Lock = threading.Lock() > self.start() Inconsistently indented code, this is a killer for Python. Please read PEP8 and use four spaces! That said, there is never a need for deriving from the Thread class, you can also use it to run a function without that. That way is IMHO clearer because the threading.Thread instance is not the thread, just like a File instance is not a file. Both just represent handles for manipulating the actual thing. Further, you have a local variable called "Lock" here (should be lowercase, see PEP 8) that you don't use. This is either a bug you missed or at least code that you didn't trim in order to produce a minimal example. > class Test1(threading.Thread): > def __init__(self): > threading.Thread.__init__ ( self ) Check out the "super()" syntax. > 1.Difference between def StartThread(self) and def __init__(self): __init__ is a special function that gets called automatically. Search online for the documentation and or further explanations. > 3. Lets say self is passed explicitly for all the methods Like > def method1(self) > method2() > def method2(self): > method3() > def method(self) > method4() > def method4(self) > What does self holds in method4 ,Is it self argument from method1? > Sorry i'm confused with self argument. "self" is just a name like others, only that it is customarily used for the first parameter of memberfunctions, i.e. for the instance of the according class. That said, above seven lines don't really serve to illustrate anything, because they are far from valid Python code. I think before tackling threading, you should first go through some tutorials and documentation. I'd start with http://docs.python.org and/or do some online searches. Good luck! Uli From dfnsonfsduifb at gmx.de Wed Aug 21 14:58:55 2013 From: dfnsonfsduifb at gmx.de (Johannes Bauer) Date: Wed, 21 Aug 2013 20:58:55 +0200 Subject: Basic Python Query In-Reply-To: References: Message-ID: On 21.08.2013 11:11, Ulrich Eckhardt wrote: > That said, there is never a need for deriving > from the Thread class, you can also use it to run a function without > that. That way is IMHO clearer because the threading.Thread instance is > not the thread, just like a File instance is not a file. Both just > represent handles for manipulating the actual thing. Huh? That I find most curious. I *always* derive from threading.Thread and really like the way that thread setup works (instanciate Thread handle, call start). Very intuitive, never had the problems with clarity that you mentioned. Could you elaborate on your suggestion? I don't seem to quite get it I'm afraid. Best regards, Johannes -- >> Wo hattest Du das Beben nochmal GENAU vorhergesagt? > Zumindest nicht ?ffentlich! Ah, der neueste und bis heute genialste Streich unsere gro?en Kosmologen: Die Geheim-Vorhersage. - Karl Kaos ?ber R?diger Thomas in dsa From fabiosantosart at gmail.com Wed Aug 21 18:50:56 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Wed, 21 Aug 2013 23:50:56 +0100 Subject: Basic Python Query In-Reply-To: References: Message-ID: On 21 Aug 2013 20:07, "Johannes Bauer" wrote: > > On 21.08.2013 11:11, Ulrich Eckhardt wrote: > > > That said, there is never a need for deriving > > from the Thread class, you can also use it to run a function without > > that. That way is IMHO clearer because the threading.Thread instance is > > not the thread, just like a File instance is not a file. Both just > > represent handles for manipulating the actual thing. > > Huh? That I find most curious. > > I *always* derive from threading.Thread and really like the way that > thread setup works (instanciate Thread handle, call start). Very > intuitive, never had the problems with clarity that you mentioned. Could > you elaborate on your suggestion? I don't seem to quite get it I'm afraid. > > Best regards, > Johannes I cannot tell whether you are trolling or are just new to this, but you don't always have to use threads. You use threads when you need multiple parts of your program running concurrently. Don't inherit Thread if all you are doing is a simple object with state, nor if your program does not need concurrency. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ned at nedbatchelder.com Wed Aug 21 20:06:25 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Wed, 21 Aug 2013 20:06:25 -0400 Subject: Basic Python Query In-Reply-To: References: Message-ID: <52155601.5060608@nedbatchelder.com> On 8/21/13 6:50 PM, F?bio Santos wrote: > > > On 21 Aug 2013 20:07, "Johannes Bauer" > wrote: > > > > On 21.08.2013 11:11, Ulrich Eckhardt wrote: > > > > > That said, there is never a need for deriving > > > from the Thread class, you can also use it to run a function without > > > that. That way is IMHO clearer because the threading.Thread > instance is > > > not the thread, just like a File instance is not a file. Both just > > > represent handles for manipulating the actual thing. > > > > Huh? That I find most curious. > > > > I *always* derive from threading.Thread and really like the way that > > thread setup works (instanciate Thread handle, call start). Very > > intuitive, never had the problems with clarity that you mentioned. Could > > you elaborate on your suggestion? I don't seem to quite get it I'm > afraid. > > > > Best regards, > > Johannes > > I cannot tell whether you are trolling or are just new to this, but > you don't always have to use threads. You use threads when you need > multiple parts of your program running concurrently. Don't inherit > Thread if all you are doing is a simple object with state, nor if your > program does not need concurrency. > > I think it is safe to assume that Johannes meant, "when I use threads, I never do it the way you suggested, I always derive from threading.Thread." --Ned. -------------- next part -------------- An HTML attachment was scrubbed... URL: From chandan_psr at yahoo.co.in Thu Aug 22 03:53:57 2013 From: chandan_psr at yahoo.co.in (chandan kumar) Date: Thu, 22 Aug 2013 15:53:57 +0800 (SGT) Subject: Basic Python Query In-Reply-To: Message-ID: <1377158037.22487.YahooMailBasic@web190503.mail.sg3.yahoo.com> Hi all, Sorry for not explaining question properly.Here Its not about threading and dont worry about any indentations.Please see below example class Call_Constructor(): def __init__(self): print "In __init__ " class Test_Constructor(Call_Constructor): def method(self): print " In Test_Constructor Class" ConstructInstance = Test_Constructor() When an instace is created for Test_Constructor class ,The code flows starts __init__ in Call_Constructor class.Whys is it like that. class Call_Constructor(): def __init__(self): print "In __init__ " class Test_Constructor(Call_Constructor): def __init__(self): print " In Test_Constructor Class" ConstructInstance = Test_Constructor() But for the above case Code flows starts from Test_Constructor(). Whats is the difference for both cases described above. Best Regards, Chandan. -------------------------------------------- On Thu, 22/8/13, Johannes Bauer wrote: Subject: Re: Basic Python Query To: python-list at python.org Date: Thursday, 22 August, 2013, 12:28 AM On 21.08.2013 11:11, Ulrich Eckhardt wrote: > That said, there is never a need for deriving > from the Thread class, you can also use it to run a function without > that. That way is IMHO clearer because the threading.Thread instance is > not the thread, just like a File instance is not a file. Both just > represent handles for manipulating the actual thing. Huh? That I find most curious. I *always* derive from threading.Thread and really like the way that thread setup works (instanciate Thread handle, call start). Very intuitive, never had the problems with clarity that you mentioned. Could you elaborate on your suggestion? I don't seem to quite get it I'm afraid. Best regards, Johannes -- >> Wo hattest Du das Beben nochmal GENAU vorhergesagt? > Zumindest nicht ?ffentlich! Ah, der neueste und bis heute genialste Streich unsere gro?en Kosmologen: Die Geheim-Vorhersage. - Karl Kaos ?ber R?diger Thomas in dsa -- http://mail.python.org/mailman/listinfo/python-list From fabiosantosart at gmail.com Thu Aug 22 04:46:33 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Thu, 22 Aug 2013 09:46:33 +0100 Subject: Basic Python Query In-Reply-To: <1377158037.22487.YahooMailBasic@web190503.mail.sg3.yahoo.com> References: <1377158037.22487.YahooMailBasic@web190503.mail.sg3.yahoo.com> Message-ID: On 22 Aug 2013 08:58, "chandan kumar" wrote: > > Hi all, > > Sorry for not explaining question properly.Here Its not about threading and dont worry about any indentations.Please see below example > > class Call_Constructor(): > def __init__(self): > print "In __init__ " > > class Test_Constructor(Call_Constructor): > def method(self): > print " In Test_Constructor Class" > > ConstructInstance = Test_Constructor() > > When an instace is created for Test_Constructor class ,The code flows starts __init__ in Call_Constructor class.Whys is it like that. > > > class Call_Constructor(): > def __init__(self): > print "In __init__ " > > > class Test_Constructor(Call_Constructor): > def __init__(self): > print " In Test_Constructor Class" > > ConstructInstance = Test_Constructor() > > But for the above case Code flows starts from Test_Constructor(). > > Whats is the difference for both cases described above. > > Best Regards, > Chandan. When creating an instance, Python will call __init__. In the first example there was only an __init__ method in the base class, so that one was used. On the second example, there were __init__ methods on both classes, but since you instantiated the subclass, the subclass's __init__ method was executed. Subclass methods have precedence over base class methods. If you want the __init__ method of the base class in the second example to be called, you can either remove the subclass __init__ method or call super(TestConstructor, self).__init__() in that method. That will call the base class's __init__. -------------- next part -------------- An HTML attachment was scrubbed... URL: From dfnsonfsduifb at gmx.de Thu Aug 22 04:49:59 2013 From: dfnsonfsduifb at gmx.de (Johannes Bauer) Date: Thu, 22 Aug 2013 10:49:59 +0200 Subject: Basic Python Query In-Reply-To: References: Message-ID: On 22.08.2013 00:50, F?bio Santos wrote: >>> That said, there is never a need for deriving >>> from the Thread class, you can also use it to run a function without >>> that. That way is IMHO clearer because the threading.Thread instance is >>> not the thread, just like a File instance is not a file. Both just >>> represent handles for manipulating the actual thing. >> >> Huh? That I find most curious. >> >> I *always* derive from threading.Thread and really like the way that >> thread setup works (instanciate Thread handle, call start). Very >> intuitive, never had the problems with clarity that you mentioned. Could >> you elaborate on your suggestion? I don't seem to quite get it I'm afraid. >> > I cannot tell whether you are trolling or are just new to this, Neither! > but you > don't always have to use threads. Obviously, I meant "I always derive from threading.Thread when I need to work with a thread". Thought this was blatantly obvious. That said, I think I also grossly misunderstood Ulrichs posting. He was talking about there no need deriving from threading.Thread when you don't need threads. What I understood was that, in order to use Threads, you could also just pass a closure to some static function of threading in order to fire up a thread. That may or may not be true. However, I find deriving from Thread, instanciating an object and firing up the thread by using ".start()" much more intuitive. Hope that clears it all up. Best regards, Johannes -- >> Wo hattest Du das Beben nochmal GENAU vorhergesagt? > Zumindest nicht ?ffentlich! Ah, der neueste und bis heute genialste Streich unsere gro?en Kosmologen: Die Geheim-Vorhersage. - Karl Kaos ?ber R?diger Thomas in dsa From ulrich.eckhardt at dominolaser.com Thu Aug 22 07:54:14 2013 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Thu, 22 Aug 2013 13:54:14 +0200 Subject: Basic Python Query In-Reply-To: References: Message-ID: <6oahea-u2n.ln1@satorlaser.homedns.org> Am 21.08.2013 20:58, schrieb Johannes Bauer: > On 21.08.2013 11:11, Ulrich Eckhardt wrote: > >> That said, there is never a need for deriving >> from the Thread class, you can also use it to run a function without >> that. That way is IMHO clearer because the threading.Thread instance is >> not the thread, just like a File instance is not a file. Both just >> represent handles for manipulating the actual thing. > > Huh? That I find most curious. > > I *always* derive from threading.Thread and really like the way that > thread setup works (instanciate Thread handle, call start). Very > intuitive, never had the problems with clarity that you mentioned. Could > you elaborate on your suggestion? I don't seem to quite get it I'm afraid. What is clear, convenient or not is largely a matter of taste. I'll try to explain my motivations though, maybe it helps... Firstly, there is one observation: The Python object of type Thread is one thing, the actual thread is another thing. This is similar to the File instance and the actual file. The Python object represents the other thing (thread or file) but it "is not" this thing. It is rather a handle to the file or thread. This is different for e.g. a string, where the Python object is the string. Due to this pairing between the actual thing and the handle, there is also some arity involved. For a single thread or file, there could be multiple Python objects for handling it, or maybe even none. When the Python object goes away, it doesn't necessarily affect the thread or file it represents. This already casts a doubt on the habit of deriving from the Thread type, just like deriving from the File type is highly unusual, as you are just deriving from a handle class. Secondly, a thread is even less a "thing" than a file but rather a process or an ongoing operation. As such, it runs code and uses data but it is neither code nor data. Also, it doesn't care which code or data it currently uses. Similarly, the code and data don't care which thread uses them (synchronization problems in multithreaded apps aside). You will find that most of the code called in a thread doesn't use the thread handle, which is another sign that it doesn't care. For that reason, it is unnecessary that "self" references a Thread object. This reduces coupling, as the same code could be called synchronously and asynchronously. The code shouldn't know or care from which thread it is called. In some cases, I even find it unnecessary to have a "self" at all, a thread can just as well run a non-member function. Also, even if it runs a memberfunction initially, it doesn't have to eventually. I find that forcing an OOP approach on things is flawed (OOP is a tool and not a goal) and prefer to make this a decision, but that is a different (although slightly related) issue. Thirdly, when you derive a class from Thread, you are exposing this baseclass' interface to the public, too, even if you don't intend to. This has both the unwanted aspect that you expose all public functions of the baseclass and that even if you mean "is a thread", it actually means "is a handle to a thread", which is even less expressive. Curously, you do that in order to override a single function that is only invoked once. I prefer passing "instance.function" as callable argument to a plain Thread instance for running this, which keeps the two nicely separate. For example, I have a TCP client class here that uses a service thread to handle the data transfer. The fact that there is a background thread should not be of concern to the user of my TCP client class. If I extended this to use two threads, it would even be impossible to derive from Thread for both of them. In summary, I find that modelling something to "use a thread" is much clearer than modelling it as "is a thread". Greetings from Hamburg! Uli From steve+comp.lang.python at pearwood.info Thu Aug 22 23:28:12 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Aug 2013 03:28:12 GMT Subject: Basic Python Query References: <6oahea-u2n.ln1@satorlaser.homedns.org> Message-ID: <5216d6cc$0$29986$c3e8da3$5496439d@news.astraweb.com> On Thu, 22 Aug 2013 13:54:14 +0200, Ulrich Eckhardt wrote: > Firstly, there is one observation: The Python object of type Thread is > one thing, the actual thread is another thing. This is similar to the > File instance and the actual file. The Python object represents the > other thing (thread or file) but it "is not" this thing. It is rather a > handle to the file or thread. This is different for e.g. a string, where > the Python object is the string. Well, not quite. To users coming from other languages, "string" has a clear and common meaning; it's an array of characters, possibly fixed- width in older languages, but these days usually variable-width but prefixed with the length (as in Pascal) or suffixed with a delimiter (usually \0, as in C). Or occasionally both. So as far as those people are concerned, Python strings aren't just a string. They are rich objects, with an object header. For example, we can see that there is a whole bunch of extra "stuff" required of a Python string before you even get to the array-of-characters: py> sys.getsizeof('') 25 25 bytes to store an empty string. Even if it had a four-byte length, and a four-byte NUL character at the end, that still leaves 17 bytes unaccounted for. So obviously Python strings contain a whole lot more than just low-level Pascal/C strings. So while I agree that it is sometimes useful to distinguish between a Python Thread object and the underlying low-level thread data structure it wraps, we can do the same with strings (and floats, and lists, and everything really). In any case, it's rare to need to do so. > Due to this pairing between the actual thing and the handle, there is > also some arity involved. For a single thread or file, there could be > multiple Python objects for handling it, or maybe even none. I don't think this is correct for threads. I don't believe there is any way to handle a low-level thread in Python except via an object of some sort. (With files, you can use the os module to work with low-level OS file descriptors, which are just integers.) > When the > Python object goes away, it doesn't necessarily affect the thread or > file it represents. That's certainly not true with file objects. When the file object goes out of scope, the underlying low-level file is closed. > This already casts a doubt on the habit of deriving > from the Thread type, just like deriving from the File type is highly > unusual, as you are just deriving from a handle class. In Python 3, there is no "File" type. There are *multiple* file types, depending on whether you open a file for reading or writing in binary or text mode: py> open('/tmp/junk', 'wb') <_io.BufferedWriter name='/tmp/junk'> py> open('/tmp/junk', 'rb') <_io.BufferedReader name='/tmp/junk'> py> open('/tmp/junk', 'w') <_io.TextIOWrapper name='/tmp/junk' mode='w' encoding='UTF-8'> But even if we limit the discussion to Python 2, it is unusual to inherit from File because File already does everything we normally want from a file. There's no need to override methods, so why make your own subclass? On the other hand, threads by their very nature have to be customized. The documentation is clear that there are two acceptable ways to do this: This class represents an activity that is run in a separate thread of control. There are two ways to specify the activity: by passing a callable object to the constructor, or by overriding the run() method in a subclass. http://docs.python.org/2/library/threading.html#thread-objects So to some degree, it is just a matter of taste which you use. [...] > In summary, I find that modelling something to "use a thread" is much > clearer than modelling it as "is a thread". The rest of your arguments seem good to me, but not compelling. I think they effectively boil down to personal taste. I write lots of non-OOP code, but when it comes to threads, I prefer to subclass Thread. -- Steven From ulrich.eckhardt at dominolaser.com Fri Aug 23 03:12:51 2013 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Fri, 23 Aug 2013 09:12:51 +0200 Subject: Basic Python Query In-Reply-To: <5216d6cc$0$29986$c3e8da3$5496439d@news.astraweb.com> References: <6oahea-u2n.ln1@satorlaser.homedns.org> <5216d6cc$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: Am 23.08.2013 05:28, schrieb Steven D'Aprano: > On Thu, 22 Aug 2013 13:54:14 +0200, Ulrich Eckhardt wrote: >> When the Python object goes away, it doesn't necessarily affect >> thethread or file it represents. > > That's certainly not true with file objects. When the file object goes > out of scope, the underlying low-level file is closed. Ahem, yes, but no: Calling close(fd) is not the same as destroying the file, I'm pretty sure it's still on my harddisk after that. That is also the difference to strings, where the Python object really is all there is to it. Similarly you can only customize the Python side of things with derivation, the other side will remain the same, apart from the data you write to the file or the code you run in the thread. Steven, thank you for taking the time to read and consider what I wrote, it is appreciated! Uli From rosuav at gmail.com Fri Aug 23 11:50:51 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 24 Aug 2013 01:50:51 +1000 Subject: Basic Python Query In-Reply-To: References: <6oahea-u2n.ln1@satorlaser.homedns.org> <5216d6cc$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Aug 23, 2013 at 5:12 PM, Ulrich Eckhardt wrote: > Am 23.08.2013 05:28, schrieb Steven D'Aprano: >> >> On Thu, 22 Aug 2013 13:54:14 +0200, Ulrich Eckhardt wrote: >>> >>> When the Python object goes away, it doesn't necessarily affect >>> thethread or file it represents. >> >> >> That's certainly not true with file objects. When the file object goes >> out of scope, the underlying low-level file is closed. > > > Ahem, yes, but no: Calling close(fd) is not the same as destroying the file, > I'm pretty sure it's still on my harddisk after that. That is also the > difference to strings, where the Python object really is all there is to it. > Similarly you can only customize the Python side of things with derivation, > the other side will remain the same, apart from the data you write to the > file or the code you run in the thread. The file object doesn't represent the file on the disk; it represents the "open file", which is a thing that you can have a handle (file descriptor) to. That "thing" is indeed destroyed when the file object is __del__'d, though it's possible to dispose of it sooner than that: >>> f = open("test","w") >>> with f: f.write("Hello, world!") 13 >>> f <_io.TextIOWrapper name='test' mode='w' encoding='cp1252'> f has been closed at this point, and if I now go to open it in another application, Python won't hold any locks; but the object still exists. However, the general expectation is that the file object and the open-file in the OS will correspond. ChrisA From bob.martin at excite.com Thu Aug 22 06:43:29 2013 From: bob.martin at excite.com (Bob Martin) Date: Thu, 22 Aug 2013 06:43:29 BST Subject: Basic Python Query References: Message-ID: in 704175 20130822 010625 Ned Batchelder wrote: >This is a multi-part message in MIME format. Please post in plain text, not HTML. From ned at nedbatchelder.com Thu Aug 22 09:45:43 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Thu, 22 Aug 2013 09:45:43 -0400 Subject: Basic Python Query In-Reply-To: References: Message-ID: <52161607.5010006@nedbatchelder.com> On 8/22/13 1:43 AM, Bob Martin wrote: > in 704175 20130822 010625 Ned Batchelder wrote: >> This is a multi-part message in MIME format. > Please post in plain text, not HTML. Sorry, Bob, I will try to remember in the future. I think Thunderbird is sending in the same format as the replied-to message, and I didn't notice. So that I understand what's going on, what's the bad thing that happens with a multi-part message? I would have thought that mail readers would choose the preferred part, or is it something to do with the message quoting? --Ned. PS: Bob: email to you is bouncing, as excite.com says you don't exist. From random832 at fastmail.us Thu Aug 22 14:57:49 2013 From: random832 at fastmail.us (random832 at fastmail.us) Date: Thu, 22 Aug 2013 14:57:49 -0400 Subject: Basic Python Query In-Reply-To: <52161607.5010006@nedbatchelder.com> References: <52161607.5010006@nedbatchelder.com> Message-ID: <1377197869.12555.12994929.2C181B15@webmail.messagingengine.com> On Thu, Aug 22, 2013, at 9:45, Ned Batchelder wrote: > So that I understand what's going on, what's the bad thing that happens > with a multi-part message? I would have thought that mail readers would > choose the preferred part, or is it something to do with the message > quoting? The bad thing that happens is baby Jesus cries. In other words, some people just don't like it, for no better reason than personal ideology (and/or they have a non-mime email client and don't like seeing the extra encoded stuff, and/or they have a fully HTML capable email client, don't like it, and can't be bothered learning how to turn off HTML viewing). There's really no way to have a productive discussion about it (though it might be worthwhile to ask whoever is in charge of the list to make it automatically strip html from messages, if enough people have a strong preference) From random832 at fastmail.us Fri Aug 23 01:08:35 2013 From: random832 at fastmail.us (random832 at fastmail.us) Date: Fri, 23 Aug 2013 01:08:35 -0400 Subject: Basic Python Query In-Reply-To: References: <52161607.5010006@nedbatchelder.com> Message-ID: <1377234515.22514.13153521.2AA22478@webmail.messagingengine.com> On Thu, Aug 22, 2013, at 18:22, Dennis Lee Bieber wrote: > Well... The main thing to understand is that this particular "forum" is > NOT JUST a mailing-list. It is cross-linked with the Usenet > comp.lang.python news-group (and that, unfortunately, is cross-linked to > Google-Groups). And to compound things, Gmane makes the mailing-list > source > available as a news-group on their server. > > So it isn't being viewed using just email clients -- I view it using a > newsreader. I don't see how that's relevant, since newsreaders in general, and (based on a quick google search for information about it) Forte Agent in particular, have mime implementations as full-featured as any mail client. From steve+comp.lang.python at pearwood.info Thu Aug 22 23:12:52 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Aug 2013 03:12:52 GMT Subject: Basic Python Query References: Message-ID: <5216d333$0$29986$c3e8da3$5496439d@news.astraweb.com> On Thu, 22 Aug 2013 09:45:43 -0400, Ned Batchelder wrote: > So that I understand what's going on, what's the bad thing that happens > with a multi-part message? I would have thought that mail readers would > choose the preferred part, or is it something to do with the message > quoting? This is not just a mailing list, it is also a newsgroup, and sending HTML content to a text newsgroup is considered rude. Just to add insult to injury, one of the most popular (and otherwise excellent) news readers, Pan, treats HTML as "plain text", and displays junk like this at the end of your HTML posts: On 8/21/13 6:50 PM, Fábio Santos wrote:


On 21 Aug 2013 20:07, "Johannes Bauer" <
References: Message-ID: On 22.08.2013 02:06, Ned Batchelder wrote: >> I cannot tell whether you are trolling or are just new to this, but >> you don't always have to use threads. You use threads when you need >> multiple parts of your program running concurrently. Don't inherit >> Thread if all you are doing is a simple object with state, nor if your >> program does not need concurrency. >> > I think it is safe to assume that Johannes meant, "when I use threads, I > never do it the way you suggested, I always derive from threading.Thread." Yup, that's what I was aiming for. Best regards, Johannes -- >> Wo hattest Du das Beben nochmal GENAU vorhergesagt? > Zumindest nicht ?ffentlich! Ah, der neueste und bis heute genialste Streich unsere gro?en Kosmologen: Die Geheim-Vorhersage. - Karl Kaos ?ber R?diger Thomas in dsa From nh.jones01 at gmail.com Wed Aug 21 05:17:38 2013 From: nh.jones01 at gmail.com (Norah Jones) Date: Wed, 21 Aug 2013 09:17:38 +0000 Subject: How to change scrollbar color in pygtk ? Message-ID: Hi, I Tried the below code, the color is not reflected, Am i missing something? #add description box beside test cases testCaseDescWindow = gtk.ScrolledWindow() testCaseDescWindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) testCaseDescWindow.get_vscrollbar().modify_fg(gtk.STATE_NORMAL,gtk.gdk.color_parse('#40515F')) testCaseDescWindow.get_hscrollbar().modify_fg(gtk.STATE_NORMAL,gtk.gdk.color_parse('#40515F')) Thanks, Norah Jones From vijayendramunikoti at gmail.com Wed Aug 21 05:24:24 2013 From: vijayendramunikoti at gmail.com (vijayendramunikoti at gmail.com) Date: Wed, 21 Aug 2013 02:24:24 -0700 (PDT) Subject: Matrix sort Message-ID: <56e44970-e672-4ab9-8f7c-2183f287a274@googlegroups.com> Hi I have a matrix of numbers representing the nodal points as follows: Element No. Nodes 1 1 2 3 4 2 5 6 7 8 3 2 3 9 10 ........................... ........................... x 9 10 11 12 ........................... so this is a matrix of numbers 4 x n Elements 1 and 3 are neighbours (as they share nodes 2 3). Similarly elements 3 and x are neighbours (they share nodes 9 and 10). I want to sort the matrix in such a way all the elements are sequentially arranged. How could I script it? can any one help me? Thanks! Vijayendra From oscar.j.benjamin at gmail.com Wed Aug 21 06:40:02 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Wed, 21 Aug 2013 11:40:02 +0100 Subject: Matrix sort In-Reply-To: <56e44970-e672-4ab9-8f7c-2183f287a274@googlegroups.com> References: <56e44970-e672-4ab9-8f7c-2183f287a274@googlegroups.com> Message-ID: On 21 August 2013 10:24, wrote: > Hi > I have a matrix of numbers representing the nodal points as follows: > > Element No. Nodes > > 1 1 2 3 4 > 2 5 6 7 8 > 3 2 3 9 10 > ........................... > ........................... > x 9 10 11 12 > ........................... > > so this is a matrix of numbers 4 x n > Elements 1 and 3 are neighbours (as they share nodes 2 3). Similarly elements 3 and x are neighbours (they share nodes 9 and 10). I want to sort the matrix in such a way all the elements are sequentially arranged. How could I script it? can any one help me? I think you want a topological sort algorithm. See here: http://en.wikipedia.org/wiki/Topological_sorting Before that though you'll want to preprocess your matrix into a data structure that allows you to easily find the elements adjacent to any given element. A list of lists is one approach: graph = [ [3], # nodes adjacent to element 1 [], # element 2 [1, x], # element 3 ... [3] # element x ] Oscar From yigit at jotform.com Wed Aug 21 08:51:24 2013 From: yigit at jotform.com (yigit at jotform.com) Date: Wed, 21 Aug 2013 05:51:24 -0700 (PDT) Subject: Create an App with Python & Win $5000 Message-ID: Hi all, JotForm just announced its developer contest with their newly released API with a grand prize of $5000 to the best app and $500 for other categories. The API library can be used with Python so you can create endless apps with it. The deadline for the contest is September 24, 2013. Apply to the contest from http://developers.jotform.com/competition/ From commentholder at gmail.com Wed Aug 21 10:55:04 2013 From: commentholder at gmail.com (Comment Holder) Date: Wed, 21 Aug 2013 07:55:04 -0700 (PDT) Subject: I wonder if I would be able to collect data from such page using Python Message-ID: Hi, I am totally new to Python. I noticed that there are many videos showing how to collect data from Python, but I am not sure if I would be able to accomplish my goal using Python so I can start learning. Here is the example of the target page: http://and.medianewsonline.com/hello.html In this example, there are 10 articles. What I exactly need is to do the following: 1- Collect the article title, date, source, and contents. 2- I need to be able to export the final results to excel or a database client. That is, I need to have all of those specified in step 1 in one row, while each of them saved in separate column. For example: Title1 Date1 Source1 Contents1 Title2 Date2 Source2 Contents2 I appreciate any advise regarding my case. Thanks & Regards// From joel.goldstick at gmail.com Wed Aug 21 11:30:16 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Wed, 21 Aug 2013 11:30:16 -0400 Subject: I wonder if I would be able to collect data from such page using Python In-Reply-To: References: Message-ID: On Wed, Aug 21, 2013 at 10:55 AM, Comment Holder wrote: > Hi, > I am totally new to Python. I noticed that there are many videos showing how to collect data from Python, but I am not sure if I would be able to accomplish my goal using Python so I can start learning. > > Here is the example of the target page: > http://and.medianewsonline.com/hello.html > In this example, there are 10 articles. > > What I exactly need is to do the following: > 1- Collect the article title, date, source, and contents. > 2- I need to be able to export the final results to excel or a database client. That is, I need to have all of those specified in step 1 in one row, while each of them saved in separate column. For example: > > Title1 Date1 Source1 Contents1 > Title2 Date2 Source2 Contents2 > > I appreciate any advise regarding my case. > > Thanks & Regards// > -- > http://mail.python.org/mailman/listinfo/python-list I'm guessing that you are not only new to Python, but that you haven't much experience in writing computer programs at all. So, you need to do that. There is a good tutorial on the python site, and lots of links to other resources. then do this: 1. write code to access the page you require. The Requests module can help with that 2. write code to select the data you want. The BeautifulSoup module is excellent for this 3. write code to save your data in comma separated value format. 4. import to excel or wherever Now, go off and write the code. When you get stuck, copy and paste the portion of the code that is giving you problems, along with the traceback. You can also get help at the python-tutor mailing list -- Joel Goldstick http://joelgoldstick.com From commentholder at gmail.com Wed Aug 21 11:44:49 2013 From: commentholder at gmail.com (Comment Holder) Date: Wed, 21 Aug 2013 08:44:49 -0700 (PDT) Subject: I wonder if I would be able to collect data from such page using Python In-Reply-To: References: Message-ID: Many thanks Joel, You are right to some extent. I come from Finance background, but I am very familiar with what could be referred to as non-native languages such as Matlab, VBA,.. actually, I have developed couple of complete programs. I have asked this question, because I am a little worried about the structure of this particular page, as there are no specific defined classes. I know how powerful Python is, but I wonder if it could do the job with this particular page. Again, many thanks Joel, I appreciate your guidance. All Best// From joel.goldstick at gmail.com Wed Aug 21 11:58:30 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Wed, 21 Aug 2013 11:58:30 -0400 Subject: I wonder if I would be able to collect data from such page using Python In-Reply-To: References: Message-ID: On Wed, Aug 21, 2013 at 11:44 AM, Comment Holder wrote: > Many thanks Joel, > > You are right to some extent. I come from Finance background, but I am very familiar with what could be referred to as non-native languages such as Matlab, VBA,.. actually, I have developed couple of complete programs. > > I have asked this question, because I am a little worried about the structure of this particular page, as there are no specific defined classes. > > I know how powerful Python is, but I wonder if it could do the job with this particular page. > > Again, many thanks Joel, I appreciate your guidance. > All Best// > -- > http://mail.python.org/mailman/listinfo/python-list Your biggest hurdle will be to get proficient with python. Give yourself a weekend with a good tutorial. You won't be very skilled, but you will get the gist of things. Also, google Beautiful Soup. You need the latest version. Its v4 I think. They have a GREAT tutorial. Spend a few hours with it and you will see your way to get the data you want from your web pages. Since you gave a sample web page, I am guessing that you need to log in to the site for 'real data'. For that, you need to really understand stuff that you might not. At any rate, study the Requests Module documentation. Python comes with urllib, and urllib2 that cover the same ground, but Requests is a lot simpler to understand -- Joel Goldstick http://joelgoldstick.com From commentholder at gmail.com Wed Aug 21 13:41:26 2013 From: commentholder at gmail.com (Comment Holder) Date: Wed, 21 Aug 2013 10:41:26 -0700 (PDT) Subject: I wonder if I would be able to collect data from such page using Python In-Reply-To: References: Message-ID: <02caf0a8-1506-4746-9136-3452cbdea14b@googlegroups.com> Dear Joel, Many thanks for your help - I think I shall start with this way and see how it goes. My concerns were if the task can be accomplished with Python, and from your posts, I guess it can - so I shall give it a try :). Again, thanks a lot & all best// From joel.goldstick at gmail.com Wed Aug 21 13:52:18 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Wed, 21 Aug 2013 13:52:18 -0400 Subject: I wonder if I would be able to collect data from such page using Python In-Reply-To: <02caf0a8-1506-4746-9136-3452cbdea14b@googlegroups.com> References: <02caf0a8-1506-4746-9136-3452cbdea14b@googlegroups.com> Message-ID: On Wed, Aug 21, 2013 at 1:41 PM, Comment Holder wrote: > Dear Joel, > > Many thanks for your help - I think I shall start with this way and see how it goes. My concerns were if the task can be accomplished with Python, and from your posts, I guess it can - so I shall give it a try :). > > Again, thanks a lot & all best// > > -- > http://mail.python.org/mailman/listinfo/python-list You're welcome. One thought popped into my mind. Since the site seems to be from the Wall Street Journal, you may want to look into whether they have an api for searching and retrieving articles. If they do, this would be simpler and probably safer than parsing web pages. From time to time, websites change their layout, which would probably break your program. However APIs are more stable good luck to you -- Joel Goldstick http://joelgoldstick.com From tjreedy at udel.edu Wed Aug 21 15:18:19 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 21 Aug 2013 15:18:19 -0400 Subject: I wonder if I would be able to collect data from such page using Python In-Reply-To: References: <02caf0a8-1506-4746-9136-3452cbdea14b@googlegroups.com> Message-ID: On 8/21/2013 1:52 PM, Joel Goldstick wrote: > On Wed, Aug 21, 2013 at 1:41 PM, Comment Holder wrote: >> Many thanks for your help - I think I shall start with this way and see how it goes. My concerns were if the task can be accomplished with Python, and from your posts, I guess it can - so I shall give it a try :). CM: You still seem a bit doubtful. If you are wondering why no one else has answered, it is because Joel has given you a really good answer that cannot be beat without writing your code for you. > You're welcome. One thought popped into my mind. Since the site > seems to be from the Wall Street Journal, you may want to look into > whether they have an api for searching and retrieving articles. If > they do, this would be simpler and probably safer than parsing web > pages. From time to time, websites change their layout, which would > probably break your program. However APIs are more stable Including this suggestion, which I did not think of. -- Terry Jan Reedy From commentholder at gmail.com Thu Aug 22 10:58:31 2013 From: commentholder at gmail.com (Comment Holder) Date: Thu, 22 Aug 2013 07:58:31 -0700 (PDT) Subject: I wonder if I would be able to collect data from such page using Python In-Reply-To: References: <02caf0a8-1506-4746-9136-3452cbdea14b@googlegroups.com> Message-ID: Dear Terry, Many thanks for your comments. Actually I was, because the target-page doesn't have a neat structure. But, after all of your contributions, I think the task can be achieved very well with Python. Thanks again & all best// From piet at vanoostrum.org Thu Aug 22 00:54:36 2013 From: piet at vanoostrum.org (Piet van Oostrum) Date: Thu, 22 Aug 2013 00:54:36 -0400 Subject: I wonder if I would be able to collect data from such page using Python References: Message-ID: Comment Holder writes: > Hi, > I am totally new to Python. I noticed that there are many videos showing how to collect data from Python, but I am not sure if I would be able to accomplish my goal using Python so I can start learning. > > Here is the example of the target page: > http://and.medianewsonline.com/hello.html > In this example, there are 10 articles. > > What I exactly need is to do the following: > 1- Collect the article title, date, source, and contents. > 2- I need to be able to export the final results to excel or a database client. That is, I need to have all of those specified in step 1 in one row, while each of them saved in separate column. For example: > > Title1 Date1 Source1 Contents1 > Title2 Date2 Source2 Contents2 > > I appreciate any advise regarding my case. > > Thanks & Regards// Here is an attempt for you. It uses BeatifulSoup 4. It is written in Python 3.3, so if you want to use Python 2.x you will have to make some small changes, like from urllib import urlopen and probably something with the print statements. The formatting in columns is left as an exercise for you. I wonder how you would want that with multiparagraph contents. -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: extract.py URL: -------------- next part -------------- -- Piet van Oostrum WWW: http://pietvanoostrum.com/ PGP key: [8DAE142BE17999C4] From commentholder at gmail.com Thu Aug 22 11:03:32 2013 From: commentholder at gmail.com (Comment Holder) Date: Thu, 22 Aug 2013 08:03:32 -0700 (PDT) Subject: I wonder if I would be able to collect data from such page using Python In-Reply-To: References: Message-ID: Dear Piet, Many thanks for your assistance. It is much appreciated. I have just installed Python 3.3.2 and BeautifulSoup 4.3.1. I tried running the code, but run into some syntax errors. > I wonder how you would want that with multiparagraph contents. I am looking to save all the paragraphs of an article in one field, so that, the afterwards-analysis becomes easier. As I am new, I won't ask for assistance before I get some general idea about Python. I shall dedicate the weekend for this purpose, or at least Sunday. Once I am done, I will post my results back in here. Thanks again & all best// From rosuav at gmail.com Thu Aug 22 11:11:39 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 23 Aug 2013 01:11:39 +1000 Subject: I wonder if I would be able to collect data from such page using Python In-Reply-To: References: Message-ID: On Fri, Aug 23, 2013 at 1:03 AM, Comment Holder wrote: > As I am new, I won't ask for assistance before I get some general idea about Python. I shall dedicate the weekend for this purpose, or at least Sunday. Once I am done, I will post my results back in here. Smart move :) I strongly recommend the inbuilt tutorial, if you haven't seen it already: http://docs.python.org/3/tutorial/ And you're using the current version, which is good. Saves the hassle of figuring out what's different in an old version. All the best! ChrisA From fredrik at dolda2000.com Wed Aug 21 11:46:32 2013 From: fredrik at dolda2000.com (Fredrik Tolf) Date: Wed, 21 Aug 2013 17:46:32 +0200 (CEST) Subject: Unpickling data with classes from dynamic modules Message-ID: Dear list, I have a system in which I load modules dynamically every now and then (that is, creating a module with types.ModuleType, compiling the code for the module and then executing it in the module with exec()), and where I would wish to be able to have classes in those modules containing classes that could pickled, and then unpickled again. The problem with that, currently, is of course two-fold: The dynamically loaded module may not be loaded at the time when the unpickling takes place, but even if it were, it isn't registered in sys.modules, so the unpickler can't find it either way. And, of course, that is detected already when pickling. Is there any way to fix this, at all? I considered trying to create subclasses of the pickler and unpickler that pickle a reference to a module loader and data for the particular module along with a class that comes from such a module, by overriding Pickler.save_global and Unpickler.load_global. Unfortunately, however, those functions aren't part of the public interface and can't be overridden when the C-pickle module is used instead (which, obviously, is what normally happens). Is there any way around this without having to modify the pickle module itself? -- Fredrik Tolf From ramit.prasad at jpmorgan.com.dmarc.invalid Wed Aug 21 15:59:47 2013 From: ramit.prasad at jpmorgan.com.dmarc.invalid (Prasad, Ramit) Date: Wed, 21 Aug 2013 19:59:47 +0000 Subject: Unpickling data with classes from dynamic modules In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF4741867F701@SCACMX008.exchad.jpmchase.net> Fredrik Tolf wrote: > > Dear list, > > I have a system in which I load modules dynamically every now and then > (that is, creating a module with types.ModuleType, compiling the code for > the module and then executing it in the module with exec()), and where I > would wish to be able to have classes in those modules containing classes > that could pickled, and then unpickled again. > > The problem with that, currently, is of course two-fold: The dynamically > loaded module may not be loaded at the time when the unpickling takes > place, but even if it were, it isn't registered in sys.modules, so the > unpickler can't find it either way. And, of course, that is detected > already when pickling. > > Is there any way to fix this, at all? > > I considered trying to create subclasses of the pickler and unpickler that > pickle a reference to a module loader and data for the particular module > along with a class that comes from such a module, by overriding > Pickler.save_global and Unpickler.load_global. Unfortunately, however, > those functions aren't part of the public interface and can't be > overridden when the C-pickle module is used instead (which, obviously, is > what normally happens). > > Is there any way around this without having to modify the pickle module > itself? > > -- > > Fredrik Tolf > -- I believe rather than subclassing the pickler, you are expected to change the behavior from within the class via __getstate__ and __setstate__. http://docs.python.org/2/library/pickle.html#object.__getstate__ Although, for your use case (loading unknown classes) the section on pickle and extension types may be more appropriate. http://docs.python.org/2/library/pickle.html#pickling-and-unpickling-extension-types Maybe all you need to add is implementation for obj.__reduce__ ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From fredrik at dolda2000.com Wed Aug 21 23:05:01 2013 From: fredrik at dolda2000.com (Fredrik Tolf) Date: Thu, 22 Aug 2013 05:05:01 +0200 (CEST) Subject: Unpickling data with classes from dynamic modules In-Reply-To: <5B80DD153D7D744689F57F4FB69AF4741867F701@SCACMX008.exchad.jpmchase.net> References: <5B80DD153D7D744689F57F4FB69AF4741867F701@SCACMX008.exchad.jpmchase.net> Message-ID: On Wed, 21 Aug 2013, Prasad, Ramit wrote: > Fredrik Tolf wrote: >> [...] >> I considered trying to create subclasses of the pickler and unpickler that >> pickle a reference to a module loader and data for the particular module >> along with a class that comes from such a module, by overriding >> Pickler.save_global and Unpickler.load_global. Unfortunately, however, >> those functions aren't part of the public interface and can't be >> overridden when the C-pickle module is used instead (which, obviously, is >> what normally happens). >> [...] > > I believe rather than subclassing the pickler, you are expected to > change the behavior from within the class via __getstate__ and __setstate__. Well, that clearly wouldn't work, since that still assumes the module can be found. > Maybe all you need to add is implementation for obj.__reduce__ That would certainly work, and I guess I could perhaps use it as a work-around, but that would mean I'd have to mark every single such class as such in some way or another. What I'm looking for is a solution that could make them picklable transparently, just like normal. -- Fredrik Tolf From dieter at handshake.de Thu Aug 22 02:35:29 2013 From: dieter at handshake.de (dieter) Date: Thu, 22 Aug 2013 08:35:29 +0200 Subject: Unpickling data with classes from dynamic modules References: <5B80DD153D7D744689F57F4FB69AF4741867F701@SCACMX008.exchad.jpmchase.net> Message-ID: <871u5mb5ce.fsf@handshake.de> Fredrik Tolf writes: > ... >> Maybe all you need to add is implementation for obj.__reduce__ > > That would certainly work, and I guess I could perhaps use it as a > work-around, but that would mean I'd have to mark every single such > class as such in some way or another. What I'm looking for is a > solution that could make them picklable transparently, just like > normal. This is not (easily) possible. In the "normal/transparent" case, "pickle" treats the class/type part of a class/type instance (unless it explicitely knows the "class/type" as requiring special treatment) as a "global" (essentially a module path and a name) and it expects that "global"s can simply be imported. The ZODB ("Zope Object DataBase") is build on top of "pickle" ("cpickle", in fact) - and it provides for an application level module loader. Zope is using this feature to implement so called "ZClasses", classes the code of which are maintained inside the ZODB. Thus, this is a case similar to yours (your dynamic modules correspond to code maintained in the ZODB). This implies that there are ways to achieve something like this (in principle) and you may look how it is done in Zope to get some ideas. However, it is likely to be difficult. Your "module loader" must be able to recreate your dynamic modules - for this, it needs all the creation input data. In the Zope case, this comes from the ZODB. You would need to find a way to pass this information to the unpickling process... From anthra.norell at bluewin.ch Wed Aug 21 12:29:59 2013 From: anthra.norell at bluewin.ch (F.R.) Date: Wed, 21 Aug 2013 18:29:59 +0200 Subject: A data transformation framework. A presentation inviting commentary. Message-ID: <5214EB07.6000801@bluewin.ch> Hi all, In an effort to do some serious cleaning up of a hopelessly cluttered working environment, I developed a modular data transformation system that pretty much stands. I am very pleased with it. I expect huge time savings. I would share it, if had a sense that there is an interest out there and would appreciate comments. Here's a description. I named the module TX: The nucleus of the TX system is a Transformer class, a wrapper for any kind of transformation functionality. The Transformer takes input as calling argument and returns it transformed. This design allows the assembly of transformation chains, either nesting calls or better, using the class Chain, derived from 'Transformer' and 'list'. A Chain consists of a sequence of Transformers and is functionally equivalent to an individual Transformer. A high degree of modularity results: Chains nest. Another consequence is that many transformation tasks can be handled with a relatively modest library of a few basic prefabricated Transformers from which many different Chains can be assembled on the fly. A custom Transformer to bridge an eventual gap is quickly written and tested, because the task likely is trivial. A good analogy of the TX methodology is a road map with towns scattered all over it and highways connecting them. To get from any town to any other one is a simple matter of hopping the towns in between. The TX equivalent of the towns are data formats, the equivalent of the highways are TX Transformers. They are not so much thought of in terms of what they do than in terms of the formats they take and give. Designing a library of Transformers is essentially a matter of establishing a collection of standard data formats. First the towns, then the highways. A feature of the TX Transformer is that it retains both its input and output. This makes a Chain a breeze to build progressively, link by link, and also makes debugging easy: If a Chain doesn't work, Chain.show () reveals the failing link as the first one that has no output. It can be replaced with a corrected instance, as one would replace a blown fuse. Running the Chain again without input makes it have another try. Parameter passing runs on a track that is completely separate from the payload track. Parameters can be set in order to configure a Chain prior to running it, or can be sent at runtime by individual Transformers to its siblings and their progeny. Parameters are keyed and get picked up by those Chain links whose set of pre-defined keys includes the parameter's key. Unintended pick-ups with coincidentally shared keys for unrelated parameters can be prevented by addressing parameters to individual Translators. Below an application example. Five custom classes at the end exemplify the pattern. I join the post also as attachment, in case some auto-line-wrap messes up this text. Commentary welcome Frederic ---------------------------------------------------------------------------------------------------- An example of use: Download historic stock quotes from Yahoo Finance for a given range of dates and a list of symbols, delete a column and add three, insert the data in a MySQL table. Also write them to temporary files in tabular form for verification. "make_quotes_writer ()" returns a custom transformation tree. "run_quotes ()" makes such a tree, sets it on a given time range and runs it on a list of symbols. (Since Yahoo publishes the data for downloading, I presume it's okay to do it this way. This is a demo of TX, however, and should not be misconstrued as an encouragement to violate any publisher's terms of service.) import TX, yahoo_historic_quotes as yhq def make_quotes_writer (): Visualizer = TX.Chain ( yhq.percent (), TX.Table_Maker (has_header = True), TX.Table_Writer (), name = 'Visualizer' ) To_DB = TX.Chain (yhq.header_stripper(), TX.DB_Writer(table_name = 'quotes'), name = 'To DB') To_File = TX.Chain (Visualizer, TX.File_Writer (), name = 'To File') Splitter = TX.Splitter (To_DB, To_File, name = 'Splitter') Quotes = TX.Chain ( yhq.yahoo_quotes (), TX.CSV_To_List (delimiter = ','), TX.Numerizer (), yhq.wiggle_and_trend (), yhq.symbol (), Splitter, name = 'Quotes' ) return Quotes >>> Quotes = make_quotes_writer () >>> Quotes.show_tree() Quotes Quotes[0] - Yahoo Quotes Quotes[1] - CSV To List Quotes[2] - Numerizer Quotes[3] - Wiggle and Trend Quotes[4] - Symbol Quotes[5] - Splitter Quotes[5][0] - To DB Quotes[5][0][0] - Header Stripper Quotes[5][0][1] - DB Writer Quotes[5][1] - To File Quotes[5][1][0] - Visualizer Quotes[5][1][0][0] - Percent Quotes[5][1][0][1] - Table Maker Quotes[5][1][0][2] - Table Writer Quotes[5][1][1] - File Writer def run_quotes (symbols, from_date = '1970-01-01', to_date = '2099-12-31'): '''Downloads historic stock quotes from Yahoo Finance. Inserts data into MySQL table "quotes" relying on index to reject repeat insertions. Also writes the data in a table format to a temporary file, one for each symbol. ''' Quotes = make_quotes_writer () Quotes.set (from_date = from_date, to_date = to_date) FW = Quotes [5][1][1] # File Writer for symbol in symbols: outfilename = '/tmp/%s' % symbol FW.set (terminal = outfilename) Quotes.set (symbol = symbol) Quotes () >>> run_quotes (('MO', 'SU'), '2013-08-10') Checking: >>> DBRC = TX.DB_Run_Command () >>> for record in DBRC ("select * from quotes where symbol in ('MO','SU') and date >= 20130810;"): print record (datetime.date(2013, 8, 16), 'MO', 34.51, 34.63, 34.25, 34.29, 8364900L, 0.0110337, -0.00638792) (datetime.date(2013, 8, 15), 'MO', 34.78, 34.93, 34.5, 34.57, 15144400L, 0.0123866, -0.00604926) (datetime.date(2013, 8, 14), 'MO', 35.23, 35.25, 34.81, 35.06, 5871900L, 0.0125607, -0.00485298) (datetime.date(2013, 8, 13), 'MO', 35.17, 35.27, 35.01, 35.22, 5919700L, 0.00739898, 0.00142288) (datetime.date(2013, 8, 12), 'MO', 35.25, 35.28, 35.05, 35.16, 4884300L, 0.00654059, -0.00255936) (datetime.date(2013, 8, 16), 'SU', 33.84, 34.63, 33.81, 34.18, 7396600L, 0.0239626, 0.00993571) (datetime.date(2013, 8, 15), 'SU', 32.84, 33.99, 32.7, 33.94, 7747100L, 0.0386865, 0.0329885) (datetime.date(2013, 8, 14), 'SU', 32.06, 32.91, 32.02, 32.72, 4690200L, 0.0274141, 0.0203296) (datetime.date(2013, 8, 13), 'SU', 32.04, 32.18, 31.94, 32.01, 2051900L, 0.00748596, -0.000935745) (datetime.date(2013, 8, 12), 'SU', 32.15, 32.35, 31.99, 32.15, 2870500L, 0.0111906, 0.0) >>> FR = TX.File_Reader () >>> print FR ('/tmp/SU') Date | Symbol | Open | High | Low | Close | Volume | Wiggle | Trend | 2013-08-16 | SU | 33.84 | 34.63 | 33.81 | 34.18 | 7396600.00 | 2.40% | +0.99% | 2013-08-15 | SU | 32.84 | 33.99 | 32.70 | 33.94 | 7747100.00 | 3.87% | +3.30% | 2013-08-14 | SU | 32.06 | 32.91 | 32.02 | 32.72 | 4690200.00 | 2.74% | +2.03% | 2013-08-13 | SU | 32.04 | 32.18 | 31.94 | 32.01 | 2051900.00 | 0.75% | -0.09% | 2013-08-12 | SU | 32.15 | 32.35 | 31.99 | 32.15 | 2870500.00 | 1.12% | +0.00% | ---------------------------------------------------------------------------------------------------- Each Transformer retains input and output, freezing the state of a composite after each run. This makes developing step by step and debugging a breeze. If a Chain fails, the failing link is the first one to have no output. Chain.show () displays them all in sequence. The deficient Transformer can be replaced with a fixed instance and the Chain run again without input for another try. >>> Quotes.replace (Fixed_Numerizer, 2) >>> catch = Quotes () # catch removes the risk of flooding the display ---------------------------------------------------------------------------------------------------- These are the five custom classes. The decorator "setup" updates parameters and prevents needless reruns comparing time stamps. class wiggle_and_trend (TX.Transformer): '''Deletes column "Adj Close" and adds columns "Wiggle" and "Trend". The values are ratios: (day's high - day's low) / mean, and (day's close - day's open) / mean. ''' name = 'Wiggle and Trend' @TX.setup def transform (self): input = self.Input.data output = [] output.append (tuple (input[0][:-1]) + ('Wiggle', 'Trend')) for i in range (1, len (input)): date, open, high, low, close, vol, adj = input [i] wiggle = high - low mean = (high + low) / 2.0 wiggle_ratio = wiggle / mean trend = close - open trend_ratio = trend / mean output.append ((date, open, high, low, close, vol, wiggle_ratio, trend_ratio)) self.Output.take (output) class symbol (TX.Transformer): 'Adds a column Symbol' name = 'Symbol' def __init__ (self): TX.Transformer.__init__ (self, symbol = None) @TX.setup def transform (self): symbol = self.get ('symbol') if not symbol: self.log ('No symbol!') else: input = self.Input.data output = [] output.append ((input[0][0], 'Symbol') + input [0][1:]) for i in range (1, len (input)): output.append ((input[i][0], symbol) + input [i][1:]) self.Output.take (output) class percent (TX.Transformer): 'Converts float ratio to percent for better legibility' name = 'Percent' @TX.setup def transform (self): input = self.Input.data output = [input [0]] for i in range (1, len (input)): wiggle = '%5.2f%%' % (input [i][7] * 100.0) trend = '%+5.2f%%' % (input [i][8] * 100.0) output.append (input [i][:7] + (wiggle, trend)) self.Output.take (output) class header_stripper (TX.Transformer): 'Header names are not meant for insertion into data base table' name = 'Header Stripper' @TX.setup def transform (self): self.Output.take (self.Input.data [1:]) class yahoo_quotes (TX.WWW_Reader): 'Gets historic stock quotes from Yahoo Finance' import urllib name = 'Yahoo Quotes' URL_TRUNK = 'http://ichart.finance.yahoo.com/table.csv' def __init__ (self, from_date = '1970-01-01', to_date = '2099-12-31', **keywords): private_keys = { 'symbol' : None, 'from_date' : from_date, 'to_date' : to_date, 'url_trunk' : self.URL_TRUNK, } private_keys.update (keywords) TX.WWW_Reader.__init__ (self, **private_keys) def make_url (self): fy, fm, fd = self.get ('from_date').split ('-') ty, tm, td = self.get ('to_date').split ('-') fm = '%0d' % (int (fm) - 1) tm = '%0d' % (int (tm) - 1) symbol = self.get ('symbol') qs = ( ('s',symbol), ('a',fm),('b',fd),('c',fy), ('d',tm),('e',td),('f',ty), ('g','d'), ('ignore', '.csv'), ) self.set (url_parameters = qs) TX.WWW_Reader.make_url (self) ---------------------------------------------------------------------------------------------------- Let me know what you think. -------------- next part -------------- Hi all, In an effort to do some serious cleaning up of a hopelessly cluttered working environment, I developed a modular data transformation system that pretty much stands. I am very pleased with it. I expect huge time savings. I would share it, if had a sense that there is an interest out there and would appreciate comments. Here's a description. I named the module TX: The nucleus of the TX system is a Transformer class, a wrapper for any kind of transformation functionality. The Transformer takes input as calling argument and returns it transformed. This design allows the assembly of transformation chains, either nesting calls or better, using the class Chain, derived from 'Transformer' and 'list'. A Chain consists of a sequence of Transformers and is functionally equivalent to an individual Transformer. A high degree of modularity results: Chains nest. Another consequence is that many transformation tasks can be handled with a relatively modest library of a few basic prefabricated Transformers from which many different Chains can be assembled on the fly. A custom Transformer to bridge an eventual gap is quickly written and tested, because the task likely is trivial. A good analogy of the TX methodology is a road map with towns scattered all over it and highways connecting them. To get from any town to any other one is a simple matter of hopping the towns in between. The TX equivalent of the towns are data formats, the equivalent of the highways are TX Transformers. They are not so much thought of in terms of what they do than in terms of the formats they take and give. Designing a library of Transformers is essentially a matter of establishing a collection of standard data formats. First the towns, then the highways. A feature of the TX Transformer is that it retains both its input and output. This makes a Chain a breeze to build progressively, link by link, and also makes debugging easy: If a Chain doesn't work, Chain.show () reveals the failing link as the first one that has no output. It can be replaced with a corrected instance, as one would replace a blown fuse. Running the Chain again without input makes it have another try. Parameter passing runs on a track that is completely separate from the payload track. Parameters can be set in order to configure a Chain prior to running it, or can be sent at runtime by individual Transformers to its siblings and their progeny. Parameters are keyed and get picked up by those Chain links whose set of pre-defined keys includes the parameter's key. Unintended pick-ups with coincidentally shared keys for unrelated parameters can be prevented by addressing parameters to individual Translators. Below an application example. Five custom classes at the end exemplify the pattern. I join the post also as attachment, in case some auto-line-wrap messes up this text. Commentary welcome Frederic ---------------------------------------------------------------------------------------------------- An example of use: Download historic stock quotes from Yahoo Finance for a given range of dates and a list of symbols, delete a column and add three, insert the data in a MySQL table. Also write them to temporary files in tabular form for verification. "make_quotes_writer ()" returns a custom transformation tree. "run_quotes ()" makes such a tree, sets it on a given time range and runs it on a list of symbols. (Since Yahoo publishes the data for downloading, I presume it's okay to do it this way. This is a demo of TX, however, and should not be misconstrued as an encouragement to violate any publisher's terms of service.) import TX, yahoo_historic_quotes as yhq def make_quotes_writer (): Visualizer = TX.Chain ( yhq.percent (), TX.Table_Maker (has_header = True), TX.Table_Writer (), name = 'Visualizer' ) To_DB = TX.Chain (yhq.header_stripper(), TX.DB_Writer(table_name = 'quotes'), name = 'To DB') To_File = TX.Chain (Visualizer, TX.File_Writer (), name = 'To File') Splitter = TX.Splitter (To_DB, To_File, name = 'Splitter') Quotes = TX.Chain ( yhq.yahoo_quotes (), TX.CSV_To_List (delimiter = ','), TX.Numerizer (), yhq.wiggle_and_trend (), yhq.symbol (), Splitter, name = 'Quotes' ) return Quotes >>> Quotes = make_quotes_writer () >>> Quotes.show_tree() Quotes Quotes[0] - Yahoo Quotes Quotes[1] - CSV To List Quotes[2] - Numerizer Quotes[3] - Wiggle and Trend Quotes[4] - Symbol Quotes[5] - Splitter Quotes[5][0] - To DB Quotes[5][0][0] - Header Stripper Quotes[5][0][1] - DB Writer Quotes[5][1] - To File Quotes[5][1][0] - Visualizer Quotes[5][1][0][0] - Percent Quotes[5][1][0][1] - Table Maker Quotes[5][1][0][2] - Table Writer Quotes[5][1][1] - File Writer def run_quotes (symbols, from_date = '1970-01-01', to_date = '2099-12-31'): '''Downloads historic stock quotes from Yahoo Finance. Inserts data into MySQL table "quotes" relying on index to reject repeat insertions. Also writes the data in a table format to a temporary file, one for each symbol. ''' Quotes = make_quotes_writer () Quotes.set (from_date = from_date, to_date = to_date) FW = Quotes [5][1][1] # File Writer for symbol in symbols: outfilename = '/tmp/%s' % symbol FW.set (terminal = outfilename) Quotes.set (symbol = symbol) Quotes () >>> run_quotes (('MO', 'SU'), '2013-08-10') Checking: >>> DBRC = TX.DB_Run_Command () >>> for record in DBRC ("select * from quotes where symbol in ('MO','SU') and date >= 20130810;"): print record (datetime.date(2013, 8, 16), 'MO', 34.51, 34.63, 34.25, 34.29, 8364900L, 0.0110337, -0.00638792) (datetime.date(2013, 8, 15), 'MO', 34.78, 34.93, 34.5, 34.57, 15144400L, 0.0123866, -0.00604926) (datetime.date(2013, 8, 14), 'MO', 35.23, 35.25, 34.81, 35.06, 5871900L, 0.0125607, -0.00485298) (datetime.date(2013, 8, 13), 'MO', 35.17, 35.27, 35.01, 35.22, 5919700L, 0.00739898, 0.00142288) (datetime.date(2013, 8, 12), 'MO', 35.25, 35.28, 35.05, 35.16, 4884300L, 0.00654059, -0.00255936) (datetime.date(2013, 8, 16), 'SU', 33.84, 34.63, 33.81, 34.18, 7396600L, 0.0239626, 0.00993571) (datetime.date(2013, 8, 15), 'SU', 32.84, 33.99, 32.7, 33.94, 7747100L, 0.0386865, 0.0329885) (datetime.date(2013, 8, 14), 'SU', 32.06, 32.91, 32.02, 32.72, 4690200L, 0.0274141, 0.0203296) (datetime.date(2013, 8, 13), 'SU', 32.04, 32.18, 31.94, 32.01, 2051900L, 0.00748596, -0.000935745) (datetime.date(2013, 8, 12), 'SU', 32.15, 32.35, 31.99, 32.15, 2870500L, 0.0111906, 0.0) >>> FR = TX.File_Reader () >>> print FR ('/tmp/SU') Date | Symbol | Open | High | Low | Close | Volume | Wiggle | Trend | 2013-08-16 | SU | 33.84 | 34.63 | 33.81 | 34.18 | 7396600.00 | 2.40% | +0.99% | 2013-08-15 | SU | 32.84 | 33.99 | 32.70 | 33.94 | 7747100.00 | 3.87% | +3.30% | 2013-08-14 | SU | 32.06 | 32.91 | 32.02 | 32.72 | 4690200.00 | 2.74% | +2.03% | 2013-08-13 | SU | 32.04 | 32.18 | 31.94 | 32.01 | 2051900.00 | 0.75% | -0.09% | 2013-08-12 | SU | 32.15 | 32.35 | 31.99 | 32.15 | 2870500.00 | 1.12% | +0.00% | ---------------------------------------------------------------------------------------------------- Each Transformer retains input and output, freezing the state of a composite after each run. This makes developing step by step and debugging a breeze. If a Chain fails, the failing link is the first one to have no output. Chain.show () displays them all in sequence. The deficient Transformer can be replaced with a fixed instance and the Chain run again without input for another try. >>> Quotes.replace (Fixed_Numerizer, 2) >>> catch = Quotes () # catch removes the risk of flooding the display ---------------------------------------------------------------------------------------------------- These are the five custom classes. The decorator "setup" updates parameters and prevents needless reruns comparing time stamps. class wiggle_and_trend (TX.Transformer): '''Deletes column "Adj Close" and adds columns "Wiggle" and "Trend". The values are ratios: (day's high - day's low) / mean, and (day's close - day's open) / mean. ''' name = 'Wiggle and Trend' @TX.setup def transform (self): input = self.Input.data output = [] output.append (tuple (input[0][:-1]) + ('Wiggle', 'Trend')) for i in range (1, len (input)): date, open, high, low, close, vol, adj = input [i] wiggle = high - low mean = (high + low) / 2.0 wiggle_ratio = wiggle / mean trend = close - open trend_ratio = trend / mean output.append ((date, open, high, low, close, vol, wiggle_ratio, trend_ratio)) self.Output.take (output) class symbol (TX.Transformer): 'Adds a column Symbol' name = 'Symbol' def __init__ (self): TX.Transformer.__init__ (self, symbol = None) @TX.setup def transform (self): symbol = self.get ('symbol') if not symbol: self.log ('No symbol!') else: input = self.Input.data output = [] output.append ((input[0][0], 'Symbol') + input [0][1:]) for i in range (1, len (input)): output.append ((input[i][0], symbol) + input [i][1:]) self.Output.take (output) class percent (TX.Transformer): 'Converts float ratio to percent for better legibility' name = 'Percent' @TX.setup def transform (self): input = self.Input.data output = [input [0]] for i in range (1, len (input)): wiggle = '%5.2f%%' % (input [i][7] * 100.0) trend = '%+5.2f%%' % (input [i][8] * 100.0) output.append (input [i][:7] + (wiggle, trend)) self.Output.take (output) class header_stripper (TX.Transformer): 'Header names are not meant for insertion into data base table' name = 'Header Stripper' @TX.setup def transform (self): self.Output.take (self.Input.data [1:]) class yahoo_quotes (TX.WWW_Reader): 'Gets historic stock quotes from Yahoo Finance' import urllib name = 'Yahoo Quotes' URL_TRUNK = 'http://ichart.finance.yahoo.com/table.csv' def __init__ (self, from_date = '1970-01-01', to_date = '2099-12-31', **keywords): private_keys = { 'symbol' : None, 'from_date' : from_date, 'to_date' : to_date, 'url_trunk' : self.URL_TRUNK, } private_keys.update (keywords) TX.WWW_Reader.__init__ (self, **private_keys) def make_url (self): fy, fm, fd = self.get ('from_date').split ('-') ty, tm, td = self.get ('to_date').split ('-') fm = '%0d' % (int (fm) - 1) tm = '%0d' % (int (tm) - 1) symbol = self.get ('symbol') qs = ( ('s',symbol), ('a',fm),('b',fd),('c',fy), ('d',tm),('e',td),('f',ty), ('g','d'), ('ignore', '.csv'), ) self.set (url_parameters = qs) TX.WWW_Reader.make_url (self) ---------------------------------------------------------------------------------------------------- Let me know what you think. From rosuav at gmail.com Wed Aug 21 15:16:21 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 22 Aug 2013 05:16:21 +1000 Subject: A data transformation framework. A presentation inviting commentary. In-Reply-To: <5214EB07.6000801@bluewin.ch> References: <5214EB07.6000801@bluewin.ch> Message-ID: On Thu, Aug 22, 2013 at 2:29 AM, F.R. wrote: > The nucleus of the TX system is a Transformer class, a wrapper for any kind > of transformation functionality. The Transformer takes input as calling > argument and returns it transformed. Not to put too much of a damper on your concept, but it's seeming a little over-engineered. Your description of a Transformer sounds to me like simply... a function. It takes input, it returns something. Why the heavy class-based interface? ChrisA From tjreedy at udel.edu Wed Aug 21 16:00:50 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 21 Aug 2013 16:00:50 -0400 Subject: A data transformation framework. A presentation inviting commentary. In-Reply-To: <5214EB07.6000801@bluewin.ch> References: <5214EB07.6000801@bluewin.ch> Message-ID: On 8/21/2013 12:29 PM, F.R. wrote: > Hi all, > > In an effort to do some serious cleaning up of a hopelessly cluttered > working environment, I developed a modular data transformation system > that pretty much stands. I am very pleased with it. I expect huge time > savings. I would share it, if had a sense that there is an interest out > there and would appreciate comments. Here's a description. I named the > module TX: You appear to have developed a framework for creating data flow networks. Others exists, including Python itself and things built on top of Python, like yours. I am not familiar with others built on Python, but I would not be surprised if your occupies its own niche. It is easy enough to share on PyPI. > The nucleus of the TX system is a Transformer class, a wrapper for any > kind of transformation functionality. The Transformer takes input as > calling argument and returns it transformed. This design allows the > assembly of transformation chains, either nesting calls or better, using > the class Chain, derived from 'Transformer' and 'list'. Python 3 is built around iterables and iterators. Iterables generalize the notion of list to any structure that can be sequentially accessed. A collection can be either concrete, existing all at once in some memory, or abstract, with members created as needed. One can think of there being two types of iterator. One merely presents the items of a collection one at a time. The other transforms items one at a time. The advantage of 'lazy' collections' is that they scale up much better to processing, say, a billion items. If your framework keeps the input list and all intermediate lists, as you seem to say, then your framework is memory constrained. Python (mostly) shifted from list to iterables as the common data interchange type partly for this reason. You are right that keeping data around can help debugging. Without that, each iterator must be properly tested if its operation is not transparent. > A Chain consists > of a sequence of Transformers and is functionally equivalent to an > individual Transformer. A high degree of modularity results: Chains > nest. Because iterators are also iterables, they nest. A transformer iterator does not care if its input is a concrete non-iterator iterable, a source iterator representing an abstract collection, or another transformer. Another consequence is that many transformation tasks can be > handled with a relatively modest library of a few basic prefabricated > Transformers from which many different Chains can be assembled on the > fly. This is precisely the idea of the itertool modules. I suspect that itertools.tee is equivalent to Tx.split (from the deleted code). Application areas need more specialized iterators. There are many in various stdlib modules. > A custom Transformer to bridge an eventual gap is quickly written > and tested, because the task likely is trivial. -- Terry Jan Reedy From dieter at handshake.de Thu Aug 22 02:43:26 2013 From: dieter at handshake.de (dieter) Date: Thu, 22 Aug 2013 08:43:26 +0200 Subject: A data transformation framework. A presentation inviting commentary. References: <5214EB07.6000801@bluewin.ch> Message-ID: <87wqne9qep.fsf@handshake.de> "F.R." writes: > ... > In an effort to do some serious cleaning up of a hopelessly cluttered > working environment, I developed a modular data transformation system > that pretty much stands. I am very pleased with it. I expect huge time > savings. I would share it, if had a sense that there is an interest > out there and would appreciate comments. Here's a description. I named > the module TX: > > The nucleus of the TX system is a Transformer class, a wrapper for any > kind of transformation functionality. The Transformer takes input as > calling argument and returns it transformed. This design allows the > assembly of transformation chains, either nesting calls or better, > using the class Chain, derived from 'Transformer' and 'list'. A Chain > consists of a sequence of Transformers and is functionally equivalent > to an individual Transformer. A high degree of modularity results: > ... This high level description much resembles "Products.PortalTransforms", a transformation package used in a "Plone" context. This package is targeted towards "MIME type" based transformations, i.e. input objects have a "MIME type" and you specify the target "MIME type". A transform registry knows about the available (elementary) transformations and determines a chain of transformations to achieve a desired one. From anthra.norell at bluewin.ch Thu Aug 22 08:34:48 2013 From: anthra.norell at bluewin.ch (F.R.) Date: Thu, 22 Aug 2013 14:34:48 +0200 Subject: A data transformation framework. A presentation inviting commentary. In-Reply-To: <5214EB07.6000801@bluewin.ch> References: <5214EB07.6000801@bluewin.ch> Message-ID: <52160568.8030902@bluewin.ch> On 08/21/2013 06:29 PM, F.R. wrote: > Hi all, > > In an effort to do some serious cleaning up of a hopelessly cluttered > working environment, I developed a modular data transformation system > that pretty much stands. I am very . . . etc Chris, Terry, Dieter, thanks for your suggestions. Chris: If my Transformer looks like a function, that's because it is (__call__). My idea was to have something like an erector set of elementary transformation machines that can be assembled into chains. There may be some processing overhead in managing the data flow, but I'm not even sure of that, because the flow needs to be managed somehow and throwing one's stones into someone else's garden doesn't get rid of the stones. My idea was to simplify, generalize and automate in order to deal with the kind of overhead that matters most to me: my own mental overhead. Terry: I am aware of the memory-load aspect. It is no constraint for the things I do. If it became one, I'd develop a translation assembly using a small data sample and when it reaches the stage of reliability, I'd add a line to have each Translator delete its input the moment it is done. I shall certainly look at itertools. Thanks for your suggestions and explanations. Dieter: I wish I could respond to the points you raise. I am unfamiliar with the details and they don't seem like they can be looked up in five minutes. I do make a note of your thoughts. Frederic From david.m.welch at gmail.com Wed Aug 21 12:42:11 2013 From: david.m.welch at gmail.com (David M. Welch) Date: Wed, 21 Aug 2013 11:42:11 -0500 Subject: Raw_input with readline in a daemon thread makes terminal text disappear Message-ID: Hi all, This is an old thread, but I'm having the same behavior in my terminal when I run some code but kill the process in the terminal (Ctrl-C). The code has two prime suspects (from a simple google search): 1. Creates ssh port forward via the subprocess module (http://unix.stackexchange.com/questions/4740/screen-remote-login-failure-an d-disappearing-text) 2. Using the getpass module (raw_input?) Calling "$ reset" brings back the disappearing text, so I'm just wondering if this issue has been addressed and if so, what should I be doing that I'm not. Thank you, Dave W. Response to post: http://mail.python.org/pipermail/python-list/2009-October/554784.html I'm getting input for a program while it's running by using raw_input in a loop in separate thread. This works except for the inconvenience of not having a command history or the use of backspace etc. That can be solved by loading the readline module; however, it results in a loss of visible access to the terminal when the program ends: nothing is echoed to the screen and the history is invisible (although it is there - hitting return executes whatever should be there normally). The only way to get it back is to close the terminal and open a new one. Here is minimal code that reproduces the problem (python 2.5 on Linux): from threading import Thread import readline get_input = Thread(target=raw_input) get_input.setDaemon(True) get_input.start() If the thread is not set to daemon mode, there is no such problem (don't know why this makes a difference), but in the real program, it needs to be a daemon or it hangs the exit waiting for more input. Any suggestions appreciated. Thanks, John -------------- next part -------------- An HTML attachment was scrubbed... URL: From random832 at fastmail.us Wed Aug 21 17:27:23 2013 From: random832 at fastmail.us (random832 at fastmail.us) Date: Wed, 21 Aug 2013 17:27:23 -0400 Subject: Raw_input with readline in a daemon thread makes terminal text disappear In-Reply-To: References: Message-ID: <1377120443.18348.12614625.36E3C068@webmail.messagingengine.com> On Wed, Aug 21, 2013, at 12:42, David M. Welch wrote: > Hi all, > > This is an old thread, but I'm having the same behavior in my terminal > when > I run some code but kill the process in the terminal (Ctrl-C). The code > has > two prime suspects (from a simple google search): > 1. Creates ssh port forward via the subprocess module > (http://unix.stackexchange.com/questions/4740/screen-remote-login-failure-an > d-disappearing-text) > 2. Using the getpass module (raw_input?) > Calling "$ reset" brings back the disappearing text, so I'm just > wondering > if this issue has been addressed and if so, what should I be doing that > I'm > not. Do you understand how tty modes (in particular, echo vs non-echo mode) work? What you've got is two different pieces of code (I think running readline in two threads qualifies) fighting over the tty mode, and probably one of them is turning echo mode off and then either never restoring it because of how the program exits, or it gets into the other's idea of the "original" mode. Why does your program design require input to be handled in a thread other than the main thread? From skip at python.org Wed Aug 21 14:05:29 2013 From: skip at python.org (Skip Montanaro) Date: Wed, 21 Aug 2013 13:05:29 -0500 Subject: utcoffset v. _utcoffset Message-ID: Consider this little Python script: import dateutil.parser import pytz x = dateutil.parser.parse("2013-08-16 23:00:00+01:00") localtz = pytz.timezone("America/Chicago") y = localtz.normalize(x) When I execute it (Python 2.7.2, dateutil 1.5, pytz 2011h), I get this traceback: Traceback (most recent call last): File "/home/skipm/tmp/localtzex.py", line 8, in y = localtz.normalize(x) File "/opt/TWWfsw/python27p/lib/python2.7/site-packages/pytz/tzinfo.py", line 233, in normalize offset = dt.tzinfo._utcoffset AttributeError: 'tzoffset' object has no attribute '_utcoffset' Looking at the tzinfo attribute, I see that it has "utcoffset", but not "_utcoffset". I realize those are the latest, most up-to-datest versions of all three elements. I'm having trouble updating dateutil and pytz on my Mac at home (stuck on even older versions). Can someone with newer versions of dateutil and pytz see if this problem is still present? Thx, Skip From tjreedy at udel.edu Wed Aug 21 16:25:10 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 21 Aug 2013 16:25:10 -0400 Subject: utcoffset v. _utcoffset In-Reply-To: References: Message-ID: On 8/21/2013 2:05 PM, Skip Montanaro wrote: > Consider this little Python script: > > import dateutil.parser > import pytz Neither of these are stdlib modules, so I cannot run this. > x = dateutil.parser.parse("2013-08-16 23:00:00+01:00") > localtz = pytz.timezone("America/Chicago") > y = localtz.normalize(x) > > When I execute it (Python 2.7.2, dateutil 1.5, pytz 2011h), I get this > traceback: > > Traceback (most recent call last): > File "/home/skipm/tmp/localtzex.py", line 8, in > y = localtz.normalize(x) > File "/opt/TWWfsw/python27p/lib/python2.7/site-packages/pytz/tzinfo.py", > line 233, in normalize > offset = dt.tzinfo._utcoffset > AttributeError: 'tzoffset' object has no attribute '_utcoffset' > > Looking at the tzinfo attribute, I see that it has "utcoffset", but > not "_utcoffset". I realize those are the latest, most up-to-datest > versions of all three elements. I'm having trouble updating dateutil > and pytz on my Mac at home (stuck on even older versions). Can > someone with newer versions of dateutil and pytz see if this problem > is still present? -- Terry Jan Reedy From nad at acm.org Wed Aug 21 16:33:50 2013 From: nad at acm.org (Ned Deily) Date: Wed, 21 Aug 2013 13:33:50 -0700 Subject: utcoffset v. _utcoffset References: Message-ID: In article , Skip Montanaro wrote: > Consider this little Python script: > > import dateutil.parser > import pytz > > x = dateutil.parser.parse("2013-08-16 23:00:00+01:00") > localtz = pytz.timezone("America/Chicago") > y = localtz.normalize(x) > > When I execute it (Python 2.7.2, dateutil 1.5, pytz 2011h), I get this > traceback: > > Traceback (most recent call last): > File "/home/skipm/tmp/localtzex.py", line 8, in > y = localtz.normalize(x) > File "/opt/TWWfsw/python27p/lib/python2.7/site-packages/pytz/tzinfo.py", > line 233, in normalize > offset = dt.tzinfo._utcoffset > AttributeError: 'tzoffset' object has no attribute '_utcoffset' > > Looking at the tzinfo attribute, I see that it has "utcoffset", but > not "_utcoffset". I realize those are the latest, most up-to-datest > versions of all three elements. I'm having trouble updating dateutil > and pytz on my Mac at home (stuck on even older versions). Can > someone with newer versions of dateutil and pytz see if this problem > is still present? I believe the problem is that you are using them incorrectly. IAMNADatetimeExpert, but I think you are trying to normalize a non-tz-aware datetime object returned from dateutil.parser.parse. -- Ned Deily, nad at acm.org From ramit.prasad at jpmorgan.com.dmarc.invalid Wed Aug 21 16:58:58 2013 From: ramit.prasad at jpmorgan.com.dmarc.invalid (Prasad, Ramit) Date: Wed, 21 Aug 2013 20:58:58 +0000 Subject: utcoffset v. _utcoffset In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF4741867F80B@SCACMX008.exchad.jpmchase.net> Skip Montanaro wrote: > > Consider this little Python script: > > import dateutil.parser > import pytz > > x = dateutil.parser.parse("2013-08-16 23:00:00+01:00") > localtz = pytz.timezone("America/Chicago") > y = localtz.normalize(x) > > When I execute it (Python 2.7.2, dateutil 1.5, pytz 2011h), I get this > traceback: > > Traceback (most recent call last): > File "/home/skipm/tmp/localtzex.py", line 8, in > y = localtz.normalize(x) > File "/opt/TWWfsw/python27p/lib/python2.7/site-packages/pytz/tzinfo.py", > line 233, in normalize > offset = dt.tzinfo._utcoffset > AttributeError: 'tzoffset' object has no attribute '_utcoffset' > > Looking at the tzinfo attribute, I see that it has "utcoffset", but > not "_utcoffset". I realize those are the latest, most up-to-datest > versions of all three elements. I'm having trouble updating dateutil > and pytz on my Mac at home (stuck on even older versions). Can > someone with newer versions of dateutil and pytz see if this problem > is still present? > > Thx, > > Skip > -- Using Python 2.6, dateutil 1.5, pytz 2013b Snipped from the documentation of pytz via help(localtz.normalize) ''' Correct the timezone information on the given datetime If date arithmetic crosses DST boundaries, the tzinfo is not magically adjusted. This method normalizes the tzinfo to the correct one. ''' Going from +1 to +6 will not cross the DST boundary. The documentation for localtz.normalize (in pytz 2013b) has a sample of what to do if it does cross DST which seems to boil down to using datetime.astimezone() and then localtz.normalize() if your date arithmetic crosses DST. >>> import dateutil.parser >>> import pytz >>> x = dateutil.parser.parse("2013-08-16 23:00:00+01:00") >>> localtz = pytz.timezone("America/Chicago") >>> x.astimezone( localtz ) datetime.datetime(2013, 8, 16, 17, 0, tzinfo=) ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From mohsen at pahlevanzadeh.org Wed Aug 21 14:41:50 2013 From: mohsen at pahlevanzadeh.org (Mohsen Pahlevanzadeh) Date: Wed, 21 Aug 2013 23:11:50 +0430 Subject: python3-sqlalchemy and debian repo Message-ID: <1377110510.21442.12.camel@debian> Dear all, I want to use sqlalchemy library, When i use "apt-cashe search sqlalchemy" , get the following result(part of result): /////////////// python-sqlalchemy - SQL toolkit and Object Relational Mapper for Python python-sqlalchemy-doc - documentation for the SQLAlchemy Python library python-sqlalchemy-ext - SQL toolkit and Object Relational Mapper for Python - C extension python3-sqlalchemy - SQL toolkit and Object Relational Mapper for Python 3 //////////////////// Question: I want to use python 3, So i didn't doc and ext suffix for sqlalchemy compatiable with python 3. The given suffix removed in python 3 or merged in python3-sqlalchemy package? --mohsen From rosuav at gmail.com Thu Aug 22 08:31:01 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 22 Aug 2013 22:31:01 +1000 Subject: python3-sqlalchemy and debian repo In-Reply-To: <1377110510.21442.12.camel@debian> References: <1377110510.21442.12.camel@debian> Message-ID: On Thu, Aug 22, 2013 at 4:41 AM, Mohsen Pahlevanzadeh wrote: > Dear all, > > I want to use sqlalchemy library, When i use "apt-cashe search > sqlalchemy" , get the following result(part of result): > /////////////// > python-sqlalchemy - SQL toolkit and Object Relational Mapper for Python > python-sqlalchemy-doc - documentation for the SQLAlchemy Python library > python-sqlalchemy-ext - SQL toolkit and Object Relational Mapper for > Python - C extension > python3-sqlalchemy - SQL toolkit and Object Relational Mapper for Python > 3 > //////////////////// > > Question: I want to use python 3, So i didn't doc and ext suffix for > sqlalchemy compatiable with python 3. The given suffix removed in python > 3 or merged in python3-sqlalchemy package? I don't know about -ext, but -doc applies to both: rosuav at sikorsky:~$ apt-cache show python3-sqlalchemy Package: python3-sqlalchemy Source: sqlalchemy Version: 0.7.8-1 Installed-Size: 2824 Maintainer: Piotr O?arowski Architecture: all Depends: python3 (>= 3.1.3-13~) Suggests: python-sqlalchemy-doc It may be that the -ext accelerator isn't available for Py3 in package form, or it might be incorporated, I don't know. Certainly python-sqlalchemy-ext demands the Py2 version (and python2.7 | python2.6). ChrisA From lele at metapensiero.it Thu Aug 22 16:36:08 2013 From: lele at metapensiero.it (Lele Gaifax) Date: Thu, 22 Aug 2013 22:36:08 +0200 Subject: python3-sqlalchemy and debian repo References: <1377110510.21442.12.camel@debian> Message-ID: <87r4dlzcnb.fsf@nautilus.nautilus> Chris Angelico writes: > It may be that the -ext accelerator isn't available for Py3 in package > form, or it might be incorporated, I don't know. AFAICT, the Py3 accelerators will be available with SA 0.9. ciao, lele. -- nickname: Lele Gaifax | Quando vivr? di quello che ho pensato ieri real: Emanuele Gaifas | comincer? ad aver paura di chi mi copia. lele at metapensiero.it | -- Fortunato Depero, 1929. From zughumancapital at yahoo.com.br Wed Aug 21 16:07:28 2013 From: zughumancapital at yahoo.com.br (zughumancapital) Date: Wed, 21 Aug 2013 20:07:28 -0000 Subject: Arpex Capital seleciona: Desenvolvedor Python (MongoDB) / SP Message-ID: Arpex Capital seleciona para uma de suas empresas: Desenvolvedor Python (MongoDB) Objetivo geral da Posi??o: Desenvolver software est?vel e de primeira linha, que ser? incorporado ? plataforma de WiFi mais moderna atualmente. Responsabilidades: escrever software que rodar? tanto no backend (Python) quanto no frontend (HTML5). Pr?-requisitos: - Conhecimento em Python, MongoDB - Cloud computing, BigData - Pensamento l?gico e anal?tico - Capacidade de absorver tecnologias novas de forma constante Deveres: - Escrever software s?lido em Python Forma??o: Irrelevante (n?o cobramos) Local de Trabalho: S?o Paulo/SP A empresa oferece remunera??o compat?vel com o mercado + Benef?cios. Os interessados dever?o enviar o CV para kgarcia at arpexcapital.com.br , mencionando no assunto Desenvolvedor Python (MongoDB). From gary719_list1 at verizon.net Wed Aug 21 21:54:19 2013 From: gary719_list1 at verizon.net (Gary Roach) Date: Wed, 21 Aug 2013 18:54:19 -0700 Subject: Python and mysql 3 tier programming Message-ID: <52156F4B.60107@verizon.net> Hi all, I'm now to the list so apologies if I don't always follow the local protocol. My problem is the interface between python and mysql using a three tier model. First, some background: System Debian Wheezy Linux Python 2.7 Mysql 5.5.31 Apache Server I am somewhat conversant with html, css, SQL, mysql, Apache and Debian Linux. Actually I have been using Debian for over 10 year. I spent over 5 year, prior to retirement, programming database based applications in Foxpro. I can also struggle through Java Script. I am just starting to use python. I've started with development of a rather complicated document archiving system with about 5 different levels of users and over 100 years of documents. photos, etc. The database setup has gone smoothly and other than one trial web page I'm leaving that for later. Finally to the problem. Where does python start and mysql stored procedures stop and visa versa. I'm trying to stick to a 3 tier philosophy but am having trouble figuring out where the dividing line is between the two packages. Further python seems to like cursor tables a lot and Oracles Mysql 5.5 discourages their use. Are they talking about the same thing. My problem is mostly with the basic architecture of the system. I think I will be able to figure out the code. Also, any comments on the use of the Django framework for this project. Thanks in advance Gary R. From jsf80238 at gmail.com Fri Aug 23 11:38:37 2013 From: jsf80238 at gmail.com (Jason Friedman) Date: Fri, 23 Aug 2013 09:38:37 -0600 Subject: Python and mysql 3 tier programming In-Reply-To: <52156F4B.60107@verizon.net> References: <52156F4B.60107@verizon.net> Message-ID: > System Debian Wheezy Linux > Python 2.7 > Mysql 5.5.31 > Apache Server > > I am somewhat conversant with html, css, SQL, mysql, Apache and Debian > Linux. Actually I have been using Debian for over 10 year. I spent over 5 > year, prior to retirement, programming database based applications in > Foxpro. I can also struggle through Java Script. I am just starting to use > python. I've started with development of a rather complicated document > archiving system with about 5 different levels of users and over 100 years > of documents. photos, etc. The database setup has gone smoothly and other > than one trial web page I'm leaving that for later. Finally to the problem. > Where does python start and mysql stored procedures stop and visa versa. I'm > trying to stick to a 3 tier philosophy but am having trouble figuring out > where the dividing line is between the two packages. Further python seems to > like cursor tables a lot and Oracles Mysql 5.5 discourages their use. Are > they talking about the same thing. > > My problem is mostly with the basic architecture of the system. I think I > will be able to figure out the code. Also, any comments on the use of the > Django framework for this project. Hello Gary, Is your primary goal to write code or is it to have available to you a document archiving solution? If the latter, you will probably save time by using an existing system (e.g., Google Docs). If the former, I have a few thoughts. One, you should probably be using Python 3 rather than 2, I think the command is "sudo aptitude install python3". Two, I think of stored procedures as being good at manipulating data really fast. With a document management system most activity will be retrieval rather than manipulation. From gary719_list1 at verizon.net Sun Aug 25 16:53:31 2013 From: gary719_list1 at verizon.net (Gary Roach) Date: Sun, 25 Aug 2013 13:53:31 -0700 Subject: Python and mysql 3 tier programming In-Reply-To: References: <52156F4B.60107@verizon.net> Message-ID: <521A6ECB.1090205@verizon.net> On 08/23/2013 08:38 AM, Jason Friedman wrote: >> System Debian Wheezy Linux >> Python 2.7 >> Mysql 5.5.31 >> Apache Server >> >> I am somewhat conversant with html, css, SQL, mysql, Apache and Debian >> Linux. Actually I have been using Debian for over 10 year. I spent over 5 >> year, prior to retirement, programming database based applications in >> Foxpro. I can also struggle through Java Script. I am just starting to use >> python. I've started with development of a rather complicated document >> archiving system with about 5 different levels of users and over 100 years >> of documents. photos, etc. The database setup has gone smoothly and other >> than one trial web page I'm leaving that for later. Finally to the problem. >> Where does python start and mysql stored procedures stop and visa versa. I'm >> trying to stick to a 3 tier philosophy but am having trouble figuring out >> where the dividing line is between the two packages. Further python seems to >> like cursor tables a lot and Oracles Mysql 5.5 discourages their use. Are >> they talking about the same thing. >> >> My problem is mostly with the basic architecture of the system. I think I >> will be able to figure out the code. Also, any comments on the use of the >> Django framework for this project. > Hello Gary, > Is your primary goal to write code or is it to have available to you a > document archiving solution? > If the latter, you will probably save time by using an existing system > (e.g., Google Docs). > If the former, I have a few thoughts. > One, you should probably be using Python 3 rather than 2, I think the > command is "sudo aptitude install python3". > Two, I think of stored procedures as being good at manipulating data > really fast. With a document management system most activity will be > retrieval rather than manipulation. Hi Jason; Thanks for the reply. Partly this is an experiment and learning process. In addition, I am never completely satisfied with other peoples software so like to control my own. It may not be the most efficient way to go but I'm retired and have lots of time. Since writing the original missive I decided to try the Django framework and have found that it pretty well forces the choice of architecture. As for as the version of Python; I use the Debian Wheezy linux release. Debian is pretty conservative about their stable releases. This is good for stability but doesn't always supply the latest versions. I have Python 3 on my system but will have to be careful about using it. I may break other things. Another damned investigation. Thanks again for your reply Gary R. From tausciam at gmail.com Wed Aug 21 22:04:47 2013 From: tausciam at gmail.com (Michael Staggs) Date: Wed, 21 Aug 2013 21:04:47 -0500 Subject: Using PyQT with QT Designer Message-ID: <1900623.ob0ITRdHWt@tannhaus-pc> I'm learning Python and I have a problem. I've asked the question everywhere and no one helps me, so I'm hoping someone here will. I am making a program that shows album covers and you click on the album cover in the top window. In the bottom window, the list of songs appear and you can click the individual song to play it. It's going to be a media player for children. I'm thinking I'll be able to use a dict and have the album as the key and the list of songs as the value to accomplish this. Right now, I'm just using my picture directory to try and get the basic layout right. I designed a form in QT Designer: http://i.imgur.com/Wrp1zHW.png Here is my gui file I got from running pyuic4 on the ui file: # -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'window.ui' # # Created by: PyQt4 UI code generator 4.9.6 # # WARNING! All changes made in this file will be lost! from PyQt4 import QtCore, QtGui try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try: _encoding = QtGui.QApplication.UnicodeUTF8 def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig, _encoding) except AttributeError: def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig) class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName(_fromUtf8("MainWindow")) MainWindow.resize(800, 600) self.centralwidget = QtGui.QWidget(MainWindow) self.centralwidget.setObjectName(_fromUtf8("centralwidget")) self.tableWidget = QtGui.QTableWidget(self.centralwidget) self.tableWidget.setGeometry(QtCore.QRect(70, 20, 661, 381)) self.tableWidget.setObjectName(_fromUtf8("tableWidget")) self.tableWidget.setColumnCount(0) self.tableWidget.setRowCount(0) self.listWidget = QtGui.QListWidget(self.centralwidget) self.listWidget.setGeometry(QtCore.QRect(70, 400, 661, 181)) self.listWidget.setObjectName(_fromUtf8("listWidget")) MainWindow.setCentralWidget(self.centralwidget) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None)) Now, according to websites I read, I should just have to add the following to my program to get it to use the form: from window import Ui_MainWindow class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None, **kwargs): super(MainWindow, self).__init__(parent) self.setupUi(self) and here is my program: from PyQt4.QtCore import * from PyQt4.QtGui import * from window import Ui_MainWindow THUMBNAIL_SIZE = 128 SPACING = 10 IMAGES_PER_ROW = 5 class TableWidget(QTableWidget): def __init__(self, parent=None, **kwargs): QTableWidget.__init__(self, parent, **kwargs) self.setIconSize(QSize(128,128)) self.setColumnCount(IMAGES_PER_ROW) self.setGridStyle(Qt.NoPen) # Set the default column width and hide the header self.verticalHeader().setDefaultSectionSize(THUMBNAIL_SIZE+SPACING) self.verticalHeader().hide() # Set the default row height and hide the header self.horizontalHeader().setDefaultSectionSize(THUMBNAIL_SIZE+SPACING) self.horizontalHeader().hide() # Set the table width to show all images without horizontal scrolling self.setMinimumWidth((THUMBNAIL_SIZE+SPACING)*IMAGES_PER_ROW+(SPACING*2)) def addPicture(self, row, col, picturePath): item=QTableWidgetItem() # Scale the image by either height or width and then 'crop' it to the # desired size, this prevents distortion of the image. p=QPixmap(picturePath) if p.height()>p.width(): p=p.scaledToWidth(THUMBNAIL_SIZE) else: p=p.scaledToHeight(THUMBNAIL_SIZE) p=p.copy(0,0,THUMBNAIL_SIZE,THUMBNAIL_SIZE) item.setIcon(QIcon(p)) self.setItem(row,col,item) class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None, **kwargs): super(MainWindow, self).__init__(parent) self.setupUi(self) centralWidget=QWidget(self) l=QVBoxLayout(centralWidget) self.tableWidget=TableWidget(self) l.addWidget(self.tableWidget) self.setCentralWidget(centralWidget) picturesPath=QDesktopServices.storageLocation(QDesktopServices.PicturesLocation) pictureDir=QDir(picturesPath) pictures=pictureDir.entryList(['*.jpg','*.png','*.gif']) rowCount=len(pictures)//IMAGES_PER_ROW if len(pictures)%IMAGES_PER_ROW: rowCount+=1 self.tableWidget.setRowCount(rowCount) row=-1 for i,picture in enumerate(pictures): col=i%IMAGES_PER_ROW if not col: row+=1 self.tableWidget.addPicture(row, col, pictureDir.absoluteFilePath(picture)) if __name__=="__main__": from sys import argv, exit a=QApplication(argv) m=MainWindow() m.show() m.raise_() exit(a.exec_()) But, it doesn't work. It ignores the form and the two windows go from side to side in the frame. When I add buttons, it adds them on TOP of those two windows instead of beside them in the empty space....because there is no empty space. http://i.imgur.com/ZQfsMDa.png Please tell me what I'm doing wrong. From phil at riverbankcomputing.com Thu Aug 22 04:26:17 2013 From: phil at riverbankcomputing.com (Phil Thompson) Date: Thu, 22 Aug 2013 09:26:17 +0100 Subject: Using PyQT with QT Designer In-Reply-To: <1900623.ob0ITRdHWt@tannhaus-pc> References: <1900623.ob0ITRdHWt@tannhaus-pc> Message-ID: On Wed, 21 Aug 2013 21:04:47 -0500, Michael Staggs wrote: > I'm learning Python and I have a problem. I've asked the question > everywhere > and no one helps me, so I'm hoping someone here will. I am making a > program > that shows album covers and you click on the album cover in the top > window. In > the bottom window, the list of songs appear and you can click the > individual > song to play it. It's going to be a media player for children. I'm > thinking > I'll be able to use a dict and have the album as the key and the list of > songs > as the value to accomplish this. > > Right now, I'm just using my picture directory to try and get the basic > layout > right. I designed a form in QT Designer: http://i.imgur.com/Wrp1zHW.png > > Here is my gui file I got from running pyuic4 on the ui file: > > > # -*- coding: utf-8 -*- > > # Form implementation generated from reading ui file 'window.ui' > # > # Created by: PyQt4 UI code generator 4.9.6 > # > # WARNING! All changes made in this file will be lost! > > from PyQt4 import QtCore, QtGui > > try: > _fromUtf8 = QtCore.QString.fromUtf8 > except AttributeError: > def _fromUtf8(s): > return s > > try: > _encoding = QtGui.QApplication.UnicodeUTF8 > def _translate(context, text, disambig): > return QtGui.QApplication.translate(context, text, disambig, > _encoding) > except AttributeError: > def _translate(context, text, disambig): > return QtGui.QApplication.translate(context, text, disambig) > > class Ui_MainWindow(object): > def setupUi(self, MainWindow): > MainWindow.setObjectName(_fromUtf8("MainWindow")) > MainWindow.resize(800, 600) > self.centralwidget = QtGui.QWidget(MainWindow) > self.centralwidget.setObjectName(_fromUtf8("centralwidget")) > self.tableWidget = QtGui.QTableWidget(self.centralwidget) > self.tableWidget.setGeometry(QtCore.QRect(70, 20, 661, 381)) > self.tableWidget.setObjectName(_fromUtf8("tableWidget")) > self.tableWidget.setColumnCount(0) > self.tableWidget.setRowCount(0) > self.listWidget = QtGui.QListWidget(self.centralwidget) > self.listWidget.setGeometry(QtCore.QRect(70, 400, 661, 181)) > self.listWidget.setObjectName(_fromUtf8("listWidget")) > MainWindow.setCentralWidget(self.centralwidget) > > self.retranslateUi(MainWindow) > QtCore.QMetaObject.connectSlotsByName(MainWindow) > > def retranslateUi(self, MainWindow): > MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", > None)) > > Now, according to websites I read, I should just have to add the following > to > my program to get it to use the form: > > from window import Ui_MainWindow > class MainWindow(QMainWindow, Ui_MainWindow): > def __init__(self, parent=None, **kwargs): > super(MainWindow, self).__init__(parent) > self.setupUi(self) > > and here is my program: > > from PyQt4.QtCore import * > from PyQt4.QtGui import * > from window import Ui_MainWindow > > THUMBNAIL_SIZE = 128 > SPACING = 10 > IMAGES_PER_ROW = 5 > > class TableWidget(QTableWidget): > def __init__(self, parent=None, **kwargs): > QTableWidget.__init__(self, parent, **kwargs) > > self.setIconSize(QSize(128,128)) > self.setColumnCount(IMAGES_PER_ROW) > self.setGridStyle(Qt.NoPen) > > # Set the default column width and hide the header > self.verticalHeader().setDefaultSectionSize(THUMBNAIL_SIZE+SPACING) > self.verticalHeader().hide() > > # Set the default row height and hide the header > self.horizontalHeader().setDefaultSectionSize(THUMBNAIL_SIZE+SPACING) > self.horizontalHeader().hide() > > # Set the table width to show all images without horizontal > scrolling > self.setMinimumWidth((THUMBNAIL_SIZE+SPACING)*IMAGES_PER_ROW+(SPACING*2)) > > def addPicture(self, row, col, picturePath): > item=QTableWidgetItem() > > # Scale the image by either height or width and then 'crop' it to > the > # desired size, this prevents distortion of the image. > p=QPixmap(picturePath) > if p.height()>p.width(): p=p.scaledToWidth(THUMBNAIL_SIZE) > else: p=p.scaledToHeight(THUMBNAIL_SIZE) > p=p.copy(0,0,THUMBNAIL_SIZE,THUMBNAIL_SIZE) > item.setIcon(QIcon(p)) > > self.setItem(row,col,item) > > class MainWindow(QMainWindow, Ui_MainWindow): > def __init__(self, parent=None, **kwargs): > super(MainWindow, self).__init__(parent) > self.setupUi(self) > > centralWidget=QWidget(self) > l=QVBoxLayout(centralWidget) > > self.tableWidget=TableWidget(self) > l.addWidget(self.tableWidget) > > self.setCentralWidget(centralWidget) > > picturesPath=QDesktopServices.storageLocation(QDesktopServices.PicturesLocation) > pictureDir=QDir(picturesPath) > pictures=pictureDir.entryList(['*.jpg','*.png','*.gif']) > > rowCount=len(pictures)//IMAGES_PER_ROW > if len(pictures)%IMAGES_PER_ROW: rowCount+=1 > self.tableWidget.setRowCount(rowCount) > > row=-1 > for i,picture in enumerate(pictures): > col=i%IMAGES_PER_ROW > if not col: row+=1 > self.tableWidget.addPicture(row, col, > pictureDir.absoluteFilePath(picture)) > > if __name__=="__main__": > from sys import argv, exit > > a=QApplication(argv) > m=MainWindow() > m.show() > m.raise_() > exit(a.exec_()) > > But, it doesn't work. It ignores the form and the two windows go from side > to > side in the frame. When I add buttons, it adds them on TOP of those two > windows instead of beside them in the empty space....because there is no > empty > space. > > http://i.imgur.com/ZQfsMDa.png > > Please tell me what I'm doing wrong. It looks like you aren't using a layout to arrange your widgets. Explicitly specifying geometries is a bad idea. Phil From tausciam at gmail.com Thu Aug 22 21:08:14 2013 From: tausciam at gmail.com (tausciam at gmail.com) Date: Thu, 22 Aug 2013 18:08:14 -0700 (PDT) Subject: Using PyQT with QT Designer In-Reply-To: References: <1900623.ob0ITRdHWt@tannhaus-pc> Message-ID: <8f9d0148-fda3-4139-ab45-a8be6da8320f@googlegroups.com> On Thursday, August 22, 2013 3:26:17 AM UTC-5, Phil Thompson wrote: > It looks like you aren't using a layout to arrange your widgets. > > Explicitly specifying geometries is a bad idea. > > > > Phil Thanks.QT Designer uses set geometry and I'm totally lost as how to implement it. I've tried using a layout on the central widget. I've tried specifically referencing the Ui_MainWindow in the window.py ui file... This is what I tried: from PyQt4.QtCore import * from PyQt4.QtGui import * from window import Ui_MainWindow THUMBNAIL_SIZE = 128 SPACING = 10 IMAGES_PER_ROW = 4 class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self): super(MainWindow, self).__init__() # self.setWindowTitle("Image Gallery") centralWidget=QWidget(self) l=QVBoxLayout(centralWidget) self.tableWidget=TableWidget(self) l.addWidget(self.tableWidget) self.listWidget=ListWidget(self) l.addWidget(self.listWidget) Ui_MainWindow.pushButton = QPushButton(self) l.addWidget(Ui_MainWindow.pushButton) self.pushButton_2 = QPushButton(self) l.addWidget(self.pushButton_2) self.pushButton_3 = QPushButton(self) l.addWidget(self.pushButton_3) self.setCentralWidget(centralWidget) picturesPath=QDesktopServices.storageLocation(QDesktopServices.PicturesLocation) pictureDir=QDir(picturesPath) pictures=pictureDir.entryList(['*.jpg','*.png','*.gif']) rowCount=len(pictures)//IMAGES_PER_ROW if len(pictures)%IMAGES_PER_ROW: rowCount+=1 self.tableWidget.setRowCount(rowCount) row=-1 for i,picture in enumerate(pictures): col=i%IMAGES_PER_ROW if not col: row+=1 self.tableWidget.addPicture(row, col, pictureDir.absoluteFilePath(picture)) class ListWidget(QListWidget): def __init__(self, parent=MainWindow, **kwargs): QListWidget.__init__(self, parent, **kwargs) self.setGeometry(QRect(70, 400, 661, 181)) class TableWidget(QTableWidget): def __init__(self, parent=MainWindow, **kwargs): QTableWidget.__init__(self, parent, **kwargs) self.setIconSize(QSize(128,128)) self.setColumnCount(IMAGES_PER_ROW) self.setGridStyle(Qt.NoPen) # Set the default column width and hide the header self.verticalHeader().setDefaultSectionSize(THUMBNAIL_SIZE+SPACING) self.verticalHeader().hide() # Set the default row height and hide the header self.horizontalHeader().setDefaultSectionSize(THUMBNAIL_SIZE+SPACING) self.horizontalHeader().hide() # Set the table width to show all images without horizontal scrolling self.setMinimumWidth((THUMBNAIL_SIZE+SPACING)*IMAGES_PER_ROW+(SPACING*2)) def addPicture(self, row, col, picturePath): item=QTableWidgetItem() # Scale the image by either height or width and then 'crop' it to the # desired size, this prevents distortion of the image. p=QPixmap(picturePath) if p.height()>p.width(): p=p.scaledToWidth(THUMBNAIL_SIZE) else: p=p.scaledToHeight(THUMBNAIL_SIZE) p=p.copy(0,0,THUMBNAIL_SIZE,THUMBNAIL_SIZE) item.setIcon(QIcon(p)) self.setItem(row,col,item) if __name__=="__main__": from sys import argv, exit a=QApplication(argv) m=MainWindow() m.show() m.raise_() exit(a.exec_()) and I'm getting this (not even starting at 800x600): http://i.imgur.com/Xg4Qnzl.png instead of this as it was designed in QT Designer: http://i.imgur.com/ULRolq8.png Here is the ui file window.py that I got by running pyuic4 on window.ui: # -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'window.ui' # # Created by: PyQt4 UI code generator 4.9.6 # # WARNING! All changes made in this file will be lost! from PyQt4 import QtCore, QtGui try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try: _encoding = QtGui.QApplication.UnicodeUTF8 def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig, _encoding) except AttributeError: def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig) class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName(_fromUtf8("MainWindow")) MainWindow.resize(800, 600) MainWindow.setAnimated(False) self.centralwidget = QtGui.QWidget(MainWindow) self.centralwidget.setObjectName(_fromUtf8("centralwidget")) self.tableWidget = QtGui.QTableWidget(self.centralwidget) self.tableWidget.setGeometry(QtCore.QRect(70, 20, 661, 381)) self.tableWidget.setObjectName(_fromUtf8("tableWidget")) self.tableWidget.setColumnCount(0) self.tableWidget.setRowCount(0) self.listWidget = QtGui.QListWidget(self.centralwidget) self.listWidget.setGeometry(QtCore.QRect(70, 400, 661, 181)) self.listWidget.setObjectName(_fromUtf8("listWidget")) self.pushButton = QtGui.QPushButton(self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(0, 150, 71, 81)) self.pushButton.setText(_fromUtf8("")) icon = QtGui.QIcon() icon.addPixmap(QtGui.QPixmap(_fromUtf8("pics/eye.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.pushButton.setIcon(icon) self.pushButton.setIconSize(QtCore.QSize(64, 64)) self.pushButton.setObjectName(_fromUtf8("pushButton")) self.pushButton_2 = QtGui.QPushButton(self.centralwidget) self.pushButton_2.setGeometry(QtCore.QRect(0, 230, 71, 81)) self.pushButton_2.setText(_fromUtf8("")) icon1 = QtGui.QIcon() icon1.addPixmap(QtGui.QPixmap(_fromUtf8("pics/ear.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.pushButton_2.setIcon(icon1) self.pushButton_2.setIconSize(QtCore.QSize(64, 64)) self.pushButton_2.setObjectName(_fromUtf8("pushButton_2")) self.pushButton_3 = QtGui.QPushButton(self.centralwidget) self.pushButton_3.setGeometry(QtCore.QRect(730, 20, 61, 61)) self.pushButton_3.setText(_fromUtf8("")) icon2 = QtGui.QIcon() icon2.addPixmap(QtGui.QPixmap(_fromUtf8("pics/exit.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.pushButton_3.setIcon(icon2) self.pushButton_3.setIconSize(QtCore.QSize(48, 48)) self.pushButton_3.setObjectName(_fromUtf8("pushButton_3")) MainWindow.setCentralWidget(self.centralwidget) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None)) From phil at riverbankcomputing.com Fri Aug 23 03:39:39 2013 From: phil at riverbankcomputing.com (Phil Thompson) Date: Fri, 23 Aug 2013 08:39:39 +0100 Subject: Using PyQT with QT Designer In-Reply-To: <8f9d0148-fda3-4139-ab45-a8be6da8320f@googlegroups.com> References: <1900623.ob0ITRdHWt@tannhaus-pc> <8f9d0148-fda3-4139-ab45-a8be6da8320f@googlegroups.com> Message-ID: <5e17639a06ae1e82ca2ba37b5346d001@localhost> On Thu, 22 Aug 2013 18:08:14 -0700 (PDT), tausciam at gmail.com wrote: > On Thursday, August 22, 2013 3:26:17 AM UTC-5, Phil Thompson wrote: > >> It looks like you aren't using a layout to arrange your widgets. >> >> Explicitly specifying geometries is a bad idea. >> >> >> >> Phil > > Thanks.QT Designer uses set geometry ...only because you have told it to... > and I'm totally lost as how to > implement it. I've tried using a layout on the central widget. I've tried > specifically referencing the Ui_MainWindow in the window.py ui file... You need to read up on how to use layouts in Designer. The generated .py file will then do what you want automatically. Phil From tausciam at gmail.com Fri Aug 23 09:00:29 2013 From: tausciam at gmail.com (Michael Staggs) Date: Fri, 23 Aug 2013 08:00:29 -0500 Subject: Using PyQT with QT Designer In-Reply-To: <5e17639a06ae1e82ca2ba37b5346d001@localhost> References: <1900623.ob0ITRdHWt@tannhaus-pc> <8f9d0148-fda3-4139-ab45-a8be6da8320f@googlegroups.com> <5e17639a06ae1e82ca2ba37b5346d001@localhost> Message-ID: I tried that this morning and it destroyed my form. So, right now, that's probably not what I'm looking for. But, if you look at that picture, the app isn't resized to 800x600 like it says in the ui file. The pixmaps aren't on the buttons like I set them up in the ui file. It's not using the ui file at all. So, what's the point of making a QT Designer file at all if it doesn't use it? I'm guessing it CAN use it and there is just something I'm missing. You may be right and I may not want to set the geometry in qt designer down the road. But, right now I do and not only is it not getting that from the ui file....it's not getting anything at all...even though I added all the lines I thought I needed to. If I decide to actually change the gui later, I'd like to be able to use QT Designer to do so...design a layout and not really have to change my program. As it stands, it's totally ignoring my ui file and I have to redo all the work in the program. In which case, there's no point to using qt designer at all. I know I have to be missing something though.... there has to be a way to use the work qt designer did. Thanks On Aug 23, 2013 2:39 AM, "Phil Thompson" wrote: > On Thu, 22 Aug 2013 18:08:14 -0700 (PDT), tausciam at gmail.com wrote: > > On Thursday, August 22, 2013 3:26:17 AM UTC-5, Phil Thompson wrote: > > > >> It looks like you aren't using a layout to arrange your widgets. > >> > >> Explicitly specifying geometries is a bad idea. > >> > >> > >> > >> Phil > > > > Thanks.QT Designer uses set geometry > > ...only because you have told it to... > > > and I'm totally lost as how to > > implement it. I've tried using a layout on the central widget. I've > tried > > specifically referencing the Ui_MainWindow in the window.py ui file... > > You need to read up on how to use layouts in Designer. The generated .py > file will then do what you want automatically. > > Phil > -------------- next part -------------- An HTML attachment was scrubbed... URL: From phil at riverbankcomputing.com Fri Aug 23 13:17:24 2013 From: phil at riverbankcomputing.com (Phil Thompson) Date: Fri, 23 Aug 2013 18:17:24 +0100 Subject: Using PyQT with QT Designer In-Reply-To: References: <1900623.ob0ITRdHWt@tannhaus-pc> <8f9d0148-fda3-4139-ab45-a8be6da8320f@googlegroups.com> <5e17639a06ae1e82ca2ba37b5346d001@localhost> Message-ID: On Fri, 23 Aug 2013 08:00:29 -0500, Michael Staggs wrote: > I tried that this morning and it destroyed my form. So, right now, that's > probably not what I'm looking for. > > But, if you look at that picture, the app isn't resized to 800x600 like it > says in the ui file. The pixmaps aren't on the buttons like I set them up > in the ui file. It's not using the ui file at all. So, what's the point of > making a QT Designer file at all if it doesn't use it? pyuic4 uses it to generate the corresponding Python code. Any time you change the .ui file with Designer you have to run pyuic4 again. You should not modify the Python code that pyuic4 generates. > I'm guessing it CAN use it and there is just something I'm missing. > > You may be right and I may not want to set the geometry in qt designer down > the road. But, right now I do and not only is it not getting that from the > ui file....it's not getting anything at all...even though I added all the > lines I thought I needed to. > > If I decide to actually change the gui later, I'd like to be able to use QT > Designer to do so...design a layout and not really have to change my > program. As it stands, it's totally ignoring my ui file and I have to redo > all the work in the program. In which case, there's no point to using qt > designer at all. > > I know I have to be missing something though.... there has to be a way to > use the work qt designer did. I strongly suggest you do some more reading about using Designer. Phil From tausciam at gmail.com Fri Aug 23 13:30:41 2013 From: tausciam at gmail.com (Michael Staggs) Date: Fri, 23 Aug 2013 12:30:41 -0500 Subject: Using PyQT with QT Designer In-Reply-To: References: <1900623.ob0ITRdHWt@tannhaus-pc> <8f9d0148-fda3-4139-ab45-a8be6da8320f@googlegroups.com> <5e17639a06ae1e82ca2ba37b5346d001@localhost> Message-ID: Right. I know that if I redesign it I have to run pyuic4 again and that I shouldn't change that file...let qt designer do its job. But, that's exactly what I'm having the problem with...incorporating the file pyuic4 gave me... and why I posted here. If you can point me towards something I need to read then by all means... I'd be grateful. But, all the little tutorials I've found told me to do it this way and obviously my program has no access to it....it's not resizing the window or doing anything the ui file states. So, I do know I'm doing something wrong and doing something the little tutorials didn't account for. So, I'm asking here. Again, I'm just learning. I took the codecademy python course and started trying to learn to build a media player...and haven't figured out how to build the gui yet. If you or anyone else can point me to something that would explain what I'm doing wrong, I'll read it from front to back. If it's too advanced it will lose me....but I would like to learn to do this. On Aug 23, 2013 12:17 PM, "Phil Thompson" wrote: > On Fri, 23 Aug 2013 08:00:29 -0500, Michael Staggs > wrote: > > I tried that this morning and it destroyed my form. So, right now, > that's > > probably not what I'm looking for. > > > > But, if you look at that picture, the app isn't resized to 800x600 like > it > > says in the ui file. The pixmaps aren't on the buttons like I set them > up > > in the ui file. It's not using the ui file at all. So, what's the point > of > > making a QT Designer file at all if it doesn't use it? > > pyuic4 uses it to generate the corresponding Python code. Any time you > change the .ui file with Designer you have to run pyuic4 again. You should > not modify the Python code that pyuic4 generates. > > > I'm guessing it CAN use it and there is just something I'm missing. > > > > You may be right and I may not want to set the geometry in qt designer > down > > the road. But, right now I do and not only is it not getting that from > the > > ui file....it's not getting anything at all...even though I added all > the > > lines I thought I needed to. > > > > If I decide to actually change the gui later, I'd like to be able to use > QT > > Designer to do so...design a layout and not really have to change my > > program. As it stands, it's totally ignoring my ui file and I have to > redo > > all the work in the program. In which case, there's no point to using qt > > designer at all. > > > > I know I have to be missing something though.... there has to be a way > to > > use the work qt designer did. > > I strongly suggest you do some more reading about using Designer. > > Phil > -------------- next part -------------- An HTML attachment was scrubbed... URL: From phil at riverbankcomputing.com Fri Aug 23 13:42:28 2013 From: phil at riverbankcomputing.com (Phil Thompson) Date: Fri, 23 Aug 2013 18:42:28 +0100 Subject: Using PyQT with QT Designer In-Reply-To: References: <1900623.ob0ITRdHWt@tannhaus-pc> <8f9d0148-fda3-4139-ab45-a8be6da8320f@googlegroups.com> <5e17639a06ae1e82ca2ba37b5346d001@localhost> Message-ID: On Fri, 23 Aug 2013 12:30:41 -0500, Michael Staggs wrote: > Right. I know that if I redesign it I have to run pyuic4 again and that I > shouldn't change that file...let qt designer do its job. > > But, that's exactly what I'm having the problem with...incorporating the > file pyuic4 gave me... and why I posted here. > > If you can point me towards something I need to read then by all means... > I'd be grateful. But, all the little tutorials I've found told me to do it > this way and obviously my program has no access to it....it's not resizing > the window or doing anything the ui file states. So, I do know I'm doing > something wrong and doing something the little tutorials didn't account > for. So, I'm asking here. > > Again, I'm just learning. I took the codecademy python course and started > trying to learn to build a media player...and haven't figured out how to > build the gui yet. If you or anyone else can point me to something that > would explain what I'm doing wrong, I'll read it from front to back. If > it's too advanced it will lose me....but I would like to learn to do this. http://qt-project.org/doc/qt-4.8/designer-manual.html Designer has a preview option that creates your UI on the fly. The first step would be to get it working as far as you can with that before you try generating any Python code. Phil From tausciam at gmail.com Fri Aug 23 13:58:04 2013 From: tausciam at gmail.com (Michael Staggs) Date: Fri, 23 Aug 2013 12:58:04 -0500 Subject: Using PyQT with QT Designer In-Reply-To: References: <1900623.ob0ITRdHWt@tannhaus-pc> <8f9d0148-fda3-4139-ab45-a8be6da8320f@googlegroups.com> <5e17639a06ae1e82ca2ba37b5346d001@localhost> Message-ID: Thanks for the manual. I will look into it but all the examples are probably c++. Ive tried zetcode and some of the other tutorials. That's the problem though. It is exactly how I want it in designer. It's perfect as it is in designer when I preview it. Here is a screenshot of the preview: http://i.imgur.com/ULRolq8.png The problem isn't that I can't design it in QT Designer. It is designed just like I want it. The problem is, when I try to follow zetcode and other tutorials about how to import and use my form as designed by qt designer and run through pyuic4.... it doesn't seem to even notice my ui file...and certainly isnt acting on it. I posted my code above where I was trying anything just to get it to use that ui I designed in qt designer.....so far to no avail ---------- Forwarded message ---------- From: "Michael Staggs" Date: Aug 23, 2013 12:54 PM Subject: Re: Using PyQT with QT Designer To: "Phil Thompson" Cc: Thanks for the manual. I will look into it but all the examples are probably c++. Ive tried zetcode and some of the other tutorials. That's the problem though. It is exactly how I want it in designer. It's perfect as it is in designer when I preview it. Here is a screenshot of the preview: http://i.imgur.com/ULRolq8.png The problem isn't that I can't design it in QT Designer. It is designed just like I want it. The problem is, when I try to follow zetcode and other tutorials about how to import and use my form as designed by qt designer and run through pyuic4.... it doesn't seem to even notice my ui file...and certainly isnt acting on it. I posted my code above where I was trying anything just to get it to use that ui I designed in qt designer.....so far to no avail On Aug 23, 2013 12:42 PM, "Phil Thompson" wrote: > On Fri, 23 Aug 2013 12:30:41 -0500, Michael Staggs > wrote: > > Right. I know that if I redesign it I have to run pyuic4 again and that > I > > shouldn't change that file...let qt designer do its job. > > > > But, that's exactly what I'm having the problem with...incorporating the > > file pyuic4 gave me... and why I posted here. > > > > If you can point me towards something I need to read then by all > means... > > I'd be grateful. But, all the little tutorials I've found told me to do > it > > this way and obviously my program has no access to it....it's not > resizing > > the window or doing anything the ui file states. So, I do know I'm doing > > something wrong and doing something the little tutorials didn't account > > for. So, I'm asking here. > > > > Again, I'm just learning. I took the codecademy python course and > started > > trying to learn to build a media player...and haven't figured out how to > > build the gui yet. If you or anyone else can point me to something that > > would explain what I'm doing wrong, I'll read it from front to back. If > > it's too advanced it will lose me....but I would like to learn to do > this. > > http://qt-project.org/doc/qt-4.8/designer-manual.html > > Designer has a preview option that creates your UI on the fly. The first > step would be to get it working as far as you can with that before you try > generating any Python code. > > Phil > -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Fri Aug 23 16:16:59 2013 From: davea at davea.name (Dave Angel) Date: Fri, 23 Aug 2013 20:16:59 +0000 (UTC) Subject: Using PyQT with QT Designer References: <1900623.ob0ITRdHWt@tannhaus-pc> <8f9d0148-fda3-4139-ab45-a8be6da8320f@googlegroups.com> <5e17639a06ae1e82ca2ba37b5346d001@localhost> Message-ID: Michael Staggs wrote: > > That's the problem though. It is exactly how I want it in designer. It's > perfect as it is in designer when I preview it. Here is a screenshot of the > preview: http://i.imgur.com/ULRolq8.png > > The problem isn't that I can't design it in QT Designer. It is designed > just like I want it. The problem is, when I try to follow zetcode and other > tutorials about how to import and use my form as designed by qt designer > and run through pyuic4.... it doesn't seem to even notice my ui file...and > certainly isnt acting on it. > I don't know PyQT, so I've kept quiet so far... You don't say what the name of the generated file is, but perhaps since the source file was window.ui, the generated one is window.py My guess is that when you do the from window import Ui_MainWindow it is finding some OTHER window.py file. Have you tried simply adding an illegal line to the generated file, to force the compiler to fail the import? Once you're sure that it is importing this particular file, you can remove such a line. Could be that you have some other window.py file (or window.pyc, or whatever) and that it's finding that one. Or it's finding some older version of this one. -- DaveA From tausciam at gmail.com Fri Aug 23 19:39:12 2013 From: tausciam at gmail.com (tausciam at gmail.com) Date: Fri, 23 Aug 2013 16:39:12 -0700 (PDT) Subject: Using PyQT with QT Designer In-Reply-To: References: <1900623.ob0ITRdHWt@tannhaus-pc> <8f9d0148-fda3-4139-ab45-a8be6da8320f@googlegroups.com> <5e17639a06ae1e82ca2ba37b5346d001@localhost> Message-ID: <8fd16ce2-c620-45ae-ad6e-403dda99d050@googlegroups.com> Thank you. I just deleted all of them, reran pyuic4 on window.ui and regenerated window.py just to make sure. Unfortunately, I get the same problem. I've got the GUI perfectly designed just like I want it in window.py... just can't figure out how to use it in my program. On Friday, August 23, 2013 3:16:59 PM UTC-5, Dave Angel wrote: > Michael Staggs wrote: > > > > > > > > > > That's the problem though. It is exactly how I want it in designer. It's > > > perfect as it is in designer when I preview it. Here is a screenshot of the > > > preview: http://i.imgur.com/ULRolq8.png > > > > > > The problem isn't that I can't design it in QT Designer. It is designed > > > just like I want it. The problem is, when I try to follow zetcode and other > > > tutorials about how to import and use my form as designed by qt designer > > > and run through pyuic4.... it doesn't seem to even notice my ui file...and > > > certainly isnt acting on it. > > > > > > > I don't know PyQT, so I've kept quiet so far... > > > > You don't say what the name of the generated file is, but perhaps since > > the source file was window.ui, the generated one is window.py > > > > My guess is that when you do the > > > > from window import Ui_MainWindow > > > > it is finding some OTHER window.py file. > > > > Have you tried simply adding an illegal line to the generated file, to > > force the compiler to fail the import? Once you're sure that it is > > importing this particular file, you can remove such a line. > > > > Could be that you have some other window.py file (or window.pyc, or > > whatever) and that it's finding that one. Or it's finding some older > > version of this one. > > > > > > > > -- > > DaveA From tausciam at gmail.com Fri Aug 23 14:05:26 2013 From: tausciam at gmail.com (Michael Staggs) Date: Fri, 23 Aug 2013 13:05:26 -0500 Subject: Using PyQT with QT Designer In-Reply-To: References: <1900623.ob0ITRdHWt@tannhaus-pc> <8f9d0148-fda3-4139-ab45-a8be6da8320f@googlegroups.com> <5e17639a06ae1e82ca2ba37b5346d001@localhost> Message-ID: Again though....I'm finished with QT Designer. I have the finished product I want exactly like I want it. But, as ive shown in the screenshots, I'm doing exactly what ive seen in zetcode and other tutorials but It doesn't seem to incorporate and act upon that ui file. The first thing you notice is that it doesn't resize the main window to 800x600...which is one of the first lines in the ui file. I know whatever I'm doing wrong has to be a 1 or 2 line solution...just something I should change a little....but I dont know what that is On Aug 23, 2013 12:42 PM, "Phil Thompson" wrote: > On Fri, 23 Aug 2013 12:30:41 -0500, Michael Staggs > wrote: > > Right. I know that if I redesign it I have to run pyuic4 again and that > I > > shouldn't change that file...let qt designer do its job. > > > > But, that's exactly what I'm having the problem with...incorporating the > > file pyuic4 gave me... and why I posted here. > > > > If you can point me towards something I need to read then by all > means... > > I'd be grateful. But, all the little tutorials I've found told me to do > it > > this way and obviously my program has no access to it....it's not > resizing > > the window or doing anything the ui file states. So, I do know I'm doing > > something wrong and doing something the little tutorials didn't account > > for. So, I'm asking here. > > > > Again, I'm just learning. I took the codecademy python course and > started > > trying to learn to build a media player...and haven't figured out how to > > build the gui yet. If you or anyone else can point me to something that > > would explain what I'm doing wrong, I'll read it from front to back. If > > it's too advanced it will lose me....but I would like to learn to do > this. > > http://qt-project.org/doc/qt-4.8/designer-manual.html > > Designer has a preview option that creates your UI on the fly. The first > step would be to get it working as far as you can with that before you try > generating any Python code. > > Phil > -------------- next part -------------- An HTML attachment was scrubbed... URL: From missive at hotmail.com Fri Aug 23 20:35:53 2013 From: missive at hotmail.com (Lee Harr) Date: Sat, 24 Aug 2013 05:05:53 +0430 Subject: Using PyQT with QT Designer Message-ID: > That's the problem though. It is exactly how I want it in designer. It's > perfect as it is in designer when I preview it. Here is a screenshot of the > preview: http://i.imgur.com/ULRolq8.png That's not a preview. That's just the regular design view. (you can tell by the little dots in the background) You need to go to Form -> Preview... to see the actual preview. That said... 1.) You may want to ask your question on the PyQt mailing list. Though you are talking with the undisputed PyQt expert in Phil, there are more people on the other list who are familiar with PyQt and who may be willing to look more closely at your specific code. 2.) It may be that the examples you are looking at are not sufficient to help you with the situation you are in. For instance, I've written several programs using Designer and PyQt and I would recommend against using the pyuic method. When I first started with PyQt I also used pyuic and eventually I found the PyQt4.uic method works better for me. 3.) Layouts. You have to use them with Qt or you're going to have a bad time. Looking at your design, I would do something like ... - select the two buttons on the left and click "Lay Out Vertically" - select the two large white boxes and click "Lay Out Vertically" - put a vertical spacer underneath the red X button - select the red button and the spacer and click "Lay Out Vertically" - at this point you may need to resize and rearrange your three vertical ? ?layouts so that they don't overlap and are in approximately the positions ? ?that you want, then - select the main window and click "Lay Out Horizontally" Something along those lines would get you about to where you want to be. The form may not look _exactly_ the way you have it there, but it will be a more flexible design and nothing will be overlapping. From tausciam at gmail.com Fri Aug 23 21:14:07 2013 From: tausciam at gmail.com (tausciam at gmail.com) Date: Fri, 23 Aug 2013 18:14:07 -0700 (PDT) Subject: Using PyQT with QT Designer In-Reply-To: References: Message-ID: Thank you... I found my problem.... class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setupUi(self) That seems to take care of it... if I comment out everything else, I get my pristine form.... I don't know if it's the super call or the setupui....but one of those was my godsend. So, that is solved... I just have to figure out how to put things in the gui where I want them....but I think you're right at any rate. If I try to resize, it doesn't function like I thought it would. I will have to use layouts.. but at least now I am able to use the UI file that I created. On Friday, August 23, 2013 7:35:53 PM UTC-5, Lee Harr wrote: > > That's the problem though. It is exactly how I want it in designer. It's > > perfect as it is in designer when I preview it. Here is a screenshot of the > > preview: http://i.imgur.com/ULRolq8.png > > That's not a preview. That's just the regular design view. > (you can tell by the little dots in the background) > > You need to go to Form -> Preview... to see the actual preview. > > That said... > > 1.) You may want to ask your question on the PyQt mailing list. Though > you are talking with the undisputed PyQt expert in Phil, there are more > people on the other list who are familiar with PyQt and who may be willing > to look more closely at your specific code. > > 2.) It may be that the examples you are looking at are not sufficient to > help you with the situation you are in. For instance, I've written several > programs using Designer and PyQt and I would recommend against > using the pyuic method. > > When I first started with PyQt I also used pyuic and eventually I found > the PyQt4.uic method works better for me. > > 3.) Layouts. You have to use them with Qt or you're going to have a > bad time. > > Looking at your design, I would do something like ... > > - select the two buttons on the left and click "Lay Out Vertically" > - select the two large white boxes and click "Lay Out Vertically" > - put a vertical spacer underneath the red X button > - select the red button and the spacer and click "Lay Out Vertically" > - at this point you may need to resize and rearrange your three vertical > ? ?layouts so that they don't overlap and are in approximately the positions > ? ?that you want, then > - select the main window and click "Lay Out Horizontally" > > Something along those lines would get you about to where you want > to be. The form may not look _exactly_ the way you have it there, but > it will be a more flexible design and nothing will be overlapping. From tausciam at gmail.com Sat Aug 24 17:09:15 2013 From: tausciam at gmail.com (tausciam at gmail.com) Date: Sat, 24 Aug 2013 14:09:15 -0700 (PDT) Subject: Using PyQT with QT Designer In-Reply-To: References: Message-ID: <528c5595-4b81-45d0-8487-6ad5ae880662@googlegroups.com> Thanks. I probably will do exactly like you suggested later on. But, those two lines have solved the problem I had and I can work on the actual program now. I can come back to the GUI later. Here is what it looks like now: http://i.imgur.com/sLiSU6M.png On Friday, August 23, 2013 7:35:53 PM UTC-5, Lee Harr wrote: > > That's the problem though. It is exactly how I want it in designer. It's > > perfect as it is in designer when I preview it. Here is a screenshot of the > > preview: http://i.imgur.com/ULRolq8.png > > That's not a preview. That's just the regular design view. > (you can tell by the little dots in the background) > > You need to go to Form -> Preview... to see the actual preview. > > That said... > > 1.) You may want to ask your question on the PyQt mailing list. Though > you are talking with the undisputed PyQt expert in Phil, there are more > people on the other list who are familiar with PyQt and who may be willing > to look more closely at your specific code. > > 2.) It may be that the examples you are looking at are not sufficient to > help you with the situation you are in. For instance, I've written several > programs using Designer and PyQt and I would recommend against > using the pyuic method. > > When I first started with PyQt I also used pyuic and eventually I found > the PyQt4.uic method works better for me. > > 3.) Layouts. You have to use them with Qt or you're going to have a > bad time. > > Looking at your design, I would do something like ... > > - select the two buttons on the left and click "Lay Out Vertically" > - select the two large white boxes and click "Lay Out Vertically" > - put a vertical spacer underneath the red X button > - select the red button and the spacer and click "Lay Out Vertically" > - at this point you may need to resize and rearrange your three vertical > ? ?layouts so that they don't overlap and are in approximately the positions > ? ?that you want, then > - select the main window and click "Lay Out Horizontally" > > Something along those lines would get you about to where you want > to be. The form may not look _exactly_ the way you have it there, but > it will be a more flexible design and nothing will be overlapping. From tausciam at gmail.com Fri Aug 30 20:33:15 2013 From: tausciam at gmail.com (tausciam at gmail.com) Date: Fri, 30 Aug 2013 17:33:15 -0700 (PDT) Subject: Using PyQT with QT Designer In-Reply-To: References: Message-ID: Lee Harr, thank you. I took your suggestion after I finished coding the audio section. You can see the improved project here: http://i.imgur.com/permuRQ.jpg On Friday, August 23, 2013 7:35:53 PM UTC-5, Lee Harr wrote: > > That's the problem though. It is exactly how I want it in designer. It's > > perfect as it is in designer when I preview it. Here is a screenshot of the > > preview: http://i.imgur.com/ULRolq8.png > > That's not a preview. That's just the regular design view. > (you can tell by the little dots in the background) > > You need to go to Form -> Preview... to see the actual preview. > > That said... > > 1.) You may want to ask your question on the PyQt mailing list. Though > you are talking with the undisputed PyQt expert in Phil, there are more > people on the other list who are familiar with PyQt and who may be willing > to look more closely at your specific code. > > 2.) It may be that the examples you are looking at are not sufficient to > help you with the situation you are in. For instance, I've written several > programs using Designer and PyQt and I would recommend against > using the pyuic method. > > When I first started with PyQt I also used pyuic and eventually I found > the PyQt4.uic method works better for me. > > 3.) Layouts. You have to use them with Qt or you're going to have a > bad time. > > Looking at your design, I would do something like ... > > - select the two buttons on the left and click "Lay Out Vertically" > - select the two large white boxes and click "Lay Out Vertically" > - put a vertical spacer underneath the red X button > - select the red button and the spacer and click "Lay Out Vertically" > - at this point you may need to resize and rearrange your three vertical > ? ?layouts so that they don't overlap and are in approximately the positions > ? ?that you want, then > - select the main window and click "Lay Out Horizontally" > > Something along those lines would get you about to where you want > to be. The form may not look _exactly_ the way you have it there, but > it will be a more flexible design and nothing will be overlapping. From dan at tombstonezero.net Thu Aug 22 01:13:03 2013 From: dan at tombstonezero.net (Dan Sommers) Date: Thu, 22 Aug 2013 05:13:03 GMT Subject: pydoc vs. non-def'd methods Message-ID: Greetings, I'm hava a class in which there are two equally useful names for one method. Consider this design (there are other approaches, but that's not what my question is about): class Spam1: def eggs(self): '''Return the Meaning of Life.''' return 42 ham = eggs help(Spam1) shows that ham = eggs(self), which isn't all bad, but it could be better. help(Spam1.ham) shows the help for eggs; I know why, but this could be better as well. And in any case, eggs looks somehow better than ham, because eggs has its own def statement and ham doesn't. Now consider this design, designed to overcome the previous issues: class Spam2: def _private(self): '''Return the Meaning of Life.''' return 42 ham = _private eggs = _private Now help(Spam2.ham) and help(Spam2.eggs) show the same thing, but help(Spam2) hides _private and its docstring and shows that ham and eggs both call _private. That's no good. I can expose _private (e.g., by renaming it to public), but that defeats the purpose of making it private in the first place. I can go ahead and define ham to invoke eggs, but then I have to duplicate the docstring, and it's not being-hit-on-the-head obvious that ham and eggs are simply synonyms for the same functionality. I could put the documentation at the class level, but then it doesn't show up as part of help(Spam2.eggs) or help(Spam1.ham). So is there a clean way to define SpamN such that help(SpamN), help(SpamN.ham), and help(SpamN.eggs) all do the Right Thing, and the symmetry of ham and eggs is perfectly obvious to the most casual observer? Thanks, Dan From steve at pearwood.info Thu Aug 22 02:39:48 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 22 Aug 2013 06:39:48 GMT Subject: pydoc vs. non-def'd methods References: Message-ID: <5215b234$0$29885$c3e8da3$5496439d@news.astraweb.com> On Thu, 22 Aug 2013 05:13:03 +0000, Dan Sommers wrote: > Greetings, > > I'm hava a class in which there are two equally useful names for one > method. Consider this design (there are other approaches, but that's > not what my question is about): Generally though, one name will be the canonical or preferred name, and the other merely an alias. That being the case, it's reasonable for the alias to be "second class" in some fashion, as you show below. > class Spam1: > > def eggs(self): > '''Return the Meaning of Life.''' > return 42 > > ham = eggs > > help(Spam1) shows that ham = eggs(self), which isn't all bad, but it > could be better. help(Spam1.ham) shows the help for eggs; I know why, > but this could be better as well. I'm not entirely sure how it could possibly be better. Since ham is just another name for eggs, it makes sense that they show the same docstring. > And in any case, eggs looks somehow > better than ham, because eggs has its own def statement and ham doesn't. I don't think I agree, but perhaps that's just an aesthetic judgement where we disagree. [...] > So is there a clean way to define SpamN such that help(SpamN), > help(SpamN.ham), and help(SpamN.eggs) all do the Right Thing, and the > symmetry of ham and eggs is perfectly obvious to the most casual > observer? class Spam: def eggs(self): """eggs docstring""" return "ham and eggs" def ham(self): return self.eggs() ham.__doc__ = eggs.__doc__.replace('eggs', 'ham') This allows you two distinct docstrings, at the cost of duplicating the information in them. Spam.ham will be a *tiny* bit less efficient, due to the extra method call, but if you're worried about that, you're probably up to no good :-) But really, I would find that a confusing API. I would wonder what subtle difference in semantics there was between ham and eggs. I would much prefer to see that ham was just an alias: class Spam: def eggs(self): """eggs docstring""" pass ham = eggs which brings us back to the beginning of your post :-) -- Steven From dan at tombstonezero.net Thu Aug 22 08:54:16 2013 From: dan at tombstonezero.net (Dan Sommers) Date: Thu, 22 Aug 2013 12:54:16 GMT Subject: pydoc vs. non-def'd methods References: < 5215b234$0$29885$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, 22 Aug 2013 06:39:48 +0000, Steven D'Aprano wrote: > On Thu, 22 Aug 2013 05:13:03 +0000, Dan Sommers wrote: >> class Spam1: >> >> def eggs(self): >> '''Return the Meaning of Life.''' >> return 42 >> >> ham = eggs >> >> >> help(Spam1) shows that ham = eggs(self), which isn't all bad, but it >> could be better. help(Spam1.ham) shows the help for eggs; I know >> why, but this could be better as well. > I'm not entirely sure how it could possibly be better. Since ham is > just another name for eggs, it makes sense that they show the same > docstring. Yes, help(Spam1.eggs) and help(Spam1.ham) show the same docstring. help(Spam1), however, doesn't show any docstring for ham; it shows that ham = eggs(self). >> And in any case, eggs looks somehow better than ham, because eggs has >> its own def statement and ham doesn't. > > I don't think I agree, but perhaps that's just an aesthetic judgement > where we disagree. Yep, just the aesthetics. > class Spam: > def eggs(self): > """eggs docstring""" > return "ham and eggs" > def ham(self): > return self.eggs() > ham.__doc__ = eggs.__doc__.replace('eggs', 'ham') > > This allows you two distinct docstrings, at the cost of duplicating > the information in them. Spam.ham will be a *tiny* bit less efficient, > due to the extra method call, but if you're worried about that, you're > probably up to no good :-) That I am up to no good goes without saying! ;-) > But really, I would find that a confusing API. I would wonder what > subtle difference in semantics there was between ham and eggs. I would > much prefer to see that ham was just an alias: > > class Spam: > def eggs(self): > """eggs docstring""" > pass > ham = eggs > > which brings us back to the beginning of your post :-) Yeah, okay, I'll go with that, despite the asymmetry. The names in question are encrypt and decrypt, which for a stream cipher, are the same. Thanks, Steven, for confirming my ability to read the documentation and play around in my interactive shell. ;-) And Thanks, F?bio, for your suggestions, too. -- Dan From fabiosantosart at gmail.com Thu Aug 22 05:10:33 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Thu, 22 Aug 2013 10:10:33 +0100 Subject: pydoc vs. non-def'd methods In-Reply-To: References: Message-ID: On 22 Aug 2013 06:17, "Dan Sommers" wrote: > > Greetings, > > I'm hava a class in which there are two equally useful names for one > method. Consider this design (there are other approaches, but that's > not what my question is about): > > class Spam1: > > def eggs(self): > '''Return the Meaning of Life.''' > return 42 > > ham = eggs > > help(Spam1) shows that ham = eggs(self), which isn't all bad, but it > could be better. help(Spam1.ham) shows the help for eggs; I know why, > but this could be better as well. And in any case, eggs looks somehow > better than ham, because eggs has its own def statement and ham doesn't. > > Now consider this design, designed to overcome the previous issues: > > class Spam2: > > def _private(self): > '''Return the Meaning of Life.''' > return 42 > > ham = _private > eggs = _private > > Now help(Spam2.ham) and help(Spam2.eggs) show the same thing, but > help(Spam2) hides _private and its docstring and shows that ham and eggs > both call _private. That's no good. I can expose _private (e.g., by > renaming it to public), but that defeats the purpose of making it > private in the first place. I can go ahead and define ham to invoke > eggs, but then I have to duplicate the docstring, and it's not > being-hit-on-the-head obvious that ham and eggs are simply synonyms for > the same functionality. > > I could put the documentation at the class level, but then it doesn't > show up as part of help(Spam2.eggs) or help(Spam1.ham). > > So is there a clean way to define SpamN such that help(SpamN), > help(SpamN.ham), and help(SpamN.eggs) all do the Right Thing, and the > symmetry of ham and eggs is perfectly obvious to the most casual > observer? > > Thanks, > Dan If if one of them is the canonical method name, you could define the other with a docstring indicating it is an alias for the other. If you don't want to spend code lines you can just define a helper function for that. Heck, you can even warn a DeprecationWarning if the alias is just backwards compatibility. -------------- next part -------------- An HTML attachment was scrubbed... URL: From Ian.Simcock at Internode.on.net Thu Aug 22 01:51:10 2013 From: Ian.Simcock at Internode.on.net (Ian Simcock) Date: Thu, 22 Aug 2013 15:21:10 +0930 Subject: Running a command line program and reading the result as it runs Message-ID: <5215a6cf$0$6512$c3e8da3$5496439d@news.astraweb.com> Greetings all. I'm using Python 2.7 under Windows and am trying to run a command line program and process the programs output as it is running. A number of web searches have indicated that the following code would work. import subprocess p = subprocess.Popen("D:\Python\Python27\Scripts\pip.exe list -o", stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True, shell=False) for line in p.stdout: print line When I use this code I can see that the Popen works, any code between the Popen and the for will run straight away, but as soon as it gets to the for and tries to read p.stdout the code blocks until the command line program completes, then all of the lines are returned. Does anyone know how to get the results of the program without it blocking? Thanks, Ian Simcock. From rosuav at gmail.com Thu Aug 22 02:22:19 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 22 Aug 2013 16:22:19 +1000 Subject: Running a command line program and reading the result as it runs In-Reply-To: <5215a6cf$0$6512$c3e8da3$5496439d@news.astraweb.com> References: <5215a6cf$0$6512$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, Aug 22, 2013 at 3:51 PM, Ian Simcock wrote: > When I use this code I can see that the Popen works, any code between the > Popen and the for will run straight away, but as soon as it gets to the for > and tries to read p.stdout the code blocks until the command line program > completes, then all of the lines are returned. > > Does anyone know how to get the results of the program without it blocking? Is the program actually producing output progressively? I just tried your exact code with "dir /ad /s /b" and it worked fine, producing output while the dir was still spinning (obviously setting shell=True to make that work, but I don't think that'll make a difference). It may be that pip buffers its output. Is there a parameter to pip to make it pipe-compatible? ChrisA From Ian.Simcock at Internode.on.net Thu Aug 22 11:26:32 2013 From: Ian.Simcock at Internode.on.net (Ian Simcock) Date: Fri, 23 Aug 2013 00:56:32 +0930 Subject: Running a command line program and reading the result as it runs In-Reply-To: References: <5215a6cf$0$6512$c3e8da3$5496439d@news.astraweb.com> Message-ID: <52162da8$0$29987$c3e8da3$5496439d@news.astraweb.com> Chris Angelico wrote: > Is the program actually producing output progressively? I just tried > your exact code with "dir /ad /s /b" and it worked fine, producing > output while the dir was still spinning (obviously setting shell=True > to make that work, but I don't think that'll make a difference). It > may be that pip buffers its output. Is there a parameter to pip to > make it pipe-compatible? > > ChrisA > If I run pip in the command window I can see it's output appearing line by line rather than on one block. I tried the code with the dir command but it's too fast for me to be sure if it's working or not. I tried again using the command "ping google.com" instead since I know that output's slowly and it something that everyone should have. In the command window I can see that the output appears over time, but from python I get nothing for a while and then suddenly get all the output in one rapid go. Can you think of anything else I can look at? Ian Simcock. From rosuav at gmail.com Thu Aug 22 11:33:12 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 23 Aug 2013 01:33:12 +1000 Subject: Running a command line program and reading the result as it runs In-Reply-To: <52162da8$0$29987$c3e8da3$5496439d@news.astraweb.com> References: <5215a6cf$0$6512$c3e8da3$5496439d@news.astraweb.com> <52162da8$0$29987$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Aug 23, 2013 at 1:26 AM, Ian Simcock wrote: > Chris Angelico wrote: >> >> Is the program actually producing output progressively? I just tried >> your exact code with "dir /ad /s /b" and it worked fine, producing >> output while the dir was still spinning (obviously setting shell=True >> to make that work, but I don't think that'll make a difference). It >> may be that pip buffers its output. Is there a parameter to pip to >> make it pipe-compatible? >> >> ChrisA >> > > If I run pip in the command window I can see it's output appearing line by > line rather than on one block. > > I tried the code with the dir command but it's too fast for me to be sure if > it's working or not. > > I tried again using the command "ping google.com" instead since I know that > output's slowly and it something that everyone should have. In the command > window I can see that the output appears over time, but from python I get > nothing for a while and then suddenly get all the output in one rapid go. > > > Can you think of anything else I can look at? A lot of programs, when their output is not going to the console, will buffer output. It's more efficient for many purposes. With Unix utilities, there's often a parameter like --pipe or --unbuffered that says "please produce output line by line", but Windows ping doesn't have that - and so I'm seeing the same thing you are. You should be able to see the time delay in dir by looking for some particular directory name, and searching from the root directory. Unless you're on a BLAZINGLY fast drive, that'll take Windows a good while! ChrisA From Ian.Simcock at Internode.on.net Fri Aug 23 02:52:08 2013 From: Ian.Simcock at Internode.on.net (Ian Simcock) Date: Fri, 23 Aug 2013 16:22:08 +0930 Subject: Running a command line program and reading the result as it runs In-Reply-To: References: <5215a6cf$0$6512$c3e8da3$5496439d@news.astraweb.com> <52162da8$0$29987$c3e8da3$5496439d@news.astraweb.com> Message-ID: <52170699$0$29998$c3e8da3$5496439d@news.astraweb.com> Chris Angelico wrote: > On Fri, Aug 23, 2013 at 1:26 AM, Ian Simcock > wrote: >> Chris Angelico wrote: >>> > > A lot of programs, when their output is not going to the console, will > buffer output. It's more efficient for many purposes. With Unix > utilities, there's often a parameter like --pipe or --unbuffered that > says "please produce output line by line", but Windows ping doesn't > have that - and so I'm seeing the same thing you are. > > You should be able to see the time delay in dir by looking for some > particular directory name, and searching from the root directory. > Unless you're on a BLAZINGLY fast drive, that'll take Windows a good > while! > > ChrisA I tried it again with the dir command and, while my drive is pretty fast, it does look like it works. I've done come looking around and found that the standard C libraries apparently automatically buffer output when the output is being redirected to a file handle unless specifically told not to. I did a further test and created a unique file name in the root of my D drive and then use dir to search the entire drive for that name. In the command window the name appears instantly and then after a slight pause the command prompt reappears. When run from python however the pause comes first and then the name appears and then the command prompt returns. So yep, seems like I'm screwed :-) Thanks for your help with this. At least now I know it's not that I'm doing something wrong. Ian Simcock. From invalid at invalid.invalid Fri Aug 23 10:02:02 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Fri, 23 Aug 2013 14:02:02 +0000 (UTC) Subject: Running a command line program and reading the result as it runs References: <5215a6cf$0$6512$c3e8da3$5496439d@news.astraweb.com> <52162da8$0$29987$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2013-08-22, Chris Angelico wrote: > On Fri, Aug 23, 2013 at 1:26 AM, Ian Simcock > wrote: >> Chris Angelico wrote: >>> >>> Is the program actually producing output progressively? I just tried >>> your exact code with "dir /ad /s /b" and it worked fine, producing >>> output while the dir was still spinning (obviously setting shell=True >>> to make that work, but I don't think that'll make a difference). It >>> may be that pip buffers its output. Is there a parameter to pip to >>> make it pipe-compatible? >>> >>> ChrisA >>> >> >> If I run pip in the command window I can see it's output appearing line by >> line rather than on one block. >> >> I tried the code with the dir command but it's too fast for me to be sure if >> it's working or not. >> >> I tried again using the command "ping google.com" instead since I know that >> output's slowly and it something that everyone should have. In the command >> window I can see that the output appears over time, but from python I get >> nothing for a while and then suddenly get all the output in one rapid go. >> >> >> Can you think of anything else I can look at? > > A lot of programs, when their output is not going to the console, > will buffer output. It's more efficient for many purposes. With Unix > utilities, there's often a parameter like --pipe or --unbuffered that > says "please produce output line by line", but Windows ping doesn't > have that - and so I'm seeing the same thing you are. Another way this problem can be avoided on Unix is to connect the slave end of a pty (instead of a pipe) to the command's stdout/stderr and then read the command's output from the master end of the pty. [On Unix, the buffering decision is based on whether stdout is a tty device, not on whether it's the console.] Dunno whether Windows has ptys or not. They're a very simple, elegent solution to a number of problems, so I'm guessing not. ;) -- Grant Edwards grant.b.edwards Yow! ... the MYSTERIANS are at in here with my CORDUROY gmail.com SOAP DISH!! From rw at smsnet.pl Thu Aug 22 17:14:26 2013 From: rw at smsnet.pl (Rob Wolfe) Date: Thu, 22 Aug 2013 23:14:26 +0200 Subject: Running a command line program and reading the result as it runs References: <5215a6cf$0$6512$c3e8da3$5496439d@news.astraweb.com> Message-ID: <87y57to2bx.fsf@smsnet.pl> Ian Simcock writes: > Greetings all. > > I'm using Python 2.7 under Windows and am trying to run a command line > program and process the programs output as it is running. A number of > web searches have indicated that the following code would work. > > import subprocess > > p = subprocess.Popen("D:\Python\Python27\Scripts\pip.exe list -o", > stdout=subprocess.PIPE, > stderr=subprocess.STDOUT, > bufsize=1, > universal_newlines=True, > shell=False) > for line in p.stdout: > print line > > When I use this code I can see that the Popen works, any code between > the Popen and the for will run straight away, but as soon as it gets > to the for and tries to read p.stdout the code blocks until the > command line program completes, then all of the lines are returned. > > Does anyone know how to get the results of the program without it blocking? When file object is used in a for loop it works like an iterator and then it uses a hidden read-ahead buffer. It might cause this kind of blocking. You can read more details here (description of method ``next``): http://docs.python.org/lib/bltin-file-objects.html So basically non-blocking loop might look like this: while True: line = p.stdout.readline() if not line: break print line HTH, Rob From Ian.Simcock at Internode.on.net Fri Aug 23 03:01:40 2013 From: Ian.Simcock at Internode.on.net (Ian Simcock) Date: Fri, 23 Aug 2013 16:31:40 +0930 Subject: Running a command line program and reading the result as it runs In-Reply-To: <87y57to2bx.fsf@smsnet.pl> References: <5215a6cf$0$6512$c3e8da3$5496439d@news.astraweb.com> <87y57to2bx.fsf@smsnet.pl> Message-ID: <521708d5$0$29998$c3e8da3$5496439d@news.astraweb.com> Rob Wolfe wrote: > Ian Simcock writes: > > When file object is used in a for loop it works like an iterator > and then it uses a hidden read-ahead buffer. > It might cause this kind of blocking. > You can read more details here (description of method ``next``): > http://docs.python.org/lib/bltin-file-objects.html > > So basically non-blocking loop might look like this: > > while True: > line = p.stdout.readline() > if not line: break > print line > > HTH, > Rob > Thanks, but some further research seems to indicate that the problem is that the standard C libraries are probably buffering the output when the it's being redirected, so the problem is coming from the command line tool rather than the python code. Ian Simcock. From gklein at xs4all.nl Fri Aug 23 05:32:00 2013 From: gklein at xs4all.nl (Gertjan Klein) Date: Fri, 23 Aug 2013 11:32:00 +0200 Subject: Running a command line program and reading the result as it runs In-Reply-To: <5215a6cf$0$6512$c3e8da3$5496439d@news.astraweb.com> References: <5215a6cf$0$6512$c3e8da3$5496439d@news.astraweb.com> Message-ID: <52172c10$0$15879$e4fe514c@news2.news.xs4all.nl> Ian Simcock wrote: > When I use this code I can see that the Popen works, any code between > the Popen and the for will run straight away, but as soon as it gets to > the for and tries to read p.stdout the code blocks until the command > line program completes, then all of the lines are returned. > > Does anyone know how to get the results of the program without it blocking? I have tried your code with "ping google.com" as command and got the same results; apparently something buffers the output. The result is different when using Python 3.3: there, the lines are printed as they come in. This seems to indicate a bug in the Python 2.7 implementation. There are some bug reports on bugs.python.org that may be related; see for example: http://bugs.python.org/issue15532 I have been playing around a bit with the suggested approach of using the io library directly. I managed to get unbuffered output, but unfortunately the program hangs when the subprocess is done. It can't even be terminated with Control-C, I have to use task manager to kill python.exe. Below is as far as I got; perhaps someone with more experience with pipes knows how to fix this. Regards, Gertjan. #!/usr/bin/env python2.7 # coding: CP1252 from __future__ import print_function import subprocess import io, os def main(): i, o = os.pipe() piperead = io.open(i, 'rb', buffering=1) p = subprocess.Popen(["ping", "google.com"], stdout=o, stderr=subprocess.PIPE, bufsize=0, shell=False) for line in piperead: print(line) if __name__ == '__main__': main() From antoon.pardon at rece.vub.ac.be Fri Aug 23 05:53:13 2013 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Fri, 23 Aug 2013 11:53:13 +0200 Subject: Running a command line program and reading the result as it runs In-Reply-To: <5215a6cf$0$6512$c3e8da3$5496439d@news.astraweb.com> References: <5215a6cf$0$6512$c3e8da3$5496439d@news.astraweb.com> Message-ID: <52173109.5090304@rece.vub.ac.be> Op 22-08-13 07:51, Ian Simcock schreef: > Greetings all. > > I'm using Python 2.7 under Windows and am trying to run a command line > program and process the programs output as it is running. A number of > web searches have indicated that the following code would work. > > import subprocess > > p = subprocess.Popen("D:\Python\Python27\Scripts\pip.exe list -o", > stdout=subprocess.PIPE, > stderr=subprocess.STDOUT, > bufsize=1, > universal_newlines=True, > shell=False) > for line in p.stdout: > print line > > When I use this code I can see that the Popen works, any code between > the Popen and the for will run straight away, but as soon as it gets to > the for and tries to read p.stdout the code blocks until the command > line program completes, then all of the lines are returned. > > Does anyone know how to get the results of the program without it blocking? Maybe the following can work? Untested code: from pty import openpty from subprocess import Popen master, slave = openpty() p = Popen("D:\Python\Python27\Scripts\pip.exe list -o", stdout = slave, stderr = slave, stdin = slave, close_fds = True) for line in master: print line The idea is to set a a pseudo terminal for pip so that the system thinks pip is doing IO with a terminal and so the IO will be line buffered. But all IO from pip will be available through the master in your program. -- Antoon Pardon From antoon.pardon at rece.vub.ac.be Fri Aug 23 06:34:58 2013 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Fri, 23 Aug 2013 12:34:58 +0200 Subject: Running a command line program and reading the result as it runs In-Reply-To: <52173109.5090304@rece.vub.ac.be> References: <5215a6cf$0$6512$c3e8da3$5496439d@news.astraweb.com> <52173109.5090304@rece.vub.ac.be> Message-ID: <52173AD2.4050509@rece.vub.ac.be> Op 23-08-13 11:53, Antoon Pardon schreef: > Op 22-08-13 07:51, Ian Simcock schreef: >> Greetings all. >> >> I'm using Python 2.7 under Windows and am trying to run a command line >> program and process the programs output as it is running. A number of >> web searches have indicated that the following code would work. >> >> import subprocess >> >> p = subprocess.Popen("D:\Python\Python27\Scripts\pip.exe list -o", >> stdout=subprocess.PIPE, >> stderr=subprocess.STDOUT, >> bufsize=1, >> universal_newlines=True, >> shell=False) >> for line in p.stdout: >> print line >> >> When I use this code I can see that the Popen works, any code between >> the Popen and the for will run straight away, but as soon as it gets to >> the for and tries to read p.stdout the code blocks until the command >> line program completes, then all of the lines are returned. >> >> Does anyone know how to get the results of the program without it blocking? > > Maybe the following can work? Never mind. I had overlooked that using pty requires linux and you are using windows. -- Antoon Pardon From jcasale at activenetwerx.com Fri Aug 23 06:50:18 2013 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Fri, 23 Aug 2013 10:50:18 +0000 Subject: Running a command line program and reading the result as it runs In-Reply-To: <52173AD2.4050509@rece.vub.ac.be> References: <5215a6cf$0$6512$c3e8da3$5496439d@news.astraweb.com> <52173109.5090304@rece.vub.ac.be> <52173AD2.4050509@rece.vub.ac.be> Message-ID: > >> I'm using Python 2.7 under Windows and am trying to run a command line > >> program and process the programs output as it is running. A number of > >> web searches have indicated that the following code would work. > >> > >> import subprocess > >> > >> p = subprocess.Popen("D:\Python\Python27\Scripts\pip.exe list -o", > >> stdout=subprocess.PIPE, > >> stderr=subprocess.STDOUT, > >> bufsize=1, > >> universal_newlines=True, > >> shell=False) > >> for line in p.stdout: > >> print line > >> > >> When I use this code I can see that the Popen works, any code between > >> the Popen and the for will run straight away, but as soon as it gets to > >> the for and tries to read p.stdout the code blocks until the command > >> line program completes, then all of the lines are returned. > >> > >> Does anyone know how to get the results of the program without it > >> blocking? Try this: p = subprocess.Popen(args, stdout=subprocess.PIPE) for line in p.stdout: print(line) p.wait() jlc From __peter__ at web.de Fri Aug 23 07:14:46 2013 From: __peter__ at web.de (Peter Otten) Date: Fri, 23 Aug 2013 13:14:46 +0200 Subject: Running a command line program and reading the result as it runs References: <5215a6cf$0$6512$c3e8da3$5496439d@news.astraweb.com> Message-ID: Ian Simcock wrote: > Greetings all. > > I'm using Python 2.7 under Windows and am trying to run a command line > program and process the programs output as it is running. A number of > web searches have indicated that the following code would work. > > import subprocess > > p = subprocess.Popen("D:\Python\Python27\Scripts\pip.exe list -o", > stdout=subprocess.PIPE, > stderr=subprocess.STDOUT, > bufsize=1, > universal_newlines=True, > shell=False) > for line in p.stdout: > print line > > When I use this code I can see that the Popen works, any code between > the Popen and the for will run straight away, but as soon as it gets to > the for and tries to read p.stdout the code blocks until the command > line program completes, then all of the lines are returned. > > Does anyone know how to get the results of the program without it > blocking? The following works on my linux system: import subprocess p = subprocess.Popen( ["ping", "google.com"], stdout=subprocess.PIPE) instream = iter(p.stdout.readline, "") for line in instream: print line.rstrip() I don't have Windows available to test, but if it works there, too, the problem is the internal buffer used by Python's implementation of file iteration rather than the OS. From random832 at fastmail.us Fri Aug 23 12:04:14 2013 From: random832 at fastmail.us (random832 at fastmail.us) Date: Fri, 23 Aug 2013 12:04:14 -0400 Subject: Running a command line program and reading the result as it runs In-Reply-To: References: <5215a6cf$0$6512$c3e8da3$5496439d@news.astraweb.com> Message-ID: <1377273854.3835.13339609.6D94109D@webmail.messagingengine.com> On Fri, Aug 23, 2013, at 7:14, Peter Otten wrote: > The following works on my linux system: > > instream = iter(p.stdout.readline, "") > > for line in instream: > print line.rstrip() > > I don't have Windows available to test, but if it works there, too, the > problem is the internal buffer used by Python's implementation of file > iteration rather than the OS. I can confirm this on Windows. Doesn't this surprising difference between for line in iter(f.readline,'') vs for line in f violate TOOWTDI? We're led to believe from the documentation that iterating over a file does _not_ read lines into memory before returning them. It's not clear to me what performance benefit can be gained from waiting when there is no more data available, either. I don't understand how it's even happening - from looking at the code, it looks like next() just calls readline() once, no fancy buffering specific to itself. From __peter__ at web.de Fri Aug 23 12:39:47 2013 From: __peter__ at web.de (Peter Otten) Date: Fri, 23 Aug 2013 18:39:47 +0200 Subject: Running a command line program and reading the result as it runs References: <5215a6cf$0$6512$c3e8da3$5496439d@news.astraweb.com> <1377273854.3835.13339609.6D94109D@webmail.messagingengine.com> Message-ID: random832 at fastmail.us wrote: > On Fri, Aug 23, 2013, at 7:14, Peter Otten wrote: >> The following works on my linux system: >> >> instream = iter(p.stdout.readline, "") >> >> for line in instream: >> print line.rstrip() >> >> I don't have Windows available to test, but if it works there, too, the >> problem is the internal buffer used by Python's implementation of file >> iteration rather than the OS. > > I can confirm this on Windows. > > Doesn't this surprising difference between for line in > iter(f.readline,'') vs for line in f violate TOOWTDI? We're led to > believe from the documentation that iterating over a file does _not_ > read lines into memory before returning them. It's not clear to me what > performance benefit can be gained from waiting when there is no more > data available, either. > > I don't understand how it's even happening - from looking at the code, > it looks like next() just calls readline() once, no fancy buffering > specific to itself. Maybe you are looking in the wrong version? For 2.x you can use the file_iternext() function as a starting point, see: http://hg.python.org/cpython/file/1ea833ecaf5a/Objects/fileobject.c#l2316 Python 3 uses a different approach that allows you to mix iteration and readline(): $ python -c 'f = open("tmp.txt"); next(f); f.readline()' Traceback (most recent call last): File "", line 1, in ValueError: Mixing iteration and read methods would lose data $ python3 -c 'f = open("tmp.txt"); next(f); f.readline()' The relevant code is likely in the Modules/_io/ directory. There is also [New I/O] http://www.python.org/dev/peps/pep-3116/ From gklein at xs4all.nl Fri Aug 23 08:03:43 2013 From: gklein at xs4all.nl (Gertjan Klein) Date: Fri, 23 Aug 2013 14:03:43 +0200 Subject: Running a command line program and reading the result as it runs In-Reply-To: References: <5215a6cf$0$6512$c3e8da3$5496439d@news.astraweb.com> Message-ID: <52174f9f$0$15982$e4fe514c@news2.news.xs4all.nl> Peter Otten wrote: > The following works on my linux system: > > import subprocess > > p = subprocess.Popen( > ["ping", "google.com"], > stdout=subprocess.PIPE) > > instream = iter(p.stdout.readline, "") > > for line in instream: > print line.rstrip() > > I don't have Windows available to test, but if it works there, too, the > problem is the internal buffer used by Python's implementation of file > iteration rather than the OS. Excellent, that works on Windows as well. That conclusively proves that the buffering problem is in Python, not in the command that is executed. (Although that may happen, too, for some commends.) Regards, Gertjan. From Ian.Simcock at Internode.on.net Sat Aug 24 05:36:25 2013 From: Ian.Simcock at Internode.on.net (Ian Simcock) Date: Sat, 24 Aug 2013 19:06:25 +0930 Subject: Running a command line program and reading the result as it runs In-Reply-To: References: <5215a6cf$0$6512$c3e8da3$5496439d@news.astraweb.com> Message-ID: <52187e99$0$29995$c3e8da3$5496439d@news.astraweb.com> Peter Otten wrote: > Ian Simcock wrote: > >> Greetings all. >> >> I'm using Python 2.7 under Windows and am trying to run a command line >> program and process the programs output as it is running. A number of >> web searches have indicated that the following code would work. >> >> import subprocess >> >> p = subprocess.Popen("D:\Python\Python27\Scripts\pip.exe list -o", >> stdout=subprocess.PIPE, >> stderr=subprocess.STDOUT, >> bufsize=1, >> universal_newlines=True, >> shell=False) >> for line in p.stdout: >> print line >> >> When I use this code I can see that the Popen works, any code between >> the Popen and the for will run straight away, but as soon as it gets to >> the for and tries to read p.stdout the code blocks until the command >> line program completes, then all of the lines are returned. >> >> Does anyone know how to get the results of the program without it >> blocking? > > The following works on my linux system: > > import subprocess > > p = subprocess.Popen( > ["ping", "google.com"], > stdout=subprocess.PIPE) > > instream = iter(p.stdout.readline, "") > > for line in instream: > print line.rstrip() > > I don't have Windows available to test, but if it works there, too, the > problem is the internal buffer used by Python's implementation of file > iteration rather than the OS. > Hmm... and so it comes full circle. I thought that the inclusion of the iter call looked familiar so I checked my original code and found that it was there. I removed it when shrinking the code down to a minimal example for posting. Then, after removing it, which triggered the blocking, I changed the command to ping so that it's easier for anyone to test. I've tried a copy of my original code using the ping command and it works fine. So it looks like the python package manager pip must see that it's not going to a console and buffer the output, and my original code was not the problem. So I can't do what I want, but it's interesting to know that for some reason the iter is required for the occasions when it can work. Thanks to everyone who helped with this. Ian Simcock From list at qtrac.plus.com Thu Aug 22 05:39:01 2013 From: list at qtrac.plus.com (Mark Summerfield) Date: Thu, 22 Aug 2013 02:39:01 -0700 (PDT) Subject: New book: Python in Practice Message-ID: Hi, My new Python 3-based book, "Python in Practice", is due out next week. The book is aimed at people who can already program in Python and want to take their coding further. The book's web page (http://www.qtrac.eu/pipbook.html) has the table of contents and a link to a free PDF of Chapter 1 so you can see if it appeals to you. The book's examples are also available for download from that page. I hope you'll take a look:-) From steve+comp.lang.python at pearwood.info Thu Aug 22 22:50:24 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Aug 2013 02:50:24 GMT Subject: New book: Python in Practice References: Message-ID: <5216cdf0$0$29986$c3e8da3$5496439d@news.astraweb.com> On Thu, 22 Aug 2013 02:39:01 -0700, Mark Summerfield wrote: > Hi, > > My new Python 3-based book, "Python in Practice", is due out next week. > > The book is aimed at people who can already program in Python and want > to take their coding further. > > The book's web page (http://www.qtrac.eu/pipbook.html) has the table of > contents and a link to a free PDF of Chapter 1 so you can see if it > appeals to you. The book's examples are also available for download from > that page. > > I hope you'll take a look:-) Looks good! -- Steven From ndbecker2 at gmail.com Thu Aug 22 07:29:58 2013 From: ndbecker2 at gmail.com (Neal Becker) Date: Thu, 22 Aug 2013 07:29:58 -0400 Subject: c# async, await Message-ID: So my son is now spending his days on c# and .net. He's enthusiastic about async and await, and said to me last evening, "I don't think python has anything like that". I'm not terribly knowledgeable myself regarding async programming (since I never need to use it). I did look at this: http://tirania.org/blog/archive/2013/Aug-15.html I wonder what response the python community might have. From torriem at gmail.com Thu Aug 22 09:57:40 2013 From: torriem at gmail.com (Michael Torrie) Date: Thu, 22 Aug 2013 07:57:40 -0600 Subject: c# async, await In-Reply-To: References: Message-ID: <521618D4.7060000@gmail.com> On 08/22/2013 05:29 AM, Neal Becker wrote: > So my son is now spending his days on c# and .net. He's enthusiastic about > async and await, and said to me last evening, "I don't think python has anything > like that". I'm not terribly knowledgeable myself regarding async programming > (since I never need to use it). I did look at this: > > http://tirania.org/blog/archive/2013/Aug-15.html Any time you use a GUI library, you can often use its own async primitives (in fact you probably need to). For example glib from Gtk+ provides io wait primitives. Or if you want a completely asynchronous programming experience from top to bottom, you can use python twisted. There are also other libraries to do this. Having first-class language support is certainly nice, and it would be nice if Python had this. GvR himself agrees. http://www.youtube.com/watch?v=sOQLVm0-8Yg From tjreedy at udel.edu Thu Aug 22 19:04:32 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 22 Aug 2013 19:04:32 -0400 Subject: c# async, await In-Reply-To: <521618D4.7060000@gmail.com> References: <521618D4.7060000@gmail.com> Message-ID: On 8/22/2013 9:57 AM, Michael Torrie wrote: > On 08/22/2013 05:29 AM, Neal Becker wrote: >> So my son is now spending his days on c# and .net. He's enthusiastic about >> async and await, and said to me last evening, "I don't think python has anything >> like that". I'm not terribly knowledgeable myself regarding async programming >> (since I never need to use it). I did look at this: >> >> http://tirania.org/blog/archive/2013/Aug-15.html The iterator protocol, introduced in 2.2, was explicitly intended (by Tim Peters) to replace many uses of synchonous callbacks. This example from the blog, of callback replacement, int sum_values (Hashtable hash) { int sum = 0; hash.foreach ((key, value) => { sum += value; }); return sum; } is written in Python *much more generally* as def sum(iterable): sum = 0 for item in iterable: sum += item return sum Notice that this is not limited to summing ints, nor to summing values in a hash. sum(somedic.values()) does the specific task of summing hash values. > Any time you use a GUI library, you can often use its own async > primitives (in fact you probably need to). For example glib from Gtk+ > provides io wait primitives. Or if you want a completely asynchronous > programming experience from top to bottom, you can use python twisted. > There are also other libraries to do this. > > Having first-class language support is certainly nice, and it would be > nice if Python had this. GvR himself agrees. > http://www.youtube.com/watch?v=sOQLVm0-8Yg C#'s await was part of the early discussion about Python's new asynch library. Last I knew, 'Tulip' uses callbacks at the lowest level, but the user level uses generators and 'yield from'. I hope this makes in into 3.4. -- Terry Jan Reedy From neilc at norwich.edu Thu Aug 22 10:21:02 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 22 Aug 2013 14:21:02 GMT Subject: c# async, await References: Message-ID: On 2013-08-22, Neal Becker wrote: > So my son is now spending his days on c# and .net. He's enthusiastic about > async and await, and said to me last evening, "I don't think python has anything > like that". I'm not terribly knowledgeable myself regarding async programming > (since I never need to use it). I did look at this: > > http://tirania.org/blog/archive/2013/Aug-15.html > > I wonder what response the python community might have. GVR and a team of top people is working on the problem: http://www.python.org/dev/peps/pep-3156/ There's also Twisted: http://twistedmatrix.com/trac/ -- Neil Cerutti From ian.g.kelly at gmail.com Fri Aug 23 00:11:19 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 22 Aug 2013 22:11:19 -0600 Subject: c# async, await In-Reply-To: References: Message-ID: On Thu, Aug 22, 2013 at 5:29 AM, Neal Becker wrote: > So my son is now spending his days on c# and .net. He's enthusiastic about > async and await, and said to me last evening, "I don't think python has anything > like that". I'm not terribly knowledgeable myself regarding async programming > (since I never need to use it). I did look at this: > > http://tirania.org/blog/archive/2013/Aug-15.html > > I wonder what response the python community might have. I've done something sort of similar to await in Python using restartable functions. The code looks like this (using Twisted Deferreds, but any sort of promise could be substituted in): from functools import wraps from twisted.internet import defer def restartable(func): def resume(result, is_failure, results, args, kws): def await(get_deferred, *args, **kws): try: is_failure, result = reversed_results.pop() except IndexError: raise Await(get_deferred(*args, **kws)) if is_failure: result.raiseException() return result def do_once(func, *args, **kws): return await(defer.maybeDeferred, func, *args, **kws) await.do_once = do_once if results is None: results = [] else: results.append((is_failure, result)) reversed_results = list(reversed(results)) try: func(await, *args, **kws) except Await as exc: deferred = exc.args[0] deferred.addCallback(resume, False, results, args, kws) deferred.addErrback(resume, True, results, args, kws) @wraps(func) def wrapper(*args, **kws): return resume(None, None, None, args, kws) return wrapper class Await(BaseException): pass The usage of restartable and await then looks something like this: @restartable def random_sum(await): try: a = await(random_number) b = await(random_number) c = await(random_number) d = await(random_number) except ValueError as exc: print("Couldn't get four numbers: " + exc.message) return print('{} + {} + {} + {} = {}'.format(a, b, c, d, a + b + c + d)) The "await" argument is passed in by the restartable machinery, not by the caller. The argument passed to await is a callable that is expected to return a Deferred, and any additional arguments are passed along to the callable. A boring implementation of the "random_number" callable might look like this: def random_number(): from random import randrange from twisted.internet import defer, reactor deferred = defer.Deferred() if randrange(4) > 0: number = randrange(42) print("Generated {}".format(number)) reactor.callLater(1, deferred.callback, number) else: print("Failed") reactor.callLater(1, deferred.errback, ValueError("Not available")) return deferred Of course the big caveat to all this is that since the function is restartable, the "random_sum" function above actually gets called five times, and so if there are any side effects before the last await, they'll end up happening multiple times. This can be averted using await.do_once: @restartable def random_sum(await): try: await.do_once(print, 1) a = await(random_number) await.do_once(print, 2) b = await(random_number) await.do_once(print, 3) c = await(random_number) await.do_once(print, 4) d = await(random_number) except ValueError as exc: print("Couldn't get four numbers: " + exc.message) return print('{} + {} + {} + {} = {}'.format(a, b, c, d, a + b + c + d)) The result of running this is: 1 Generated 35 2 Generated 28 3 Generated 32 4 Generated 16 35 + 28 + 32 + 16 = 111 From esnhoesnho at gmail.com Thu Aug 22 11:32:29 2013 From: esnhoesnho at gmail.com (esnho esnho) Date: Thu, 22 Aug 2013 08:32:29 -0700 (PDT) Subject: Python, VST and Csound Message-ID: Hello to all, I have a question for you: is possible to develop VST using Python interfaced with Csound? In case it is possible, there are any resources that can help in the developing? Thank you very much. From toby at tobiah.org Thu Aug 22 13:38:00 2013 From: toby at tobiah.org (Tobiah) Date: Thu, 22 Aug 2013 10:38:00 -0700 Subject: Python, VST and Csound In-Reply-To: References: Message-ID: <52164C78.50708@tobiah.org> Have you tried the csound mailing list? There's a lot of strong talent there. To subscribe, send an e-mail to: sympa at lists.bath.ac.uk On 08/22/2013 08:32 AM, esnho esnho wrote: > Hello to all, > I have a question for you: > is possible to develop VST using Python interfaced with Csound? > In case it is possible, there are any resources that can help in the developing? > > Thank you very much. > From bithead0101 at gmail.com Thu Aug 22 17:03:04 2013 From: bithead0101 at gmail.com (Bitswapper) Date: Thu, 22 Aug 2013 14:03:04 -0700 (PDT) Subject: Can a child access parent attributes if that child added post-hoc as an attribute to the parent? Message-ID: <14d50efb-e88b-4259-960a-97ef58e345d8@googlegroups.com> So I have a parent and child class: class Map(object): def __init__(self, name=''): self.mapName = name self.rules = {} class Rule(Map): def __init__(self, number): Map.__init__(self) self.number = number def __repr__(self): return "Map " + self.mapName + " rule number " + str(self.number) if __name__ == "__main__": map = Map("thismap") rule = Rule(1) map.rules[rule.number] = rule with the above: $ python -i inherit.py >>> map <__main__.Map object at 0xb7e889ec> >>> map.rules {1: Map rule number 1} >>> map.rules[1] Map rule number 1 >>> I have tried adding: map.rules[2] = Rule(2) but that still gets: $ python -i inherit.py >>> map.rules {1: Map rule number 1, 2: Map rule number 2} >>> and: map.rule = Rule(3) which also doesn't really get me what I'm looking for: >>> map.rules {1: Map rule number 1, 2: Map rule number 2} >>> map.rule Map rule number 3 >>> It seems to me what I'm trying to do is link an arbitrary child instance to an arbitrary instance of a parent class, which in this case would be handy Because I'd like to populate a map with rules and print the rules including the parent map name for each rule. I'm just not sure how I would go about doing this in python. Any thoughts are welcome, and thanks in advance From ramit.prasad at jpmorgan.com.dmarc.invalid Thu Aug 22 17:26:24 2013 From: ramit.prasad at jpmorgan.com.dmarc.invalid (Prasad, Ramit) Date: Thu, 22 Aug 2013 21:26:24 +0000 Subject: Can a child access parent attributes if that child added post-hoc as an attribute to the parent? In-Reply-To: <14d50efb-e88b-4259-960a-97ef58e345d8@googlegroups.com> References: <14d50efb-e88b-4259-960a-97ef58e345d8@googlegroups.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF47418683AD8@SCACMX008.exchad.jpmchase.net> Bitswapper wrote: > > So I have a parent and child class: > > > class Map(object): > def __init__(self, name=''): > self.mapName = name > self.rules = {} > > class Rule(Map): > def __init__(self, number): > Map.__init__(self) > self.number = number This means that rules will never have a name. I think you need def __init__(self, name='', number=None): Map.__init__(self, name) self.number = number > > def __repr__(self): > return "Map " + self.mapName + " rule number " + str(self.number) > > if __name__ == "__main__": > map = Map("thismap") > rule = Rule(1) > map.rules[rule.number] = rule > > > > with the above: > $ python -i inherit.py > >>> map > <__main__.Map object at 0xb7e889ec> > >>> map.rules > {1: Map rule number 1} > >>> map.rules[1] > Map rule number 1 > >>> > > > I have tried adding: > map.rules[2] = Rule(2) > > but that still gets: > > $ python -i inherit.py > >>> map.rules > {1: Map rule number 1, 2: Map rule number 2} > >>> > > and: > map.rule = Rule(3) > > which also doesn't really get me what I'm looking for: > > >>> map.rules > {1: Map rule number 1, 2: Map rule number 2} > >>> map.rule > Map rule number 3 > >>> > > > It seems to me what I'm trying to do is link an arbitrary child instance to an arbitrary instance of a > parent class, which in this case would be handy Because I'd like to populate a map with rules and > print the rules including the parent map name for each rule. I'm just not sure how I would go about > doing this in python. > > Any thoughts are welcome, and thanks in advance I not sure what you mean by the above. Can you provide an example of what you want to occur and the output for it? ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From ian.g.kelly at gmail.com Thu Aug 22 17:59:17 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 22 Aug 2013 15:59:17 -0600 Subject: Can a child access parent attributes if that child added post-hoc as an attribute to the parent? In-Reply-To: <5B80DD153D7D744689F57F4FB69AF47418683AD8@SCACMX008.exchad.jpmchase.net> References: <14d50efb-e88b-4259-960a-97ef58e345d8@googlegroups.com> <5B80DD153D7D744689F57F4FB69AF47418683AD8@SCACMX008.exchad.jpmchase.net> Message-ID: On Thu, Aug 22, 2013 at 3:26 PM, Prasad, Ramit wrote: > Bitswapper wrote: >> >> So I have a parent and child class: >> >> >> class Map(object): >> def __init__(self, name=''): >> self.mapName = name >> self.rules = {} >> >> class Rule(Map): >> def __init__(self, number): >> Map.__init__(self) >> self.number = number > > This means that rules will never have a name. I think you need > def __init__(self, name='', number=None): > Map.__init__(self, name) > self.number = number No, that's still wrong. The OP talks abut maps having names, not rules having names. Unless a Rule is-a Map, which sounds unlikely, Rule should not be inheriting from Map in the first place. >> It seems to me what I'm trying to do is link an arbitrary child instance to an arbitrary instance of a >> parent class, which in this case would be handy Because I'd like to populate a map with rules and >> print the rules including the parent map name for each rule. I'm just not sure how I would go about >> doing this in python. You'll need to keep a reference to the Map on each Rule instance. So instead of self.mapName you'll have self.map.mapName. Your Rule class should probably look something like this: class Rule(object): def __init__(self, map, number): self.map = map self.number = number And then when you construct it you'll need to tell it what map it belongs to: rule = Rule(map, 1) From ramit.prasad at jpmorgan.com.dmarc.invalid Tue Aug 27 14:38:27 2013 From: ramit.prasad at jpmorgan.com.dmarc.invalid (Prasad, Ramit) Date: Tue, 27 Aug 2013 18:38:27 +0000 Subject: Can a child access parent attributes if that child added post-hoc as an attribute to the parent? In-Reply-To: References: <14d50efb-e88b-4259-960a-97ef58e345d8@googlegroups.com> <5B80DD153D7D744689F57F4FB69AF47418683AD8@SCACMX008.exchad.jpmchase.net> Message-ID: <5B80DD153D7D744689F57F4FB69AF4741868E152@SCACMX008.exchad.jpmchase.net> Ian Kelly wrote: > On Thu, Aug 22, 2013 at 3:26 PM, Prasad, Ramit > wrote: > > Bitswapper wrote: > >> > >> So I have a parent and child class: > >> > >> > >> class Map(object): > >> def __init__(self, name=''): > >> self.mapName = name > >> self.rules = {} > >> > >> class Rule(Map): > >> def __init__(self, number): > >> Map.__init__(self) > >> self.number = number > > > > This means that rules will never have a name. I think you need > > def __init__(self, name='', number=None): > > Map.__init__(self, name) > > self.number = number > > No, that's still wrong. The OP talks abut maps having names, not > rules having names. Unless a Rule is-a Map, which sounds unlikely, > Rule should not be inheriting from Map in the first place. > Good point. Composition definitely makes more sense as I was confused by how the inheritance was supposed to work anyway. :) ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From bithead0101 at gmail.com Fri Aug 23 15:39:58 2013 From: bithead0101 at gmail.com (Bitswapper) Date: Fri, 23 Aug 2013 12:39:58 -0700 (PDT) Subject: Can a child access parent attributes if that child added post-hoc as an attribute to the parent? In-Reply-To: References: <14d50efb-e88b-4259-960a-97ef58e345d8@googlegroups.com> <5B80DD153D7D744689F57F4FB69AF47418683AD8@SCACMX008.exchad.jpmchase.net> Message-ID: <94549b6a-908b-4d7b-bc2c-23ef22532ba8@googlegroups.com> On Thursday, August 22, 2013 4:59:17 PM UTC-5, Ian wrote: > On Thu, Aug 22, 2013 at 3:26 PM, Prasad, Ramit > > <> wrote: > > > Bitswapper wrote: > > >> > > >> So I have a parent and child class: > > >> > > >> > > >> class Map(object): > > >> def __init__(self, name=''): > > >> self.mapName = name > > >> self.rules = {} > > >> > > >> class Rule(Map): > > >> def __init__(self, number): > > >> Map.__init__(self) > > >> self.number = number > > > > > > This means that rules will never have a name. I think you need > > > def __init__(self, name='', number=None): > > > Map.__init__(self, name) > > > self.number = number > > > > No, that's still wrong. The OP talks abut maps having names, not > > rules having names. Unless a Rule is-a Map, which sounds unlikely, > > Rule should not be inheriting from Map in the first place. > > > > >> It seems to me what I'm trying to do is link an arbitrary child instance to an arbitrary instance of a > > >> parent class, which in this case would be handy Because I'd like to populate a map with rules and > > >> print the rules including the parent map name for each rule. I'm just not sure how I would go about > > >> doing this in python. > > > > You'll need to keep a reference to the Map on each Rule instance. So > > instead of self.mapName you'll have self.map.mapName. Your Rule class > > should probably look something like this: > > > > class Rule(object): > > def __init__(self, map, number): > > self.map = map > > self.number = number > > > > And then when you construct it you'll need to tell it what map it belongs to: > > > > rule = Rule(map, 1) Actually yea, that makes sense. I was looking for a way for a child to 'automagically' inherit parent instance-specific data via inheritance only by virtue of being a child of a parent instance. What you're suggesting makes more sense. Thanks! From bithead0101 at gmail.com Thu Aug 22 18:00:38 2013 From: bithead0101 at gmail.com (Bitswapper) Date: Thu, 22 Aug 2013 15:00:38 -0700 (PDT) Subject: Can a child access parent attributes if that child added post-hoc as an attribute to the parent? In-Reply-To: References: <14d50efb-e88b-4259-960a-97ef58e345d8@googlegroups.com> Message-ID: On Thursday, August 22, 2013 4:26:24 PM UTC-5, Prasad, Ramit wrote: > Bitswapper wrote: > > > > > > So I have a parent and child class: > > > > > > > > > class Map(object): > > > def __init__(self, name=''): > > > self.mapName = name > > > self.rules = {} > > > > > > class Rule(Map): > > > def __init__(self, number): > > > Map.__init__(self) > > > self.number = number > > > > This means that rules will never have a name. I think you need > > def __init__(self, name='', number=None): > > Map.__init__(self, name) > > self.number = number > > > > > > def __repr__(self): > > > return "Map " + self.mapName + " rule number " + str(self.number) > > > > > > if __name__ == "__main__": > > > map = Map("thismap") > > > rule = Rule(1) > > > map.rules[rule.number] = rule > > > > > > > > > > > > with the above: > > > $ python -i inherit.py > > > >>> map > > > <__main__.Map object at 0xb7e889ec> > > > >>> map.rules > > > {1: Map rule number 1} > > > >>> map.rules[1] > > > Map rule number 1 > > > >>> > > > > > > > > > I have tried adding: > > > map.rules[2] = Rule(2) > > > > > > but that still gets: > > > > > > $ python -i inherit.py > > > >>> map.rules > > > {1: Map rule number 1, 2: Map rule number 2} > > > >>> > > > > > > and: > > > map.rule = Rule(3) > > > > > > which also doesn't really get me what I'm looking for: > > > > > > >>> map.rules > > > {1: Map rule number 1, 2: Map rule number 2} > > > >>> map.rule > > > Map rule number 3 > > > >>> > > > > > > > > > It seems to me what I'm trying to do is link an arbitrary child instance to an arbitrary instance of a > > > parent class, which in this case would be handy Because I'd like to populate a map with rules and > > > print the rules including the parent map name for each rule. I'm just not sure how I would go about > > > doing this in python. > > > > > > Any thoughts are welcome, and thanks in advance > > > > I not sure what you mean by the above. Can you provide an example of what you want to occur and the output for it? > I was thinking of: map = Map('myMap') map.rules[1] = Rule[1] map.rules[2] = Rule[2] >>> print map.rules[1] >>> Map myMap rule number 1 >>> print map.rules[2] >>> Map myMap rule number 2 >>> >>> map.mapName = "newname" >>> print map.rules[1] >>> Map newname rule number 1 >>> print map.rules[2] >>> Map newname rule number 2 From bithead0101 at gmail.com Fri Aug 23 10:11:52 2013 From: bithead0101 at gmail.com (Bitswapper) Date: Fri, 23 Aug 2013 07:11:52 -0700 (PDT) Subject: Can a child access parent attributes if that child added post-hoc as an attribute to the parent? In-Reply-To: References: <14d50efb-e88b-4259-960a-97ef58e345d8@googlegroups.com> Message-ID: <39447032-7220-4350-b24a-62f2810c230e@googlegroups.com> On Thursday, August 22, 2013 5:00:38 PM UTC-5, Bitswapper wrote: > On Thursday, August 22, 2013 4:26:24 PM UTC-5, Prasad, Ramit wrote: > > > Bitswapper wrote: > > > > > > > > > > > > > > So I have a parent and child class: > > > > > > > > > > > > > > > > > > > > > class Map(object): > > > > > > > def __init__(self, name=''): > > > > > > > self.mapName = name > > > > > > > self.rules = {} > > > > > > > > > > > > > > class Rule(Map): > > > > > > > def __init__(self, number): > > > > > > > Map.__init__(self) > > > > > > > self.number = number > > > > > > > > > > > > This means that rules will never have a name. I think you need > > > > > > def __init__(self, name='', number=None): > > > > > > Map.__init__(self, name) > > > > > > self.number = number > > > > > > > > > > > > > > def __repr__(self): > > > > > > > return "Map " + self.mapName + " rule number " + str(self.number) > > > > > > > > > > > > > > if __name__ == "__main__": > > > > > > > map = Map("thismap") > > > > > > > rule = Rule(1) > > > > > > > map.rules[rule.number] = rule > > > > > > > > > > > > > > > > > > > > > > > > > > > > with the above: > > > > > > > $ python -i inherit.py > > > > > > > >>> map > > > > > > > <__main__.Map object at 0xb7e889ec> > > > > > > > >>> map.rules > > > > > > > {1: Map rule number 1} > > > > > > > >>> map.rules[1] > > > > > > > Map rule number 1 > > > > > > > >>> > > > > > > > > > > > > > > > > > > > > > I have tried adding: > > > > > > > map.rules[2] = Rule(2) > > > > > > > > > > > > > > but that still gets: > > > > > > > > > > > > > > $ python -i inherit.py > > > > > > > >>> map.rules > > > > > > > {1: Map rule number 1, 2: Map rule number 2} > > > > > > > >>> > > > > > > > > > > > > > > and: > > > > > > > map.rule = Rule(3) > > > > > > > > > > > > > > which also doesn't really get me what I'm looking for: > > > > > > > > > > > > > > >>> map.rules > > > > > > > {1: Map rule number 1, 2: Map rule number 2} > > > > > > > >>> map.rule > > > > > > > Map rule number 3 > > > > > > > >>> > > > > > > > > > > > > > > > > > > > > > It seems to me what I'm trying to do is link an arbitrary child instance to an arbitrary instance of a > > > > > > > parent class, which in this case would be handy Because I'd like to populate a map with rules and > > > > > > > print the rules including the parent map name for each rule. I'm just not sure how I would go about > > > > > > > doing this in python. > > > > > > > > > > > > > > Any thoughts are welcome, and thanks in advance > > > > > > > > > > > > I not sure what you mean by the above. Can you provide an example of what you want to occur and the output for it? > > > > > > > I was thinking of: > > > > map = Map('myMap') > > map.rules[1] = Rule(1) > > map.rules[2] = Rule(2) > > > > >>> print map.rules[1] > > >>> Map myMap rule number 1 > > >>> print map.rules[2] > > >>> Map myMap rule number 2 > > >>> > > >>> map.mapName = "newname" > > >>> print map.rules[1] > > >>> Map newname rule number 1 > > >>> print map.rules[2] > > >>> Map newname rule number 2 Or rather: map = Map('myMap') map.rules[1] = Rule(1) map.rules[2] = Rule(2) >>> print map.rules[1] >>> Map myMap rule number 1 >>> print map.rules[2] >>> Map myMap rule number 2 >>> >>> map.mapName = "newname" >>> print map.rules[1] >>> Map newname rule number 1 >>> print map.rules[2] >>> Map newname rule number 2 From rohitchormale at gmail.com Fri Aug 23 00:25:31 2013 From: rohitchormale at gmail.com (Rohit Chormale) Date: Thu, 22 Aug 2013 21:25:31 -0700 (PDT) Subject: Current trends in python Message-ID: <1481ac9b-ecf2-471c-a4f2-36dffe7c1841@googlegroups.com> Dear friends, can anybody help me to know latest trends in python...back,middle,front end softwares...frameworks etc also how exactly python based projects are developed? thank u From sepatan at sibmail.com Fri Aug 23 01:52:29 2013 From: sepatan at sibmail.com (sepatan at sibmail.com) Date: Fri, 23 Aug 2013 12:52:29 +0700 (NOVT) Subject: Sergei Ivachev Message-ID: <47446.83.172.33.173.1377237149.squirrel@www.sibmail.com> Maybe someone from the Python community tried to run Python (with a possible set of standard library modules) under the Google Native Client in browser? If you have a positive experience, can share? From sepatan at sibmail.com Fri Aug 23 04:09:30 2013 From: sepatan at sibmail.com (sepatan at sibmail.com) Date: Fri, 23 Aug 2013 15:09:30 +0700 (NOVT) Subject: Who run the Python for Google Native Client? Message-ID: <47956.83.172.33.173.1377245370.squirrel@www.sibmail.com> Maybe someone from the Python community tried to run Python (with a possible set of standard library modules) under the Google Native Client in browser? If you have a positive experience, can share? From inq1ltd at inqvista.com Fri Aug 23 11:13:17 2013 From: inq1ltd at inqvista.com (inq1ltd) Date: Fri, 23 Aug 2013 11:13:17 -0400 Subject: python interface to iMacros In-Reply-To: <47956.83.172.33.173.1377245370.squirrel@www.sibmail.com> References: <47956.83.172.33.173.1377245370.squirrel@www.sibmail.com> Message-ID: <4306837.BlUqk9OlCd@mach-114-20.site> Python help, I am running iMacros from linux/firefox and doing most of what I want. But, there are times when I want to do something of the net and then back to the iMacros script. Are there any projects out there that will connect python to imacros, something on the order of pexpect? There is a commercial version available for $400.00 If I could justify the cost, I would probably pay it. jimonlinux inqvista.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From torriem at gmail.com Sat Aug 24 12:03:22 2013 From: torriem at gmail.com (Michael Torrie) Date: Sat, 24 Aug 2013 10:03:22 -0600 Subject: python interface to iMacros In-Reply-To: <4306837.BlUqk9OlCd@mach-114-20.site> References: <47956.83.172.33.173.1377245370.squirrel@www.sibmail.com> <4306837.BlUqk9OlCd@mach-114-20.site> Message-ID: <5218D94A.2000103@gmail.com> On 08/23/2013 09:13 AM, inq1ltd wrote: > Python help, > > I am running iMacros from linux/firefox > and doing most of what I want. > > But, there are times when I want to do > something of the net and then back > to the iMacros script. > > Are there any projects out there > that will connect python to imacros, > something on the order of pexpect? I have no idea really, but you can drive Firefox using a thing called selenium. http://docs.seleniumhq.org/ There is a python library for interfacing with selenium, and from that you can drive firefox from python. From inq1ltd at inqvista.com Sat Aug 24 14:07:47 2013 From: inq1ltd at inqvista.com (inq1ltd) Date: Sat, 24 Aug 2013 14:07:47 -0400 Subject: python interface to iMacros In-Reply-To: <5218D94A.2000103@gmail.com> References: <47956.83.172.33.173.1377245370.squirrel@www.sibmail.com> <4306837.BlUqk9OlCd@mach-114-20.site> <5218D94A.2000103@gmail.com> Message-ID: <1384333.rVWRnzNGzO@mach-114-20.site> > On 08/23/2013 09:13 AM, inq1ltd wrote: > > Python help, > > > > I am running iMacros from > > linux/firefox and doing most of > > what I want. > > > > But, there are times when I want to > > do something of the net and then > > back to the iMacros script. > > > > Are there any projects out there > > that will connect python to imacros, > > something on the order of pexpect? > > I have no idea really, but you can > drive Firefox using a thing called > selenium. > http://docs.seleniumhq.org/ > > There is a python library for > interfacing with selenium, and from > that you can drive firefox from > python. I've looked at the site. Sounds like something I'll try. Thanks for the reply. jimonlinux -------------- next part -------------- An HTML attachment was scrubbed... URL: From jake.angulo at gmail.com Fri Aug 23 07:40:06 2013 From: jake.angulo at gmail.com (Jake Angulo) Date: Fri, 23 Aug 2013 21:40:06 +1000 Subject: Python variable as a string Message-ID: Sorry this is a very basic question. I have a list *var* which after some evaluation I need to refer to *var* as a string. Pseudocode: var = ['a', 'b' , 'c' , 'd'] adict = dict(var='string', anothervar='anotherstring') anotherdict = dict() if : anotherdict[akey] = adict['var'] Basically im evaluating the list *var*, and if true, i want to use *var* as a string so that i can refer to a key-value pair in *adict *(whose key name is also var for convenience). * * Or maybe i should do things differently? Any help and code will be appreciated! -------------- next part -------------- An HTML attachment was scrubbed... URL: From neilc at norwich.edu Fri Aug 23 09:23:27 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 23 Aug 2013 13:23:27 GMT Subject: Python variable as a string References: Message-ID: On 2013-08-23, Jake Angulo wrote: > I have a list *var* which after some evaluation I need to refer > to *var* as a string. You must make a str version of var. > Pseudocode: > > var = ['a', 'b' , 'c' , 'd'] > adict = dict(var='string', anothervar='anotherstring') > anotherdict = dict() > if : > anotherdict[akey] = adict['var'] anotherdict[akey] = adict[str(var)] Will actually work, though you might prefer: anotherdict[akey] = adict[''.join(var)] Try them out and see. -- Neil Cerutti From jake.angulo at gmail.com Sat Aug 24 07:11:55 2013 From: jake.angulo at gmail.com (Jake Angulo) Date: Sat, 24 Aug 2013 21:11:55 +1000 Subject: Python variable as a string In-Reply-To: References: Message-ID: Thank you all for the reply. Actually yes this was a confusing question, and borne out of trying to make a shortcut. I didnt ask to convert the contents of var into a string. All I needed was to get the literal equivalent "var" because I needed to use it in another dict object - whose keys i named the same (eg 'var') for convenience. Instead i ended up complicating stuff. I resolved this by doing things differently with (if - elif - else). Sorry for the confusion - but thanks all for answering - i can use a code or two of what you have shared! On Fri, Aug 23, 2013 at 11:23 PM, Neil Cerutti wrote: > On 2013-08-23, Jake Angulo wrote: > > I have a list *var* which after some evaluation I need to refer > > to *var* as a string. > > You must make a str version of var. > > > Pseudocode: > > > > var = ['a', 'b' , 'c' , 'd'] > > adict = dict(var='string', anothervar='anotherstring') > > anotherdict = dict() > > if : > > anotherdict[akey] = adict['var'] > > anotherdict[akey] = adict[str(var)] > > Will actually work, though you might prefer: > > anotherdict[akey] = adict[''.join(var)] > > Try them out and see. > > -- > Neil Cerutti > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Fri Aug 23 09:40:04 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Aug 2013 13:40:04 GMT Subject: Python variable as a string References: Message-ID: <52176634$0$29986$c3e8da3$5496439d@news.astraweb.com> On Fri, 23 Aug 2013 21:40:06 +1000, Jake Angulo wrote: > Sorry this is a very basic question. Not so much "basic" as confusing. > I have a list *var* which after some evaluation I need to refer to *var* > as a string. > > Pseudocode: > > var = ['a', 'b' , 'c' , 'd'] > adict = dict(var='string', anothervar='anotherstring') This creates a dict with two keys, "var" and "anothervar". If you print it, you will get this: {'var': 'string', 'anothervar': 'anotherstring'} Is that what you intended? If not, what did you intend? > anotherdict = dict() > if : > anotherdict[akey] = adict['var'] I don't understand what this code has to do with your question. Your explanation below doesn't seem to have anything to do with the code you show here. You don't evaluate the list var, or test it in a truth context. Apart from wrapping condition in angle brackets, for no reason I understand, the above is perfectly fine Python code (except, of course, condition and akey are undefined). > Basically im evaluating the list *var*, and if true, i want to use *var* > as a string so that i can refer to a key-value pair in *adict *(whose > key name is also var for convenience). I don't understand what you are trying to accomplish, but I have two guesses. If you want a string "var", just type "var" in quotation marks, like you do above. If you want to use the *contents* of variable var as a string, just call the str() function on it: py> alist = ['a', 'b' , 'c' , 'd'] py> key = str(alist) py> adict={} py> adict[key] = "whatever you like" py> adict {"['a', 'b', 'c', 'd']": 'whatever you like'} Or if you prefer: py> {key: "whatever"} {"['a', 'b', 'c', 'd']": 'whatever'} If you want something else, you'll need to explain more carefully what you want. > Or maybe i should do things differently? Possibly. What sort of things did you have in mind? :-) -- Steven From vlastimil.brom at gmail.com Fri Aug 23 12:13:38 2013 From: vlastimil.brom at gmail.com (Vlastimil Brom) Date: Fri, 23 Aug 2013 18:13:38 +0200 Subject: Python variable as a string In-Reply-To: References: Message-ID: 2013/8/23 Jake Angulo : > Sorry this is a very basic question. > > I have a list var which after some evaluation I need to refer to var as a > string. > > Pseudocode: > > var = ['a', 'b' , 'c' , 'd'] > adict = dict(var='string', anothervar='anotherstring') > anotherdict = dict() > if : > anotherdict[akey] = adict['var'] > > > Basically im evaluating the list var, and if true, i want to use var as a > string so that i can refer to a key-value pair in adict (whose key name is > also var for convenience). > > Or maybe i should do things differently? > > Any help and code will be appreciated! > > -- > http://mail.python.org/mailman/listinfo/python-list > Hi, if I understand correctly, you would like to achieve something like the following [fictional shell session]: >>> var = "a string variable named 'var'" >>> some_magic_function(var) 'var' >>> is it the case? I believe, it is not (easily and reliably) possible, and especially it doesn't seem to be of any real use, I can think of. cf. e.g. the discussion http://stackoverflow.com/questions/2553354/how-to-get-a-variable-name-as-a-string-in-python What is your use case, where you can't use the actual object reference, but only the string? What name string should be retrieved in case of multiple names referencing the same object? hth, vbr From gouzounakis at hotmail.com Fri Aug 23 07:58:40 2013 From: gouzounakis at hotmail.com (D. Xenakis) Date: Fri, 23 Aug 2013 04:58:40 -0700 (PDT) Subject: What does sys.stdout.flush() do? Message-ID: Somewhere i read.. sys.stdout.flush(): Flush on a file object pushes out all the data that has been buffered to that point. Can someone post here a script example with sys.stdout.flush(), where in case i commented that i could understand what the difference really would be? Whenever i try to remove that line (in scripts that i find online), i cant find any differences. I've just noticed that it is usually called right after sys.stdout.write(..) thx From __peter__ at web.de Fri Aug 23 08:21:16 2013 From: __peter__ at web.de (Peter Otten) Date: Fri, 23 Aug 2013 14:21:16 +0200 Subject: What does sys.stdout.flush() do? References: Message-ID: D. Xenakis wrote: > Somewhere i read.. > sys.stdout.flush(): Flush on a file object pushes out all the data that > has been buffered to that point. > > Can someone post here a script example with sys.stdout.flush(), where in > case i commented that i could understand what the difference really would > be? > > Whenever i try to remove that line (in scripts that i find online), i cant > find any differences. I've just noticed that it is usually called right > after sys.stdout.write(..) thx Use time.sleep() to see the effects of (line) buffering. Type the following in your interactive interpreter and be enlightened ;) >>> import sys, time >>> for i in range(10): ... print i, ... time.sleep(.1) ... 0 1 2 3 4 5 6 7 8 9 >>> for i in range(10): ... print i, ... sys.stdout.flush() ... time.sleep(.1) ... 0 1 2 3 4 5 6 7 8 9 From gordon at panix.com Fri Aug 23 10:06:51 2013 From: gordon at panix.com (John Gordon) Date: Fri, 23 Aug 2013 14:06:51 +0000 (UTC) Subject: What does sys.stdout.flush() do? References: Message-ID: In "D. Xenakis" writes: > Can someone post here a script example with sys.stdout.flush(), where in > case i commented that i could understand what the difference really would > be? Depending what sys.stdout is connected to (a file, the screen, a pipe, etc.), data doesn't necessarily get written right away. In particular, screen output often waits until it receives a newline before displaying anything. flush() makes sure it all gets written *right now*. -- 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 bv4bv4bv4 at gmail.com Fri Aug 23 08:26:56 2013 From: bv4bv4bv4 at gmail.com (bv4bv4bv4 at gmail.com) Date: Fri, 23 Aug 2013 05:26:56 -0700 (PDT) Subject: WAS ISLAM SPREAD BY THE SWORD ????????????? Message-ID: WAS ISLAM SPREAD BY THE SWORD? Refutation of a common myth that states Islam was spread by the sword and that people were forced to become Muslims. It is a common misconception with some non-Muslims that Islam would not have millions of adherents all over the world, if it had not been spread by the use of force. The following points will make it clear, that far from being spread by the sword, it was the inherent force of truth, reason and logic that was responsible for the rapid spread of Islam. Islam has always given respect and freedom of religion to all faiths. Freedom of religion is ordained in the Quran itself: ?There shall be no compulsion in [acceptance of] the religion. The right course has become clear from the wrong.? (Quran 2:256) The noted historian De Lacy O?Leary wrote:[1] ?History makes it clear however, that the legend of fanatical Muslims sweeping through the world and forcing Islam at the point of the sword upon conquered races is one of the most fantastically absurd myths that historians have ever repeated.? The famous historian, Thomas Carlyle, in his book Heroes and Hero worship, refers to this misconception about the spread of Islam: ?The sword indeed, but where will you get your sword? Every new opinion, at its starting is precisely in a minority of one; in one man?s head alone. There it dwells as yet. One man alone of the whole world believes it, there is one man against all men. That he takes a sword and tries to propagate with that will do little for him. You must get your sword! On the whole, a thing will propagate itself as it can.? If Islam was spread by the sword, it was the sword of intellect and convincing arguments. It is this sword that conquers the hearts and minds of people. The Quran says in this connection: ?Invite to the way of your Lord with wisdom and good instruction, and argue with them in a way that is best.? (Quran 16:125) The facts speak for themselves ? Indonesia is the country that has the largest number of Muslims in the world, and the majority of people in Malaysia are Muslims. But, no Muslim army ever went to Indonesia or Malaysia. It is an established historical fact that Indonesia entered Islam not due to war, but because of its moral message. Despite the disappearance of Islamic government from many regions once ruled by it, their original inhabitants have remained Muslims. Moreover, they carried the message of truth, inviting others to it as well, and in so doing endured harm, affliction and oppression. The same can be said for those in the regions of Syria and Jordan, Egypt, Iraq, North Africa, Asia, the Balkans and in Spain. This shows that the effect of Islam on the population was one of moral conviction, in contrast to occupation by western colonialists, finally compelled to leave lands whose peoples held only memories of affliction, sorrow, subjugation and oppression. ? Muslims ruled Spain (Andalusia) for about 800 years. During this period the Christians and Jews enjoyed freedom to practice their respective religions, and this is a documented historical fact. ? Christian and Jewish minorities have survived in the Muslim lands of the Middle East for centuries. Countries such as Egypt, Morocco, Palestine, Lebanon, Syria, and Jordan all have significant Christian and Jewish populations. ? Muslims ruled India for about a thousand years, and therefore had the power to force each and every non-Muslim of India to convert to Islam, but they did not, and thus more than 80% of the Indian population remains non-Muslim. ? Similarly, Islam spread rapidly on the East Coast of Africa. And likewise no Muslim army was ever dispatched to the East Coast of Africa. ? An article in Reader?s Digest ?Almanac?, yearbook 1986, gives the statistics of the increase of the percentage of the major religions of the world in half a century from 1934 to 1984. This article also appeared in The Plain Truth magazine. At the top was Islam, which increased by 235%, while Christianity had increased by 47%. During this fifty-year period, there was no ?Islamic conquest? yet Islam spread at an extraordinary rate. ? Today the fastest growing religion in America and Europe is Islam. The Muslims in these lands are a minority. The only sword they have in their possession is the sword of truth. It is this sword that is converting thousands to Islam. ? Islamic law protects the privileged status of minorities, and that is why non-Muslim places of worship have flourished all over the Islamic world. Islamic law also allows non-Muslim minorities to set up their own courts, which implement family laws drawn up by the minorities themselves. The life and property of all citizens in an Islamic state are considered sacred whether they are Muslims or not. Conclusion It is clear, therefore, that Islam did not spread by the sword. The ?sword of Islam? did not convert all the non-Muslim minorities in Muslim countries. In India, where Muslims ruled for 800 years, they are still a minority. In the U.S.A., Islam is the fastest growing religion and has over six million followers. In his book The World?s Religions, Huston Smith discusses how the prophet Muhammad granted freedom of religion to the Jews and Christians under Muslim rule: The Prophet had a document drawn up in which he stipulated that Jews and Christians ?shall be protected from all insults and harm; they shall have an equal right with our own people to our assistance and good offices,? and further, ?they shall practice their religion as freely as the Muslims.?[2] Smith points out that Muslims regard that document as the first charter of freedom of conscience in human history and the authoritative model for those of every subsequent Muslim state. Footnotes: [1] In his book Islam at the Crossroads, p.8. [2] Quoted in The World?s Religions by Huston Smith, Harper Collins, 1991, p. 256 http://www.islamreligion.com/articles/677/ thank you From lightaiyee at gmail.com Fri Aug 23 08:32:10 2013 From: lightaiyee at gmail.com (lightaiyee at gmail.com) Date: Fri, 23 Aug 2013 05:32:10 -0700 (PDT) Subject: How to send broadcast IP address to network? Message-ID: <659fa544-4fd6-406c-ad44-aa155e2c0e40@googlegroups.com> I want to send a broadcast packet to all the computers connected to my home router. The following 2 lines of code do not work; host="192.168.0.102" s.connect((host, port)) Can someone advise? Thank you. From lightaiyee at gmail.com Fri Aug 23 08:36:37 2013 From: lightaiyee at gmail.com (lightaiyee at gmail.com) Date: Fri, 23 Aug 2013 05:36:37 -0700 (PDT) Subject: How to send broadcast IP address to network? In-Reply-To: <659fa544-4fd6-406c-ad44-aa155e2c0e40@googlegroups.com> References: <659fa544-4fd6-406c-ad44-aa155e2c0e40@googlegroups.com> Message-ID: Some typo mistake. Should be host="192.168.0.255", not "192.168.0.102" On Friday, August 23, 2013 8:32:10 PM UTC+8, light... at gmail.com wrote: > I want to send a broadcast packet to all the computers connected to my home router. > > > > The following 2 lines of code do not work; > > host="192.168.0.102" > > s.connect((host, port)) > > > > Can someone advise? > > > > Thank you. From neilc at norwich.edu Fri Aug 23 09:05:51 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 23 Aug 2013 13:05:51 GMT Subject: How to send broadcast IP address to network? References: <659fa544-4fd6-406c-ad44-aa155e2c0e40@googlegroups.com> Message-ID: On 2013-08-23, lightaiyee at gmail.com wrote: >> The following 2 lines of code do not work; >> >> host="192.168.0.255" >> host="192.168.0.102" >> s.connect((host, port)) Traceback (most recent call last): File "", line 1, in NameError: name 's' is not defined I bet that's not the same traceback you get. Furthermore, port isn't defined either. -- Neil Cerutti From rosuav at gmail.com Fri Aug 23 10:11:07 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 24 Aug 2013 00:11:07 +1000 Subject: How to send broadcast IP address to network? In-Reply-To: <659fa544-4fd6-406c-ad44-aa155e2c0e40@googlegroups.com> References: <659fa544-4fd6-406c-ad44-aa155e2c0e40@googlegroups.com> Message-ID: On Fri, Aug 23, 2013 at 10:32 PM, wrote: > I want to send a broadcast packet to all the computers connected to my home router. > > The following 2 lines of code do not work; > host="192.168.0.102" > s.connect((host, port)) > > Can someone advise? You can't establish a TCP socket with a broadcast address. That just doesn't work. Can you please show a whole lot more context so we can see what's happening here? Thanks! ChrisA From irmen.NOSPAM at xs4all.nl Sat Aug 24 07:33:12 2013 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Sat, 24 Aug 2013 13:33:12 +0200 Subject: How to send broadcast IP address to network? In-Reply-To: <659fa544-4fd6-406c-ad44-aa155e2c0e40@googlegroups.com> References: <659fa544-4fd6-406c-ad44-aa155e2c0e40@googlegroups.com> Message-ID: <521899fb$0$16011$e4fe514c@news.xs4all.nl> On 23-8-2013 14:32, lightaiyee at gmail.com wrote: > I want to send a broadcast packet to all the computers connected to my home router. > > The following 2 lines of code do not work; > host="192.168.0.102" > s.connect((host, port)) > > Can someone advise? > > Thank you. > Use UDP (datagram) sockets. Use sendto() and not connect(), because UDP is connectionless. This should work: import socket sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) sock.sendto(b"thedata", 0, ("", 9999)) # 9999 = port sock.close() Irmen From maildragons1 at gmail.com Fri Aug 23 11:49:15 2013 From: maildragons1 at gmail.com (maildragons1 at gmail.com) Date: Fri, 23 Aug 2013 08:49:15 -0700 (PDT) Subject: How to add additional font face to Python 3.3.2 IDLE? Message-ID: How can I add for example Droid Sans Mono to python 3.3.2 IDLE? I'm not very familliar with font faces, really. Thank's! From tjreedy at udel.edu Fri Aug 23 12:33:41 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 23 Aug 2013 12:33:41 -0400 Subject: How to add additional font face to Python 3.3.2 IDLE? In-Reply-To: References: Message-ID: On 8/23/2013 11:49 AM, maildragons1 at gmail.com wrote: > How can I add for example Droid Sans Mono to python 3.3.2 IDLE? I'm not very familliar with font faces, really. I suspect that Idle just looks in the standard font directory of your system to make the list of available fonts that it displays in the Options dialog. So I think that you just need to 'install' the font, whatever than means for your OS. -- Terry Jan Reedy From maildragons1 at gmail.com Fri Aug 23 16:20:58 2013 From: maildragons1 at gmail.com (maildragons1 at gmail.com) Date: Fri, 23 Aug 2013 13:20:58 -0700 (PDT) Subject: How to add additional font face to Python 3.3.2 IDLE? In-Reply-To: References: Message-ID: <199848f0-5ebd-4d70-9ec1-0dfb5e6e224e@googlegroups.com> 23 ?????? 2013, ?????, 19:33:41 UTC+3, Terry Reedy ??????: > On 8/23/2013 11:49 AM, maildragons1 at gmail.com wrote: > > > How can I add for example Droid Sans Mono to python 3.3.2 IDLE? I'm not very familliar with font faces, really. > > > > I suspect that Idle just looks in the standard font directory of your > > system to make the list of available fonts that it displays in the > > Options dialog. So I think that you just need to 'install' the font, > > whatever than means for your OS. > > > > -- > > Terry Jan Reedy I searched a little on that topic and i found that this is actualy what the IDLE is doing. Thanks Terry Jan ! From zughumancapital at yahoo.com.br Fri Aug 23 15:38:10 2013 From: zughumancapital at yahoo.com.br (zughumancapital) Date: Fri, 23 Aug 2013 19:38:10 -0000 Subject: Arpex Capital seleciona: Desenvolvedor Python / SP Message-ID: Arpex Capital seleciona para uma de suas empresas: Desenvolvedor Python Objetivo geral da Posi??o: Desenvolver software est?vel e de primeira linha, que ser? incorporado ? plataforma de WiFi mais moderna atualmente. Responsabilidades: escrever software que rodar? tanto no backend (Python) quanto no frontend (HTML5). Pr?-requisitos: - Conhecimento em Python, MongoDB - Cloud computing, BigData - Pensamento l?gico e anal?tico - Capacidade de absorver tecnologias novas de forma constante Deveres: - Escrever software s?lido em Python Forma??o: Irrelevante (n?o cobramos) Local de Trabalho: S?o Paulo/SP A empresa oferece remunera??o compat?vel com o mercado + Benef?cios. Os interessados dever?o enviar o CV para kgarcia at arpexcapital.com.br , mencionando no assunto Desenvolvedor Python. From me at davecotter.com Fri Aug 23 16:49:23 2013 From: me at davecotter.com (David M. Cotter) Date: Fri, 23 Aug 2013 13:49:23 -0700 (PDT) Subject: can't get utf8 / unicode strings from embedded python Message-ID: note everything works great if i use Ascii, but: in my utf8-encoded script i have this: > print "fr??n???" in my embedded C++ i have this: PyObject* CPython_Script::print(PyObject *args) { PyObject *resultObjP = NULL; const char *utf8_strZ = NULL; if (PyArg_ParseTuple(args, "s", &utf8_strZ)) { Log(utf8_strZ, false); resultObjP = Py_None; Py_INCREF(resultObjP); } return resultObjP; } Now, i know that my Log() can print utf8 (has for years, very well debugged) but what it *actually* prints is this: > print "fr??n???" --> fr????n?????? another method i use looks like this: > kj_commands.menu("??", "????", "?????") or > kj_commands.menu(u"??", u"????", u"?????") and in my C++ i have: SuperString ScPyObject::GetAs_String() { SuperString str; if (PyUnicode_Check(i_objP)) { #if 1 // method 1 { ScPyObject utf8Str(PyUnicode_AsUTF8String(i_objP)); str = utf8Str.GetAs_String(); } #elif 0 // method 2 { UTF8Char *uniZ = (UTF8Char *)PyUnicode_AS_UNICODE(i_objP); str.assign(&uniZ[0], &uniZ[PyUnicode_GET_DATA_SIZE(i_objP)], kCFStringEncodingUTF16); } #else // method 3 { UTF32Vec charVec(32768); CF_ASSERT(sizeof(UTF32Vec::value_type) == sizeof(wchar_t)); PyUnicodeObject *uniObjP = (PyUnicodeObject *)(i_objP); Py_ssize_t sizeL(PyUnicode_AsWideChar(uniObjP, (wchar_t *)&charVec[0], charVec.size())); charVec.resize(sizeL); charVec.push_back(0); str.Set(SuperString(&charVec[0])); } #endif } else { str.Set(uc(PyString_AsString(i_objP))); } Log(str.utf8Z()); return str; } for the string, "??", i get: --> ?????? for the *unicode* string, u"??", Methods 1, 2, and 3, i get the same thing: --> ?????? okay so what am i doing wrong??? From steve+comp.lang.python at pearwood.info Fri Aug 23 21:54:01 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 24 Aug 2013 01:54:01 GMT Subject: can't get utf8 / unicode strings from embedded python References: Message-ID: <52181238$0$29986$c3e8da3$5496439d@news.astraweb.com> On Fri, 23 Aug 2013 13:49:23 -0700, David M. Cotter wrote: > note everything works great if i use Ascii, but: > > in my utf8-encoded script i have this: > >> print "fr??n???" I see you are using Python 2, in which case there are probably two or three errors being made here. Firstly, in Python 2, the compiler assumes that the source code is encoded in ASCII, actually ASCII plus arbitrary bytes. Since your source code is *actually* UTF-8, the bytes in the file are: 70 72 69 6E 74 20 22 66 72 C3 B8 C3 A2 6E C3 A7 C3 AF C3 A9 22 But Python doesn't know the file is encoded in UTF-8, it thinks it is reading ASCII plus junk, so when it reads the file it parses those bytes into a line of code: print "~~~~~" where the ~~~~~ represents a bunch of 13 rubbish junk bytes. So that's the first problem to fix. You can fix this by adding an encoding cookie at the beginning of your module, in the first or second line: # -*- coding: utf-8 -*- The second problem is that even once you've fixed the source encoding, you're still not dealing with a proper Unicode string. In Python 2, you need to use u" ... " delimiters for Unicode, otherwise the results you get are completely arbitrary and depend on the encoding of your terminal. For example, if I set my terminal encoding to IBM-850, I get: fr??n??? from those bytes. If I set it to Central European ISO-8859-3 I get this: fr??n??? Clearly not what I intended. So change the line of code to: print u"fr??n???" Those two changes ought to fix the problem, but if they don't, try setting your terminal encoding to UTF-8 as well and see if that helps. [...] > but what it *actually* prints is this: > >> print "fr??n???" > --> fr????n?????? It's hard to say what *exactly* is happening here, because you don't explain how the python print statement somehow gets into your C++ Log code. Do I guess right that it catches stdout? If so, then what I expect is happening is that Python has read in the source code of print "~~~~~" with ~~~~~ as a bunch of junk bytes, and then your terminal is displaying those junk bytes according to whatever encoding it happens to be using. Since you are seeing this: fr????n?????? my guess is that you're using a Mac, and the encoding is set to the MacRoman encoding. Am I close? To summarise: * Add an encoding cookie, to tell Python to use UTF-8 when parsing your source file. * Use a Unicode string u"fr??n???". * Consider setting your terminal to use UTF-8, otherwise it may not be able to print all the characters you would like. * You may need to change the way data gets into your C++ Log function. If it expects bytes, you may need to use u"...".encode('utf-8') rather than just u"...". But since I don't understand how data is getting into your Log function, I can't be sure about this. I think that is everything. Does that fix your problem? -- Steven From me at davecotter.com Sat Aug 24 02:45:29 2013 From: me at davecotter.com (David M. Cotter) Date: Fri, 23 Aug 2013 23:45:29 -0700 (PDT) Subject: can't get utf8 / unicode strings from embedded python In-Reply-To: References: Message-ID: > I see you are using Python 2 correct > Firstly, in Python 2, the compiler assumes that the source code is encoded in ASCII gar, i must have been looking at doc for v3, as i thought it was all assumed to be utf8 > # -*- coding: utf-8 -*- okay, did that, still no change > you need to use u" ... " delimiters for Unicode, otherwise the results you get are completely arbitrary and depend on the encoding of your terminal. okay, well, i'm on a mac, and not using "terminal" at all. but if i were, it would be utf8 but it's still not flying :( > For example, if I set my terminal encoding to IBM-850 okay how do you even do that? this is not an interactive session, this is embedded python, within a C++ app, so there's no terminal. but that is a good question: all the docs say "default encoding" everywhere (as in "If string is a Unicode object, this function computes the default encoding of string and operates on that"), but fail to specify just HOW i can set the default encoding. if i could just say "hey, default encoding is utf8", i think i'd be done? > So change the line of code to: > print u"fr??n???" okay, sure... but i get the exact same results > Those two changes ought to fix the problem, but if they don't, try setting your terminal encoding to UTF-8 as well well, i'm not sure what you mean by that. i don't have a terminal here. i'm logging to a utf8 log file (when i print) > but what it *actually* prints is this: > > print "fr??n???" > --> fr????n?????? >It's hard to say what *exactly* is happening here, because you don't explain how the python print statement somehow gets into your C++ Log code. Do I guess right that it catches stdout? yes, i'm redirecting stdout to my own custom print class, and then from that function i call into my embedded C++ print function >If so, then what I expect is happening is that Python has read in the source code of >print "~~~~~" >with ~~~~~ as a bunch of junk bytes, and then your terminal is displaying those junk bytes according to whatever encoding it happens to be using. >Since you are seeing this: >fr????n?????? >my guess is that you're using a Mac, and the encoding is set to the MacRoman encoding. Am I close? you hit the nail on the head there, i think. using that as a hint, i took this text "fr????n??????" and pasted that into a "macRoman" document, then *reinterpreted* it as UTF8, and voala: "fr??n???" so, it seems that i AM getting my utf8 bytes, but i'm getting them converted to macRoman. huh? where is macRoman specified, and how to i change that to utf8? i think that's the missing golden ticket From davea at davea.name Sat Aug 24 03:04:41 2013 From: davea at davea.name (Dave Angel) Date: Sat, 24 Aug 2013 07:04:41 +0000 (UTC) Subject: can't get utf8 / unicode strings from embedded python References: Message-ID: David M. Cotter wrote: > Steven wrote: >> I see you are using Python 2 > correct > >>It's hard to say what *exactly* is happening here, because you don't explain how the python print statement somehow gets into your C++ Log code. Do I guess right that it catches stdout? > yes, i'm redirecting stdout to my own custom print class, and then from that function i call into my embedded C++ print function > I don't know much about embedding Python, but each file object has an encoding property. Why not examine sys.stdout.encoding ? And change it to "UTF-8" ? print "encoding is", sys.stdout.encoding sys.stdout.encoding = "UTF-8" -- DaveA From random832 at fastmail.us Sat Aug 24 09:49:31 2013 From: random832 at fastmail.us (random832 at fastmail.us) Date: Sat, 24 Aug 2013 09:49:31 -0400 Subject: can't get utf8 / unicode strings from embedded python In-Reply-To: References: Message-ID: <1377352171.5447.13632301.4BA301A6@webmail.messagingengine.com> On Sat, Aug 24, 2013, at 2:45, David M. Cotter wrote: > > you need to use u" ... " delimiters for Unicode, otherwise the results you get are completely arbitrary and depend on the encoding of your terminal. > okay, well, i'm on a mac, and not using "terminal" at all. but if i > were, it would be utf8 > but it's still not flying :( > so, it seems that i AM getting my utf8 bytes, but i'm getting them > converted to macRoman. huh? where is macRoman specified, and how to i > change that to utf8? i think that's the missing golden ticket You say you're not using terminal. What _are_ you using? From me at davecotter.com Sat Aug 24 12:47:19 2013 From: me at davecotter.com (David M. Cotter) Date: Sat, 24 Aug 2013 09:47:19 -0700 (PDT) Subject: can't get utf8 / unicode strings from embedded python In-Reply-To: References: Message-ID: > What _are_ you using? i have scripts in a file, that i am invoking into my embedded python within a C++ program. there is no terminal involved. the "print" statement has been redirected (via sys.stdout) to my custom print class, which does not specify "encoding", so i tried the suggestion above to set it: static const char *s_RedirectScript = "import " kEmbeddedModuleName "\n" "import sys\n" "\n" "class CustomPrintClass:\n" " def write(self, stuff):\n" " " kEmbeddedModuleName "." kCustomPrint "(stuff)\n" "class CustomErrClass:\n" " def write(self, stuff):\n" " " kEmbeddedModuleName "." kCustomErr "(stuff)\n" "sys.stdout = CustomPrintClass()\n" "sys.stderr = CustomErrClass()\n" "sys.stdout.encoding = 'UTF-8'\n" "sys.stderr.encoding = 'UTF-8'\n"; but it didn't help. I'm still getting back a string that is a utf-8 string of characters that, if converted to "macRoman" and then interpreted as UTF8, shows the original, correct string. who is specifying macRoman, and where, and how do i tell whoever that is that i really *really* want utf8? From wxjmfauth at gmail.com Sat Aug 24 14:31:45 2013 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Sat, 24 Aug 2013 11:31:45 -0700 (PDT) Subject: can't get utf8 / unicode strings from embedded python In-Reply-To: References: Message-ID: <3132f4c4-9a26-448b-b821-1c119a83ccd8@googlegroups.com> Le samedi 24 ao?t 2013 18:47:19 UTC+2, David M. Cotter a ?crit?: > > What _are_ you using? > > i have scripts in a file, that i am invoking into my embedded python within a C++ program. there is no terminal involved. the "print" statement has been redirected (via sys.stdout) to my custom print class, which does not specify "encoding", so i tried the suggestion above to set it: > > > > static const char *s_RedirectScript = > > "import " kEmbeddedModuleName "\n" > > "import sys\n" > > "\n" > > "class CustomPrintClass:\n" > > " def write(self, stuff):\n" > > " " kEmbeddedModuleName "." kCustomPrint "(stuff)\n" > > "class CustomErrClass:\n" > > " def write(self, stuff):\n" > > " " kEmbeddedModuleName "." kCustomErr "(stuff)\n" > > "sys.stdout = CustomPrintClass()\n" > > "sys.stderr = CustomErrClass()\n" > > "sys.stdout.encoding = 'UTF-8'\n" > > "sys.stderr.encoding = 'UTF-8'\n"; > > > > > > but it didn't help. > > > > I'm still getting back a string that is a utf-8 string of characters that, if converted to "macRoman" and then interpreted as UTF8, shows the original, correct string. who is specifying macRoman, and where, and how do i tell whoever that is that i really *really* want utf8? -------- Always encode a "unicode" into the coding of the "system" which will host it. Adapting the hosting system to your "unicode" (encoded unicode) is not a valid solution. A non sense. sys.std***.encodings do nothing. They only give you information about the coding of the hosting system. The "system" can be anything, a db, a terminal, a gui, ... Shortly, your "writer" should encode your "stuff" to your "host" in a adequate way. It is up to you to manage coherence. If your passive "writer" support only one coding, adapt "stuff", if "stuff" lives in its own coding (due to c++ ?) adapt your "writer". Example from my interactive interpreter. It is in Python 3, not important, basically the job is the same in Python 2. This interpreter has the capability to support many codings, and the coding of this host system can be changed on the fly. A commented session. By default, a string, type str, is a unicode. The host accepts "unicode". So, by default the sys.stdout coding is ''. >>> sys.stdout.encoding = '' >>> print("fr??n???") fr??n??? >>> Setting the host to utf-8 and printing the above string gives "something", but encoding into utf-8 works fine. >>> sys.stdout.encoding = 'utf-8' >>> sys.stdout.encoding 'utf-8' >>> print("fr??n???") fr????n?????? >>> print("fr??n???".encode('utf-8')) 'fr??n???' Setting the host to 'mac-roman' works fine too, as long it is properly encoded! >>> sys.stdout.encoding = 'mac-roman' >>> print("fr??n???".encode('mac-roman')) 'fr??n???' But >>> print("fr??n???".encode('utf-8')) 'fr????n??????' Ditto for cp850 >>> sys.stdout.encoding = 'cp850' >>> print("fr??n???".encode('cp850')) 'fr??n???' If the repertoire of characters of a coding scheme does not contain the characters -> replace >>> sys.stdout.encoding = 'cp437' >>> print("fr??n???".encode('cp437')) Traceback (most recent call last): File "", line 1, in File "c:\python32\lib\encodings\cp437.py", line 12, in encode return codecs.charmap_encode(input,errors,encoding_map) UnicodeEncodeError: 'charmap' codec can't encode character '\xf8' in position 2: character maps to >>> print("fr??n???".encode('cp437', 'replace')) 'fr??n???' Curiousities >>> sys.stdout.encoding = 'utf-16-be' >>> print("fr??n???") f r ? ? n ? ? ? >>> print("fr??n???".encode('utf-16-be')) 'fr??n???' >>> sys.stdout.encoding = 'utf-32-be' >>> print("fr??n???".encode('utf-32-be')) 'fr??n???' jmf From benjamin.kaplan at case.edu Sat Aug 24 15:45:37 2013 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Sat, 24 Aug 2013 12:45:37 -0700 Subject: can't get utf8 / unicode strings from embedded python In-Reply-To: References: Message-ID: On Sat, Aug 24, 2013 at 9:47 AM, David M. Cotter wrote: > > > What _are_ you using? > i have scripts in a file, that i am invoking into my embedded python within a C++ program. there is no terminal involved. the "print" statement has been redirected (via sys.stdout) to my custom print class, which does not specify "encoding", so i tried the suggestion above to set it: > > static const char *s_RedirectScript = > "import " kEmbeddedModuleName "\n" > "import sys\n" > "\n" > "class CustomPrintClass:\n" > " def write(self, stuff):\n" > " " kEmbeddedModuleName "." kCustomPrint "(stuff)\n" > "class CustomErrClass:\n" > " def write(self, stuff):\n" > " " kEmbeddedModuleName "." kCustomErr "(stuff)\n" > "sys.stdout = CustomPrintClass()\n" > "sys.stderr = CustomErrClass()\n" > "sys.stdout.encoding = 'UTF-8'\n" > "sys.stderr.encoding = 'UTF-8'\n"; > > > but it didn't help. > > I'm still getting back a string that is a utf-8 string of characters that, if converted to "macRoman" and then interpreted as UTF8, shows the original, correct string. who is specifying macRoman, and where, and how do i tell whoever that is that i really *really* want utf8? > -- If you're running this from a C++ program, then you aren't getting back characters. You're getting back bytes. If you treat them as UTF-8, they'll work properly. The only thing wrong is the text editor you're using to open the file afterwards- since you aren't specifying an encoding, it's assuming MacRoman. You can try putting the UTF-8 BOM (it's not really a BOM) at the front of the file- the bytes 0xEF 0xBB 0xBF are used by some editors to identify a file as UTF-8. From random832 at fastmail.us Sat Aug 24 20:01:35 2013 From: random832 at fastmail.us (random832 at fastmail.us) Date: Sat, 24 Aug 2013 20:01:35 -0400 Subject: can't get utf8 / unicode strings from embedded python In-Reply-To: References: Message-ID: <1377388895.2243.13754893.706E496D@webmail.messagingengine.com> On Sat, Aug 24, 2013, at 12:47, David M. Cotter wrote: > > What _are_ you using? > i have scripts in a file, that i am invoking into my embedded python > within a C++ program. there is no terminal involved. the "print" > statement has been redirected (via sys.stdout) to my custom print class, > which does not specify "encoding", so i tried the suggestion above to set > it: That doesn't answer my real question. What does your "custom print class" do with the text? From me at davecotter.com Sun Aug 25 13:57:31 2013 From: me at davecotter.com (David M. Cotter) Date: Sun, 25 Aug 2013 10:57:31 -0700 (PDT) Subject: can't get utf8 / unicode strings from embedded python In-Reply-To: References: Message-ID: i'm sorry this is so confusing, let me try to re-state the problem in as clear a way as i can. I have a C++ program, with very well tested unicode support. All logging is done in utf8. I have conversion routines that work flawlessly, so i can assure you there is nothing wrong with logging and unicode support in the underlying program. I am embedding python 2.7 into the program, and extending python with routines in my C++ program. I have a script, encoded in utf8, and *marked* as utf8 with this line: # -*- coding: utf-8 -*- In that script, i have inline unicode text. When I pass that text to my C++ program, the Python interpreter decides that these bytes are macRoman, and handily "converts" them to unicode. To compensate, i must "convert" these "macRoman" characters encoded as utf8, back to macRoman, then "interpret" them as utf8. In this way i can recover the original unicode. When i return a unicode string back to python, i must do the reverse so that Python gets back what it expects. This is not related to printing, or sys.stdout, it does happen with that too but focusing on that is a red-herring. Let's focus on just passing a string into C++ then back out. This would all actually make sense IF my script was marked as being "macRoman" even tho i entered UTF8 Characters, but that is not the case. Let's prove my statements. Here is the script, *interpreted* as MacRoman: http://karaoke.kjams.com/screenshots/bugs/python_unicode/script_as_macroman.png and here it is again *interpreted* as utf8: http://karaoke.kjams.com/screenshots/bugs/python_unicode/script_as_utf8.png here is the string conversion code: SuperString ScPyObject::GetAs_String() { SuperString str; // underlying format of SuperString is unicode if (PyUnicode_Check(i_objP)) { ScPyObject utf8Str(PyUnicode_AsUTF8String(i_objP)); str = utf8Str.GetAs_String(); } else { const UTF8Char *bytes_to_interpetZ = uc(PyString_AsString(i_objP)); // the "Set" call *interprets*, does not *convert* str.Set(bytes_to_interpetZ, kCFStringEncodingUTF8); // str is now unicode characters which *represent* macRoman characters // so *convert* these to actual macRoman // fyi: Update_utf8 means "convert to this encoding and // store the resulting bytes in the variable named "utf8" str.Update_utf8(kCFStringEncodingMacRoman); // str is now unicode characters converted from macRoman // so *reinterpret* them as UTF8 // FYI, we're just taking the pure bytes that are stored in the utf8 variable // and *interpreting* them to this encoding bytes_to_interpetZ = str.utf8().c_str(); str.Set(bytes_to_interpetZ, kCFStringEncodingUTF8); } return str; } PyObject* PyString_FromString(const SuperString& str) { SuperString localStr(str); // localStr is the real, actual unicode string // but we must *interpret* it as macRoman, then take these "macRoman" characters // and "convert" them to unicode for Python to "get it" const UTF8Char *bytes_to_interpetZ = localStr.utf8().c_str(); // take the utf8 bytes (actual utf8 prepresentation of string) // and say "no, these bytes are macRoman" localStr.Set(bytes_to_interpetZ, kCFStringEncodingMacRoman); // okay so now we have unicode of MacRoman characters (!?) // return the underlying utf8 bytes of THAT as our string return PyString_FromString(localStr.utf8Z()); } And here is the results from running the script: 18: --------------- 18: Original string: fr??n??? 18: converting... 18: it worked: fr??n??? 18: --------------- 18: --------------- 18: Original string: ?? 18: converting... 18: it worked: ?? 18: --------------- Now the thing that absolutely utterly baffles me (if i'm not baffled enough) is that i get the EXACT same results on both Mac and Windows. Why do they both insist on interpreting my script's bytes as MacRoman? From vlastimil.brom at gmail.com Sun Aug 25 14:23:41 2013 From: vlastimil.brom at gmail.com (Vlastimil Brom) Date: Sun, 25 Aug 2013 20:23:41 +0200 Subject: can't get utf8 / unicode strings from embedded python In-Reply-To: References: Message-ID: 2013/8/25 David M. Cotter : > i'm sorry this is so confusing, let me try to re-state the problem in as clear a way as i can. > > I have a C++ program, with very well tested unicode support. All logging is done in utf8. I have conversion routines that work flawlessly, so i can assure you there is nothing wrong with logging and unicode support in the underlying program. > > I am embedding python 2.7 into the program, and extending python with routines in my C++ program. > > I have a script, encoded in utf8, and *marked* as utf8 with this line: > # -*- coding: utf-8 -*- > > In that script, i have inline unicode text. When I pass that text to my C++ program, the Python interpreter decides that these bytes are macRoman, and handily "converts" them to unicode. To compensate, i must "convert" these "macRoman" characters encoded as utf8, back to macRoman, then "interpret" them as utf8. In this way i can recover the original unicode. > > When i return a unicode string back to python, i must do the reverse so that Python gets back what it expects. > > This is not related to printing, or sys.stdout, it does happen with that too but focusing on that is a red-herring. Let's focus on just passing a string into C++ then back out. > > This would all actually make sense IF my script was marked as being "macRoman" even tho i entered UTF8 Characters, but that is not the case. > > Let's prove my statements. Here is the script, *interpreted* as MacRoman: > http://karaoke.kjams.com/screenshots/bugs/python_unicode/script_as_macroman.png > > and here it is again *interpreted* as utf8: > http://karaoke.kjams.com/screenshots/bugs/python_unicode/script_as_utf8.png > > here is the string conversion code: > > SuperString ScPyObject::GetAs_String() > { > SuperString str; // underlying format of SuperString is unicode > > if (PyUnicode_Check(i_objP)) { > ScPyObject utf8Str(PyUnicode_AsUTF8String(i_objP)); > > str = utf8Str.GetAs_String(); > } else { > const UTF8Char *bytes_to_interpetZ = uc(PyString_AsString(i_objP)); > > // the "Set" call *interprets*, does not *convert* > str.Set(bytes_to_interpetZ, kCFStringEncodingUTF8); > > // str is now unicode characters which *represent* macRoman characters > // so *convert* these to actual macRoman > > // fyi: Update_utf8 means "convert to this encoding and > // store the resulting bytes in the variable named "utf8" > str.Update_utf8(kCFStringEncodingMacRoman); > > // str is now unicode characters converted from macRoman > // so *reinterpret* them as UTF8 > > // FYI, we're just taking the pure bytes that are stored in the utf8 variable > // and *interpreting* them to this encoding > bytes_to_interpetZ = str.utf8().c_str(); > > str.Set(bytes_to_interpetZ, kCFStringEncodingUTF8); > } > > return str; > } > > PyObject* PyString_FromString(const SuperString& str) > { > SuperString localStr(str); > > // localStr is the real, actual unicode string > // but we must *interpret* it as macRoman, then take these "macRoman" characters > // and "convert" them to unicode for Python to "get it" > const UTF8Char *bytes_to_interpetZ = localStr.utf8().c_str(); > > // take the utf8 bytes (actual utf8 prepresentation of string) > // and say "no, these bytes are macRoman" > localStr.Set(bytes_to_interpetZ, kCFStringEncodingMacRoman); > > // okay so now we have unicode of MacRoman characters (!?) > // return the underlying utf8 bytes of THAT as our string > return PyString_FromString(localStr.utf8Z()); > } > > And here is the results from running the script: > 18: --------------- > 18: Original string: fr??n??? > 18: converting... > 18: it worked: fr??n??? > 18: --------------- > 18: --------------- > 18: Original string: ?? > 18: converting... > 18: it worked: ?? > 18: --------------- > > Now the thing that absolutely utterly baffles me (if i'm not baffled enough) is that i get the EXACT same results on both Mac and Windows. Why do they both insist on interpreting my script's bytes as MacRoman? > -- > http://mail.python.org/mailman/listinfo/python-list Hi, unfortunately, I don't have experience with embedding python and C++, but he python (for python 2) part seems to be missing the u prefix in the unicode literals. like u"fr??n???" Is the c++ part prepared for python unicode object, or does it require utf-8 encoded string (or the respective bytes)? would oldstr.encode("utf-8") in the call make a difference? regards, vbr From tjreedy at udel.edu Sun Aug 25 14:59:36 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 25 Aug 2013 14:59:36 -0400 Subject: can't get utf8 / unicode strings from embedded python In-Reply-To: References: Message-ID: On 8/25/2013 1:57 PM, David M. Cotter wrote: > i'm sorry this is so confusing, let me try to re-state the problem in as clear a way as i can. > > I have a C++ program, with very well tested unicode support. All logging is done in utf8. I have conversion routines that work flawlessly, so i can assure you there is nothing wrong with logging and unicode support in the underlying program. > I am embedding python 2.7 into the program, and extending python with routines in my C++ program. If you want 'well-tested' (correct) unicode support from Python, use 3.3. Unicode in 2.x is somewhat buggy and definitely flakey. The first fix was to make unicode *the* text type, in 3.0. The second was to redesign the internals in 3.3. It is possible that 2.7 is too broken for what you want to do. > I have a script, encoded in utf8, and *marked* as utf8 with this line: > # -*- coding: utf-8 -*- > > In that script, i have inline unicode text. The example scripts that you posted pictures of do *not* have unicode text. They have bytestring literals with (encoded) non-ascii chars inside them. This is not a great idea. I am not sure what bytes you end up with. Apparently, not what you expect. To make them 'unicode text', you must prepend the literals with 'u'. Didn't someone say this before? > When I pass that text to my C++ program, the Python interpreter decides that these bytes are macRoman, and handily "converts" them to unicode. To compensate, i must "convert" these "macRoman" characters encoded as utf8, back to macRoman, then "interpret" them as utf8. In this way i can recover the original unicode. > > When i return a unicode string back to python, i must do the reverse so that Python gets back what it expects. > > This is not related to printing, or sys.stdout, it does happen with that too but focusing on that is a red-herring. Let's focus on just passing a string into C++ then back out. > > This would all actually make sense IF my script was marked as being "macRoman" even tho i entered UTF8 Characters, but that is not the case. > > Let's prove my statements. Here is the script, *interpreted* as MacRoman: > http://karaoke.kjams.com/screenshots/bugs/python_unicode/script_as_macroman.png Why are you posting pictures of code, instead of the (runnable) code itself, as you did with C code? -- Terry Jan Reedy From me at davecotter.com Sun Aug 25 18:25:07 2013 From: me at davecotter.com (David M. Cotter) Date: Sun, 25 Aug 2013 15:25:07 -0700 (PDT) Subject: can't get utf8 / unicode strings from embedded python In-Reply-To: References: Message-ID: <323bb822-b967-412c-a6db-bc8f993e2227@googlegroups.com> fair enough. I can provide further proof of strangeness. here is my latest script: this is saved on disk as a UTF8 encoded file, and when viewing as UTF8, it shows the correct characters. ================== # -*- coding: utf-8 -*- import time, kjams, kjams_lib def log_success(msg, successB, str): if successB: print msg + " worked: " + str else: print msg + "failed: " + str def do_test(orig_str): cmd_enum = kjams.enum_cmds() print "---------------" print "Original string: " + orig_str print "converting..." oldstr = orig_str; newstr = kjams_lib.do_command(cmd_enum.kScriptCommand_Unicode_Test, oldstr) log_success("first", oldstr == newstr, newstr); oldstr = unicode(orig_str, "UTF-8") newstr = kjams_lib.do_command(cmd_enum.kScriptCommand_Unicode_Test, oldstr) newstr = unicode(newstr, "UTF-8") log_success("second", oldstr == newstr, newstr); oldstr = unicode(orig_str, "UTF-8") oldstr.encode("UTF-8") newstr = kjams_lib.do_command(cmd_enum.kScriptCommand_Unicode_Test, oldstr) newstr = unicode(newstr, "UTF-8") log_success("third", oldstr == newstr, newstr); print "---------------" def main(): do_test("fr??n???") do_test("??") #----------------------------------------------------- if __name__ == "__main__": main() ================== and the latest results: 20: --------------- 20: Original string: fr??n??? 20: converting... 20: first worked: fr??n??? 20: second worked: fr??n??? 20: third worked: fr??n??? 20: --------------- 20: --------------- 20: Original string: ?? 20: converting... 20: first worked: ?? 20: second worked: ?? 20: third worked: ?? 20: --------------- now, given the C++ source code, this should NOT work, given that i'm doing some crazy re-coding of the bytes. so, you see, it does not matter whether i pass "unicode" strings or regular "strings", they all translate to the same, weird macroman. for completeness, here is the C++ code that the script calls: =================== case kScriptCommand_Unicode_Test: { pyArg = iterP.NextArg_OrSyntaxError(); if (pyArg.get()) { SuperString str = pyArg.GetAs_String(); resultObjP = PyString_FromString(str); } break; } =================== From me at davecotter.com Sun Aug 25 18:32:52 2013 From: me at davecotter.com (David M. Cotter) Date: Sun, 25 Aug 2013 15:32:52 -0700 (PDT) Subject: can't get utf8 / unicode strings from embedded python In-Reply-To: References: Message-ID: i got it!! OMG! so sorry for the confusion, but i learned a lot, and i can share the result: the CORRECT code *was* what i had assumed. the Python side has always been correct (no need to put "u" in front of strings, it is known that the bytes are utf8 bytes) it was my "run script" function which read in the file. THAT was what was "reinterpreting" the utf8 bytes as macRoman (on both platforms). correct code below: SuperString ScPyObject::GetAs_String() { SuperString str; if (PyUnicode_Check(i_objP)) { ScPyObject utf8Str(PyUnicode_AsUTF8String(i_objP)); str = utf8Str.GetAs_String(); } else { // calling "uc" on this means "assume this is utf8" str.Set(uc(PyString_AsString(i_objP))); } return str; } PyObject* PyString_FromString(const SuperString& str) { return PyString_FromString(str.utf8Z()); } From python at mrabarnett.plus.com Sun Aug 25 20:30:15 2013 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 26 Aug 2013 01:30:15 +0100 Subject: can't get utf8 / unicode strings from embedded python In-Reply-To: References: Message-ID: <521AA197.9010605@mrabarnett.plus.com> On 25/08/2013 23:32, David M. Cotter wrote: > i got it!! OMG! so sorry for the confusion, but i learned a lot, > and i can share the result: > > the CORRECT code *was* what i had assumed. the Python side has > always been correct (no need to put "u" in front of strings, it is > known that the bytes are utf8 bytes) > > it was my "run script" function which read in the file. THAT was > what was "reinterpreting" the utf8 bytes as macRoman (on both > platforms). correct code below: > When working with Unicode, what you should be doing is: 1. Specifying the encoding line in the special comment. 2. Setting the encoding of the source file. 3. Using Unicode string literals in the source file. You're doing (1) and (2), but not (3). If you want to pass UTF-8 to the the C++, then encode the Unicode string to bytes when you pass it. Using bytestring literals and relying on the source file being UTF-8, like you doing, is just asking for trouble, as you've found out! :-) From me at davecotter.com Tue Aug 27 18:21:00 2013 From: me at davecotter.com (David M. Cotter) Date: Tue, 27 Aug 2013 15:21:00 -0700 (PDT) Subject: can't get utf8 / unicode strings from embedded python In-Reply-To: References: Message-ID: <8adbb9f0-5205-4cc7-8efe-7ea4b4e1b01c@googlegroups.com> i am already doing (3), and all is working perfectly. bytestring literals are fine, i'm not sure what this trouble is that you speak of. note that i'm not using PyRun_AnyFile(), i'm loading the script myself, assumed as utf8 (which was my original problem, i had assumed it was macRoman), then calling PyRun_SimpleString(). it works flawlessly now, on both mac and windows. From steve+comp.lang.python at pearwood.info Tue Aug 27 19:24:37 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 27 Aug 2013 23:24:37 GMT Subject: can't get utf8 / unicode strings from embedded python References: <8adbb9f0-5205-4cc7-8efe-7ea4b4e1b01c@googlegroups.com> Message-ID: <521d3535$0$29986$c3e8da3$5496439d@news.astraweb.com> On Tue, 27 Aug 2013 15:21:00 -0700, David M. Cotter wrote: > i am already doing (3), and all is working perfectly. bytestring > literals are fine, i'm not sure what this trouble is that you speak of. Neither is anyone else, because your post is completely devoid of any context. Who are you talking to? Wait, let me see if I can peer into my crystal ball and see if the spirits tell me what you are talking about... I see a post... no, repeated posts, by many people, telling you not to embed Unicode characters in Python 2.x plain byte strings... You know what? You obviously know so much more about Unicode and Python than the entire Python community, you must be right. There is no possible way that misusing byte strings in this manner could possibly go wrong. Since byte strings literals containing Unicode data are "fine", it was clearly a complete waste of time to introduce Unicode strings in the first place. Why bother using the official interface designed to work correctly with Unicode, when you can rely on an accident of implementation that just happens to work correctly in your environment but no guarantee it will work correctly anywhere else? What could *possibly* go wrong by relying on code working by accident like this? -- Steven From me at davecotter.com Wed Aug 28 01:57:45 2013 From: me at davecotter.com (David M. Cotter) Date: Tue, 27 Aug 2013 22:57:45 -0700 (PDT) Subject: can't get utf8 / unicode strings from embedded python In-Reply-To: <521d3535$0$29986$c3e8da3$5496439d@news.astraweb.com> References: <8adbb9f0-5205-4cc7-8efe-7ea4b4e1b01c@googlegroups.com> <521d3535$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: I am very sorry that I have offended you to such a degree you feel it necessary to publicly eviscerate me. Perhaps I could have worded it like this: "So far I have not seen any troubles including unicode characters in my strings, they *seem* to be fine for my use-case. What kind of trouble has been seen with this by others?" Really, I wonder why you are so angry at me for having made a mistake? I'm going to guess that you don't have kids. From steve+comp.lang.python at pearwood.info Wed Aug 28 08:03:31 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 Aug 2013 12:03:31 GMT Subject: can't get utf8 / unicode strings from embedded python References: <8adbb9f0-5205-4cc7-8efe-7ea4b4e1b01c@googlegroups.com> <521d3535$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: <521de712$0$6599$c3e8da3$5496439d@news.astraweb.com> On Tue, 27 Aug 2013 22:57:45 -0700, David M. Cotter wrote: > I am very sorry that I have offended you to such a degree you feel it > necessary to publicly eviscerate me. You know David, you are right. I did over-react. And I apologise for that. I am sorry, I was excessively confrontational. (Although I think "eviscerate" is a bit strong.) Putting aside my earlier sarcasm, the basic message remains the same: Python byte strings are not designed to work with Unicode characters, and if they do work, it is an accident, not defined behaviour. > Perhaps I could have worded it like this: "So far I have not seen any > troubles including unicode characters in my strings, they *seem* to be > fine for my use-case. What kind of trouble has been seen with this by > others?" Exactly the same sort of trouble you were having earlier when you were inadvertently decoding the source file as MacRoman rather than UTF-8. Mojibake, garbage characters in your text, corrupted data. http://en.wikipedia.org/wiki/Mojibake The point is, you might not see these errors, because by accident all the relevant factors conspire to give you the correct result. You might test it on a Mac and on Windows and it all works well. You might even test it on a dozen different machines, and it works fine on all of them. But since you're relying on an accident of implementation, none of this is guaranteed. And then in eighteen months time, *something* changes -- a minor update to Python, a different version of Mac OS/X, an unusual Registry setting in Windows, who knows what?, and all of a sudden the factors no longer line up to give you the correct results and it all comes tumbling down in a big stinking mess. If you are lucky you will get a nice clear exception telling you something is broken, but more likely you'll just get corrupted data and mojibake and you, or the poor guy who maintains the code after you, will have no idea why. And you'll probably come here asking for our help to solve it. If you came back and said "I tried it with the u prefix, and it broke a bunch of other code, and I don't have time to fix it now so I'm reverting to the u-less byte string form" I wouldn't *like* it but I could *accept* it as one of those sub-optimal compromises people make in Real Life. I've done the same thing myself, we probably all have: written code we knew was broken, but fixing it was too hard or too low a priority. > Really, I wonder why you are so angry at me for having made a mistake? > I'm going to guess that you don't have kids. What do kids have to do with this? Are you an adult or a child? *wink* You didn't offend me so much as frustrate me. You had multiple people telling you the same thing, don't embed Unicode characters in a byte string, but you choose to not just ignore them but effectively declare that they were all wrong to give that advice, not just the people here but essentially the entire Python development community responsible for adding Unicode strings to the language. Can you blame me for feeling that your reply seemed rather arrogant? In any case, I'm glad you responded with a little more restraint than I did, and I hope you can see my point of view and hopefully I haven't soured you on this forum. -- Steven From me at davecotter.com Wed Aug 28 13:46:30 2013 From: me at davecotter.com (David M. Cotter) Date: Wed, 28 Aug 2013 10:46:30 -0700 (PDT) Subject: can't get utf8 / unicode strings from embedded python In-Reply-To: References: Message-ID: <68ed2759-6df9-456c-b49d-bc9805e29e76@googlegroups.com> Thank you for your thoughtful and thorough response. I now understand much better what you (and apparently the others) were warning me against and I will certainly consider that moving forward. I very much appreciate your help as I learn about python and embedding and all these crazy encoding problems. > What do kids have to do with this? When a person has children, they quickly learn that the best way to deal with some one who seems to be not listening or having a tantrum: show understanding and compassion, restraint and patience, as you, in the most neutral way that you can, gently bit firmly guide said person back on track. You learn that if you instead express your frustration at said person, that it never, ever helps the situation, and only causes more hurt to be spread around to the very people you are ostensibly attempting to help. > Are you an adult or a child? Perhaps my comment was lost in translation, but this is rather the question that I was obliquely asking you. *wink right back* In any case I thank you for your help, which has in fact been quite great! My demo script is working, and I know now to properly advise my script writers regarding how to properly encode strings. From jeangawron at gmail.com Fri Aug 23 19:38:43 2013 From: jeangawron at gmail.com (jeangawron at gmail.com) Date: Fri, 23 Aug 2013 16:38:43 -0700 (PDT) Subject: Setting the value of True Message-ID: <9da851be-9ab3-4984-8f3e-71582f8ff4da@googlegroups.com> Python allows you set the value of True >>> True = 1.3 Now this is consistent with the decision to let you set the value of various builtin names. But why is this case different: >>> None = 1.3 File "", line 1 SyntaxError: cannot assign to None Mark Gawron From gary.herron at islandtraining.com Fri Aug 23 19:48:33 2013 From: gary.herron at islandtraining.com (Gary Herron) Date: Fri, 23 Aug 2013 16:48:33 -0700 Subject: Setting the value of True In-Reply-To: <9da851be-9ab3-4984-8f3e-71582f8ff4da@googlegroups.com> References: <9da851be-9ab3-4984-8f3e-71582f8ff4da@googlegroups.com> Message-ID: <5217F4D1.7020801@islandtraining.com> On 08/23/2013 04:38 PM, jeangawron at gmail.com wrote: > Python allows you set the value of True > >>>> True = 1.3 > Now this is consistent with the decision to let you set the > value of various builtin names. But why is this case different: > >>>> None = 1.3 > File "", line 1 > SyntaxError: cannot assign to None > > Mark Gawron Python3 fixes this inconsistency, by disallowing all such assignments. They all raise an exception: SyntaxError: assignment to keyword From tjreedy at udel.edu Fri Aug 23 19:56:28 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 23 Aug 2013 19:56:28 -0400 Subject: Setting the value of True In-Reply-To: <9da851be-9ab3-4984-8f3e-71582f8ff4da@googlegroups.com> References: <9da851be-9ab3-4984-8f3e-71582f8ff4da@googlegroups.com> Message-ID: On 8/23/2013 7:38 PM, jeangawron at gmail.com wrote: > Python allows you set the value of True Unqualified 'Python', used in the present tense, refers to the latest release or repository version. >>> True = 1.3 SyntaxError: assignment to keyword >>>> True = 1.3 > > Now this is consistent with the decision to let you set the > value of various builtin names. But why is this case different: > >>>> None = 1.3 > File "", line 1 > SyntaxError: cannot assign to None -- Terry Jan Reedy From steve+comp.lang.python at pearwood.info Fri Aug 23 21:14:20 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 24 Aug 2013 01:14:20 GMT Subject: Setting the value of True References: <9da851be-9ab3-4984-8f3e-71582f8ff4da@googlegroups.com> Message-ID: <521808ec$0$29986$c3e8da3$5496439d@news.astraweb.com> On Fri, 23 Aug 2013 16:38:43 -0700, jeangawron wrote: > Python allows you set the value of True > >>>> True = 1.3 Only in older versions of Python. This is for historical reasons: True and False were added as built-ins relatively late in Python's history (2.2, I think) and so there is still old code that legitimately does this at the start of a module: True = 1 False = not True Hence, making True and False actual keywords could only occur in Python 3, which was allowed to break backwards compatibility. None, on the other hand, has always existed in Python. The very oldest versions way back in the mists of time allowed you to rebind None: [steve at ando ~]$ python1.5 Python 1.5.2 (#1, Aug 27 2012, 09:09:18) [GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> None = 3 >>> None 3 but as that was considered a mistake, *and* it was considered unlikely to be any code in the wild that legitimately rebinds None, that was made a SyntaxError in Python 2.x. As for why None, True and False are treated differently than built-ins, if I remember the reason why, it is because they are considered fundamental to the inner workings of Python, unlike mere builtins like len, map, etc. and therefore should be protected. -- Steven From greg.ewing at canterbury.ac.nz Sat Aug 24 21:27:00 2013 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Sun, 25 Aug 2013 13:27:00 +1200 Subject: Setting the value of True In-Reply-To: <521808ec$0$29986$c3e8da3$5496439d@news.astraweb.com> References: <9da851be-9ab3-4984-8f3e-71582f8ff4da@googlegroups.com> <521808ec$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > As for why None, True and False are treated differently than built-ins, > if I remember the reason why, it is because they are considered > fundamental to the inner workings of Python, unlike mere builtins like > len, map, etc. and therefore should be protected. It's probably more to permit optimisation. Treating them as constants avoids a name lookup every time they're used. -- Greg From shankhabanerjee at gmail.com Fri Aug 23 22:10:51 2013 From: shankhabanerjee at gmail.com (shankha) Date: Sat, 24 Aug 2013 07:40:51 +0530 Subject: Conversion Issue in Converting code of 2.7 to 3 Message-ID: Hi, I am trying to run the following piece of code: https://greyhat.gatech.edu/wiki/index.php?title=Java_Bytecode_Tutorial/Getting_Started python Krakatau/assemble.py minimal.j. The scripts are written for 2.7. I want to convert them to 3.3. I am struck with the following error: []$ python Krakatau/assemble.py minimal.j Traceback (most recent call last): File "Krakatau/assemble.py", line 4, in from Krakatau.assembler import tokenize, parse, assembler File "c:\tmp\ByteCode\Krakatau\Krakatau\assembler\tokenize.py", line 3, in from ..classfile import ClassFile File "c:\tmp\ByteCode\Krakatau\Krakatau\classfile.py", line 1, in from . import constant_pool, method, field File "c:\tmp\ByteCode\Krakatau\Krakatau\constant_pool.py", line 10 def decodeStr((s,)): ^ SyntaxError: invalid syntax The code where this error originates from: def decodeStr((s,)): return s.replace('\xc0\x80','\0').decode('utf8'), I looked at http://docs.python.org/3/whatsnew/3.0.html but I couldn't figure out where I am going wrong? -- Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Fri Aug 23 22:45:19 2013 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 24 Aug 2013 03:45:19 +0100 Subject: Conversion Issue in Converting code of 2.7 to 3 In-Reply-To: References: Message-ID: <52181E3F.3010206@mrabarnett.plus.com> On 24/08/2013 03:10, shankha wrote: > Hi, > I am trying to run the following piece of code: > > https://greyhat.gatech.edu/wiki/index.php?title=Java_Bytecode_Tutorial/Getting_Started > > python Krakatau/assemble.py minimal.j. > > The scripts are written for 2.7. I want to convert them to 3.3. > > I am struck with the following error: > > > > []$ python Krakatau/assemble.py minimal.j > Traceback (most recent call last): > File "Krakatau/assemble.py", line 4, in > from Krakatau.assembler import tokenize, parse, assembler > > > File "c:\tmp\ByteCode\Krakatau\Krakatau\assembler\tokenize.py", line 3, in > from ..classfile import ClassFile > File "c:\tmp\ByteCode\Krakatau\Krakatau\classfile.py", line 1, in > > > from . import constant_pool, method, field > File "c:\tmp\ByteCode\Krakatau\Krakatau\constant_pool.py", line 10 > def decodeStr((s,)): > ^ > SyntaxError: invalid syntax > > > > > > The code where this error originates from: > > def decodeStr((s,)): > return s.replace('\xc0\x80','\0').decode('utf8'), > > > > I looked athttp://docs.python.org/3/whatsnew/3.0.html but I couldn't figure out where > > I am going wrong? > Look here: http://docs.python.org/3/whatsnew/3.0.html#changed-syntax at the "Removed Syntax" section where it mentions PEP 3113. A simple fix is: def decodeStr(arg): (s,) = arg return s.replace('\xc0\x80','\0').decode('utf8'), or: def decodeStr(s): return s[0].replace('\xc0\x80','\0').decode('utf8'), From steve+comp.lang.python at pearwood.info Fri Aug 23 23:30:47 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 24 Aug 2013 03:30:47 GMT Subject: Fast conversion of numbers to numerator/denominator pairs Message-ID: <521828e7$0$29986$c3e8da3$5496439d@news.astraweb.com> I have a need to convert arbitrary non-complex numbers into numerator/ denominator pairs. Numbers could be ints, floats, Fractions or Decimals. For example: 2 => (2, 1) 0.25 => (1, 4) Fraction(2, 3) => (2, 3) Decimal("0.5") => (1, 2) The first three cases are easy and fast: # ints and Fractions number.numerator, number.denominator # floats are a little slower number.as_integer_ratio() But Decimals are unfortunately slower. MUCH slower, about 40 times slower than Fractions in Python 3.3: tmp = Fraction.from_decimal(number) (tmp.numerator, tmp.denominator) This ends up being the bottleneck in my code: once you include the scaffolding code to select the right conversion method, processing a large list of Decimals is about fifty times slower than large lists of floats or fractions. Is there a fast way to convert a Decimal into a pair of numbers numerator/ denominator? It *must* be exact, but it doesn't have to be simplest form. For example, Decimal("0.5") => (5, 10) would be okay, although (1, 2) would be preferred. I've tried this function: def convert(d): sign, digits, exp = d.as_tuple() num = int(''.join([str(digit) for digit in digits])) if sign: num = -num return num, 10**-exp which is faster, but not fast enough. Any suggestions? -- Steven From ian.g.kelly at gmail.com Sat Aug 24 03:37:53 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sat, 24 Aug 2013 01:37:53 -0600 Subject: Fast conversion of numbers to numerator/denominator pairs In-Reply-To: <521828e7$0$29986$c3e8da3$5496439d@news.astraweb.com> References: <521828e7$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Aug 23, 2013 at 9:30 PM, Steven D'Aprano wrote: > Is there a fast way to convert a Decimal into a pair of numbers numerator/ > denominator? It *must* be exact, but it doesn't have to be simplest form. > For example, Decimal("0.5") => (5, 10) would be okay, although (1, 2) > would be preferred. > > > I've tried this function: > > def convert(d): > sign, digits, exp = d.as_tuple() > num = int(''.join([str(digit) for digit in digits])) > if sign: num = -num > return num, 10**-exp > > > which is faster, but not fast enough. Any suggestions? I time this function at about 33% faster than your version for a six-digit decimal, and almost 50% faster for a 12-digit decimal. My guess would be because it's not calling str() on every individual digit. def convert(d): exp = d.as_tuple().exponent num = int(d.scaleb(-exp)) return num, 10**-exp From ian.g.kelly at gmail.com Sat Aug 24 03:50:08 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sat, 24 Aug 2013 01:50:08 -0600 Subject: Fast conversion of numbers to numerator/denominator pairs In-Reply-To: References: <521828e7$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, Aug 24, 2013 at 1:37 AM, Ian Kelly wrote: > > I time this function at about 33% faster than your version for a > six-digit decimal, and almost 50% faster for a 12-digit decimal. My > guess would be because it's not calling str() on every individual > digit. > > def convert(d): > exp = d.as_tuple().exponent > num = int(d.scaleb(-exp)) > return num, 10**-exp Although, you would need to be careful with handling the decimal context for the scaleb operation to make sure the result is exact. From __peter__ at web.de Sat Aug 24 08:52:59 2013 From: __peter__ at web.de (Peter Otten) Date: Sat, 24 Aug 2013 14:52:59 +0200 Subject: Fast conversion of numbers to numerator/denominator pairs References: <521828e7$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > I have a need to convert arbitrary non-complex numbers into numerator/ > denominator pairs. Numbers could be ints, floats, Fractions or Decimals. > For example: > > 2 => (2, 1) > 0.25 => (1, 4) > Fraction(2, 3) => (2, 3) > Decimal("0.5") => (1, 2) > > > The first three cases are easy and fast: > > # ints and Fractions > number.numerator, number.denominator > > # floats are a little slower > number.as_integer_ratio() > > > But Decimals are unfortunately slower. MUCH slower, about 40 times slower > than Fractions in Python 3.3: > > tmp = Fraction.from_decimal(number) > (tmp.numerator, tmp.denominator) > > > This ends up being the bottleneck in my code: once you include the > scaffolding code to select the right conversion method, processing a > large list of Decimals is about fifty times slower than large lists of > floats or fractions. > > Is there a fast way to convert a Decimal into a pair of numbers numerator/ > denominator? It *must* be exact, but it doesn't have to be simplest form. > For example, Decimal("0.5") => (5, 10) would be okay, although (1, 2) > would be preferred. > > > I've tried this function: > > def convert(d): > sign, digits, exp = d.as_tuple() > num = int(''.join([str(digit) for digit in digits])) > if sign: num = -num > return num, 10**-exp > > > which is faster, but not fast enough. Any suggestions? Maybe these micro-optimisations will be sufficient: _trans = bytes.maketrans(bytes(range(10)), b"0123456789") def convert(d): sign, digits, exp = d.as_tuple() num = int(bytes(digits).translate(_trans)) if sign: num = -num return num, 10**-exp You can get the "simplest form" with co-prime numerator and denominator by dividing by fractions.gcd(), but that will of course slow down things. From timothy.c.delaney at gmail.com Sat Aug 24 17:59:15 2013 From: timothy.c.delaney at gmail.com (Tim Delaney) Date: Sun, 25 Aug 2013 07:59:15 +1000 Subject: Fast conversion of numbers to numerator/denominator pairs In-Reply-To: <521828e7$0$29986$c3e8da3$5496439d@news.astraweb.com> References: <521828e7$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 24 August 2013 13:30, Steven D'Aprano < steve+comp.lang.python at pearwood.info> wrote: > > def convert(d): > sign, digits, exp = d.as_tuple() > num = int(''.join([str(digit) for digit in digits])) > if sign: num = -num > return num, 10**-exp > > which is faster, but not fast enough. Any suggestions? > Straightforward multiply and add takes about 60% of the time for a single digit on my machine compared to the above, and 55% for 19 digits (so reasonably consistent). It's about 10x slower than fractions. def convert_muladd(d, _trans=_trans, bytes=bytes): sign, digits, exp = d.as_tuple() num = 0 for digit in digits: num *= 10 num += digit if sign: num = -num return num, 10**-exp Breakdown of the above (for 19 digits): d.as_tuple() takes about 35% of the time. The multiply and add takes about 55% of the time. The exponentiation takes about 10% of the time. Tim Delaney -------------- next part -------------- An HTML attachment was scrubbed... URL: From timothy.c.delaney at gmail.com Sat Aug 24 18:05:50 2013 From: timothy.c.delaney at gmail.com (Tim Delaney) Date: Sun, 25 Aug 2013 08:05:50 +1000 Subject: Fast conversion of numbers to numerator/denominator pairs In-Reply-To: References: <521828e7$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 25 August 2013 07:59, Tim Delaney wrote: > Breakdown of the above (for 19 digits): > > d.as_tuple() takes about 35% of the time. > > The multiply and add takes about 55% of the time. > > The exponentiation takes about 10% of the time. > Bah - sent before complete. Since the multiply and add takes such a significant proportion of the time, compiling the above with Cython should gain you a big win as well. Or find some other way to turn that loop into native code. Tim Delaney -------------- next part -------------- An HTML attachment was scrubbed... URL: From frank.ruiz at gmail.com Sat Aug 24 01:25:55 2013 From: frank.ruiz at gmail.com (snarf) Date: Fri, 23 Aug 2013 22:25:55 -0700 (PDT) Subject: Exception Handling Practices / Patterns Message-ID: <6446b59c-ff06-4c1b-8c54-79bcb1140be5@googlegroups.com> Greetings, As I tread through my journey of OO I am trying to determine if there is a good approach for exception handling within classes. >From my readings and gatherings - it seems I have found a common theme, but I am trying to solicit from the experts. Here is what I have found (I may be restating the obvious so please forgive me in advance): * Seems like exception handing within Classes is largely avoided and is typically only used when calling external libraries. * Try/Except type statements seem to be used more within modules, main functions, wrapper scripts. * Classes should be coded in a way that exceptions * Better to never write your own exceptions (unless you absolutely have to). * Using Exception is typically a bad. More specific the better. * Exceptions should never fail silently. (Should exceptions always be logged?) Best site I have found for exceptions (hopefully this helps someone): * http://c2.com/cgi/wiki?ExceptionPatterns I'd be interested in hearing others thoughts on this topic with regards to best practices for when to use exceptions, and when to avoid using exceptions. Thank you in advance! From davea at davea.name Sat Aug 24 03:18:59 2013 From: davea at davea.name (Dave Angel) Date: Sat, 24 Aug 2013 07:18:59 +0000 (UTC) Subject: Exception Handling Practices / Patterns References: <6446b59c-ff06-4c1b-8c54-79bcb1140be5@googlegroups.com> Message-ID: snarf wrote: > Greetings, > > As I tread through my journey of OO I am trying to determine if there is a good approach for exception handling within classes. > > From my readings and gatherings - it seems I have found a common theme, but I am trying to solicit from the experts. > > Here is what I have found (I may be restating the obvious so please forgive me in advance): > > * Seems like exception handing within Classes is largely avoided and is typically only used when calling external libraries. > * Try/Except type statements seem to be used more within modules, main functions, wrapper scripts. Exceptions are used when useful. I don't see any bias towards any one location. > * Classes should be coded in a way that exceptions You seem to be missing the last part of this sentence. > * Better to never write your own exceptions (unless you absolutely have to). If you mean to avoid writing exception classes, then I say nonsense. Just derive them from the closest meaningful exception class, so that a user can combine handlers when reasonable. > * Using Exception is typically a bad. More specific the better. If you mean in an except statement, then I'd agree. > * Exceptions should never fail silently. (Should exceptions always be logged?) Exceptions should be caught if you can handle them, or if you need to convert them to a different exception that someone further up the stack can handle. Sometimes handling means do nothing. > > Best site I have found for exceptions (hopefully this helps someone): > * http://c2.com/cgi/wiki?ExceptionPatterns But that's for Java. java is not C++, and neither is it Python. For one thing, Python exception overhead is deliberately much less, and they are used more freely. Notice that exceptions are used to terminate for loops, and that's a *normal* exit to the loop. They also appear in other places under the covers. Don't be afraid of them. -- DaveA From steve+comp.lang.python at pearwood.info Sat Aug 24 06:27:45 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 24 Aug 2013 10:27:45 GMT Subject: Exception Handling Practices / Patterns References: <6446b59c-ff06-4c1b-8c54-79bcb1140be5@googlegroups.com> Message-ID: <52188aa1$0$29986$c3e8da3$5496439d@news.astraweb.com> On Fri, 23 Aug 2013 22:25:55 -0700, snarf wrote: [...] > * Seems like exception handing within Classes is largely avoided and is > typically only used when calling external libraries. There is certainly no rule "avoid exceptions inside classes". Methods often raise exceptions to signal an error, e.g.: "My string".index("spam") Less common, methods can raise exceptions as part of their flow control. The most obvious example is StopIteration, used by iterators and often by __iter__ or next methods. > * Try/Except type > statements seem to be used more within modules, main functions, wrapper > scripts. It depends on whose code you are reading. I don't write a lot of classes, but when I do, I often use try...except inside them. If try...except gets used more frequently in module's top level, it is because the sort of things that you do at the top level often needs exception handling. For example, you might have a fallback module: try: import this_module except ImportError: import that_module as this_module You will very rarely see that inside a class, since you very rarely import modules inside a class. > * Classes should be coded in a way that exceptions I think you forgot to finish the sentence. > * Better to > never write your own exceptions (unless you absolutely have to). That depends. On the one hand, nobody wants a million different exception types. On the other hand, nobody wants just *one* exception type, and no way to distinguish between different kinds of errors. Somewhere between one and one million is an appropriate number of exception types. The right answer is to be conservative about creating new exceptions, but don't be scared to create one when you need one. But when you do, it is often better to subclass from an appropriate built- in exception like ValueError or TypeError or similar, than to subclass from Exception itself. > * Using > Exception is typically a bad. More specific the better. Yes, you should always try to catch the specific exceptions you care about: # Best except ValueError, OverflowError, ZeroDivisionError: # Not so good except Exception: # Worst except: Don't use the last one, except maybe in the interactive interpreter, since it will catch *everything*, even exceptions that probably shouldn't be caught like KeyboardInterrupt. > * Exceptions > should never fail silently. (Should exceptions always be logged?) Certainly not. Exceptions should fail silently if you don't care about them. For example, when connecting to a website, there are many temporary errors that can occur. The best way to handle them is to catch the exception, sleep for a little while, then try again. You need only care if repeated attempts to connect, with larger and larger sleeps, continue to fail. Of course, you might have a debug mode that logs all of these, but if your web browser logged every single time a webpage was slow to respond, you would soon run out of disk space :-) *Errors* should never fail silently, unless explicitly silenced. But an error isn't an error if you don't care about it, and an exception is not necessarily an error. This is an error, because converting a number to uppercase cannot possibly mean anything: mystring = 42 mystring.upper() This is not necessarily an error, since "the list is empty" could be a legitimate situation: mylist = [] first = mylist[0] In this case, it may be appropriate to catch the exception, and either silently swallow it, or do something else. > Best site I have found for exceptions (hopefully this helps someone): * > http://c2.com/cgi/wiki?ExceptionPatterns I haven't read that page for a long time, but as I recall the c2.com website, a lot of the ideas there are better suited to Java and C/C++ (and occasionally Lisp) rather than Python. But still, a valuable (if often confusing) resource. > I'd be interested in hearing others thoughts on this topic with regards > to best practices for when to use exceptions, and when to avoid using > exceptions. The try part of a try...except is *very* fast to set up. It's about as fast as a "pass" (do nothing), so it has little overhead. On the other hand, actually *catching* an exception is quite heavy. So code that catches lots and lots of exceptions may be slow. In that case, it may be faster to "look before you leap" and test ahead of time: # this is faster if most lists are not empty try: process(mylist[0]) except IndexError: handle_empty_list() # this is faster if many lists are empty if mylist: process(mylist[0]) else: handle_empty_list() Only profiling your data can tell you which you should use. On the other hand, here you should *always* use try...except: try: myfile = open("name") except IOError: handle_error() Because this code is wrong: if os.path.exists("name"): myfile = open("name") else: handle_error() It's wrong for a couple of reasons: - just because the file exists, doesn't mean you can open it; - even if the file exists when you call the os.path.exists function, doesn't mean it will still exist a millisecond later when you try to open it. Hope this is helpful, -- Steven From python at mrabarnett.plus.com Sat Aug 24 12:32:39 2013 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 24 Aug 2013 17:32:39 +0100 Subject: Exception Handling Practices / Patterns In-Reply-To: <52188aa1$0$29986$c3e8da3$5496439d@news.astraweb.com> References: <6446b59c-ff06-4c1b-8c54-79bcb1140be5@googlegroups.com> <52188aa1$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5218E027.7070703@mrabarnett.plus.com> On 24/08/2013 11:27, Steven D'Aprano wrote: > On Fri, 23 Aug 2013 22:25:55 -0700, snarf wrote: [snip] >> * Using >> Exception is typically a bad. More specific the better. > > Yes, you should always try to catch the specific exceptions you care > about: > > > # Best > except ValueError, OverflowError, ZeroDivisionError: > That should be: except (ValueError, OverflowError, ZeroDivisionError): > > # Not so good > except Exception: > > > # Worst > except: > [snip] From tjreedy at udel.edu Sat Aug 24 15:57:55 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 24 Aug 2013 15:57:55 -0400 Subject: Exception Handling Practices / Patterns In-Reply-To: <52188aa1$0$29986$c3e8da3$5496439d@news.astraweb.com> References: <6446b59c-ff06-4c1b-8c54-79bcb1140be5@googlegroups.com> <52188aa1$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 8/24/2013 6:27 AM, Steven D'Aprano wrote: > On Fri, 23 Aug 2013 22:25:55 -0700, snarf wrote: > > [...] >> * Seems like exception handing within Classes is largely avoided and is >> typically only used when calling external libraries. > > There is certainly no rule "avoid exceptions inside classes". Methods > often raise exceptions to signal an error, e.g.: > > "My string".index("spam") The rule only makes sense if it referring to try: except: in top-level class code, outside of def statements. Even then, it is wrong. ... > # Worst > except: > > Don't use the last one, except maybe in the interactive interpreter, Stick with never. "except:" means the same thing as "except BaseException:", except that the latter indicates a deliberate choice rather than an indication of carelessness or laziness. A bare except: is a disservice to the next maintainer of the code. > since it will catch *everything*, even exceptions that probably shouldn't > be caught like KeyboardInterrupt. In Idle, when you type 'expression(' and hesitate, Idle tries to evaluate 'expression' to a function, behind the scenes, in order to provide a calltip with the function signature. Any error in 'eval(expression)' should be caught and ignored so the user can continue typing. This is one place where the original authors were too specific. They only caught NameError and AttributeError, when those were not the only possible eval errors in practice. The result was that people would occasionally type '(' and see idle quit. idlelib .py files have about 20 bare 'except:'s, which I will try to fill out when I have reviewed the try part and understand what should be caught. It would be easier for me to read the code if the original authors had added their best guess as to what should be expected and caught. -- Terry Jan Reedy From steve+comp.lang.python at pearwood.info Sat Aug 24 19:11:52 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 24 Aug 2013 23:11:52 GMT Subject: Exception Handling Practices / Patterns References: <6446b59c-ff06-4c1b-8c54-79bcb1140be5@googlegroups.com> <52188aa1$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: <52193db8$0$29986$c3e8da3$5496439d@news.astraweb.com> On Sat, 24 Aug 2013 15:57:55 -0400, Terry Reedy wrote: >> # Worst >> except: >> >> Don't use the last one, except maybe in the interactive interpreter, > > Stick with never. "except:" means the same thing as "except > BaseException:", except that the latter indicates a deliberate choice > rather than an indication of carelessness or laziness. > > A bare except: is a disservice to the next maintainer of the code. Do you know anyone who maintains code typed in the interactive interpreter? :-) -- Steven From frank.ruiz at gmail.com Sat Aug 24 19:32:53 2013 From: frank.ruiz at gmail.com (frank.ruiz at gmail.com) Date: Sat, 24 Aug 2013 16:32:53 -0700 (PDT) Subject: Exception Handling Practices / Patterns In-Reply-To: <52188aa1$0$29986$c3e8da3$5496439d@news.astraweb.com> References: <6446b59c-ff06-4c1b-8c54-79bcb1140be5@googlegroups.com> <52188aa1$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: <314d5447-3002-4c10-a302-303fa8d3c36a@googlegroups.com> Hi Steven, Yea this is great. Thanks for the feedback. On Saturday, August 24, 2013 3:27:45 AM UTC-7, Steven D'Aprano wrote: > On Fri, 23 Aug 2013 22:25:55 -0700, snarf wrote: > > > > [...] > > > * Seems like exception handing within Classes is largely avoided and is > > > typically only used when calling external libraries. > > > > There is certainly no rule "avoid exceptions inside classes". Methods > > often raise exceptions to signal an error, e.g.: > > > > "My string".index("spam") > > > > > > Less common, methods can raise exceptions as part of their flow control. > > The most obvious example is StopIteration, used by iterators and often by > > __iter__ or next methods. > > > > > > > * Try/Except type > > > statements seem to be used more within modules, main functions, wrapper > > > scripts. > > > > It depends on whose code you are reading. I don't write a lot of classes, > > but when I do, I often use try...except inside them. > > > > If try...except gets used more frequently in module's top level, it is > > because the sort of things that you do at the top level often needs > > exception handling. For example, you might have a fallback module: > > > > try: > > import this_module > > except ImportError: > > import that_module as this_module > > > > > > You will very rarely see that inside a class, since you very rarely > > import modules inside a class. > > > > > > > * Classes should be coded in a way that exceptions > > > > I think you forgot to finish the sentence. > > > > > > > * Better to > > > never write your own exceptions (unless you absolutely have to). > > > > That depends. > > > > On the one hand, nobody wants a million different exception types. On the > > other hand, nobody wants just *one* exception type, and no way to > > distinguish between different kinds of errors. Somewhere between one and > > one million is an appropriate number of exception types. > > > > The right answer is to be conservative about creating new exceptions, but > > don't be scared to create one when you need one. > > > > But when you do, it is often better to subclass from an appropriate built- > > in exception like ValueError or TypeError or similar, than to subclass > > from Exception itself. > > > > > > > * Using > > > Exception is typically a bad. More specific the better. > > > > Yes, you should always try to catch the specific exceptions you care > > about: > > > > > > # Best > > except ValueError, OverflowError, ZeroDivisionError: > > > > > > # Not so good > > except Exception: > > > > > > # Worst > > except: > > > > > > Don't use the last one, except maybe in the interactive interpreter, > > since it will catch *everything*, even exceptions that probably shouldn't > > be caught like KeyboardInterrupt. > > > > > > > * Exceptions > > > should never fail silently. (Should exceptions always be logged?) > > > > Certainly not. Exceptions should fail silently if you don't care about > > them. For example, when connecting to a website, there are many temporary > > errors that can occur. The best way to handle them is to catch the > > exception, sleep for a little while, then try again. You need only care > > if repeated attempts to connect, with larger and larger sleeps, continue > > to fail. > > > > Of course, you might have a debug mode that logs all of these, but if > > your web browser logged every single time a webpage was slow to respond, > > you would soon run out of disk space :-) > > > > *Errors* should never fail silently, unless explicitly silenced. But an > > error isn't an error if you don't care about it, and an exception is not > > necessarily an error. > > > > This is an error, because converting a number to uppercase cannot > > possibly mean anything: > > > > mystring = 42 > > mystring.upper() > > > > > > This is not necessarily an error, since "the list is empty" could be a > > legitimate situation: > > > > mylist = [] > > first = mylist[0] > > > > In this case, it may be appropriate to catch the exception, and either > > silently swallow it, or do something else. > > > > > > > Best site I have found for exceptions (hopefully this helps someone): * > > > http://c2.com/cgi/wiki?ExceptionPatterns > > > > I haven't read that page for a long time, but as I recall the c2.com > > website, a lot of the ideas there are better suited to Java and C/C++ > > (and occasionally Lisp) rather than Python. But still, a valuable (if > > often confusing) resource. > > > > > > > > > I'd be interested in hearing others thoughts on this topic with regards > > > to best practices for when to use exceptions, and when to avoid using > > > exceptions. > > > > The try part of a try...except is *very* fast to set up. It's about as > > fast as a "pass" (do nothing), so it has little overhead. > > > > On the other hand, actually *catching* an exception is quite heavy. So > > code that catches lots and lots of exceptions may be slow. In that case, > > it may be faster to "look before you leap" and test ahead of time: > > > > # this is faster if most lists are not empty > > try: > > process(mylist[0]) > > except IndexError: > > handle_empty_list() > > > > # this is faster if many lists are empty > > if mylist: > > process(mylist[0]) > > else: > > handle_empty_list() > > > > > > Only profiling your data can tell you which you should use. > > > > > > On the other hand, here you should *always* use try...except: > > > > try: > > myfile = open("name") > > except IOError: > > handle_error() > > > > > > Because this code is wrong: > > > > if os.path.exists("name"): > > myfile = open("name") > > else: > > handle_error() > > > > > > It's wrong for a couple of reasons: > > > > - just because the file exists, doesn't mean you can open it; > > > > - even if the file exists when you call the os.path.exists function, > > doesn't mean it will still exist a millisecond later when you try to open > > it. > > > > > > > > Hope this is helpful, > > > > > > > > > > -- > > Steven From frank.ruiz at gmail.com Sat Aug 24 19:30:39 2013 From: frank.ruiz at gmail.com (frank.ruiz at gmail.com) Date: Sat, 24 Aug 2013 16:30:39 -0700 (PDT) Subject: Exception Handling Practices / Patterns In-Reply-To: References: <6446b59c-ff06-4c1b-8c54-79bcb1140be5@googlegroups.com> Message-ID: <3be95a3a-0005-45fa-bf65-a83da968cd05@googlegroups.com> Appreciate the feedback. I was hoping to get as much perspective as possible. On Saturday, August 24, 2013 12:18:59 AM UTC-7, Dave Angel wrote: > snarf wrote: > > > > > Greetings, > > > > > > As I tread through my journey of OO I am trying to determine if there is a good approach for exception handling within classes. > > > > > > From my readings and gatherings - it seems I have found a common theme, but I am trying to solicit from the experts. > > > > > > Here is what I have found (I may be restating the obvious so please forgive me in advance): > > > > > > * Seems like exception handing within Classes is largely avoided and is typically only used when calling external libraries. > > > * Try/Except type statements seem to be used more within modules, main functions, wrapper scripts. > > > > Exceptions are used when useful. I don't see any bias towards any one > > location. > > > > > * Classes should be coded in a way that exceptions > > > > You seem to be missing the last part of this sentence. > > > > > * Better to never write your own exceptions (unless you absolutely have to). > > > > If you mean to avoid writing exception classes, then I say nonsense. > > Just derive them from the closest meaningful exception class, so that a > > user can combine handlers when reasonable. > > > > > * Using Exception is typically a bad. More specific the better. > > > > If you mean in an except statement, then I'd agree. > > > > > * Exceptions should never fail silently. (Should exceptions always be logged?) > > > > Exceptions should be caught if you can handle them, or if you need to > > convert them to a different exception that someone further up the stack > > can handle. Sometimes handling means do nothing. > > > > > > > > Best site I have found for exceptions (hopefully this helps someone): > > > * http://c2.com/cgi/wiki?ExceptionPatterns > > > > But that's for Java. java is not C++, and neither is it Python. For > > one thing, Python exception overhead is deliberately much less, and they > > are used more freely. > > > > Notice that exceptions are used to terminate for loops, and that's a > > *normal* exit to the loop. They also appear in other places under the > > covers. Don't be afraid of them. > > > > -- > > DaveA From malhar.v.ntech at gmail.com Sat Aug 24 06:45:01 2013 From: malhar.v.ntech at gmail.com (malhar vora) Date: Sat, 24 Aug 2013 03:45:01 -0700 (PDT) Subject: Help regarding urllib Message-ID: <31b7c124-d5f3-485d-a838-a083773c6c31@googlegroups.com> Hello All, I am simply fetching data from robots.txt of a url. Below is my code. siteurl = siteurl.rstrip("/") From malhar.v.ntech at gmail.com Sat Aug 24 06:49:47 2013 From: malhar.v.ntech at gmail.com (malhar vora) Date: Sat, 24 Aug 2013 03:49:47 -0700 (PDT) Subject: Help regarding urllib In-Reply-To: <31b7c124-d5f3-485d-a838-a083773c6c31@googlegroups.com> References: <31b7c124-d5f3-485d-a838-a083773c6c31@googlegroups.com> Message-ID: On Saturday, August 24, 2013 4:15:01 PM UTC+5:30, malhar vora wrote: > Hello All, > > > > > > I am simply fetching data from robots.txt of a url. Below is my code. > > > > siteurl = siteurl.rstrip("/") Sorry for last complete. It was sent by mistake. Here is my code. siteurl = siteurl.rstrip("/") roboturl = siteurl + r'/robots.txt' robotdata = urllib.urlopen(roboturl).read() # Reading robots.txt of given url print robotdata In above code siteurl is fetched simply from local text file. Whenever I run above code. In place of "/" before robots.txt, it writes "\\" in url as I found in error. The error is given below. This is main function Main URL : www.bestrecipes.com.au $$$$$$$$$$:www.bestrecipes.com.au ###########-->www.bestrecipes.com.au/robots.txt Traceback (most recent call last): File "dataintegrator.py", line 104, in main() File "dataintegrator.py", line 81, in main print "Sitemap Url : " + getSiteMapUrl(i) File "D:\Malhar Data\Projects\Data Parsing\My Code\Final Part\libs\datareader. py", line 50, in getSiteMapUrl robotdata = urllib.urlopen(roboturl).read() # Reading robots.txt of given ur l File "C:\Python26\lib\urllib.py", line 87, in urlopen return opener.open(url) File "C:\Python26\lib\urllib.py", line 203, in open return getattr(self, name)(url) File "C:\Python26\lib\urllib.py", line 461, in open_file return self.open_local_file(url) File "C:\Python26\lib\urllib.py", line 475, in open_local_file raise IOError(e.errno, e.strerror, e.filename) IOError: [Errno 2] The system cannot find the path specified: 'www.bestrecipes.c om.au\\robots.txt' I am new to Python and not able to figure out this problem. Please help me. Thank you, Malhar Vora From davea at davea.name Sat Aug 24 09:07:57 2013 From: davea at davea.name (Dave Angel) Date: Sat, 24 Aug 2013 13:07:57 +0000 (UTC) Subject: Help regarding urllib References: <31b7c124-d5f3-485d-a838-a083773c6c31@googlegroups.com> Message-ID: malhar vora wrote: > On Saturday, August 24, 2013 4:15:01 PM UTC+5:30, malhar vora wrote: >> Hello All, >> >> >> >> >> >> I am simply fetching data from robots.txt of a url. Below is my code. >> >> >> >> siteurl = siteurl.rstrip("/") > > Sorry for last complete. It was sent by mistake. > > Here is my code. > > siteurl = siteurl.rstrip("/") > roboturl = siteurl + r'/robots.txt' > robotdata = urllib.urlopen(roboturl).read() # Reading robots.txt of given url > print robotdata > > In above code siteurl is fetched simply from local text file. Why aren't you showing us what is in that local text file? Or more specifically what siteurl turns out to be? I suspect it's missing the http:// prefix > IOError: [Errno 2] The system cannot find the path specified: 'www.bestrecipes.c > om.au\\robots.txt' > Looks to me like it decided this url referred to a file. That's the default behavior when you don't specify the scheme identifier (eg. 'http") Also it might well have been necessary to specify what Python version and OS you're running this on. For example, the single backslash character is specific to Windows. (The doubling presumably is an artifact of how the error message is displayed, eg. look at how repr() displays strings) -- DaveA From hujia07 at gmail.com Sat Aug 24 12:23:37 2013 From: hujia07 at gmail.com (Jia Hu) Date: Sat, 24 Aug 2013 12:23:37 -0400 Subject: how to read mixed from multiple csv file Message-ID: Hi, My 20 csv files has string header, and first two columns are string (e.g., 1999-01-02, 01:00:00) among the 50 columns. Other columns store numerical values (int, or float) I need to do data analysis for these data. For example, extract the each month data from each of the cvs files (each csv file stores 1 year data) and there are 20 year data. in addition, I want to store the data in disk so that I can retrieve data quickly, just like save and load in Matlab. Currently, I use structured array data = [] i = 0 for s in range(1991, 2011): fileName = folder +_{_sY}0101_{_sY}1231_725300.csv".format(_sY=s) data.append(np.genfromtxt(fileName, delimiter=",", dtype=None, names=True)) i += 1 np.save("alldata", data) ==== However, when I load data "np.load("alldata.npy")", it is becomes 0-d array which is different from original one. My question is that (1) How to store or save the data? (2) as you can see, I use list to store all the 20 ndarrays, I do not feel it is a good way. Is there any suggestion for the data structure I should use? Thanks in advance. -------------- next part -------------- An HTML attachment was scrubbed... URL: From projectfack at gmail.com Sat Aug 24 17:41:00 2013 From: projectfack at gmail.com (projectfack at gmail.com) Date: Sat, 24 Aug 2013 14:41:00 -0700 (PDT) Subject: Help in Nltk Message-ID: Can anyone help me for the tasks below in nltk 1. The system must demonstrate false positive and false negative examples using any stemmer (Task 1.1) 2. The system must demonstrate the differences between successive layers of the Porter stemmer through a couple of examples. (Task 1.2). 3. The system must be able to automatically detect whether a text is in French or English and stem it in the detected language. The stemmer for French will only be removing plurals. (Tasks 2 and 3). 4. The system must demonstrate the use of the Brill Tagger (Task 4.2). 5. The system must demonstrate the use of POS tags for information extraction to extract all types of fish and all types of trees mentioned on the Wikipedia pages for Fish and Tree (Task 6) 6. The system must demonstrate the use of WordNet to expand nouns with their synonyms and their hypernyms (Task 9) From joel.goldstick at gmail.com Sun Aug 25 10:59:08 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Sun, 25 Aug 2013 10:59:08 -0400 Subject: Help in Nltk In-Reply-To: References: Message-ID: On Sat, Aug 24, 2013 at 5:41 PM, wrote: > > Can anyone help me for the tasks below in nltk > > > 1. The system must demonstrate false positive and false negative examples > using any stemmer (Task 1.1) > 2. The system must demonstrate the differences between successive layers of > the Porter stemmer through a couple of examples. (Task 1.2). > 3. The system must be able to automatically detect whether a text is in French > or English and stem it in the detected language. The stemmer for French > will only be removing plurals. (Tasks 2 and 3). > 4. The system must demonstrate the use of the Brill Tagger (Task 4.2). > 5. The system must demonstrate the use of POS tags for information > extraction to extract all types of fish and all types of trees mentioned on the > Wikipedia pages for Fish and Tree (Task 6) > 6. The system must demonstrate the use of WordNet to expand nouns with > their synonyms and their hypernyms (Task 9) > -- > http://mail.python.org/mailman/listinfo/python-list I'm guessing that you copied this list from a homework assignment. It refers to 'the system', but what system is that? I see that you posted the same on stack exchange and somewhere else. I suggest you try to give more information on what you are up to, what your skill level is with python and nltk. Also, people here generally don't do people's homework. You might also google nltk mailing lists to see if you can find people with specific knowledge of nltk. -- Joel Goldstick http://joelgoldstick.com From projectfack at gmail.com Sun Aug 25 23:32:51 2013 From: projectfack at gmail.com (projectfack at gmail.com) Date: Sun, 25 Aug 2013 20:32:51 -0700 (PDT) Subject: Help in Nltk In-Reply-To: References: Message-ID: <1cbf09bf-211e-46c9-979e-af590e927e89@googlegroups.com> im a beginner so im trying to code using python ,Here system is just the program ,as the program can be done in any language i chose to do in nltk lib in python From vasudevram at gmail.com Sat Aug 24 17:48:39 2013 From: vasudevram at gmail.com (vasudevram) Date: Sat, 24 Aug 2013 14:48:39 -0700 (PDT) Subject: New way of writing socket servers in #Linux kernel 3.9 (and in #Python too) Message-ID: This may be of interest to readers of this newsgroup: Original article: http://lnkd.in/taAFNt Content (without links): A new way of writing socket servers has been introduced with the Linux kernel 3.9. It involves the ability to bind multiple listening sockets to the same port on the same host, by unrelated processes or threads. The socket option is called SO_REUSEPORT. Article about SO_REUSEPORT. The above article includes a demo of how to use the feature in Python, using the example of a simple echo server and netcat. Another article about SO_REUSEPORT on LWN.net by Michael Kerrisk. Hacker News thread about SO_REUSEPORT. About Michael Kerrisk. He has worked at DEC (Digital Equipment Corporation) and Google in the past. Coincidentally, I happened to have come across Michael Kerrisk's Unix and Linux work some months earlier and had emailed him with a few questions about it, which he was kind enough to answer. Kerrisk is the author of the book The Linux Programming Interface, which seems like an interesting and useful book. >From the web site for the The Linux Programming Interface book: [ The Linux Programming Interface (published in October 2010, No Starch Press, ISBN 978-1-59327-220-3) is a detailed guide and reference for Linux and UNIX system programming. With 1552 pages, 115 diagrams, 88 tables, nearly 200 example programs, and over 200 exercises, TLPI is the most comprehensive description of Linux and UNIX system programming available. The author, Michael Kerrisk, is the maintainer of the Linux man-pages project, which documents the Linux kernel and glibc APIs. He has long been active in the documentation, testing, and design review of Linux kernel-userspace interfaces. ] And if you want to write command-line programs in Linux using C (an area closely related to the topic of the TLPI book), you may wish to check out my article on the subject, written for IBM developerWorks: Developing a Linux command-line utility. I have not yet tried out the SO_REUSEPORT option, because I need to get Linux kernel 3.9 first, but it seems like a useful technique for increasing performance of socket servers. Note that there are various other issues involved, so you may not get increased performance just by using this option in your code. As always with performance tuning, you have to profile your code, identify hotspots, and then only work on improving certain parts of it that seem to be the bottlenecks. And in this case, even before all that, you may need to evaluate whether this socket option is relevant to your application at all. So, caveat lector :-) - Vasudev Ram - Dancing Bison Enterprises From torriem at gmail.com Sat Aug 24 22:08:06 2013 From: torriem at gmail.com (Michael Torrie) Date: Sat, 24 Aug 2013 20:08:06 -0600 Subject: New way of writing socket servers in #Linux kernel 3.9 (and in #Python too) In-Reply-To: References: Message-ID: <52196706.2090306@gmail.com> #Linux, #Python? This this hash tag stuff is getting out of hand, don't you think? From benjamin.kaplan at case.edu Sun Aug 25 00:06:02 2013 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Sat, 24 Aug 2013 21:06:02 -0700 Subject: New way of writing socket servers in #Linux kernel 3.9 (and in #Python too) In-Reply-To: <52196706.2090306@gmail.com> References: <52196706.2090306@gmail.com> Message-ID: On Sat, Aug 24, 2013 at 7:08 PM, Michael Torrie wrote: > #Linux, #Python? This this hash tag stuff is getting out of hand, don't > you think? Didn't you hear? In an effort to redefine itself for the modern Internet, Usenet is adding support for hash tags and limiting posts to 140 characters because kids these days just don't want to read anything longer. From torriem at gmail.com Sun Aug 25 00:27:46 2013 From: torriem at gmail.com (Michael Torrie) Date: Sat, 24 Aug 2013 22:27:46 -0600 Subject: New way of writing socket servers in #Linux kernel 3.9 (and in #Python too) In-Reply-To: References: <52196706.2090306@gmail.com> Message-ID: <521987C2.3040407@gmail.com> On 08/24/2013 10:06 PM, Benjamin Kaplan wrote: > On Sat, Aug 24, 2013 at 7:08 PM, Michael Torrie wrote: >> #Linux, #Python? This this hash tag stuff is getting out of hand, don't >> you think? > > Didn't you hear? In an effort to redefine itself for the modern > Internet, Usenet is adding support for hash tags and limiting posts to > 140 characters because kids these days just don't want to read > anything longer. Sweet. And here I thought it was irc channels. From i.am.songoku at gmail.com Sat Aug 24 23:52:27 2013 From: i.am.songoku at gmail.com (Krishnan Shankar) Date: Sun, 25 Aug 2013 09:22:27 +0530 Subject: List getting extended when assigned to itself Message-ID: Hi Python Friends, I came across an example which is as below, >>> var = [1, 12, 123, 1234] >>> var [1, 12, 123, 1234] >>> var[:0] [] >>> var[:0] = var >>> var [1, 12, 123, 1234, 1, 12, 123, 1234] >>> Here in var[:0] = var we are assigning an entire list to the beginning of itself. So shouldn't it be something like, [[1, 12, 123, 1234], 1, 12, 123, 1234] It happens when we do the below, >>> var = [1, 12, 123, 1234] >>> var[0] = var >>> var [[...], 12, 123, 1234] >>> Literally var[0] = var and var[:0] = var almost meens the same. But why is the difference in output? Can anyone explain what happens when slicing assignment and direct assignment. Regards, Krishnan -------------- next part -------------- An HTML attachment was scrubbed... URL: From benjamin.kaplan at case.edu Sun Aug 25 00:03:22 2013 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Sat, 24 Aug 2013 21:03:22 -0700 Subject: List getting extended when assigned to itself In-Reply-To: References: Message-ID: On Sat, Aug 24, 2013 at 8:52 PM, Krishnan Shankar wrote: > Hi Python Friends, > > I came across an example which is as below, > >>>> var = [1, 12, 123, 1234] >>>> var > [1, 12, 123, 1234] >>>> var[:0] > [] >>>> var[:0] = var >>>> var > [1, 12, 123, 1234, 1, 12, 123, 1234] >>>> > > Here in var[:0] = var we are assigning an entire list to the beginning of > itself. So shouldn't it be something like, > > [[1, 12, 123, 1234], 1, 12, 123, 1234] > > It happens when we do the below, > >>>> var = [1, 12, 123, 1234] >>>> var[0] = var >>>> var > [[...], 12, 123, 1234] >>>> > > Literally var[0] = var and var[:0] = var almost meens the same. But why is > the difference in output? Can anyone explain what happens when slicing > assignment and direct assignment. Are you sure they're almost the same? >>> var[0] 1 >>> var[:0] [] One's a list and one is an integer. It's hard for them to be any more different. var[0] means that you should be setting an element of the list. var[:0] says you want to update a piece of the list, not an element inside it. From steve+comp.lang.python at pearwood.info Sun Aug 25 01:31:48 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Aug 2013 05:31:48 GMT Subject: List getting extended when assigned to itself References: Message-ID: <521996c3$0$29986$c3e8da3$5496439d@news.astraweb.com> On Sun, 25 Aug 2013 09:22:27 +0530, Krishnan Shankar wrote: > Hi Python Friends, > > I came across an example which is as below, > >>>> var = [1, 12, 123, 1234] >>>> var > [1, 12, 123, 1234] >>>> var[:0] > [] >>>> var[:0] = var >>>> var > [1, 12, 123, 1234, 1, 12, 123, 1234] >>>> >>>> > Here in var[:0] = var we are assigning an entire list to the beginning > of itself. So shouldn't it be something like, > > [[1, 12, 123, 1234], 1, 12, 123, 1234] No, you have misunderstood how slicing works. When you assign to a slice, you *replace* the existing slice with items from the other list: py> L = [1, 2, 3, 4, 5] py> L[2:4] = [None, None, None] py> L [1, 2, None, None, None, 5] Notice that it does not insert the list [None, None, None] as a single item. If the slice you replace is empty, this is equivalent to inserting the items: py> L = [1, 2, 3, 4, 5] py> L[2:2] = [None, None, None] py> L [1, 2, None, None, None, 3, 4, 5] The beginning of the list is just an empty slice: L[:0] means "all the items, starting at the beginning of the list, and finishing *before* index zero". So that makes it an empty slice, and items are inserted after the start of the list but before index zero: py> L = [1, 2, 3, 4, 5] py> L[:0] = [None, None, None] py> L [None, None, None, 1, 2, 3, 4, 5] So assigning to a slice *always* extends. If you try to assign a single value, not inside a list or other iterable, it fails: py> L = [1, 2, 3, 4, 5] py> L[:0] = None Traceback (most recent call last): File "", line 1, in TypeError: can only assign an iterable Remember that indexes in Python are *before* the item, so when slicing, Python cuts at the vertical lines | as shown below: | a | b | c | d | e | f | g | The vertical lines are numbered 0 through 7, and the slice [2:5] cuts as shown: | a | b | c | d | e | f | g | 0---1---2---3---4---5---6---7 [2:5] => | c | d | e | > It happens when we do the below, > >>>> var = [1, 12, 123, 1234] >>>> var[0] = var >>>> var > [[...], 12, 123, 1234] Here you are *not* assigning to a slice, but setting a single item. The item at position 0 is replaced with the contents of var, which happens to be the same list, but that is just a distraction. It is more clear when you use a different value: py> L = [1, 2, 3, 4, 5] py> L[0] = "something" py> L ['something', 2, 3, 4, 5] > Literally var[0] = var and var[:0] = var almost meens the same. No, they are very different. Even though the difference is a single character, var[:index] and var[index] are very different, no matter what the value of index. The first is a slice ending at index, the second is a single item at index. The same naturally applies to var[index:] as well, which is a slice starting at index. If you wish to insert a sequence as a single object, you have two choices: you can use the list insert() method, or you can wrap the sequence in a list, and then use slice assignment: py> L = [1, 2, 3, 4, 5] py> L[2:4] = [L] py> L [1, 2, [...], 5] -- Steven From sahil301290 at gmail.com Sun Aug 25 01:50:24 2013 From: sahil301290 at gmail.com (sahil301290 at gmail.com) Date: Sat, 24 Aug 2013 22:50:24 -0700 (PDT) Subject: Checking homogeneity of Array using List in Python Message-ID: <39cf54bd-3772-448a-a56e-74e0b3cc8017@googlegroups.com> I am unable to check homogeneity of Array. I have take Array type Int to be default for my code. Instead of getting Error on NON-INT Values. I want to take input as string. Then check if all input is in (0-9) form, I typecast it into int and Accept. Else, I would like to skip that input. eg. my input is ['1', ' ', 'asdasd231231', '1213asasd', '43242'] I want it to be interpreted as: [1, [None], [None], [None], 43242] NOTE: NO INBUILT FUNCTION BE USED. Thank you in advance. From rosuav at gmail.com Sun Aug 25 02:03:21 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 25 Aug 2013 16:03:21 +1000 Subject: Checking homogeneity of Array using List in Python In-Reply-To: <39cf54bd-3772-448a-a56e-74e0b3cc8017@googlegroups.com> References: <39cf54bd-3772-448a-a56e-74e0b3cc8017@googlegroups.com> Message-ID: On Sun, Aug 25, 2013 at 3:50 PM, wrote: > NOTE: NO INBUILT FUNCTION BE USED. Thank you in advance. You'll have to do your own homework, then. Python strongly favours the use of inbuilt functions. ChrisA From joel.goldstick at gmail.com Sun Aug 25 11:54:25 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Sun, 25 Aug 2013 11:54:25 -0400 Subject: Checking homogeneity of Array using List in Python In-Reply-To: References: <39cf54bd-3772-448a-a56e-74e0b3cc8017@googlegroups.com> Message-ID: On Sun, Aug 25, 2013 at 2:03 AM, Chris Angelico wrote: > On Sun, Aug 25, 2013 at 3:50 PM, wrote: >> NOTE: NO INBUILT FUNCTION BE USED. Thank you in advance. You don't make it easy to help you with your homework by leaving out information about what you have learned so far, and perhaps what you have tried that doesn't work Here are some hints: Do you know what the any and all functions do in python? Do you know about list comprehensions or how to write a for loop that iterates over all of the values in a list? If you know about that stuff, think how those skills could be used to solve your problem. This problem is so completely contrived that without knowing the course of your study (what you have learned about python programming so far) it is really useless to try to guess an acceptable answer. Let me suggest you discuss this with your classmates if possible. -- Joel Goldstick http://joelgoldstick.com From davea at davea.name Sun Aug 25 16:49:57 2013 From: davea at davea.name (Dave Angel) Date: Sun, 25 Aug 2013 20:49:57 +0000 (UTC) Subject: Checking homogeneity of Array using List in Python References: <39cf54bd-3772-448a-a56e-74e0b3cc8017@googlegroups.com> Message-ID: sahil301290 at gmail.com wrote: > I am unable to check homogeneity of Array. > I have take Array type Int to be default for my code. > > Instead of getting Error on NON-INT Values. But none of them below are int values. > I want to take input as string. > Then check if all input is in (0-9) form, I typecast it into int and Accept. > Else, I would like to skip that input. > > eg. my input is ['1', ' ', 'asdasd231231', '1213asasd', '43242'] > I want it to be interpreted as: > [1, [None], [None], [None], 43242] > > NOTE: NO INBUILT FUNCTION BE USED. Thank you in advance. I don't see any Arrays. You show a list, however. Lists don't have any type for the whole list, only for each element. Seems to me like the minimum functions you'll need are int(), type(), isinstance(). They're built-in, not inbuilt, so perhaps that's okay. Or perhaps you're going to write your own equivalents. But your example contradicts your description enough that I could be wrong about any of the three. > > I want to take input as string. So why do you show it as a list? Do you know how to write a function? Try writing one that takes a string and returns True if all the characters are digits, and False if either: 1) there are no characters 2) there are some non-digit characters Try writing another one that takes a string made up entirely of digit characters, and produces an int from them. Note you'll be doing some multiplies by 10, since presumably you're working in decimal. You could call this function my_int() Write some code that actually illustrates what you're trying to do, show some data being fed into it, and indicate where you ran out of ideas. Then somebody could perhaps help. As it stands, you're referring to types that don't fit, and making assumptions that make no sense. -- DaveA From neilc at norwich.edu Mon Aug 26 09:49:01 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 26 Aug 2013 13:49:01 GMT Subject: Checking homogeneity of Array using List in Python References: <39cf54bd-3772-448a-a56e-74e0b3cc8017@googlegroups.com> Message-ID: On 2013-08-25, sahil301290 at gmail.com wrote: > I am unable to check homogeneity of Array. > I have take Array type Int to be default for my code. > > Instead of getting Error on NON-INT Values. > I want to take input as string. > Then check if all input is in (0-9) form, I typecast it into int and Accept. > Else, I would like to skip that input. > > eg. my input is ['1', ' ', 'asdasd231231', '1213asasd', '43242'] > I want it to be interpreted as: > [1, [None], [None], [None], 43242] > > NOTE: NO INBUILT FUNCTION BE USED. Impossible. I think. -- Neil Cerutti From joshua at landau.ws Mon Aug 26 18:39:02 2013 From: joshua at landau.ws (Joshua Landau) Date: Mon, 26 Aug 2013 23:39:02 +0100 Subject: Checking homogeneity of Array using List in Python In-Reply-To: References: <39cf54bd-3772-448a-a56e-74e0b3cc8017@googlegroups.com> Message-ID: On 26 August 2013 14:49, Neil Cerutti wrote: > On 2013-08-25, sahil301290 at gmail.com wrote: >> >> eg. my input is ['1', ' ', 'asdasd231231', '1213asasd', '43242'] >> I want it to be interpreted as: >> [1, [None], [None], [None], 43242] >> >> NOTE: NO INBUILT FUNCTION BE USED. > > Impossible. I think. class BoilerplateToStopCheating: def __init__(self): """Factor things out to prevent cheating.""" self.digit_to_number = {"0":0, "1":1, "2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9} def __call__(self, items): def fudging(): """More cheat-fudging.""" for item in items: try: as_number = 0 for char in item: as_number *= 10 as_number += self.digit_to_number[char] yield as_number except KeyError: yield [None] [*z] = fudging() return z converter = BoilerplateToStopCheating() # Can't use "print"... # Erm... converter(['1', ' ', 'asdasd231231', '1213asasd', '43242']) # Output: [1, [None], [None], [None], 43242] From neilc at norwich.edu Tue Aug 27 08:03:22 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 27 Aug 2013 12:03:22 GMT Subject: Checking homogeneity of Array using List in Python References: <39cf54bd-3772-448a-a56e-74e0b3cc8017@googlegroups.com> Message-ID: On 2013-08-26, Joshua Landau wrote: > On 26 August 2013 14:49, Neil Cerutti wrote: >> On 2013-08-25, sahil301290 at gmail.com wrote: >>> >>> eg. my input is ['1', ' ', 'asdasd231231', '1213asasd', '43242'] >>> I want it to be interpreted as: >>> [1, [None], [None], [None], 43242] >>> >>> NOTE: NO INBUILT FUNCTION BE USED. >> >> Impossible. I think. > > class BoilerplateToStopCheating: > def __init__(self): > """Factor things out to prevent cheating.""" > self.digit_to_number = {"0":0, "1":1, "2":2, "3":3, "4":4, > "5":5, "6":6, "7":7, "8":8, "9":9} > > def __call__(self, items): > def fudging(): > """More cheat-fudging.""" > for item in items: > try: > as_number = 0 > for char in item: > as_number *= 10 > as_number += self.digit_to_number[char] > yield as_number > > except KeyError: > yield [None] > > [*z] = fudging() > return z > > converter = BoilerplateToStopCheating() > > # Can't use "print"... > # Erm... > converter(['1', ' ', 'asdasd231231', '1213asasd', '43242']) > # Output: [1, [None], [None], [None], 43242] Very nice! It seems like unlucky students sometimes get C programming courses ham-fisted into Python without much thought. -- Neil Cerutti From mohsen at pahlevanzadeh.org Sun Aug 25 04:16:40 2013 From: mohsen at pahlevanzadeh.org (Mohsen Pahlevanzadeh) Date: Sun, 25 Aug 2013 12:46:40 +0430 Subject: multifile programming on python 3 Message-ID: <1377418600.6069.9.camel@debian> Dear all, I need help about multifile programming on python 3 and i questioned on stackoverflow : http://stackoverflow.com/questions/18391230/eclipse-python-nameerror-name-mymodule-is-not-defined I thank you if you answer me. --mohsen From davea at davea.name Sun Aug 25 08:13:23 2013 From: davea at davea.name (Dave Angel) Date: Sun, 25 Aug 2013 12:13:23 +0000 (UTC) Subject: multifile programming on python 3 References: <1377418600.6069.9.camel@debian> Message-ID: Mohsen Pahlevanzadeh wrote: > Dear all, > > I need help about multifile programming on python 3 and i questioned on > stackoverflow : > http://stackoverflow.com/questions/18391230/eclipse-python-nameerror-name-mymodule-is-not-defined > > I thank you if you answer me. > > --mohsen You have a response there, so I'm not sure why you're trying to duplicate it here. A portion of your question there: > import repository; > > > x = Repository(); > When i run my application, i get the following error: > > x = Repository(); > > NameError: name 'Repository' is not defined That's not the whole error, that's just the last line of it. Still the immediate answer is clear: after the statement import repository one new symbol is available to your code, the symbol 'repository' if you want to use something defined in that imported module, then use obj = repository.Repository() Or you could do one of these two: import repository Repository = repository.Repository #defines new name for the class Materials = repository.Materials #ditto or from repository import Repository, Materials In either of those latter cases, you could now create the object by obj = Repository() As for the Eclipse problem, you'll have to study the other answer; I've never found it worthwhile to install Eclipse for Python. -- DaveA From mohsen at pahlevanzadeh.org Sun Aug 25 15:31:30 2013 From: mohsen at pahlevanzadeh.org (Mohsen Pahlevanzadeh) Date: Mon, 26 Aug 2013 00:01:30 +0430 Subject: multifile programming on python 3 In-Reply-To: References: <1377418600.6069.9.camel@debian> Message-ID: <1377459090.14456.3.camel@debian> On Sun, 2013-08-25 at 12:13 +0000, Dave Angel wrote: > Mohsen Pahlevanzadeh wrote: > > > Dear all, > > > > I need help about multifile programming on python 3 and i questioned on > > stackoverflow : > > http://stackoverflow.com/questions/18391230/eclipse-python-nameerror-name-mymodule-is-not-defined > > > > I thank you if you answer me. > > > > --mohsen > > You have a response there, so I'm not sure why you're trying to > duplicate it here. > > A portion of your question there: > > > > > import repository; > > > > > > x = Repository(); > > When i run my application, i get the following error: > > > > x = Repository(); > > > > NameError: name 'Repository' is not defined > > That's not the whole error, that's just the last line of it. Still the > immediate answer is clear: > > after the statement > import repository > > one new symbol is available to your code, the symbol 'repository' > > if you want to use something defined in that imported module, then use > > obj = repository.Repository() > > > > Or you could do one of these two: > > import repository > Repository = repository.Repository #defines new name for the class > Materials = repository.Materials #ditto > > or > > from repository import Repository, Materials > > > In either of those latter cases, you could now create the object by > > obj = Repository() > > As for the Eclipse problem, you'll have to study the other answer; I've > never found it worthwhile to install Eclipse for Python. > > -- > DaveA > > At first , thank you, When i post into ML, i get answer on stackoverflow. --mohsen From mohsen at pahlevanzadeh.org Sun Aug 25 04:40:13 2013 From: mohsen at pahlevanzadeh.org (Mohsen Pahlevanzadeh) Date: Sun, 25 Aug 2013 13:10:13 +0430 Subject: typeid(obj).name in C++ vs (python) Message-ID: <1377420013.6069.13.camel@debian> Dear all, If you C++, I need to (typeid(obj).name) in python. Does python same thing? --mohsen From mohsen at pahlevanzadeh.org Sun Aug 25 04:43:25 2013 From: mohsen at pahlevanzadeh.org (Mohsen Pahlevanzadeh) Date: Sun, 25 Aug 2013 13:13:25 +0430 Subject: typeid(obj).name in C++ vs (python) In-Reply-To: <1377420013.6069.13.camel@debian> References: <1377420013.6069.13.camel@debian> Message-ID: <1377420205.6069.14.camel@debian> On Sun, 2013-08-25 at 13:10 +0430, Mohsen Pahlevanzadeh wrote: > Dear all, > > If you C++, I need to (typeid(obj).name) in python. Does python same > thing? > > --mohsen > Of course i need to typeof(). From ian.g.kelly at gmail.com Sun Aug 25 05:50:56 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 25 Aug 2013 03:50:56 -0600 Subject: typeid(obj).name in C++ vs (python) In-Reply-To: <1377420013.6069.13.camel@debian> References: <1377420013.6069.13.camel@debian> Message-ID: type(obj).__name__ On Sun, Aug 25, 2013 at 2:40 AM, Mohsen Pahlevanzadeh wrote: > Dear all, > > If you C++, I need to (typeid(obj).name) in python. Does python same > thing? > > --mohsen > > -- > http://mail.python.org/mailman/listinfo/python-list From mohsen at pahlevanzadeh.org Sun Aug 25 05:33:23 2013 From: mohsen at pahlevanzadeh.org (Mohsen Pahlevanzadeh) Date: Sun, 25 Aug 2013 14:03:23 +0430 Subject: cpmplex function call Message-ID: <1377423203.6069.17.camel@debian> Dear all, Suppose I have function with with 10 args: "def foo(arg1,arg2,arg3,arg4.....): " I need to call somtimes with arg1 , somtimes arg1, arg4, , somtimes arg 4 , arg7 , my program doesn't specify to type of call. Does python same way to help me? My link: http://stackoverflow.com/questions/18427564/order-of-parameters-of-function-call-of-python --mohsen From rosuav at gmail.com Sun Aug 25 05:35:51 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 25 Aug 2013 19:35:51 +1000 Subject: cpmplex function call In-Reply-To: <1377423203.6069.17.camel@debian> References: <1377423203.6069.17.camel@debian> Message-ID: On Sun, Aug 25, 2013 at 7:33 PM, Mohsen Pahlevanzadeh wrote: > Dear all, > > > Suppose I have function with with 10 > args: > > "def foo(arg1,arg2,arg3,arg4.....): " > > I need to call somtimes with arg1 , somtimes arg1, arg4, , somtimes arg 4 , arg7 , my program doesn't specify to type of call. > > Does python same way to help me? > > My link: > http://stackoverflow.com/questions/18427564/order-of-parameters-of-function-call-of-python > --mohsen You seem to have some good answers there already. What don't you understand about those answers? Follow up with them. ChrisA From petr_aleksandrov at mail.ru Sun Aug 25 07:23:08 2013 From: petr_aleksandrov at mail.ru (=?UTF-8?B?0JDQu9C10LrRgdCw0L3QtNGA0L7QsiDQn9C10YLRgA==?=) Date: Sun, 25 Aug 2013 15:23:08 +0400 Subject: Which DLL did fail to load Message-ID: <5219E91C.4030809@mail.ru> Hello. When DLL load is failed, Python doesn't writes the DLL name. Wouldn't it better to add a feature to Python to write the name of DLL which load has been failed? From tjreedy at udel.edu Sun Aug 25 15:06:34 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 25 Aug 2013 15:06:34 -0400 Subject: Which DLL did fail to load In-Reply-To: <5219E91C.4030809@mail.ru> References: <5219E91C.4030809@mail.ru> Message-ID: On 8/25/2013 7:23 AM, ??????????? ???? wrote: > When DLL load is failed, Python doesn't writes the DLL name. Wouldn't it > better to add a feature to Python to write the name of DLL which load > has been failed? If Python initiates a dll load and Python knows the name of the dll and Python writes the failure message, then yes. Without more information, it is impossible to know if all these conditions are true. -- Terry Jan Reedy From miki.tebeka at gmail.com Mon Aug 26 16:12:34 2013 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Mon, 26 Aug 2013 13:12:34 -0700 (PDT) Subject: Which DLL did fail to load In-Reply-To: References: Message-ID: > Wouldn't it > better to add a feature to Python to write the name of DLL which load > has been failed? If you start Python with the -v (verbose) flag, you can see all the calls to dlopen. From lele at metapensiero.it Sun Aug 25 12:41:48 2013 From: lele at metapensiero.it (Lele Gaifax) Date: Sun, 25 Aug 2013 18:41:48 +0200 Subject: PyPI and TestPyPI registration problems Message-ID: <877gf9yb77.fsf@nautilus.nautilus> Hi all, I tried registering myself on the TestPyPI site, but all the methods failed: * the "manual" method terminates with an Internal Error: Error... Internal Server Error Traceback (most recent call last): File "/data/testpypi/src/pypi/webui.py", line 324, in run self.inner_run() File "/data/testpypi/src/pypi/webui.py", line 645, in inner_run getattr(self, action)() File "/data/testpypi/src/pypi/webui.py", line 2879, in user self.send_email(info['email'], rego_message%info) File "/data/testpypi/src/pypi/webui.py", line 3153, in send_email smtp.sendmail(self.config.adminemail, recipient, message) File "/usr/lib/python2.6/smtplib.py", line 709, in sendmail raise SMTPRecipientsRefused(senderrs) SMTPRecipientsRefused: {u'myself at gmail.com': (554, '5.7.1 <myself at gmail.com>: Relay access denied\n5.7.1 Contact your postmaster/admin for assistance. Please provide the following information in your problem report: time (Aug 25 18:22:10) and client (2001:888:2000:d::a4).')} * the "openid" methods (I tried both gmail and launchpad) terminate with an "NotAuthenticated" error, after a successful trip over the remote authentication site Then I tried to register directly on the live PyPI, and when going thru Google's OpenID I got an immediate "502 Bad Gateway" from the underlying nginx... I was finally able to register an account with the "manual" method on the live site, but I'd prefer to learn the upload procedure on the test site... Are these known problems? Thanks in advance, bye, lele. -- nickname: Lele Gaifax | Quando vivr? di quello che ho pensato ieri real: Emanuele Gaifas | comincer? ad aver paura di chi mi copia. lele at metapensiero.it | -- Fortunato Depero, 1929. From mohsen at pahlevanzadeh.org Sun Aug 25 15:18:34 2013 From: mohsen at pahlevanzadeh.org (Mohsen Pahlevanzadeh) Date: Sun, 25 Aug 2013 23:48:34 +0430 Subject: python eval function Message-ID: <1377458314.14456.0.camel@debian> Dear all, eval doesn't run my code at the following link: http://stackoverflow.com/questions/18432198/eval-function-doesnt-work-in-python Thank you before your answer... --mohsen From tjreedy at udel.edu Sun Aug 25 19:49:15 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 25 Aug 2013 19:49:15 -0400 Subject: python eval function In-Reply-To: <1377458314.14456.0.camel@debian> References: <1377458314.14456.0.camel@debian> Message-ID: On 8/25/2013 3:18 PM, Mohsen Pahlevanzadeh wrote: > eval doesn't run my code at the following link: > http://stackoverflow.com/questions/18432198/eval-function-doesnt-work-in-python Already answered there (code is statememt, not expression) -- Terry Jan Reedy From ned at nedbatchelder.com Sun Aug 25 20:25:17 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Sun, 25 Aug 2013 20:25:17 -0400 Subject: python eval function In-Reply-To: References: <1377458314.14456.0.camel@debian> Message-ID: <521AA06D.9070901@nedbatchelder.com> On 8/25/13 7:49 PM, Terry Reedy wrote: > On 8/25/2013 3:18 PM, Mohsen Pahlevanzadeh wrote: > >> eval doesn't run my code at the following link: >> http://stackoverflow.com/questions/18432198/eval-function-doesnt-work-in-python >> > > Already answered there (code is statememt, not expression) > The larger issue is that your use of eval seems unnecessary. If you are trying to set variable variable names, you probably want a dict instead. Oh, and get rid of the semicolons at the ends of lines, they are unnecessary and a sign of old habits from another programming language. --Ned. From steve+comp.lang.python at pearwood.info Sun Aug 25 20:55:06 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 26 Aug 2013 00:55:06 GMT Subject: python eval function References: Message-ID: <521aa76a$0$29986$c3e8da3$5496439d@news.astraweb.com> On Sun, 25 Aug 2013 23:48:34 +0430, Mohsen Pahlevanzadeh wrote: > Dear all, > > eval doesn't run my code at the following link: > http://stackoverflow.com/questions/18432198/eval-function-doesnt-work- in-python Are you asking a question or just sharing a useful link? > Thank you before your answer... What question do you want us to answer? The question on Stackoverflow has already been answered correctly: there is no need to use eval (or exec) for this basic task. Also, you're not writing C code. Get rid of the semi-colons, they are just a waste of time, and make you look like an incompetent programmer. Using exec is not the right solution. That is unsafe, if the field name and content come from a database or external input, your code now has a *serious* Code Injection vulnerability. AND it is slow. So you have written slow, unsafe code. Get rid of it, and use setattr like you have already been told: setattr(self, fieldName, fieldValue) -- Steven From mohsen at pahlevanzadeh.org Mon Aug 26 11:13:53 2013 From: mohsen at pahlevanzadeh.org (Mohsen Pahlevanzadeh) Date: Mon, 26 Aug 2013 19:43:53 +0430 Subject: python eval function In-Reply-To: <521aa76a$0$29986$c3e8da3$5496439d@news.astraweb.com> References: <521aa76a$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: <1377530033.14456.12.camel@debian> On Mon, 2013-08-26 at 00:55 +0000, Steven D'Aprano wrote: > On Sun, 25 Aug 2013 23:48:34 +0430, Mohsen Pahlevanzadeh wrote: > > > Dear all, > > > > eval doesn't run my code at the following link: > > http://stackoverflow.com/questions/18432198/eval-function-doesnt-work- > in-python > > > Are you asking a question or just sharing a useful link? > > > > > Thank you before your answer... > > What question do you want us to answer? The question on Stackoverflow has > already been answered correctly: there is no need to use eval (or exec) > for this basic task. > > Also, you're not writing C code. Get rid of the semi-colons, they are > just a waste of time, and make you look like an incompetent programmer. > > Using exec is not the right solution. That is unsafe, if the field name > and content come from a database or external input, your code now has a > *serious* Code Injection vulnerability. AND it is slow. So you have > written slow, unsafe code. Get rid of it, and use setattr like you have > already been told: > > setattr(self, fieldName, fieldValue) > > > > -- > Steven I didn't post a useful link,i questioned my problem.Of course, At first i put it @stackoverflow then post its link for the following idea: 1. collecting votes for me and someone answered. 2. If anyone have my question in future, can use it. Thank you for answer. --mohsen From luisjosenovoa at gmail.com Sun Aug 25 16:44:52 2013 From: luisjosenovoa at gmail.com (=?ISO-8859-1?Q?Luis_Jos=E9_Novoa?=) Date: Sun, 25 Aug 2013 13:44:52 -0700 (PDT) Subject: About executable Message-ID: <63c24c62-014e-40db-bc05-4f202850636f@googlegroups.com> Hi All. I am trying to create an executable file containing an optimization code using the pyomo package for optimization modeling along with ither packages like Numpy. When using py2exe to perform the task it generates the executable file, but when I try to run it it reports: Import Error: No module named pyomo. Any guidance on how to solve this problem would be really appreciated. From fabiosantosart at gmail.com Mon Aug 26 05:20:04 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Mon, 26 Aug 2013 10:20:04 +0100 Subject: About executable In-Reply-To: <63c24c62-014e-40db-bc05-4f202850636f@googlegroups.com> References: <63c24c62-014e-40db-bc05-4f202850636f@googlegroups.com> Message-ID: On 25 Aug 2013 21:47, "Luis Jos? Novoa" wrote: > > Hi All. I am trying to create an executable file containing an optimization code using the pyomo package for optimization modeling along with ither packages like Numpy. When using py2exe to perform the task it generates the executable file, but when I try to run it it reports: Import Error: No module named pyomo. > > Any guidance on how to solve this problem would be really appreciated. Have you checked py2exe's documentation? Bundling external modules with an exe ought to be a very, very, common task. If all else fails, you can try cx_freeze too. I think it generates windows executables too. Good luck! -------------- next part -------------- An HTML attachment was scrubbed... URL: From rob at attoenterprises.com Sun Aug 25 18:39:20 2013 From: rob at attoenterprises.com (rob at attoenterprises.com) Date: Sun, 25 Aug 2013 15:39:20 -0700 (PDT) Subject: Imaplib gmail move email from inbox to another folder then delete from inbox not working Message-ID: <48b1c30d-95cd-48da-bb2f-d54848403386@googlegroups.com> I am using imaplib to copy a message from inbox to another folder. I then use /Deleted on the message. When I use expunge it does not remove the message from the inbox. I still have the 'Inbox' and 'newfolder' labels on the message. I want to only have the 'newfolder' label on the message. From chris at chrisdown.name Mon Aug 26 05:45:38 2013 From: chris at chrisdown.name (Chris Down) Date: Mon, 26 Aug 2013 11:45:38 +0200 Subject: Resetting state of http.client/httplib HTTPSConnection objects Message-ID: <20130826094538.GI800@chrisdown.name> This is a crosspost from python-tutor upon suggestion. I am experiencing intermittent issues where an exception will be raised when calling getresponse(), which makes the entire connection stuck in Request-sent state. Is it possible to reset to idle state somehow without reinstantiating the HTTPSConnection? I ideally want to keep the connection to the server, and just reset the state to idle so that I can make another request (like you would usually do by calling getresponse()). >>> a.conn >>> a.conn.getresponse() Traceback (most recent call last): File "", line 1, in File "/usr/lib64/python3.3/http/client.py", line 1143, in getresponse response.begin() File "/usr/lib64/python3.3/http/client.py", line 354, in begin version, status, reason = self._read_status() File "/usr/lib64/python3.3/http/client.py", line 324, in _read_status raise BadStatusLine(line) http.client.BadStatusLine: '' >>> a.conn.request("GET", "foo") Traceback (most recent call last): File "", line 1, in File "/usr/lib64/python3.3/http/client.py", line 1061, in request self._send_request(method, url, body, headers) File "/usr/lib64/python3.3/http/client.py", line 1089, in _send_request self.putrequest(method, url, **skips) File "/usr/lib64/python3.3/http/client.py", line 944, in putrequest raise CannotSendRequest(self.__state) http.client.CannotSendRequest: Request-sent -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From chandan_psr at yahoo.co.in Mon Aug 26 05:54:00 2013 From: chandan_psr at yahoo.co.in (chandan kumar) Date: Mon, 26 Aug 2013 17:54:00 +0800 (SGT) Subject: Python Global variable Message-ID: <1377510840.32281.YahooMailBasic@web190503.mail.sg3.yahoo.com> Hi all, Please see the below code,in which i'm verifying the global value in python. CHK=10 def test1(): print "Value of CHK in test1",CHK def test2(): CHK=40 print "Value of CHK in test2",CHK test1() def test3(): global CHK test2() test3() When i ran above code ,I'm getting vlaue of CHK as 40 in test2 method and 10 in test1 method Can somone explain me why the value of CHK is different in method test1 and test2. Best Regards, Chandan From joel.goldstick at gmail.com Mon Aug 26 12:10:43 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 26 Aug 2013 12:10:43 -0400 Subject: Python Global variable In-Reply-To: <1377510840.32281.YahooMailBasic@web190503.mail.sg3.yahoo.com> References: <1377510840.32281.YahooMailBasic@web190503.mail.sg3.yahoo.com> Message-ID: On Mon, Aug 26, 2013 at 5:54 AM, chandan kumar wrote: > Hi all, > > Please see the below code,in which i'm verifying the global value in python. > > CHK=10 > > def test1(): > print "Value of CHK in test1",CHK > > def test2(): > CHK=40 > print "Value of CHK in test2",CHK > test1() > > def test3(): > global CHK > test2() > > test3() > > When i ran above code ,I'm getting vlaue of CHK as 40 in test2 method and 10 in test1 method > Can somone explain me why the value of CHK is different in method test1 and test2. First, you are using incorrect terminology. You are defining functions here, not methods. A method is a 'function' that is contained within a class. Now, on to your question. You define CHK = 10 at the top of your code. CHK is a name defined within the global namespace of your file. In test1 you print CHK which will be the value of the global CHK. This is because, in python if a referenced name isn't present in the most immediate namespace (test1), it will look in the successive containing namespaces. to find the value. In test2 you create a new name CHK which is not the same as the outer CHK. The test2 CHK is only visible within the function. You assign it the value 40. When it executes, it prints 40. In test3 you let test3 know that CHK refers to the global (outer) name CHK. However when you call test2, you do it with no parameters. test2 knows nothing of your test3 internal names. You need to look up python namespaces, and scoping. I believe these are covered in the python.org tutorial. Be aware that if you learned another language (C, Java) that names will confuse you for a while in python. > > Best Regards, > Chandan > > > -- > http://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com From python at mrabarnett.plus.com Mon Aug 26 12:18:54 2013 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 26 Aug 2013 17:18:54 +0100 Subject: Python Global variable In-Reply-To: <1377510840.32281.YahooMailBasic@web190503.mail.sg3.yahoo.com> References: <1377510840.32281.YahooMailBasic@web190503.mail.sg3.yahoo.com> Message-ID: <521B7FEE.2050701@mrabarnett.plus.com> On 26/08/2013 10:54, chandan kumar wrote: > Hi all, > > Please see the below code,in which i'm verifying the global value in python. > > CHK=10 > > def test1(): > print "Value of CHK in test1",CHK > > def test2(): > CHK=40 > print "Value of CHK in test2",CHK > test1() > > def test3(): > global CHK > test2() > > test3() > > When i ran above code ,I'm getting vlaue of CHK as 40 in test2 method and 10 in test1 method > Can somone explain me why the value of CHK is different in method test1 and test2. > In a function, if you bind to a name then Python assumes that the name is local to that function unless you declare that it's global. In test1, you refer to 'CHK' but don't bind to it, therefore it's global. In test2, you bind to 'CHK' with 'CHK=40', and you haven't declared that it's global, therefore it's local to test2. In test3, you declare that 'CHK' is global, but don't mention it anywhere else in that function, so it serves no purpose. From simeon.chaos at gmail.com Mon Aug 26 08:04:17 2013 From: simeon.chaos at gmail.com (Simeon Chaos) Date: Mon, 26 Aug 2013 05:04:17 -0700 (PDT) Subject: Peasy: an easy but powerful parser Message-ID: I've released Peasy, which is an easy but powerful parser. Peasy brings a new method to write the parser. with Peasy, you write the parser by hand, just like to write any other kind of program. Do not be confused by "by hand", with the method brought by Peasy, it's easier and more powerful than using other tools or libraries. Peasy presents itself as a module of single file. To use Peasy, copy peasy.py to your project, read it, modify it, write the grammar rules, and remove any unnecessary stuffs in Peasy, and parse with the grammar. Peasy has an simple, elegant and fast support for direct/indirect left recursive grammar, See [here]( http://chaosim.github.io/peasy/doc/peasy.html#peasysample ) for a concrete sample. The [annotated code]( http://chaosim.github.io/peasy/doc/pypeasy.html ) is best document for Peasy at the moment. From guytamir1 at gmail.com Mon Aug 26 09:10:41 2013 From: guytamir1 at gmail.com (Guy Tamir) Date: Mon, 26 Aug 2013 06:10:41 -0700 (PDT) Subject: Getting request's source site Message-ID: <6dc9b8f6-7eb7-42a3-b3ea-ccc3aef072ee@googlegroups.com> Hi all, I was wondering how i can get the source from which the request came from. If a user posted a link that directs to my server on facebook i'd like to know that a specific click was from facebook or twitter etc.. Thanks From steve+comp.lang.python at pearwood.info Mon Aug 26 10:25:49 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 26 Aug 2013 14:25:49 GMT Subject: Getting request's source site References: <6dc9b8f6-7eb7-42a3-b3ea-ccc3aef072ee@googlegroups.com> Message-ID: <521b656d$0$29986$c3e8da3$5496439d@news.astraweb.com> On Mon, 26 Aug 2013 06:10:41 -0700, Guy Tamir wrote: > Hi all, > > I was wondering how i can get the source from which the request came > from. If a user posted a link that directs to my server on facebook i'd > like to know that a specific click was from facebook or twitter etc.. That would be the "referer" header in the HTTP request. (Note that it is an accidental misspelling of "referrer".) Including a referrer is not compulsory, so be prepared for it not to be available. -- Steven From guytamir1 at gmail.com Mon Aug 26 11:03:33 2013 From: guytamir1 at gmail.com (Guy Tamir) Date: Mon, 26 Aug 2013 08:03:33 -0700 (PDT) Subject: Getting request's source site In-Reply-To: <521b656d$0$29986$c3e8da3$5496439d@news.astraweb.com> References: <6dc9b8f6-7eb7-42a3-b3ea-ccc3aef072ee@googlegroups.com> <521b656d$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: <03e29dbb-d95d-495e-8937-2b630897d36b@googlegroups.com> On Monday, August 26, 2013 5:25:49 PM UTC+3, Steven D'Aprano wrote: > On Mon, 26 Aug 2013 06:10:41 -0700, Guy Tamir wrote: > > > > > Hi all, > > > > > > I was wondering how i can get the source from which the request came > > > from. If a user posted a link that directs to my server on facebook i'd > > > like to know that a specific click was from facebook or twitter etc.. > > > > That would be the "referer" header in the HTTP request. (Note that it is > > an accidental misspelling of "referrer".) Including a referrer is not > > compulsory, so be prepared for it not to be available. > > > > > > > > -- > > Steven Thanks for reply, In what cases will i have this header? is there something i can do to enforce having it? Are there any other ways? From joel.goldstick at gmail.com Mon Aug 26 11:16:01 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 26 Aug 2013 11:16:01 -0400 Subject: Getting request's source site In-Reply-To: <03e29dbb-d95d-495e-8937-2b630897d36b@googlegroups.com> References: <6dc9b8f6-7eb7-42a3-b3ea-ccc3aef072ee@googlegroups.com> <521b656d$0$29986$c3e8da3$5496439d@news.astraweb.com> <03e29dbb-d95d-495e-8937-2b630897d36b@googlegroups.com> Message-ID: On Mon, Aug 26, 2013 at 11:03 AM, Guy Tamir wrote: > On Monday, August 26, 2013 5:25:49 PM UTC+3, Steven D'Aprano wrote: >> On Mon, 26 Aug 2013 06:10:41 -0700, Guy Tamir wrote: >> >> >> >> > Hi all, >> >> > >> >> > I was wondering how i can get the source from which the request came >> >> > from. If a user posted a link that directs to my server on facebook i'd >> >> > like to know that a specific click was from facebook or twitter etc.. >> >> >> >> That would be the "referer" header in the HTTP request. (Note that it is >> >> an accidental misspelling of "referrer".) Including a referrer is not >> >> compulsory, so be prepared for it not to be available. >> >> >> >> >> >> >> >> -- >> >> Steven > > Thanks for reply, > In what cases will i have this header? is there something i can do to enforce having it? > Are there any other ways? > -- You can look in the server logs if you have them available. If the visitor supplied a referer value in the header, it will be in the log. You don't have any say in this from your end. If you are the creator of your own website you might like to add google analytics to your pages. This is done with a little javascript that google provides. Although there may be a python angle to your question, it isn't apparent yet. > http://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com From random832 at fastmail.us Mon Aug 26 13:51:32 2013 From: random832 at fastmail.us (random832 at fastmail.us) Date: Mon, 26 Aug 2013 13:51:32 -0400 Subject: Getting request's source site In-Reply-To: <03e29dbb-d95d-495e-8937-2b630897d36b@googlegroups.com> References: <6dc9b8f6-7eb7-42a3-b3ea-ccc3aef072ee@googlegroups.com> <521b656d$0$29986$c3e8da3$5496439d@news.astraweb.com> <03e29dbb-d95d-495e-8937-2b630897d36b@googlegroups.com> Message-ID: <1377539492.20008.14320385.763BC1CF@webmail.messagingengine.com> On Mon, Aug 26, 2013, at 11:03, Guy Tamir wrote: > Thanks for reply, > In what cases will i have this header? In practice, you'll usually have it, except in cases where the user has bookmarked your site, typed/pasted the URL directly, had it opened by an external program, or taken action to block referers from being sent (this last one is uncommon) > is there something i can do to > enforce having it? You can refuse to serve the content if it's not present. But this will annoy users since they can't bookmark it or open it from an external program. From steve at pearwood.info Tue Aug 27 05:23:23 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 27 Aug 2013 09:23:23 GMT Subject: Getting request's source site References: <6dc9b8f6-7eb7-42a3-b3ea-ccc3aef072ee@googlegroups.com> <521b656d$0$29986$c3e8da3$5496439d@news.astraweb.com> <03e29dbb-d95d-495e-8937-2b630897d36b@googlegroups.com> Message-ID: <521c700b$0$11100$c3e8da3@news.astraweb.com> On Mon, 26 Aug 2013 08:03:33 -0700, Guy Tamir wrote: [asking about Referer header] > In what cases will i have this header? is there something i can do to > enforce having it? Are there any other ways? The Referer header is in the HTTP Request that is made to your server. What server are you using? If it is a custom server written in Python, then this is a legitimate question for this list. Otherwise, check the documentation for your server. Any decent web server will already deal with referrers, and any decent web analytics software will show you the information collected. http://en.wikipedia.org/wiki/List_of_web_analytics_software As for enforcing a referer, that's a really low, dirty thing to do, and I will not give you any hints as to how to do such a thing. Websites that take different actions depending on where you are coming from are the lowest of the low. Need a hand disposing of a body? Yeah, there are legitimate reasons for this, I'll help if I can. Forcing visitors to your website to set the Referer header? Nope, no way. -- Steven From rosuav at gmail.com Tue Aug 27 05:36:03 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 27 Aug 2013 19:36:03 +1000 Subject: Getting request's source site In-Reply-To: <521c700b$0$11100$c3e8da3@news.astraweb.com> References: <6dc9b8f6-7eb7-42a3-b3ea-ccc3aef072ee@googlegroups.com> <521b656d$0$29986$c3e8da3$5496439d@news.astraweb.com> <03e29dbb-d95d-495e-8937-2b630897d36b@googlegroups.com> <521c700b$0$11100$c3e8da3@news.astraweb.com> Message-ID: On Tue, Aug 27, 2013 at 7:23 PM, Steven D'Aprano wrote: > Need a hand disposing of a body? Yeah, there are > legitimate reasons for this, I'll help if I can. Forcing visitors to your > website to set the Referer header? Nope, no way. Friends help friends move bodies. But even they don't help move headers. ChrisA From andy.kannberg at gmail.com Mon Aug 26 12:16:58 2013 From: andy.kannberg at gmail.com (Andy Kannberg) Date: Mon, 26 Aug 2013 18:16:58 +0200 Subject: optparse question (python 2.6) Message-ID: Hi python-guru's, I am new to Python, coming from a long history of Unix/linux shell programming. I am creating a Python script (In Python 2.6) which should be able to read command line options and arguments. So far, I figured out how to do that with optparse. I can add options (and arguments ) . There are about 7 options that can be selected. However, I can't seem to figure out how to force that only one option is allowed when the script is invoked. In other words: How to restrict the script to accept only one of the available options ? cheers, Andy -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Mon Aug 26 13:40:05 2013 From: __peter__ at web.de (Peter Otten) Date: Mon, 26 Aug 2013 19:40:05 +0200 Subject: optparse question (python 2.6) References: Message-ID: Andy Kannberg wrote: > Hi python-guru's, > > I am new to Python, coming from a long history of Unix/linux shell > programming. > I am creating a Python script (In Python 2.6) which should be able to > read command line options and arguments. > So far, I figured out how to do that with optparse. I can add options (and > arguments ) . > There are about 7 options that can be selected. > > However, I can't seem to figure out how to force that only one option is > allowed when the script is invoked. In other words: How to restrict the > script to accept only one of the available options ? You have to do it manually, like in the example http://docs.python.org/2.6/library/optparse.html#how-optparse-handles-errors if options.a and options.b: parser.error("options -a and -b are mutually exclusive") which could be generalized to (untested) option_names = ["foo", "bar", "baz", ...] toggled_options = [name for name in option_names if getattr(options, name)] if len(toggled_options) > 1: s = repr(toggled_options).strip("[]") parser.error("options %s are mutually exclusive" % s) If you are not restricted to the standard library use https://pypi.python.org/pypi/argparse which was added to the stdlib in 2.7 and has "mutually exclusive groups", see http://docs.python.org/2/library/argparse.html#mutual-exclusion From marco.buttu at gmail.com Mon Aug 26 15:37:32 2013 From: marco.buttu at gmail.com (Marco Buttu) Date: Mon, 26 Aug 2013 21:37:32 +0200 Subject: How come StopIteration.__base__ is not BaseException? Message-ID: Since StopIteration is not an error, how come does it inherit directly from Exception and not from BaseException? Thanks in advance, Marco -- Marco From random832 at fastmail.us Mon Aug 26 16:10:15 2013 From: random832 at fastmail.us (random832 at fastmail.us) Date: Mon, 26 Aug 2013 16:10:15 -0400 Subject: How come StopIteration.__base__ is not BaseException? In-Reply-To: References: Message-ID: <1377547815.30876.14372233.29332EF5@webmail.messagingengine.com> On Mon, Aug 26, 2013, at 15:37, Marco Buttu wrote: > Since StopIteration is not an error, how come does it inherit directly > from Exception and not from BaseException? The reason KeyboardInterrupt and SystemExit inherit from BaseException is because you often want them to escape (allowing the program to quit) from code that would otherwise catch them (by catching Exception). On the contrary, StopIteration is almost always used in a context that catches it specifically. From marco.buttu at gmail.com Tue Aug 27 02:17:15 2013 From: marco.buttu at gmail.com (Marco Buttu) Date: Tue, 27 Aug 2013 08:17:15 +0200 Subject: How come StopIteration.__base__ is not BaseException? References: Message-ID: <521C446B.9040605@gmail.com> On 08/26/2013 10:10 PM, random832 at fastmail.us wrote: > The reason KeyboardInterrupt and SystemExit inherit from BaseException > is because you often want them to escape (allowing the program to quit) > from code that would otherwise catch them (by catching Exception). On > the contrary, StopIteration is almost always used in a context that > catches it specifically. But if I want to catch it specifically (except BaseIteration), why doesn't it hinerit directly from BaseException? It's not an error and it's not a warning. I would like to see a piece of code in which it is useful to catch it generically with a except Exception clause instead of an except StopIteration... Regards -- Marco From marco.buttu at gmail.com Tue Aug 27 02:18:34 2013 From: marco.buttu at gmail.com (Marco Buttu) Date: Tue, 27 Aug 2013 08:18:34 +0200 Subject: How come StopIteration.__base__ is not BaseException? References: <521C446B.9040605@gmail.com> Message-ID: On 08/27/2013 08:17 AM, Marco Buttu wrote: > But if I want to catch it specifically (except BaseIteration), Sorry, except StopIteration... -- Marco Buttu From steve at pearwood.info Tue Aug 27 05:22:01 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 27 Aug 2013 09:22:01 GMT Subject: How come StopIteration.__base__ is not BaseException? References: <521C446B.9040605@gmail.com> Message-ID: <521c6fb9$0$11100$c3e8da3@news.astraweb.com> On Tue, 27 Aug 2013 08:17:15 +0200, Marco Buttu wrote: > On 08/26/2013 10:10 PM, random832 at fastmail.us wrote: > >> The reason KeyboardInterrupt and SystemExit inherit from BaseException >> is because you often want them to escape (allowing the program to quit) >> from code that would otherwise catch them (by catching Exception). On >> the contrary, StopIteration is almost always used in a context that >> catches it specifically. > > But if I want to catch it specifically (except BaseIteration), why You mean StopIteration. > doesn't it hinerit directly from BaseException? It's not an error and > it's not a warning. I would like to see a piece of code in which it is > useful to catch it generically with a except Exception clause instead of > an except StopIteration... You are focused on the wrong thing. It doesn't matter that we normally catch StopIteration specifically. We normally catch *all* exceptions specifically. try: ... except ValueError: ... This doesn't mean that ValueError should inherit from BaseException. So the fact that we catch StopIteration specifically too doesn't mean it needs to inherit from BaseException. What matters is that when you catch "nearly everything", StopIteration is included in the "nearly everything", but SysExit and KeyboardInterrupt should not be. Consider: try: main() except Exception as e: print('an unexpected error occurred') log_unhandled_exception(e) emergency_shutdown() sys.exit(1) except (KeyboardInterrupt, SysExit): # User wants to exit. clean_exit() sys.exit(0) Which except clause would you expect an unhandled StopIteration to fall under? The unexpected error clause, or the "user wants to exit cleanly" clause? -- Steven From marco.buttu at gmail.com Tue Aug 27 06:02:29 2013 From: marco.buttu at gmail.com (Marco Buttu) Date: Tue, 27 Aug 2013 12:02:29 +0200 Subject: How come StopIteration.__base__ is not BaseException? References: <521C446B.9040605@gmail.com> <521c6fb9$0$11100$c3e8da3@news.astraweb.com> Message-ID: <521C7935.8080205@gmail.com> On 08/27/2013 11:22 AM, Steven D'Aprano wrote: > > What matters is that when you catch "nearly everything", StopIteration is > included in the "nearly everything", but SysExit and KeyboardInterrupt > should not be. Consider: > > > try: > main() > except Exception as e: > print('an unexpected error occurred') > log_unhandled_exception(e) > emergency_shutdown() > sys.exit(1) > except (KeyboardInterrupt, SysExit): > # User wants to exit. > clean_exit() > sys.exit(0) > > > > Which except clause would you expect an unhandled StopIteration to fall > under? The unexpected error clause, or the "user wants to exit cleanly" > clause? Thanks Steven, that was clear for me. I was thinking about a design concept: how come doesn't it inherit directly from BaseException like GeneratorExit does? But I think I got the answer: because we can iterate manually and so it can propagate, and so we want an except Exception clause catches it. Thanks, Marco -- Marco From tjreedy at udel.edu Tue Aug 27 14:51:59 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 27 Aug 2013 14:51:59 -0400 Subject: How come StopIteration.__base__ is not BaseException? In-Reply-To: <521C7935.8080205@gmail.com> References: <521C446B.9040605@gmail.com> <521c6fb9$0$11100$c3e8da3@news.astraweb.com> <521C7935.8080205@gmail.com> Message-ID: On 8/27/2013 6:02 AM, Marco Buttu wrote: > On 08/27/2013 11:22 AM, Steven D'Aprano wrote: >> >> What matters is that when you catch "nearly everything", StopIteration is >> included in the "nearly everything", but SysExit and KeyboardInterrupt >> should not be. Consider: >> >> >> try: >> main() >> except Exception as e: >> print('an unexpected error occurred') >> log_unhandled_exception(e) >> emergency_shutdown() >> sys.exit(1) >> except (KeyboardInterrupt, SysExit): >> # User wants to exit. >> clean_exit() >> sys.exit(0) >> >> >> >> Which except clause would you expect an unhandled StopIteration to fall >> under? The unexpected error clause, or the "user wants to exit cleanly" >> clause? > > Thanks Steven, that was clear for me. I was thinking about a design > concept: how come doesn't it inherit directly from BaseException like > GeneratorExit does? But I think I got the answer: because we can iterate > manually and so it can propagate, and so we want an except Exception > clause catches it. Until relatively recently, in 2.5, Exception *was* the base exception class and for nearly everything, it still is. "All built-in, non-system-exiting exceptions are derived from this class. All user-defined exceptions should also be derived from this class." BaseException was added just so it would be possible to catch nearly everything but a few exceptions. The first two were KeyboardInterrupt and SystemExit (in 2.5). GeneratorExit was switched in 2.6, but I forget the details of why. -- Terry Jan Reedy From marco.buttu at gmail.com Tue Aug 27 15:52:35 2013 From: marco.buttu at gmail.com (Marco Buttu) Date: Tue, 27 Aug 2013 21:52:35 +0200 Subject: How come StopIteration.__base__ is not BaseException? References: <521C446B.9040605@gmail.com> <521c6fb9$0$11100$c3e8da3@news.astraweb.com> <521C7935.8080205@gmail.com> Message-ID: On 08/27/2013 08:51 PM, Terry Reedy wrote: > BaseException was added just so it would be possible to catch nearly > everything but a few exceptions. The first two were KeyboardInterrupt > and SystemExit (in 2.5). GeneratorExit was switched in 2.6, but I forget > the details of why. Maybe in order to don't catch it inside a generator using a except Exception clause, because it is used to notify an active generator is closed: >>> def foogen(): ... for i in range(10): ... try: ... yield i ... except: ... print('Catched!') ... # raise ... >>> g = foogen() >>> next(g) 0 >>> g.close() Catched! Traceback (most recent call last): File "", line 1, in RuntimeError: generator ignored GeneratorExit Do you remember if this is the reason? Thanks, -- Marco From tjreedy at udel.edu Tue Aug 27 16:37:05 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 27 Aug 2013 16:37:05 -0400 Subject: How come StopIteration.__base__ is not BaseException? In-Reply-To: References: <521C446B.9040605@gmail.com> <521c6fb9$0$11100$c3e8da3@news.astraweb.com> <521C7935.8080205@gmail.com> Message-ID: On 8/27/2013 3:52 PM, Marco Buttu wrote: > On 08/27/2013 08:51 PM, Terry Reedy wrote: > >> BaseException was added just so it would be possible to catch nearly >> everything but a few exceptions. The first two were KeyboardInterrupt >> and SystemExit (in 2.5). GeneratorExit was switched in 2.6, but I forget >> the details of why. > > Maybe in order to don't catch it inside a generator using a except > Exception clause, because it is used to notify an active generator is > closed: > > >>> def foogen(): > ... for i in range(10): > ... try: > ... yield i > ... except: > ... print('Catched!') > ... # raise > ... > >>> g = foogen() > >>> next(g) > 0 > >>> g.close() > Catched! > Traceback (most recent call last): > File "", line 1, in > RuntimeError: generator ignored GeneratorExit > > Do you remember if this is the reason? Thanks, I only remember that there was a 'problem' that necessitated a change in 2.6 after the introduction in 2.5. The above seems reasonable. -- Terry Jan Reedy From ethan at stoneleaf.us Tue Aug 27 16:00:50 2013 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 27 Aug 2013 13:00:50 -0700 Subject: How come StopIteration.__base__ is not BaseException? In-Reply-To: References: Message-ID: <521D0572.10701@stoneleaf.us> On 08/26/2013 12:37 PM, Marco Buttu wrote: > > Since StopIteration is not an error, how come does it inherit directly from Exception and not from BaseException? Not all exceptions are errors. (Although not dealing with one can be. ;) -- ~Ethan~ From menkomigen6 at gmail.com Mon Aug 26 15:45:36 2013 From: menkomigen6 at gmail.com (Paul Pittlerson) Date: Mon, 26 Aug 2013 12:45:36 -0700 (PDT) Subject: How to check client shutdown? Message-ID: I'm currently learning about the socket module. My question is how can I detect if a connection is closed from the other side, for example a KeyboardInterrupt as I frequently use. My code below: ########################################################## #server script: class client(threading.Thread): def __init__(self, connection): super(client, self).__init__() self.recv = connection.recv print dir(connection) def start(self): self.run() def run(self): while True: data = self.recv(2000) if data: msg = self.decode(data) print msg #break; def decode(self, data): return pickle.loads(data) class bullet_server: def __init__(self): self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) def start(self): server_address = ("localhost", 9006) print '/--server running--/' print server_address self.server.bind(server_address) self.server.listen(5) while True: connection, client_address = self.server.accept() c = client(connection) c.start() if __name__ == '__main__': server = bullet_server() server.start() ########################################################## #client script: # set IP and port host = '127.0.0.1' port = 9006 # connect to server connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) connection.connect((host, port)) def loop(): print 'Enter message:' msg = raw_input() encoded = pickle.dumps(msg) connection.send(encoded) loop() loop() From rosuav at gmail.com Mon Aug 26 16:09:34 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 27 Aug 2013 06:09:34 +1000 Subject: How to check client shutdown? In-Reply-To: References: Message-ID: On Tue, Aug 27, 2013 at 5:45 AM, Paul Pittlerson wrote: > I'm currently learning about the socket module. My question is how can I detect if a connection is closed from the other side, for example a KeyboardInterrupt as I frequently use. My code below: > Once the remote end has terminated (as it will presumably do in this case), any attempt to write to the socket will eventually result in an error. You should be able to see that by simple experimentation. Be aware that it might take a bit of time for the error to filter back to you; it depends on whether the remote end actually sends back a RST (reset) packet or not. (In a sane system, hitting Ctrl-C should result in a clean shutdown and proper socket closure, but your server needs to handle the case where it doesn't get one.) Play around with it, you should be able to figure out what it's doing for you. A couple of tangential comments about your script: > ########################################################## > #server script: > > def decode(self, data): > return pickle.loads(data) MAJOR security issue here. You are taking data from a networked source and running it through a trusting system (pickle). This is NOT recommended. Also: You're using a stream socket, and depending - I think - on getting an entire message in a single read() call. This is definitely not guaranteed, though when you're working with localhost it's likely to be true. To make this reliable, you'll need to do something like prefix the message with its length (eg a Hollerith string), or use some system like JSON that lets you detect the end of the message. >From the client: > def loop(): > print 'Enter message:' > msg = raw_input() > encoded = pickle.dumps(msg) > connection.send(encoded) > loop() > > loop() Why recurse? Why not simply loop? No Python has tail-call optimization (as far as I know), so this will eventually blow up with a RuntimeError: maximum recursion depth exceeded. ChrisA From python at mrabarnett.plus.com Mon Aug 26 16:20:20 2013 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 26 Aug 2013 21:20:20 +0100 Subject: How to check client shutdown? In-Reply-To: References: Message-ID: <521BB884.1030407@mrabarnett.plus.com> On 26/08/2013 20:45, Paul Pittlerson wrote: > I'm currently learning about the socket module. My question is how > can I detect if a connection is closed from the other side, for > example a KeyboardInterrupt as I frequently use. My code below: > [snip] When reading from a socket, it'll return as much data as is available, up to the given limit if specified. If _no_ data is available, then it'll block, unless the socket has been closed at the other end, in which case it'll return zero bytes (an empty bytestring). From invalid at invalid.invalid Mon Aug 26 16:24:04 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Mon, 26 Aug 2013 20:24:04 +0000 (UTC) Subject: How to check client shutdown? References: Message-ID: On 2013-08-26, Paul Pittlerson wrote: > I'm currently learning about the socket module. My question is how > can I detect if a connection is closed from the other side, recv() will return an empty value of ''. send() will eventually throw an exception. [You may be able to call send() once or twice after the connection has been closed without getting the exception.] On Linux the exception is socket.error with errno 32 (broken pipe). -- Grant Edwards grant.b.edwards Yow! Is this an out-take at from the "BRADY BUNCH"? gmail.com From menkomigen6 at gmail.com Tue Aug 27 13:44:14 2013 From: menkomigen6 at gmail.com (Paul Pittlerson) Date: Tue, 27 Aug 2013 10:44:14 -0700 (PDT) Subject: How to check client shutdown? In-Reply-To: References: Message-ID: <2f3e7c96-45a7-485c-bfc7-18bf9841114b@googlegroups.com> > MAJOR security issue here. You are taking data from a networked source > > and running it through a trusting system (pickle). This is NOT > > recommended. > Security issue!? Do you mean someone could enter devious python h4xx into the chat or something? I had no idea using pickle was so dangerous, but I don't know any other method of transmitting data in python :( > Also: You're using a stream socket, and depending - I think - on > > getting an entire message in a single read() call. This is definitely > > not guaranteed, though when you're working with localhost it's likely > > to be true. To make this reliable, you'll need to do something like > > prefix the message with its length (eg a Hollerith string), or use > > some system like JSON that lets you detect the end of the message. > I'm guessing the fix is to have a separate thread which only job is to send info about the size of the next data transmission. What is the actual downside of having the server set to anticipate a message length which is known to be more than will be sent (or be allowed to be sent?), for example connection.recv(10000). Does not the receiver know the size after the fact? Is it impacting performance somehow (I haven't noticed anything in my tests) From robotsondrugs at gmail.com Tue Aug 27 16:17:11 2013 From: robotsondrugs at gmail.com (Andrew Berg) Date: Tue, 27 Aug 2013 15:17:11 -0500 Subject: How to check client shutdown? In-Reply-To: <2f3e7c96-45a7-485c-bfc7-18bf9841114b@googlegroups.com> References: <2f3e7c96-45a7-485c-bfc7-18bf9841114b@googlegroups.com> Message-ID: <521D0947.2000503@gmail.com> On 2013.08.27 12:44, Paul Pittlerson wrote: > Security issue!? Do you mean someone could enter devious python h4xx into the chat or something? I had no idea using pickle was so dangerous, but I don't know any other method of transmitting data in python :( JSON, XML, or any other format that doesn't have the capacity to serialize directly executable Python code. Pickles are okay for internal data from trusted sources, but don't accept pickles from anyone you don't trust. JSON is simple, easy, and can handle the common data types, and there is a module for it in the stdlib. It's also a standard format you can use in pretty much any language, and it's human readable. -- CPython 3.3.2 | Windows NT 6.2.9200 / FreeBSD 9.1 From irmen.NOSPAM at xs4all.nl Tue Aug 27 16:22:48 2013 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Tue, 27 Aug 2013 22:22:48 +0200 Subject: How to check client shutdown? In-Reply-To: <2f3e7c96-45a7-485c-bfc7-18bf9841114b@googlegroups.com> References: <2f3e7c96-45a7-485c-bfc7-18bf9841114b@googlegroups.com> Message-ID: <521d0a99$0$15900$e4fe514c@news.xs4all.nl> On 27-8-2013 19:44, Paul Pittlerson wrote: >> MAJOR security issue here. You are taking data from a networked source >> >> and running it through a trusting system (pickle). This is NOT >> >> recommended. >> > > Security issue!? Do you mean someone could enter devious python h4xx into the chat or > something? I had no idea using pickle was so dangerous, but I don't know any other > method of transmitting data in python :( > Shameless plug: have a look at my remote object library Pyro: http://pythonhosted.org/Pyro4/ It transmits arbitrary objects to remote machines by what seem to be normal method calls. In the past, it was restricted to using pickle as serialization format, but since a couple of releases, it now defaults to another -safe- serializer. Because of the same reason Chris is warning you about: unpickling data from untrusted sources can result in arbitrary code execution in your server. Main thing is: don't use pickle in your networking code unless you can guarantee the trustworthiness of your sources. Instead, use another serialization format that is safe (such as marshal, json, serpent). > I'm guessing the fix is to have a separate thread which only job is to send info > about the size of the next data transmission. I'm not sure what you're proposing here. What's a separate thread got to do with things? ? What is the actual downside of having > the server set to anticipate a message length which is known to be more than will be > sent (or be allowed to be sent?), for example connection.recv(10000). Does not the > receiver know the size after the fact? Is it impacting performance somehow (I haven't > noticed anything in my tests) The issue is that recv() is not guaranteed to return you the full amount of data that is requested. It may very well just return a single byte, and leave the rest for later. The argument is an upper bound on the amount of data you receive. So to make your recv reliable, you need to have a means of deciding when the 'full' amount of data has been collected. As Chris already suggested, this is usually done by putting the recv() in a loop and collecting data until it reaches a length that you precisely know beforehand, or by detecting a special end-of-message marker in the data stream, such as a newline. Irmen de Jong From rosuav at gmail.com Tue Aug 27 16:40:07 2013 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 28 Aug 2013 06:40:07 +1000 Subject: How to check client shutdown? In-Reply-To: <521d0a99$0$15900$e4fe514c@news.xs4all.nl> References: <2f3e7c96-45a7-485c-bfc7-18bf9841114b@googlegroups.com> <521d0a99$0$15900$e4fe514c@news.xs4all.nl> Message-ID: On Wed, Aug 28, 2013 at 6:22 AM, Irmen de Jong wrote: > ? What is the actual downside of having >> the server set to anticipate a message length which is known to be more than will be >> sent (or be allowed to be sent?), for example connection.recv(10000). Does not the >> receiver know the size after the fact? Is it impacting performance somehow (I haven't >> noticed anything in my tests) > > The issue is that recv() is not guaranteed to return you the full amount of data that is > requested. It may very well just return a single byte, and leave the rest for later. The > argument is an upper bound on the amount of data you receive. So to make your recv > reliable, you need to have a means of deciding when the 'full' amount of data has been > collected. As Chris already suggested, this is usually done by putting the recv() in a > loop and collecting data until it reaches a length that you precisely know beforehand, > or by detecting a special end-of-message marker in the data stream, such as a newline. Right. When you use TCP sockets, there's no boundaries, so you could get two pickles in one recv, or you could get one and a half, or anything. It depends partly on your buffer sizes and things; if you're sending very short messages (less than a kilobyte), and have long delays between them, chances are you'll get one write in one read; but it's not guaranteed. ChrisA From irmen.NOSPAM at xs4all.nl Tue Aug 27 16:49:30 2013 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Tue, 27 Aug 2013 22:49:30 +0200 Subject: How to check client shutdown? In-Reply-To: References: <2f3e7c96-45a7-485c-bfc7-18bf9841114b@googlegroups.com> <521d0a99$0$15900$e4fe514c@news.xs4all.nl> Message-ID: <521d10db$0$15950$e4fe514c@news.xs4all.nl> On 27-8-2013 22:40, Chris Angelico wrote: > Right. When you use TCP sockets, there's no boundaries, so you could > get two pickles in one recv, or you could get one and a half, or > anything. It depends partly on your buffer sizes and things; if you're > sending very short messages (less than a kilobyte), and have long > delays between them, chances are you'll get one write in one read; but > it's not guaranteed. > > ChrisA > More horror stories: the same is also true for send(). Thankfully, Python provides a sendall(). But that doesn't work on some systems when the socket is in non-blocking mode. Meh. Irmen From rob at attoenterprises.com Mon Aug 26 19:34:26 2013 From: rob at attoenterprises.com (rob at attoenterprises.com) Date: Mon, 26 Aug 2013 16:34:26 -0700 (PDT) Subject: Imaplib & Gmail - cannot move from inbox to custom folder (message remains in both) Message-ID: I am using Imaplib and connecting to a Gmail account. I am issuing commands to copy the email to a custom folder then issuing delete. After expunge message still remains in inbox and customer folder. # COPY EMAIL TO FOLDER copyStatus = m.copy(emailid, 'CIRIMPORTED') print 'id of message copied is: %s status: %s' % (emailid,copyStatus) # SET THE MESSAGE TO HAVE '\Deleted' FLAG (EXPUNGE WILL COMPLETE DELETE PROCESS) storeStatus = m.store(emailid,"+FLAGS", r'(\Deleted)') print 'id of message stored is: %s status: %s %s' % (emailid,storeStatus[0],storeStatus[1]) #print '******' #DELETES ANY EMAIL MARKED \Deleted m.expunge() m.close() m.logout() Does anyone have any imaplib code the works with Gmail to move from inbox to another folder? From python at mrabarnett.plus.com Mon Aug 26 19:56:09 2013 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 27 Aug 2013 00:56:09 +0100 Subject: Imaplib & Gmail - cannot move from inbox to custom folder (message remains in both) In-Reply-To: References: Message-ID: <521BEB19.40409@mrabarnett.plus.com> On 27/08/2013 00:34, rob at attoenterprises.com wrote: > I am using Imaplib and connecting to a Gmail account. I am issuing commands to copy the email to a custom folder then issuing delete. After expunge message still remains in inbox and customer folder. > > # COPY EMAIL TO FOLDER > copyStatus = m.copy(emailid, 'CIRIMPORTED') > print 'id of message copied is: %s status: %s' % (emailid,copyStatus) > > # SET THE MESSAGE TO HAVE '\Deleted' FLAG (EXPUNGE WILL COMPLETE DELETE PROCESS) > storeStatus = m.store(emailid,"+FLAGS", r'(\Deleted)') > print 'id of message stored is: %s status: %s %s' % (emailid,storeStatus[0],storeStatus[1]) > #print '******' > > #DELETES ANY EMAIL MARKED \Deleted > m.expunge() > m.close() > m.logout() > > Does anyone have any imaplib code the works with Gmail to move from inbox to another folder? > There's an example in docs which says: M.store(num, '+FLAGS', '\\Deleted') You have: m.store(emailid,"+FLAGS", r'(\Deleted)') Could it be that the flags should be r'\Deleted'? From rob at attoenterprises.com Mon Aug 26 20:18:37 2013 From: rob at attoenterprises.com (rob at attoenterprises.com) Date: Mon, 26 Aug 2013 17:18:37 -0700 (PDT) Subject: Imaplib & Gmail - cannot move from inbox to custom folder (message remains in both) In-Reply-To: References: Message-ID: On Monday, August 26, 2013 5:56:09 PM UTC-6, MRAB wrote: > On 27/08/2013 00:34, rob at attoenterprises.com wrote: > > > I am using Imaplib and connecting to a Gmail account. I am issuing commands to copy the email to a custom folder then issuing delete. After expunge message still remains in inbox and customer folder. > > > > > > # COPY EMAIL TO FOLDER > > > copyStatus = m.copy(emailid, 'CIRIMPORTED') > > > print 'id of message copied is: %s status: %s' % (emailid,copyStatus) > > > > > > # SET THE MESSAGE TO HAVE '\Deleted' FLAG (EXPUNGE WILL COMPLETE DELETE PROCESS) > > > storeStatus = m.store(emailid,"+FLAGS", r'(\Deleted)') > > > print 'id of message stored is: %s status: %s %s' % (emailid,storeStatus[0],storeStatus[1]) > > > #print '******' > > > > > > #DELETES ANY EMAIL MARKED \Deleted > > > m.expunge() > > > m.close() > > > m.logout() > > > > > > Does anyone have any imaplib code the works with Gmail to move from inbox to another folder? > > > > > There's an example in docs which says: > > > > M.store(num, '+FLAGS', '\\Deleted') > > > > You have: > > > > m.store(emailid,"+FLAGS", r'(\Deleted)') > > > > Could it be that the flags should be r'\Deleted'? I have tried it with the variations on the web of \\Deleted, r'\Deleted, and \Deleted. The Delete works because I see in the status 'Deleted'. However it does not remove from Inbox. From skip at pobox.com Mon Aug 26 21:48:03 2013 From: skip at pobox.com (Skip Montanaro) Date: Mon, 26 Aug 2013 20:48:03 -0500 Subject: Missing something on exception handling in Python 3 Message-ID: I have code in the pylockfile package that looks roughly like this (apologies for the indentation - I'm growing to dislike Gmail for typing code fragments): try: write_pid_to_lockfile(somefile) except OSError as exc: if conditions_i_can_handle: do_other_stuff... else: raise LockFailed("Failed to create %s" % self.path) Thinking I would just get the final exception (not the one I caught originally), I was surprised to see this output on my screen: >>> lock.acquire() Traceback (most recent call last): File "lockfile/pidlockfile.py", line 80, in acquire write_pid_to_pidfile(self.path) File "lockfile/pidlockfile.py", line 163, in write_pid_to_pidfile pidfile_fd = os.open(pidfile_path, open_flags, open_mode) OSError: [Errno 13] Permission denied: '/tmp/skip/lock' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 1, in File "lockfile/pidlockfile.py", line 94, in acquire raise LockFailed("failed to create %s" % self.path) lockfile.LockFailed: failed to create /tmp/skip/lock When I rung the same code with Python 2.7, I get the exception output I anticipate: >>> lock.acquire() Traceback (most recent call last): File "", line 1, in File "lockfile/pidlockfile.py", line 94, in acquire raise LockFailed("failed to create %s" % self.path) LockFailed: failed to create /tmp/skip/lock It appears exception handling changed in Python 3. How do I suppress the lower level OSError? Thx, Skip From python at mrabarnett.plus.com Mon Aug 26 22:13:54 2013 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 27 Aug 2013 03:13:54 +0100 Subject: Missing something on exception handling in Python 3 In-Reply-To: References: Message-ID: <521C0B62.1030004@mrabarnett.plus.com> On 27/08/2013 02:48, Skip Montanaro wrote: > I have code in the pylockfile package that looks roughly like this > (apologies for the indentation - I'm growing to dislike Gmail for > typing code fragments): > > try: > write_pid_to_lockfile(somefile) > except OSError as exc: > if conditions_i_can_handle: > do_other_stuff... > else: > raise LockFailed("Failed to create %s" % self.path) > > Thinking I would just get the final exception (not the one I caught > originally), I was surprised to see this output on my screen: > >>>> lock.acquire() > Traceback (most recent call last): > File "lockfile/pidlockfile.py", line 80, in acquire > write_pid_to_pidfile(self.path) > File "lockfile/pidlockfile.py", line 163, in write_pid_to_pidfile > pidfile_fd = os.open(pidfile_path, open_flags, open_mode) > OSError: [Errno 13] Permission denied: '/tmp/skip/lock' > > During handling of the above exception, another exception occurred: > > Traceback (most recent call last): > File "", line 1, in > File "lockfile/pidlockfile.py", line 94, in acquire > raise LockFailed("failed to create %s" % self.path) > lockfile.LockFailed: failed to create /tmp/skip/lock > > When I rung the same code with Python 2.7, I get the exception output > I anticipate: > >>>> lock.acquire() > Traceback (most recent call last): > File "", line 1, in > File "lockfile/pidlockfile.py", line 94, in acquire > raise LockFailed("failed to create %s" % self.path) > LockFailed: failed to create /tmp/skip/lock > > It appears exception handling changed in Python 3. How do I suppress > the lower level OSError? > Do this: raise LockFailed("Failed to create %s" % self.path) from None From skip.montanaro at gmail.com Mon Aug 26 22:49:39 2013 From: skip.montanaro at gmail.com (Skip Montanaro) Date: Mon, 26 Aug 2013 21:49:39 -0500 Subject: Missing something on exception handling in Python 3 In-Reply-To: <521C0B62.1030004@mrabarnett.plus.com> References: <521C0B62.1030004@mrabarnett.plus.com> Message-ID: > Do this: > > raise LockFailed("Failed to create %s" % self.path) from None Thanks. Is there some construct which will work in 2.x and 3.x? Skip From ethan at stoneleaf.us Mon Aug 26 23:16:00 2013 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 26 Aug 2013 20:16:00 -0700 Subject: Missing something on exception handling in Python 3 In-Reply-To: References: <521C0B62.1030004@mrabarnett.plus.com> Message-ID: <521C19F0.50605@stoneleaf.us> On 08/26/2013 07:49 PM, Skip Montanaro wrote: >> Do this: >> >> raise LockFailed("Failed to create %s" % self.path) from None > > Thanks. Is there some construct which will work in 2.x and 3.x? Something like this (untested): exc = None try: write_pid_to_lockfile(somefile) except OSError as exc: exc = sys.exc_info()[0] # not sure of index if exc: if conditions_i_can_handle: do_other_stuff... else: raise LockFailed("Failed to create %s" % self.path) Since you're raising outside the try...except block you won't see the chained exception in Python3. -- ~Ethan~ From skip at pobox.com Tue Aug 27 01:00:36 2013 From: skip at pobox.com (Skip Montanaro) Date: Tue, 27 Aug 2013 00:00:36 -0500 Subject: Missing something on exception handling in Python 3 In-Reply-To: <521C19F0.50605@stoneleaf.us> References: <521C0B62.1030004@mrabarnett.plus.com> <521C19F0.50605@stoneleaf.us> Message-ID: I found this question/answer on Stack Overflow: http://stackoverflow.com/questions/15123137 but after fiddling around with it, I can't find a solution that works for Python 3.2 and 3.3, let alone 2.x. In 3.2, exceptions have both __cause__ and __context__ attributes. I tried setting both to None (in 3.2), but I still get the full double traceback: >>> try: 1/0 ... except ZeroDivisionError: ... exc = TypeError() ... exc.__context__ = None ... exc.__cause__ = None ... raise exc ... Error in sys.excepthook: IndexError: tuple index out of range Original exception was: Traceback (most recent call last): File "", line 1, in ZeroDivisionError: division by zero During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 6, in TypeError The sys.excepthook error is because I have a custom interactive sys.excepthook, which is itself apparently broken in Python 3.2. I've looked at PEP 409 and 415, but just get more confused. *sigh* Maybe I'll try again tomorrow when I've had a bit of sleep... and take a closer look at Ethan's suggestion. Skip From steve at pearwood.info Tue Aug 27 05:22:42 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 27 Aug 2013 09:22:42 GMT Subject: Missing something on exception handling in Python 3 References: <521C0B62.1030004@mrabarnett.plus.com> <521C19F0.50605@stoneleaf.us> Message-ID: <521c6fe2$0$11100$c3e8da3@news.astraweb.com> On Tue, 27 Aug 2013 00:00:36 -0500, Skip Montanaro wrote: > I found this question/answer on Stack Overflow: > > http://stackoverflow.com/questions/15123137 > > but after fiddling around with it, I can't find a solution that works > for Python 3.2 and 3.3, let alone 2.x. In 3.2, exceptions have both > __cause__ and __context__ attributes. I tried setting both to None (in > 3.2), but I still get the full double traceback: Yes, "raise from None" only works in Python 3.3. My way of dealing with this is to use "raise from None" and just accept that 3.1 and 3.2 users will see double tracebacks :-( All the more reason to encourage people to go straight to 3.3 or better and skip 3.1 and 3.2 :-) -- Steven From autobotprime.17 at gmail.com Mon Aug 26 22:45:10 2013 From: autobotprime.17 at gmail.com (autobotprime.17 at gmail.com) Date: Mon, 26 Aug 2013 19:45:10 -0700 (PDT) Subject: TypeError: 'int' object is not callable Message-ID: dear friends when i try to execute following lines import time a = time.daylight() print(a) result is TypeError: 'int' object is not callable why is this error and how can i solve this problem? From i.am.songoku at gmail.com Mon Aug 26 23:23:08 2013 From: i.am.songoku at gmail.com (Krishnan Shankar) Date: Tue, 27 Aug 2013 08:53:08 +0530 Subject: TypeError: 'int' object is not callable In-Reply-To: References: Message-ID: Hi, The problem is in the second line. a = time.daylight() The daylight is not a method in time module. It is clear here, http://docs.python.org/2/library/time.html Since it is not a method we cannot call it. It is just a integer variable . It returns zero if DST timezone is not defined and returns non zero if defined. >>> import time >>> a = time.daylight() Traceback (most recent call last): File "", line 1, in TypeError: 'int' object is not callable >>> a = time.daylight >>> a 0 >>> type(time.daylight) Regards, Krishnan On Tue, Aug 27, 2013 at 8:15 AM, wrote: > dear friends when i try to execute following lines > > import time > a = time.daylight() > print(a) > > > result is > TypeError: 'int' object is not callable > > > why is this error and how can i solve this problem? > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Tue Aug 27 04:12:41 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 27 Aug 2013 04:12:41 -0400 Subject: TypeError: 'int' object is not callable In-Reply-To: References: Message-ID: On 8/26/2013 10:45 PM, autobotprime.17 at gmail.com wrote: > dear friends when i try to execute following lines > > import time > a = time.daylight() > print(a) > > > result is You left out the traceback that shows which line had the error. In this case, Krishnan could tell, but in other code snippets people post, it is not always so obvious. Post the whole trackeback with errors. It is printed for a reason ;-) > TypeError: 'int' object is not callable > > > why is this error and how can i solve this problem? > -- Terry Jan Reedy From tausciam at gmail.com Mon Aug 26 23:42:52 2013 From: tausciam at gmail.com (tausciam at gmail.com) Date: Mon, 26 Aug 2013 20:42:52 -0700 (PDT) Subject: Phonon error: libv4l2: error getting pixformat: Invalid argument Message-ID: <12e50d6e-d3a6-465e-9d56-3ad3507964f7@googlegroups.com> Here is my code. I'm just trying to play an mp3 that I've clicked in a PyQT listwidget: @pyqtSlot() def item_clicked(self): row = self.listWidget.currentRow() song = musiclist[row] QCoreApplication.setApplicationName("Phonon") output = Phonon.AudioOutput(Phonon.MusicCategory) m_media = Phonon.MediaObject() Phonon.createPath(m_media, output) m_media.setCurrentSource(Phonon.MediaSource(song)) m_media.play() I'm running OpenSUSE Linux 12.3 Any ideas? From tausciam at gmail.com Tue Aug 27 07:45:12 2013 From: tausciam at gmail.com (tausciam at gmail.com) Date: Tue, 27 Aug 2013 04:45:12 -0700 (PDT) Subject: Phonon error: libv4l2: error getting pixformat: Invalid argument In-Reply-To: <12e50d6e-d3a6-465e-9d56-3ad3507964f7@googlegroups.com> References: <12e50d6e-d3a6-465e-9d56-3ad3507964f7@googlegroups.com> Message-ID: Looking in /var/log/messages, every time I get that error I get: [41553.128652] xc2028 9-0061: i2c input error: rc = -19 (should be 2) [41553.152537] xc2028 9-0061: i2c input error: rc = -19 (should be 2) [41553.355913] xc2028 9-0061: i2c input error: rc = -19 (should be 2) [41553.379712] xc2028 9-0061: i2c input error: rc = -19 (should be 2) and that's my video capture card! That has nothing to do with playing an mp3 From rosuav at gmail.com Tue Aug 27 10:50:27 2013 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 28 Aug 2013 00:50:27 +1000 Subject: Phonon error: libv4l2: error getting pixformat: Invalid argument In-Reply-To: <12e50d6e-d3a6-465e-9d56-3ad3507964f7@googlegroups.com> References: <12e50d6e-d3a6-465e-9d56-3ad3507964f7@googlegroups.com> Message-ID: On Tue, Aug 27, 2013 at 1:42 PM, wrote: > Here is my code. I'm just trying to play an mp3 that I've clicked in a PyQT listwidget: > > @pyqtSlot() > def item_clicked(self): > row = self.listWidget.currentRow() > song = musiclist[row] > QCoreApplication.setApplicationName("Phonon") > output = Phonon.AudioOutput(Phonon.MusicCategory) > m_media = Phonon.MediaObject() > Phonon.createPath(m_media, output) > m_media.setCurrentSource(Phonon.MediaSource(song)) > m_media.play() I can't help with Phonon itself, but here's a general comment: When you have a problem like this, post the full exception traceback. Also, if you can, try to cut down the example to the point where you can post all the code, not just one method; if you make it so we can actually run the script, it's that much more likely we can help you. But mainly, the exception traceback - it's very helpful. Help us to help you! :) ChrisA From tausciam at gmail.com Tue Aug 27 19:17:25 2013 From: tausciam at gmail.com (tausciam at gmail.com) Date: Tue, 27 Aug 2013 16:17:25 -0700 (PDT) Subject: Phonon error: libv4l2: error getting pixformat: Invalid argument In-Reply-To: References: <12e50d6e-d3a6-465e-9d56-3ad3507964f7@googlegroups.com> Message-ID: It's not giving me an exception. Here is the code I used: from PyQt4.QtCore import * from PyQt4.QtGui import * from PyQt4.phonon import Phonon import os import sys, traceback def lumberjack(): song = '/home/tannhaus/Music/A Perfect Circle/eMOTIVE/02 Imagine.mp3' QCoreApplication.setApplicationName("Phonon") output = Phonon.AudioOutput(Phonon.MusicCategory) m_media = Phonon.MediaObject() Phonon.createPath(m_media, output) m_media.setCurrentSource(Phonon.MediaSource(song)) m_media.play() class MainWindow(QMainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) try: lumberjack() except IndexError: exc_type, exc_value, exc_traceback = sys.exc_info() print "*** print_tb:" traceback.print_tb(exc_traceback, limit=1, file=sys.stdout) print "*** print_exception:" traceback.print_exception(exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout) print "*** print_exc:" traceback.print_exc() print "*** format_exc, first and last line:" formatted_lines = traceback.format_exc().splitlines() print formatted_lines[0] print formatted_lines[-1] print "*** format_exception:" print repr(traceback.format_exception(exc_type, exc_value, exc_traceback)) print "*** extract_tb:" print repr(traceback.extract_tb(exc_traceback)) print "*** format_tb:" print repr(traceback.format_tb(exc_traceback)) print "*** tb_lineno:", exc_traceback.tb_lineno if __name__=="__main__": from sys import argv, exit a=QApplication(argv) m=MainWindow() m.show() m.raise_() exit(a.exec_()) When I run it, the complete and only error I get is: libv4l2: error getting pixformat: Invalid argument When I check /var/log/messages, I see these messages: 2013-08-27T18:12:04.163062-05:00 tannhaus-PC kernel: [ 1786.397499] xc2028 9-0061: i2c input error: rc = -19 (should be 2) 2013-08-27T18:12:04.187054-05:00 tannhaus-PC kernel: [ 1786.421479] xc2028 9-0061: i2c input error: rc = -19 (should be 2) 2013-08-27T18:12:04.391057-05:00 tannhaus-PC kernel: [ 1786.625614] xc2028 9-0061: i2c input error: rc = -19 (should be 2) 2013-08-27T18:12:04.415052-05:00 tannhaus-PC kernel: [ 1786.649613] xc2028 9-0061: i2c input error: rc = -19 (should be 2) From tausciam at gmail.com Tue Aug 27 19:26:39 2013 From: tausciam at gmail.com (tausciam at gmail.com) Date: Tue, 27 Aug 2013 16:26:39 -0700 (PDT) Subject: Phonon error: libv4l2: error getting pixformat: Invalid argument In-Reply-To: References: <12e50d6e-d3a6-465e-9d56-3ad3507964f7@googlegroups.com> Message-ID: <15fbb714-ebe8-4b6b-9a96-202faa5b1dd6@googlegroups.com> I unplugged the tv capture card and got no errors at all. It didn't cause it to crash when I had it plugged in. However, it appears that it's not actually playing the mp3. I don't hear it at all. I checked my sound mixer and no channels are muted. From tausciam at gmail.com Tue Aug 27 19:39:36 2013 From: tausciam at gmail.com (tausciam at gmail.com) Date: Tue, 27 Aug 2013 16:39:36 -0700 (PDT) Subject: Phonon error: libv4l2: error getting pixformat: Invalid argument In-Reply-To: <15fbb714-ebe8-4b6b-9a96-202faa5b1dd6@googlegroups.com> References: <12e50d6e-d3a6-465e-9d56-3ad3507964f7@googlegroups.com> <15fbb714-ebe8-4b6b-9a96-202faa5b1dd6@googlegroups.com> Message-ID: <99708acd-a544-4dbf-afc1-55bf16a4571b@googlegroups.com> But, the PYQT example works: https://github.com/Werkov/PyQt4/blob/master/examples/phonon/musicplayer.py#L1 It's just my code isn't working for some reason. I don't hear anything coming from it From tausciam at gmail.com Tue Aug 27 20:42:19 2013 From: tausciam at gmail.com (tausciam at gmail.com) Date: Tue, 27 Aug 2013 17:42:19 -0700 (PDT) Subject: Phonon error: libv4l2: error getting pixformat: Invalid argument In-Reply-To: <99708acd-a544-4dbf-afc1-55bf16a4571b@googlegroups.com> References: <12e50d6e-d3a6-465e-9d56-3ad3507964f7@googlegroups.com> <15fbb714-ebe8-4b6b-9a96-202faa5b1dd6@googlegroups.com> <99708acd-a544-4dbf-afc1-55bf16a4571b@googlegroups.com> Message-ID: <527b0398-57ce-49c5-96c9-b3e1efed78cb@googlegroups.com> When I pare down the code to the following, I can't hear the mp3 play either: #!/usr/bin/env python import sip sip.setapi('QString', 2) import sys from PyQt4 import QtCore, QtGui from PyQt4.phonon import Phonon class MainWindow(QtGui.QMainWindow): def __init__(self): super(QtGui.QMainWindow, self).__init__() self.audioOutput = Phonon.AudioOutput(Phonon.MusicCategory, self) self.mediaObject = Phonon.MediaObject(self) self.metaInformationResolver = Phonon.MediaObject(self) Phonon.createPath(self.mediaObject, self.audioOutput) self.sources = "/home/tannhaus/Music/A Perfect Circle/eMOTIVE/02 Imagine.mp3" self.metaInformationResolver.setCurrentSource(Phonon.MediaSource(self.sources)) self.mediaObject.play() if __name__ == '__main__': app = QtGui.QApplication(sys.argv) app.setApplicationName("Music Player") app.setQuitOnLastWindowClosed(True) window = MainWindow() window.show() sys.exit(app.exec_()) From tausciam at gmail.com Tue Aug 27 20:56:44 2013 From: tausciam at gmail.com (tausciam at gmail.com) Date: Tue, 27 Aug 2013 17:56:44 -0700 (PDT) Subject: Phonon error: libv4l2: error getting pixformat: Invalid argument In-Reply-To: <527b0398-57ce-49c5-96c9-b3e1efed78cb@googlegroups.com> References: <12e50d6e-d3a6-465e-9d56-3ad3507964f7@googlegroups.com> <15fbb714-ebe8-4b6b-9a96-202faa5b1dd6@googlegroups.com> <99708acd-a544-4dbf-afc1-55bf16a4571b@googlegroups.com> <527b0398-57ce-49c5-96c9-b3e1efed78cb@googlegroups.com> Message-ID: <1c42ff1c-348b-46f0-9d0e-5cfa0fb8d4a6@googlegroups.com> I've played around with it and got code that plays mp3s now. I'm not sure what I was doing wrong.... or why it plays now...but it does, so I'm going to use it: #!/usr/bin/env python import sip sip.setapi('QString', 2) import sys from PyQt4 import QtCore, QtGui from PyQt4.phonon import Phonon class MainWindow(QtGui.QMainWindow): def __init__(self): super(QtGui.QMainWindow, self).__init__() self.sources = "/home/tannhaus/Music/A Perfect Circle/eMOTIVE/02 Imagine.mp3" self.mediaObject = Phonon.createPlayer(Phonon.MusicCategory) self.audioOutput = Phonon.AudioOutput(Phonon.MusicCategory, self) Phonon.createPath(self.mediaObject, self.audioOutput) self.mediaObject.setCurrentSource(Phonon.MediaSource(self.sources)) self.mediaObject.play() if __name__ == '__main__': app = QtGui.QApplication(sys.argv) app.setApplicationName("Music Player") app.setQuitOnLastWindowClosed(True) window = MainWindow() window.show() sys.exit(app.exec_()) From nikos at superhost.gr Tue Aug 27 03:13:04 2013 From: nikos at superhost.gr (=?UTF-8?B?zp3Ouc66z4zOu86xzr/Pgg==?=) Date: Tue, 27 Aug 2013 10:13:04 +0300 Subject: New VPS Provider needed Message-ID: I know this isn't the place to ask but since most of you are web programmers also, you possibly do hosting too. Problem is also that i made the mistake of paying my current provider up until 5/5/2014 I had nothing but trouble with them and they wont refund back. I lost 4 customers, good websites due to their inability. I can even provide proof for what i paid and for the period iam writing 5/5/14 So, no refunds back and 4 customers left me these days because of constant issues with them. So, if someone want to help me out it would be nice if he could provide me with services free of charge for that time period, or at least at the end of they year. Please someone help me out to this and then i will be his devoted customer. This is my need: 1. I will pay for a full year upfront but it must be a low price for the 1st year, because i have already paid them and cant get my money back to use it to the new com opany. 2. cPanel/WHM && Softaculous Licenses must be moved from them to the new VPS provider 3. Tranfer of the current 6 websites free of charge and assure that they work properly after transfers(they are joomlas and 2 dreamweavers) 4. Some help over the road when i need somthign to be installed like a new python version and a couple of modules. -- What is now proved was at first only imagined! From rosuav at gmail.com Tue Aug 27 03:17:48 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 27 Aug 2013 17:17:48 +1000 Subject: New VPS Provider needed In-Reply-To: References: Message-ID: On Tue, Aug 27, 2013 at 5:13 PM, ???????? wrote: > So, if someone want to help me out it would be nice if he could provide me > with services free of charge for that time period, or at least at the end of > they year. Are we allowed to go peeking at all your customers' private/personal data again? ChrisA From rosuav at gmail.com Tue Aug 27 04:12:09 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 27 Aug 2013 18:12:09 +1000 Subject: New VPS Provider needed In-Reply-To: References: Message-ID: On Tue, Aug 27, 2013 at 5:17 PM, Chris Angelico wrote: > On Tue, Aug 27, 2013 at 5:13 PM, ???????? wrote: >> So, if someone want to help me out it would be nice if he could provide me >> with services free of charge for that time period, or at least at the end of >> they year. > > Are we allowed to go peeking at all your customers' private/personal data again? > > ChrisA And once again Nikos demonstrates a significant lack of comprehension of trust. In private messages to me he implies that I am joking in the above; while this is partly true, it is also a completely serious comment. ChrisA From nikos at superhost.gr Tue Aug 27 04:45:10 2013 From: nikos at superhost.gr (=?UTF-8?B?zp3Ouc66z4zOu86xzr/Pgg==?=) Date: Tue, 27 Aug 2013 11:45:10 +0300 Subject: New VPS Provider needed In-Reply-To: References: Message-ID: ???? 27/8/2013 11:12 ??, ?/? Chris Angelico ??????: > On Tue, Aug 27, 2013 at 5:17 PM, Chris Angelico wrote: >> On Tue, Aug 27, 2013 at 5:13 PM, ???????? wrote: >>> So, if someone want to help me out it would be nice if he could provide me >>> with services free of charge for that time period, or at least at the end of >>> they year. >> >> Are we allowed to go peeking at all your customers' private/personal data again? >> >> ChrisA > > And once again Nikos demonstrates a significant lack of comprehension > of trust. In private messages to me he implies that I am joking in the > above; while this is partly true, it is also a completely serious > comment. > > ChrisA > Iam having major issues with my VPS provider and losign customers becaue the provider doesnt set thign u[ cprrectly. The last thing i need is Chris Angelico making fun out of this So, knock it off. -- What is now proved was at first only imagined! From timothy.c.delaney at gmail.com Tue Aug 27 05:06:58 2013 From: timothy.c.delaney at gmail.com (Tim Delaney) Date: Tue, 27 Aug 2013 19:06:58 +1000 Subject: New VPS Provider needed In-Reply-To: References: Message-ID: On 27 August 2013 18:45, ???????? wrote: > > Iam having major issues with my VPS provider and losign customers becaue > the provider doesnt set thign u[ cprrectly. > Given your posting history in this newsgroup/mailing list, I wouldn't be so sure that the problem is on your VPS provider's end. Tim Delaney -------------- next part -------------- An HTML attachment was scrubbed... URL: From nikos at superhost.gr Tue Aug 27 05:32:17 2013 From: nikos at superhost.gr (=?UTF-8?B?zp3Ouc66z4zOu86xzr/Pgg==?=) Date: Tue, 27 Aug 2013 12:32:17 +0300 Subject: New VPS Provider needed In-Reply-To: References: Message-ID: ???? 27/8/2013 12:06 ??, ?/? Tim Delaney ??????: > On 27 August 2013 18:45, ???????? > wrote: > > > Iam having major issues with my VPS provider and losign customers > becaue the provider doesnt set thign u[ cprrectly. > > Given your posting history in this newsgroup/mailing list, I wouldn't be > so sure that the problem is on your VPS provider's end. > > Tim Delaney It is not a python issue here, it is wrong VPS setup by them, mails fail to work, quota issues, cannot migrate webistes from preblematic VPS to new VPS check for example varsa.gr how it lloks after their migration. -- What is now proved was at first only imagined! From rosuav at gmail.com Tue Aug 27 05:38:45 2013 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 27 Aug 2013 19:38:45 +1000 Subject: New VPS Provider needed In-Reply-To: References: Message-ID: On Tue, Aug 27, 2013 at 7:32 PM, ???????? wrote: > ???? 27/8/2013 12:06 ??, ?/? Tim Delaney ??????: >> >> On 27 August 2013 18:45, ???????? > > wrote: >> >> >> Iam having major issues with my VPS provider and losign customers >> becaue the provider doesnt set thign u[ cprrectly. >> >> Given your posting history in this newsgroup/mailing list, I wouldn't be >> so sure that the problem is on your VPS provider's end. >> >> Tim Delaney > > > It is not a python issue here, it is wrong VPS setup by them, mails fail to > work, quota issues, cannot migrate webistes from preblematic VPS to new VPS > > check for example > > varsa.gr how it lloks after their migration. So... 1) Your host makes a change 2) You have no clue what that change has done 2a) Even though anyone who knows a bit of PHP (and I have to confess that includes me) will be able to tell at a glance what's going on, from that page. 3) You blame your host. 4) You ask python-list for some free web hosting 5) You then do not understand that, if one of us offers his server to you, there is a massive trust issue. 6) You have no clue. Deny that if you will! And deny also that you are the SHROPSHIRE SLASHER! ChrisA From joel.goldstick at gmail.com Tue Aug 27 05:50:37 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 27 Aug 2013 05:50:37 -0400 Subject: New VPS Provider needed In-Reply-To: References: Message-ID: On Tue, Aug 27, 2013 at 5:38 AM, Chris Angelico wrote: > On Tue, Aug 27, 2013 at 7:32 PM, ???????? wrote: >> ???? 27/8/2013 12:06 ??, ?/? Tim Delaney ??????: >>> >>> On 27 August 2013 18:45, ???????? >> > wrote: >>> >>> >>> Iam having major issues with my VPS provider and losign customers >>> becaue the provider doesnt set thign u[ cprrectly. >>> >>> Given your posting history in this newsgroup/mailing list, I wouldn't be >>> so sure that the problem is on your VPS provider's end. >>> >>> Tim Delaney >> >> >> It is not a python issue here, it is wrong VPS setup by them, mails fail to >> work, quota issues, cannot migrate webistes from preblematic VPS to new VPS >> >> check for example >> >> varsa.gr how it lloks after their migration. > > So... > > 1) Your host makes a change > 2) You have no clue what that change has done > 2a) Even though anyone who knows a bit of PHP (and I have to confess > that includes me) will be able to tell at a glance what's going on, > from that page. > 3) You blame your host. > 4) You ask python-list for some free web hosting > 5) You then do not understand that, if one of us offers his server to > you, there is a massive trust issue. > 6) You have no clue. > > Deny that if you will! And deny also that you are the SHROPSHIRE SLASHER! > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list Michael Corleone: Just when I thought I was out... they pull me back in. -- Joel Goldstick http://joelgoldstick.com From nikos at superhost.gr Tue Aug 27 06:00:47 2013 From: nikos at superhost.gr (=?UTF-8?B?zp3Ouc66z4zOu86xzr/Pgg==?=) Date: Tue, 27 Aug 2013 13:00:47 +0300 Subject: New VPS Provider needed In-Reply-To: References: Message-ID: ???? 27/8/2013 12:38 ??, ?/? Chris Angelico ??????: > On Tue, Aug 27, 2013 at 7:32 PM, ???????? wrote: >> ???? 27/8/2013 12:06 ??, ?/? Tim Delaney ??????: >>> >>> On 27 August 2013 18:45, ???????? >> > wrote: >>> >>> >>> Iam having major issues with my VPS provider and losign customers >>> becaue the provider doesnt set thign u[ cprrectly. >>> >>> Given your posting history in this newsgroup/mailing list, I wouldn't be >>> so sure that the problem is on your VPS provider's end. >>> >>> Tim Delaney >> >> >> It is not a python issue here, it is wrong VPS setup by them, mails fail to >> work, quota issues, cannot migrate webistes from preblematic VPS to new VPS >> >> check for example >> >> varsa.gr how it lloks after their migration. > > So... > > 1) Your host makes a change > 2) You have no clue what that change has done > 2a) Even though anyone who knows a bit of PHP (and I have to confess > that includes me) will be able to tell at a glance what's going on, > from that page. > 3) You blame your host. > 4) You ask python-list for some free web hosting > 5) You then do not understand that, if one of us offers his server to > you, there is a massive trust issue. > 6) You have no clue. > > Deny that if you will! And deny also that you are the SHROPSHIRE SLASHER! > > ChrisA > When the VPS Provider makes a change and move me form an old vPD to a new one they should assure all work as expected. They dont bother ven replying. ven my mail dont owrk as expected. And the task for handlign php or altering complex linux configurations is up to them not to me that why i pay pay for a managed VPS. They cannot ven install 'pymysql' and 'pyfeoip' properly so superhost.gr will run as intended and not give internal servers errors let alone the faulty migrtaion of joomla's varsa.gr and other weird php errors. Will you continue to blame me or propose an alternative? -- What is now proved was at first only imagined! From antoon.pardon at rece.vub.ac.be Tue Aug 27 06:26:31 2013 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Tue, 27 Aug 2013 12:26:31 +0200 Subject: New VPS Provider needed In-Reply-To: References: Message-ID: <521C7ED7.9030902@rece.vub.ac.be> Op 27-08-13 12:00, ???????? schreef: > ???? 27/8/2013 12:38 ??, ?/? Chris Angelico ??????: >> On Tue, Aug 27, 2013 at 7:32 PM, ???????? wrote: >>> ???? 27/8/2013 12:06 ??, ?/? Tim Delaney ??????: >>>> >>>> On 27 August 2013 18:45, ???????? >>> > wrote: >>>> >>>> >>>> Iam having major issues with my VPS provider and losign customers >>>> becaue the provider doesnt set thign u[ cprrectly. >>>> >>>> Given your posting history in this newsgroup/mailing list, I >>>> wouldn't be >>>> so sure that the problem is on your VPS provider's end. >>>> >>>> Tim Delaney >>> >>> >>> It is not a python issue here, it is wrong VPS setup by them, mails >>> fail to >>> work, quota issues, cannot migrate webistes from preblematic VPS to >>> new VPS >>> >>> check for example >>> >>> varsa.gr how it lloks after their migration. >> >> So... >> >> 1) Your host makes a change >> 2) You have no clue what that change has done >> 2a) Even though anyone who knows a bit of PHP (and I have to confess >> that includes me) will be able to tell at a glance what's going on, >> from that page. >> 3) You blame your host. >> 4) You ask python-list for some free web hosting >> 5) You then do not understand that, if one of us offers his server to >> you, there is a massive trust issue. >> 6) You have no clue. >> >> Deny that if you will! And deny also that you are the SHROPSHIRE SLASHER! >> >> ChrisA >> > > When the VPS Provider makes a change and move me form an old vPD to a > new one they should assure all work as expected. Maybe it does. It is possible your website depended on undocumented features and that one of those features is no longer available. So your website works as expected. > They dont bother ven > replying. ven my mail dont owrk as expected. > And the task for handlign php or altering complex linux configurations > is up to them not to me that why i pay pay for a managed VPS. > They cannot ven install 'pymysql' and 'pyfeoip' properly so superhost.gr > will run as intended and not give internal servers errors let alone the > faulty migrtaion of joomla's varsa.gr and other weird php errors. > > Will you continue to blame me or propose an alternative? > From nikos at superhost.gr Tue Aug 27 06:37:42 2013 From: nikos at superhost.gr (=?UTF-8?B?zp3Ouc66z4zOu86xzr/Pgg==?=) Date: Tue, 27 Aug 2013 13:37:42 +0300 Subject: New VPS Provider needed In-Reply-To: References: Message-ID: ???? 27/8/2013 1:26 ??, ?/? Antoon Pardon ??????: > Maybe it does. It is possible your website depended on undocumented > features and that one of those features is no longer available. So > your website works as expected. This is not the case here. No fancy modules. in the old VPS varsa.gr was working well. They just had other issues there. Sicne it was working in the old VPS they should migrate and configure appropriately the new VPS top work well there too. -- What is now proved was at first only imagined! From invalid at invalid.invalid Tue Aug 27 10:25:55 2013 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 27 Aug 2013 14:25:55 +0000 (UTC) Subject: New VPS Provider needed References: Message-ID: On 2013-08-27, ???????????????? wrote: > Iam having major issues with my VPS provider and losign customers becaue > the provider doesnt set thign u[ cprrectly. Perhaps he's also having problems with a faulty keyboard. -- Grant Edwards grant.b.edwards Yow! They collapsed at ... like nuns in the gmail.com street ... they had no teen appeal! From rosuav at gmail.com Tue Aug 27 10:41:17 2013 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 28 Aug 2013 00:41:17 +1000 Subject: New VPS Provider needed In-Reply-To: References: Message-ID: On Wed, Aug 28, 2013 at 12:25 AM, Grant Edwards wrote: > On 2013-08-27, ???????????????? wrote: > >> Iam having major issues with my VPS provider and losign customers becaue >> the provider doesnt set thign u[ cprrectly. > > Perhaps he's also having problems with a faulty keyboard. Just a faulty key actuator, I think. Nothing unusual in this world. ChrisA From nikos at superhost.gr Tue Aug 27 11:13:16 2013 From: nikos at superhost.gr (Ferrous Cranus) Date: Tue, 27 Aug 2013 18:13:16 +0300 Subject: New VPS Provider needed In-Reply-To: References: Message-ID: ???? 27/8/2013 5:25 ??, ?/? Grant Edwards ??????: > On 2013-08-27, ???????????????? wrote: > >> Iam having major issues with my VPS provider and losign customers becaue >> the provider doesnt set thign u[ cprrectly. > > Perhaps he's also having problems with a faulty keyboard. > Perhaps.... Then perhaps its just you faulty head understanding my typing. -- What is now proved was at first only imagined! From steve+comp.lang.python at pearwood.info Tue Aug 27 12:18:57 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 27 Aug 2013 16:18:57 GMT Subject: New VPS Provider needed References: Message-ID: <521cd171$0$29986$c3e8da3$5496439d@news.astraweb.com> On Wed, 28 Aug 2013 00:41:17 +1000, Chris Angelico wrote: > On Wed, Aug 28, 2013 at 12:25 AM, Grant Edwards > wrote: >> On 2013-08-27, ???????????????? wrote: >> >>> Iam having major issues with my VPS provider and losign customers >>> becaue the provider doesnt set thign u[ cprrectly. >> >> Perhaps he's also having problems with a faulty keyboard. > > Just a faulty key actuator, I think. Nothing unusual in this world. Guys, PLEASE stop baiting Nikos with snide remarks. It makes this a very unpleasant environment, and sets the tone of the community, badly. -- Steven From neilc at norwich.edu Tue Aug 27 14:46:50 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 27 Aug 2013 18:46:50 GMT Subject: New VPS Provider needed References: <521cd171$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2013-08-27, Steven D'Aprano wrote: > On Wed, 28 Aug 2013 00:41:17 +1000, Chris Angelico wrote: > >> On Wed, Aug 28, 2013 at 12:25 AM, Grant Edwards >> wrote: >>> On 2013-08-27, ???????????????? wrote: >>> >>>> Iam having major issues with my VPS provider and losign customers >>>> becaue the provider doesnt set thign u[ cprrectly. >>> >>> Perhaps he's also having problems with a faulty keyboard. >> >> Just a faulty key actuator, I think. Nothing unusual in this world. > > Guys, PLEASE stop baiting Nikos with snide remarks. It makes > this a very unpleasant environment, and sets the tone of the > community, badly. I limit myself to one snide remark every time he escapes my killfile. 'Cause I wish he would stop. -- Neil Cerutti From gheskett at wdtv.com Tue Aug 27 15:27:15 2013 From: gheskett at wdtv.com (Gene Heskett) Date: Tue, 27 Aug 2013 15:27:15 -0400 Subject: New VPS Provider needed In-Reply-To: References: <521cd171$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: <201308271527.16065.gheskett@wdtv.com> On Tuesday 27 August 2013 15:26:30 Neil Cerutti did opine: > On 2013-08-27, Steven D'Aprano wrote: > > On Wed, 28 Aug 2013 00:41:17 +1000, Chris Angelico wrote: > >> On Wed, Aug 28, 2013 at 12:25 AM, Grant Edwards > >> > >> wrote: > >>> On 2013-08-27, ???????????????? wrote: > >>>> Iam having major issues with my VPS provider and losign customers > >>>> becaue the provider doesnt set thign u[ cprrectly. > >>> > >>> Perhaps he's also having problems with a faulty keyboard. > >> > >> Just a faulty key actuator, I think. Nothing unusual in this world. > > > > Guys, PLEASE stop baiting Nikos with snide remarks. It makes > > this a very unpleasant environment, and sets the tone of the > > community, badly. > > I limit myself to one snide remark every time he escapes my > killfile. 'Cause I wish he would stop. He hasn't escaped from mine yet, but all this backsplatter is unbecoming. Cheers, Gene -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author) My web page: is up! My views Of ______course it's the murder weapon. Who would frame someone with a fake? A pen in the hand of this president is far more dangerous than 200 million guns in the hands of law-abiding citizens. From antoon.pardon at rece.vub.ac.be Wed Aug 28 03:19:40 2013 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Wed, 28 Aug 2013 09:19:40 +0200 Subject: New VPS Provider needed In-Reply-To: <521cd171$0$29986$c3e8da3$5496439d@news.astraweb.com> References: <521cd171$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: <521DA48C.6070108@rece.vub.ac.be> Op 27-08-13 18:18, Steven D'Aprano schreef: > On Wed, 28 Aug 2013 00:41:17 +1000, Chris Angelico wrote: > >> On Wed, Aug 28, 2013 at 12:25 AM, Grant Edwards >> wrote: >>> On 2013-08-27, ???????????????? wrote: >>> >>>> Iam having major issues with my VPS provider and losign customers >>>> becaue the provider doesnt set thign u[ cprrectly. >>> >>> Perhaps he's also having problems with a faulty keyboard. >> >> Just a faulty key actuator, I think. Nothing unusual in this world. > > > Guys, PLEASE stop baiting Nikos with snide remarks. It makes this a very > unpleasant environment, and sets the tone of the community, badly. Why don't you tell Nikos he should stop baiting the list with off topic requestsm like whether someone is willing to provide him free hosting? I also think you are over reacting. One thread where people come with snide remarks doesn't turn the whole newsgroup/mailinglist into an unpleasant environment. -- Antoon Pardon From steve+comp.lang.python at pearwood.info Wed Aug 28 07:25:18 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 Aug 2013 11:25:18 GMT Subject: New VPS Provider needed References: <521cd171$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: <521dde1e$0$6599$c3e8da3$5496439d@news.astraweb.com> On Wed, 28 Aug 2013 09:19:40 +0200, Antoon Pardon wrote: > Op 27-08-13 18:18, Steven D'Aprano schreef: >> On Wed, 28 Aug 2013 00:41:17 +1000, Chris Angelico wrote: >> >>> On Wed, Aug 28, 2013 at 12:25 AM, Grant Edwards >>> wrote: >>>> On 2013-08-27, ???????????????? wrote: >>>> >>>>> Iam having major issues with my VPS provider and losign customers >>>>> becaue the provider doesnt set thign u[ cprrectly. >>>> >>>> Perhaps he's also having problems with a faulty keyboard. >>> >>> Just a faulty key actuator, I think. Nothing unusual in this world. >> >> >> Guys, PLEASE stop baiting Nikos with snide remarks. It makes this a >> very unpleasant environment, and sets the tone of the community, badly. > > Why don't you tell Nikos he should stop baiting the list with off topic > requestsm like whether someone is willing to provide him free hosting? I quote: 1. I will pay for a full year upfront but it must be a low price for the 1st year, because i have already paid them and cant get my money back to use it to the new com opany. I get it. You don't like Nikos. I'm not suggesting you should. But misrepresenting him is Not Cool. He has offered to pay. Besides, Python-friendly web hosting is not off-topic. Plenty of people have asked for recommendations on hosting providers that support Python before. > I also think you are over reacting. One thread where people come with > snide remarks doesn't turn the whole newsgroup/mailinglist into an > unpleasant environment. My wife often tells me it is a miracle that I don't forget my own name, but even my memory is not so bad as to forget just how unpleasant and hostile this list became a month or so ago when a whole bunch of people decided to drive Nikos away, and he decided to fight back. -- Steven From antoon.pardon at rece.vub.ac.be Wed Aug 28 08:37:13 2013 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Wed, 28 Aug 2013 14:37:13 +0200 Subject: New VPS Provider needed In-Reply-To: <521dde1e$0$6599$c3e8da3$5496439d@news.astraweb.com> References: <521cd171$0$29986$c3e8da3$5496439d@news.astraweb.com> <521dde1e$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <521DEEF9.3050201@rece.vub.ac.be> Op 28-08-13 13:25, Steven D'Aprano schreef: > On Wed, 28 Aug 2013 09:19:40 +0200, Antoon Pardon wrote: > >> Op 27-08-13 18:18, Steven D'Aprano schreef: >>> On Wed, 28 Aug 2013 00:41:17 +1000, Chris Angelico wrote: >>> >>>> On Wed, Aug 28, 2013 at 12:25 AM, Grant Edwards >>>> wrote: >>>>> On 2013-08-27, ???????????????? wrote: >>>>> >>>>>> Iam having major issues with my VPS provider and losign customers >>>>>> becaue the provider doesnt set thign u[ cprrectly. >>>>> >>>>> Perhaps he's also having problems with a faulty keyboard. >>>> >>>> Just a faulty key actuator, I think. Nothing unusual in this world. >>> >>> >>> Guys, PLEASE stop baiting Nikos with snide remarks. It makes this a >>> very unpleasant environment, and sets the tone of the community, badly. >> >> Why don't you tell Nikos he should stop baiting the list with off topic >> requestsm like whether someone is willing to provide him free hosting? > > I quote: > > 1. I will pay for a full year upfront but it must be a low price > for the 1st year, because i have already paid them and cant get my > money back to use it to the new com opany. You are quoting selectively. > I get it. You don't like Nikos. I'm not suggesting you should. But > misrepresenting him is Not Cool. He has offered to pay. IMO you are misrepresenting by quoting selectively. This from earlier in that same post: | So, if someone want to help me out it would be nice if he could | provide me with services free of charge for that time period, or at | least at the end of they year. That he offered to pay, doesn't contradict his primary request is for services free of charge. > Besides, Python-friendly web hosting is not off-topic. Plenty of people > have asked for recommendations on hosting providers that support Python > before. He didn't ask for people experiences with various hosting providers. He was asking people here to provide hosting services for him. >> I also think you are over reacting. One thread where people come with >> snide remarks doesn't turn the whole newsgroup/mailinglist into an >> unpleasant environment. > > My wife often tells me it is a miracle that I don't forget my own name, > but even my memory is not so bad as to forget just how unpleasant and > hostile this list became a month or so ago when a whole bunch of people > decided to drive Nikos away, and he decided to fight back. This list didn't became unpleasand and hostile. The hostility was contained to a few threads. It was easy enough to avoid the hostility if that was important enough to you. I also find your reporting on the facts misleading. People didn't just decide to drive Nikos away. People were getting fed up with the ongoing annoying behaviour of Nikos. It wasn't Nikos who decided to fight back, it were the people who had enough of Nikos's inconsiderate behaviour who decided to fight back. -- Antoon Pardon From bouncingcats at gmail.com Tue Aug 27 07:18:10 2013 From: bouncingcats at gmail.com (David) Date: Tue, 27 Aug 2013 21:18:10 +1000 Subject: New VPS Provider needed In-Reply-To: References: Message-ID: On 27 August 2013 17:13, ???????? wrote: > > I know this isn't the place to ask ???????? is 100% correct about this. So, this list is also not the correct place to answer. So please everyone, do not respond. Thanks. From nikos at superhost.gr Tue Aug 27 07:21:33 2013 From: nikos at superhost.gr (=?UTF-8?B?zp3Ouc66z4zOu86xzr/Pgg==?=) Date: Tue, 27 Aug 2013 14:21:33 +0300 Subject: New VPS Provider needed In-Reply-To: References: Message-ID: ???? 27/8/2013 2:18 ??, ?/? David ??????: > On 27 August 2013 17:13, ???????? wrote: >> >> I know this isn't the place to ask > > ???????? is 100% correct about this. > > So, this list is also not the correct place to answer. > So please everyone, do not respond. Thanks. > Actually it is. Poeple are web programmers here and lots of them doing hosting. You dont like my questions, block me, but just stop answering unhelpfully. -- What is now proved was at first only imagined! From kwpolska at gmail.com Tue Aug 27 07:43:14 2013 From: kwpolska at gmail.com (=?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?=) Date: Tue, 27 Aug 2013 13:43:14 +0200 Subject: New VPS Provider needed In-Reply-To: References: Message-ID: On Tue, Aug 27, 2013 at 1:21 PM, ???????? wrote: > Actually it is. > Poeple are web programmers here and lots of them doing hosting. Fortunately, most of them would not trust you. And in case they would, a quick search through what happened on this list a while ago should fix that. -- Chris ?Kwpolska? Warrick PGP: 5EAAEA16 stop html mail | always bottom-post | only UTF-8 makes sense From antoon.pardon at rece.vub.ac.be Tue Aug 27 07:39:01 2013 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Tue, 27 Aug 2013 13:39:01 +0200 Subject: New VPS Provider needed In-Reply-To: References: Message-ID: <521C8FD5.5020804@rece.vub.ac.be> Op 27-08-13 13:21, ???????? schreef: > ???? 27/8/2013 2:18 ??, ?/? David ??????: >> On 27 August 2013 17:13, ???????? wrote: >>> >>> I know this isn't the place to ask >> >> ???????? is 100% correct about this. >> >> So, this list is also not the correct place to answer. >> So please everyone, do not respond. Thanks. >> > > Actually it is. > Poeple are web programmers here and lots of them doing hosting. Which is irrelevant. People are cat owners too. That doesn't make this list the correct place to ask and get answers about cats. > You dont like my questions, block me, but just stop answering unhelpfully. You are not in a position to demand anything from people here. -- Antoon Pardon From nikos at superhost.gr Tue Aug 27 08:47:40 2013 From: nikos at superhost.gr (=?UTF-8?B?zp3Ouc66z4zOu86xzr/Pgg==?=) Date: Tue, 27 Aug 2013 15:47:40 +0300 Subject: New VPS Provider needed In-Reply-To: References: Message-ID: ???? 27/8/2013 3:25 ??, ?/? Dennis Lee Bieber ??????: > Your provider made some change and suddenly only YOUR sites have > failed? No one else is complaining to the provider? > > That would seem to indicate that the fault is in your coding -- perhaps > making use of undocumented features or were coded to work around some buggy > implementation -- and in the new system the bug is now gone, but your code > is still trying to use it. How come then they were working in the old VPS in the same company and in the new VPS implementation do not? -- What is now proved was at first only imagined! From python.list at tim.thechases.com Tue Aug 27 08:55:04 2013 From: python.list at tim.thechases.com (Tim Chase) Date: Tue, 27 Aug 2013 07:55:04 -0500 Subject: New VPS Provider needed In-Reply-To: References: Message-ID: <20130827075504.658c9cbc@bigbox.christie.dr> On 2013-08-27 08:25, Dennis Lee Bieber wrote: > On Tue, 27 Aug 2013 nikos at superhost.gr wrote: > >Actually it is. Frankly, it's not. It's called the Python mailing list because it's for discussing *Python*. Otherwise, it would be called "Python and random irrelevant topics" mailing list. There are plenty of web-hosting forums (forae?) where such topics would be appropriate. I'm sure there are plenty of folks here who read SciFi/Fantasy books, or cook, but it's not an appropriate forum on which to discuss literature or culinary arts. > >You dont like my questions, block me, but just stop answering > >unhelpfully. > > I'd like to -- but your username is full of characters my > (paid) news reader chokes on when defining a filter. I've found it helpful to just use "@superhost.gr" as a From/Sender filter, along with the couple gmail addresses I've caught for him. -tkc From python at mrabarnett.plus.com Tue Aug 27 09:22:33 2013 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 27 Aug 2013 14:22:33 +0100 Subject: New VPS Provider needed In-Reply-To: <20130827075504.658c9cbc@bigbox.christie.dr> References: <20130827075504.658c9cbc@bigbox.christie.dr> Message-ID: <521CA819.5010103@mrabarnett.plus.com> On 27/08/2013 13:55, Tim Chase wrote: > On 2013-08-27 08:25, Dennis Lee Bieber wrote: >> On Tue, 27 Aug 2013 nikos at superhost.gr wrote: >> >Actually it is. > > Frankly, it's not. It's called the Python mailing list because it's > for discussing *Python*. Otherwise, it would be called "Python and > random irrelevant topics" mailing list. > > There are plenty of web-hosting forums (forae?) where such topics > would be appropriate. I'm sure there are plenty of folks here who > read SciFi/Fantasy books, or cook, but it's not an appropriate forum > on which to discuss literature or culinary arts. > [snip] Re Latin: Singular: forum Plural: fora If the plural was "forae" then the singular would be "fora". However, in English, just use "forums". From neilc at norwich.edu Tue Aug 27 08:41:31 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 27 Aug 2013 12:41:31 GMT Subject: New VPS Provider needed References: Message-ID: On 2013-08-27, David wrote: > On 27 August 2013 17:13, ???????? wrote: >> >> I know this isn't the place to ask > > ???????? is 100% correct about this. > > So, this list is also not the correct place to answer. So > please everyone, do not respond. Thanks. I can't understand how this savant at anti-killfile-fu can't otherwise manage his server. -- Neil Cerutti From steve+comp.lang.python at pearwood.info Tue Aug 27 12:25:16 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 27 Aug 2013 16:25:16 GMT Subject: New VPS Provider needed References: Message-ID: <521cd2ec$0$29986$c3e8da3$5496439d@news.astraweb.com> On Tue, 27 Aug 2013 21:18:10 +1000, David wrote: > On 27 August 2013 17:13, ???????? wrote: >> >> I know this isn't the place to ask > > ???????? is 100% correct about this. > > So, this list is also not the correct place to answer. So please > everyone, do not respond. Thanks. Asking for recommendations for a hosting provider that supports Python 3 very much is on-topic. (Besides, we often have off-topic discussions on this list.) ???????? has asked for help finding a Python-friendly hosting company. I don't know any hosting companies other than what I would discover by googling, so I can't help him. Nikos, I'm not interested in getting into a debate about who is to blame for your website breaking. You have asked your question, nobody is obliged to help you, if anyone has a recommendation they will reply, and if they don't, they won't. That is the way things are, so just accept it. -- Steven From nikos at superhost.gr Tue Aug 27 12:40:21 2013 From: nikos at superhost.gr (Ferrous Cranus) Date: Tue, 27 Aug 2013 19:40:21 +0300 Subject: New VPS Provider needed In-Reply-To: <521cd2ec$0$29986$c3e8da3$5496439d@news.astraweb.com> References: <521cd2ec$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: ???? 27/8/2013 7:25 ??, ?/? Steven D'Aprano ??????: > On Tue, 27 Aug 2013 21:18:10 +1000, David wrote: > >> On 27 August 2013 17:13, ???????? wrote: >>> >>> I know this isn't the place to ask >> >> ???????? is 100% correct about this. >> >> So, this list is also not the correct place to answer. So please >> everyone, do not respond. Thanks. > > Asking for recommendations for a hosting provider that supports Python 3 > very much is on-topic. (Besides, we often have off-topic discussions on > this list.) > > ???????? has asked for help finding a Python-friendly hosting company. I > don't know any hosting companies other than what I would discover by > googling, so I can't help him. > > Nikos, I'm not interested in getting into a debate about who is to blame > for your website breaking. You have asked your question, nobody is > obliged to help you, if anyone has a recommendation they will reply, and > if they don't, they won't. That is the way things are, so just accept it. > > Okey, i can accept that, what i don't accept is fun on my dispense especially when iam facing such difficulties. I alredy lost 4 customers :( -- What is now proved was at first only imagined! From steve+comp.lang.python at pearwood.info Tue Aug 27 13:17:32 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 27 Aug 2013 17:17:32 GMT Subject: New VPS Provider needed References: <521cd2ec$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: <521cdf2c$0$29986$c3e8da3$5496439d@news.astraweb.com> On Tue, 27 Aug 2013 19:40:21 +0300, Ferrous Cranus wrote: > I alredy lost 4 customers :( Nikos, you are mistaking us for your personal friends. We're not. We don't care if you lose 4 customers or 400, whether you become successful web developer and become rich, or end up homeless. Our only relationship to you is that you are a fellow Python user, and we are volunteering to help teach people Python. That includes you. But you are just another stranger half a world away. I am truly sorry that I have to tell you these harsh truths, but you need to understand that trying to beg for sympathy is just going to alienate people and make them less willing to help. You need to be professional here. That doesn't mean you can't be friendly. Maybe, if you are pleasant and interesting and give as much help as you receive, you will even make some friends here. But don't presume that we are your friends and care about your troubles. We all have troubles of our own. -- Steven From nikos.gr33k at gmail.com Wed Aug 28 04:01:11 2013 From: nikos.gr33k at gmail.com (Ferrous Cranus) Date: Wed, 28 Aug 2013 01:01:11 -0700 (PDT) Subject: New VPS Provider needed In-Reply-To: <521cdf2c$0$29986$c3e8da3$5496439d@news.astraweb.com> References: <521cd2ec$0$29986$c3e8da3$5496439d@news.astraweb.com> <521cdf2c$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: <2b886271-729c-442a-91b8-0a5372bf4e84@googlegroups.com> Hello Steven, Iam not trying to beg for your sympathy, i'am just expressing my frustration for my web hosting issues and i do that because i wanted to ask you for an alternative web service(python 3 enabled). Its totally human for one to express his own feeling to a python related and web host related issue in a python newsgroup. Also is a matter of character each person has and iam an open human being and feel like makign friend easy. Let alone that it is also a business offer for someone that wants to host my limited/joomla websites and make money out of it. From nikos at superhost.gr Tue Aug 27 03:53:18 2013 From: nikos at superhost.gr (=?UTF-8?B?zp3Ouc66z4zOu86xzr/Pgg==?=) Date: Tue, 27 Aug 2013 10:53:18 +0300 Subject: New VPS Provider needed Message-ID: Hello, My Problem is also that i made the mistake of payimg my current provider up until 5/5/2014 I had nothing but trouble with them and they wont refund back. I lost 4 customers, good webistes due to their inablity. I can even prvide proof for what i paid and for the perioed iam writing 5/5/14 So, no refuns back and 4 customers left me these days becaus eof constant issues with them. So, if someone want to help me out it would be nice if he could provide me with services free of charge for that time period, or at least at the end of they year. Please someone help me out to this and then i will be his devoted customer. What i need is the follwing: 1. I will pay for a full year upfront but it must be a low price for the 1st year, because i have already paid them and cant get my money back to use it to the new com opany. 2. cPanel/WHM && Softaculous Licenses must be moved from them to the new VPS provider 3. Tranfer of the current 6 websites free of charge and assure that they work properly after transfers(they are joomlas) 4. Some help over the road when i need somthign to be installed like a new python version and a couple of modules. Pleas help me out here and if you do i will be your devoted customer. -- What is now proved was at first only imagined! From andy.kannberg at gmail.com Tue Aug 27 07:31:02 2013 From: andy.kannberg at gmail.com (Andy Kannberg) Date: Tue, 27 Aug 2013 13:31:02 +0200 Subject: New VPS Provider needed Message-ID: Niko, No offense, but I have to agree with David. This list is about Python, not about webprogramming nor hosting. If you are in need of a mailing group for such, again, Google or any other search engine can help you with that. best regards, Andy 2013/8/27 ???????? > ???? 27/8/2013 2:18 ??, ?/? David ??????: > >> On 27 August 2013 17:13, ???????? wrote: >> >>> >>> I know this isn't the place to ask >>> >> >> ???????? is 100% correct about this. >> >> >> So, this list is also not the correct place to answer. >> So please everyone, do not respond. Thanks. >> >> > Actually it is. > Poeple are web programmers here and lots of them doing hosting. > You dont like my questions, block me, but just stop answering unhelpfully. > > > -- > What is now proved was at first only imagined! > -- > http://mail.python.org/**mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From nikos at superhost.gr Tue Aug 27 08:52:34 2013 From: nikos at superhost.gr (Ferrous Cranus) Date: Tue, 27 Aug 2013 15:52:34 +0300 Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of Message-ID: I think that something iam missing in the new VPS implementation concerning my python web page. After: tail -F /usr/local/apache/logs/error_log & ================ [Tue Aug 27 12:49:52 2013] [error] [client 178.59.111.223] (2)No such file or directory: exec of '/home/nikos/public_html/cgi-bin/metrites.py' failed [Tue Aug 27 12:49:52 2013] [error] [client 178.59.111.223] Premature end of script headers: metrites.py ================ I do not understand what this type of error is trying to say. From nikos at superhost.gr Tue Aug 27 09:05:28 2013 From: nikos at superhost.gr (Ferrous Cranus) Date: Tue, 27 Aug 2013 16:05:28 +0300 Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: References: Message-ID: ???? 27/8/2013 3:52 ??, ?/? Ferrous Cranus ??????: > I think that something iam missing in the new VPS implementation > concerning my python web page. > > After: tail -F /usr/local/apache/logs/error_log & > > ================ > [Tue Aug 27 12:49:52 2013] [error] [client 178.59.111.223] (2)No such > file or directory: exec of '/home/nikos/public_html/cgi-bin/metrites.py' > failed > [Tue Aug 27 12:49:52 2013] [error] [client 178.59.111.223] Premature end > of script headers: metrites.py > ================ > > I do not understand what this type of error is trying to say. > > Okey it was the python path (shebang that was causing this) Now i have correct it from /usr/local/bin/python3 => /usr/bin/python System has beens etup this way to point directly to python 3.3.2 in the new VPS. Admin setit up that way hwile in the old one was different. But i cannot overocme thjis: [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] Error in sys.excepthook: [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] ValueError: underlying buffer has been detached [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] Original exception was: [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] Traceback (most recent call last): [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] File "/home/nikos/public_html/cgi-bin/metrites.py", line 169, in [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] cur.execute('''SELECT ID FROM counters WHERE url = %s''', page ) [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] File "/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py", line 108, in execute [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] query = query % escaped_args [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] TypeError: unsupported operand type(s) for %: 'bytes' and 'str' -- What is now proved was at first only imagined! From nikos at superhost.gr Tue Aug 27 09:35:34 2013 From: nikos at superhost.gr (Ferrous Cranus) Date: Tue, 27 Aug 2013 16:35:34 +0300 Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: References: Message-ID: ???? 27/8/2013 4:05 ??, ?/? Ferrous Cranus ??????: > ???? 27/8/2013 3:52 ??, ?/? Ferrous Cranus ??????: >> I think that something iam missing in the new VPS implementation >> concerning my python web page. >> >> After: tail -F /usr/local/apache/logs/error_log & >> >> ================ >> [Tue Aug 27 12:49:52 2013] [error] [client 178.59.111.223] (2)No such >> file or directory: exec of '/home/nikos/public_html/cgi-bin/metrites.py' >> failed >> [Tue Aug 27 12:49:52 2013] [error] [client 178.59.111.223] Premature end >> of script headers: metrites.py >> ================ >> >> I do not understand what this type of error is trying to say. >> >> > > Okey it was the python path (shebang that was causing this) > > Now i have correct it from > > /usr/local/bin/python3 => /usr/bin/python > System has beens etup this way to point directly to python 3.3.2 in the > new VPS. > Admin setit up that way hwile in the old one was different. > > But i cannot overocme thjis: > > [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] Error in > sys.excepthook: > [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] ValueError: > underlying buffer has been detached > [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] > [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] Original > exception was: > [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] Traceback > (most recent call last): > [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] File > "/home/nikos/public_html/cgi-bin/metrites.py", line 169, in > [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] > cur.execute('''SELECT ID FROM counters WHERE url = %s''', page ) > [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] File > "/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py", > line 108, in execute > [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] query = > query % escaped_args > [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] TypeError: > unsupported operand type(s) for %: 'bytes' and 'str' > someone please? -- What is now proved was at first only imagined! From ishish at domhain.de Tue Aug 27 09:59:39 2013 From: ishish at domhain.de (ishish) Date: Tue, 27 Aug 2013 14:59:39 +0100 Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: References: Message-ID: > [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] Error in > sys.excepthook: > [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] > ValueError: underlying buffer has been detached > [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] > [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] Original > exception was: > [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] Traceback > (most recent call last): > [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] File > "/home/nikos/public_html/cgi-bin/metrites.py", line 169, in > [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] > cur.execute('''SELECT ID FROM counters WHERE url = %s''', page ) > [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] File > > "/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py", > line 108, in execute > [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] query > = query % escaped_args > [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] > TypeError: unsupported operand type(s) for %: 'bytes' and 'str' I quote from a Python 3 Guide [http://python.about.com/od/python30/ss/30_strings_3.htm]: The two data types, str and bytes, are mutually exclusive. One cannot legally combine them into one call. With the distinction between text and data, therefore, comes the need to convert between them. From nikos at superhost.gr Tue Aug 27 11:04:23 2013 From: nikos at superhost.gr (Ferrous Cranus) Date: Tue, 27 Aug 2013 18:04:23 +0300 Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: References: Message-ID: ???? 27/8/2013 4:59 ??, ?/? ishish ??????: >> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] Error in >> sys.excepthook: >> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] >> ValueError: underlying buffer has been detached >> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] >> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] Original >> exception was: >> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] Traceback >> (most recent call last): >> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] File >> "/home/nikos/public_html/cgi-bin/metrites.py", line 169, in >> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] >> cur.execute('''SELECT ID FROM counters WHERE url = %s''', page ) >> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] File >> >> "/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py", >> line 108, in execute >> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] query >> = query % escaped_args >> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] >> TypeError: unsupported operand type(s) for %: 'bytes' and 'str' > > I quote from a Python 3 Guide > [http://python.about.com/od/python30/ss/30_strings_3.htm]: > > The two data types, str and bytes, are mutually exclusive. One cannot > legally combine them into one call. With the distinction between text > and data, therefore, comes the need to convert between them. > > So, in this line: cur.execute('''SELECT ID FROM counters WHERE url = %s''', page ) the variable 'page' needs conversion to what? all that is stores is the location of a file path = '/home/nikos/public_html/' page = form.getvalue('page') if not page and os.path.exists( file ): # it is an html template page = file.replace( path, '' ) So chnage it to what? -- What is now proved was at first only imagined! From ishish at domhain.de Tue Aug 27 11:22:32 2013 From: ishish at domhain.de (ishish) Date: Tue, 27 Aug 2013 16:22:32 +0100 Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: References: Message-ID: Am 27.08.2013 16:04, schrieb Ferrous Cranus: > ???? 27/8/2013 4:59 ??, ?/? ishish ??????: >>> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] Error >>> in >>> sys.excepthook: >>> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] >>> ValueError: underlying buffer has been detached >>> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] >>> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] >>> Original >>> exception was: >>> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] >>> Traceback >>> (most recent call last): >>> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] File >>> "/home/nikos/public_html/cgi-bin/metrites.py", line 169, in >>> >>> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] >>> cur.execute('''SELECT ID FROM counters WHERE url = %s''', page ) >>> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] File >>> >>> >>> "/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py", >>> line 108, in execute >>> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] >>> query >>> = query % escaped_args >>> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] >>> TypeError: unsupported operand type(s) for %: 'bytes' and 'str' >> >> I quote from a Python 3 Guide >> [http://python.about.com/od/python30/ss/30_strings_3.htm]: >> >> The two data types, str and bytes, are mutually exclusive. One >> cannot >> legally combine them into one call. With the distinction between >> text >> and data, therefore, comes the need to convert between them. >> >> > > So, in this line: > > cur.execute('''SELECT ID FROM counters WHERE url = %s''', page ) > > the variable 'page' needs conversion to what? > > all that is stores is the location of a file > > path = '/home/nikos/public_html/' > > page = form.getvalue('page') > > if not page and os.path.exists( file ): > # it is an html template > page = file.replace( path, '' ) > > So chnage it to what? > -- > What is now proved was at first only imagined! The error occurs in file "/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py", line 108, in execute query = query % escaped_args You can check the actual values and data types using: print repr(query) print type(query) print repr(escaped_args) print type(escaped_args) Always useful is a proper exception handling using try:/except: From steve+comp.lang.python at pearwood.info Tue Aug 27 13:07:52 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 27 Aug 2013 17:07:52 GMT Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of References: Message-ID: <521cdce7$0$29986$c3e8da3$5496439d@news.astraweb.com> On Tue, 27 Aug 2013 18:04:23 +0300, Ferrous Cranus wrote: > So, in this line: > > cur.execute('''SELECT ID FROM counters WHERE url = %s''', page ) > > the variable 'page' needs conversion to what? You tell us. You want to be a programmer, *you* need to do the programming, and not just keep asking others to solve every single bug. You have been given all the clues to solve this problem. Start by checking what the type of 'page' is, then decide what it ought to be. Hint: you can use "print(type(page), file=open('path/to/some/file', 'w'))" to see the type of the variable 'page' without displaying it on your website. > all that is stores is the location of a file > > path = '/home/nikos/public_html/' > page = form.getvalue('page') And what result does form.getvalue return? What is its type? Is it a list, a tuple, a dict, a bytes object, a float, a str object, something else? DON'T GUESS, and don't assume, find out for sure, by inspecting the result. > if not page and os.path.exists( file ): > # it is an html template > page = file.replace( path, '' ) > > So chnage it to what? What do you think you need to change it to? Pick one: 1) bytes 2) an integer 3) a string 4) a list of floats 5) something else -- Steven From nikos.gr33k at gmail.com Wed Aug 28 04:46:01 2013 From: nikos.gr33k at gmail.com (Ferrous Cranus) Date: Wed, 28 Aug 2013 01:46:01 -0700 (PDT) Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: <521cdce7$0$29986$c3e8da3$5496439d@news.astraweb.com> References: <521cdce7$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: <3d299421-7c03-4c92-b860-15b111e75bc1@googlegroups.com> ?? ?????, 27 ????????? 2013 8:07:52 ?.?. UTC+3, ? ??????? Steven D'Aprano ??????: > On Tue, 27 Aug 2013 18:04:23 +0300, Ferrous Cranus wrote: > > > > > So, in this line: > > > > > > cur.execute('''SELECT ID FROM counters WHERE url = %s''', page ) > > > > > > the variable 'page' needs conversion to what? > > > > You tell us. You want to be a programmer, *you* need to do the > > programming, and not just keep asking others to solve every single bug. > > You have been given all the clues to solve this problem. Start by > > checking what the type of 'page' is, then decide what it ought to be. > > > > Hint: you can use > > > > "print(type(page), file=open('path/to/some/file', 'w'))" > > > > to see the type of the variable 'page' without displaying it on your > > website. > > > > > > > > > all that is stores is the location of a file > > > > > > path = '/home/nikos/public_html/' > > > page = form.getvalue('page') > > > > And what result does form.getvalue return? > > > > What is its type? Is it a list, a tuple, a dict, a bytes object, a float, > > a str object, something else? DON'T GUESS, and don't assume, find out for > > sure, by inspecting the result. > > > > > > > if not page and os.path.exists( file ): > > > # it is an html template > > > page = file.replace( path, '' ) > > > > > > So chnage it to what? > > > > What do you think you need to change it to? Pick one: > > > > > > 1) bytes > > 2) an integer > > 3) a string > > 4) a list of floats > > 5) something else > > > > > > > > -- > > Steven Hello Steven, i tried to do what you said and iam receing this: [Wed Aug 28 08:43:31 2013] [error] [client 108.162.231.120] Original exception was: [Wed Aug 28 08:43:31 2013] [error] [client 108.162.231.120] Traceback (most recent call last): [Wed Aug 28 08:43:31 2013] [error] [client 108.162.231.120] File "/home/nikos/public_html/cgi-bin/metrites.py", line 39, in [Wed Aug 28 08:43:31 2013] [error] [client 108.162.231.120] print( type(page), file=open('../err.out', 'w') ) [Wed Aug 28 08:43:31 2013] [error] [client 108.162.231.120] PermissionError: [Errno 13] \\u0386\\u03c1\\u03bd\\u03b7\\u03c3\\u03b7 \\u03c0\\u03c1\\u03cc\\u03c3\\u03b2\\u03b1\\u03c3\\u03b7\\u03c2: '../err.out' [Wed Aug 28 08:43:31 2013] [error] [client 108.162.231.120] Premature end of script headers: metrites.py Also many times when i try to view the error_log by tail -F /usr/local/apache/logs/error_log & i get realtime scrolling of other joomla webistes pho errors and i have hard time trackign my own webistes erros. is ther a work around for that? From nikos.gr33k at gmail.com Wed Aug 28 05:24:08 2013 From: nikos.gr33k at gmail.com (Ferrous Cranus) Date: Wed, 28 Aug 2013 02:24:08 -0700 (PDT) Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: <3d299421-7c03-4c92-b860-15b111e75bc1@googlegroups.com> References: <521cdce7$0$29986$c3e8da3$5496439d@news.astraweb.com> <3d299421-7c03-4c92-b860-15b111e75bc1@googlegroups.com> Message-ID: <25c8e290-b2cd-4138-9634-04b6dca1cd46@googlegroups.com> In my attemtpt to be shwon only mesages pertaining to superhost.gr i try: alias err='tail -F /usr/local/apache/logs/error_log | grep nikos &' but now it only displays to me the lines that have '/home/nikos' within them and not all the relevant error lines. From steve+comp.lang.python at pearwood.info Wed Aug 28 08:11:07 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 Aug 2013 12:11:07 GMT Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of References: <521cdce7$0$29986$c3e8da3$5496439d@news.astraweb.com> <3d299421-7c03-4c92-b860-15b111e75bc1@googlegroups.com> Message-ID: <521de8db$0$6599$c3e8da3$5496439d@news.astraweb.com> On Wed, 28 Aug 2013 01:46:01 -0700, Ferrous Cranus wrote: > ?? ?????, 27 ????????? 2013 8:07:52 ?.?. UTC+3, ? ??????? Steven > D'Aprano ??????: >> Hint: you can use >> "print(type(page), file=open('path/to/some/file', 'w'))" >> >> to see the type of the variable 'page' without displaying it on your >> website. > i tried to do what you said and iam receing this: > > [Wed Aug 28 08:43:31 2013] [error] [client 108.162.231.120] Original > exception was: [Wed Aug 28 08:43:31 2013] [error] [client > 108.162.231.120] Traceback (most recent call last): [Wed Aug 28 08:43:31 > 2013] [error] [client 108.162.231.120] File > "/home/nikos/public_html/cgi-bin/metrites.py", line 39, in [Wed > Aug 28 08:43:31 2013] [error] [client 108.162.231.120] print( > type(page), file=open('../err.out', 'w') ) [Wed Aug 28 08:43:31 2013] > [error] [client 108.162.231.120] PermissionError: [Errno 13] > \\u0386\\u03c1\\u03bd\\u03b7\\u03c3\\u03b7 > \\u03c0\\u03c1\\u03cc\\u03c3\\u03b2\\u03b1\\u03c3\\u03b7\\u03c2: > '../err.out' [Wed Aug 28 08:43:31 2013] [error] [client 108.162.231.120] > Premature end of script headers: metrites.py Then pick a different location, one where you have permission to write files! Honestly Nikos, I'm not trying to be rude, but if you cannot think of this on your own after all this time we've been helping you, perhaps programming is not the right vocation for you. Perhaps you need to consider a change of career. If you do decide to stay as a web developer, you need to start thinking: * What is this error message telling me? ("Permission denied") * How can I fix that? ("I need permission to read the file. Maybe try a different file?") > Also many times when i try to view the error_log by > > tail -F /usr/local/apache/logs/error_log & > > i get realtime scrolling of other joomla webistes pho errors and i have > hard time trackign my own webistes erros. > > is ther a work around for that? That's an apache logging issue, not Python, and I don't know enough about apache to do more than guess that there probably is a solution but I have no idea what it is. -- Steven From nikos.gr33k at gmail.com Wed Aug 28 08:17:34 2013 From: nikos.gr33k at gmail.com (Ferrous Cranus) Date: Wed, 28 Aug 2013 05:17:34 -0700 (PDT) Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: <521de8db$0$6599$c3e8da3$5496439d@news.astraweb.com> References: <521cdce7$0$29986$c3e8da3$5496439d@news.astraweb.com> <3d299421-7c03-4c92-b860-15b111e75bc1@googlegroups.com> <521de8db$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: ?? ???????, 28 ????????? 2013 3:11:07 ?.?. UTC+3, ? ??????? Steven D'Aprano ??????: > On Wed, 28 Aug 2013 01:46:01 -0700, Ferrous Cranus wrote: > > > > > ?? ?????, 27 ????????? 2013 8:07:52 ?.?. UTC+3, ? ??????? Steven > > > D'Aprano ??????: > > > > >> Hint: you can use > > >> "print(type(page), file=open('path/to/some/file', 'w'))" > > >> > > >> to see the type of the variable 'page' without displaying it on your > > >> website. > > > > > > > i tried to do what you said and iam receing this: > > > > > > [Wed Aug 28 08:43:31 2013] [error] [client 108.162.231.120] Original > > > exception was: [Wed Aug 28 08:43:31 2013] [error] [client > > > 108.162.231.120] Traceback (most recent call last): [Wed Aug 28 08:43:31 > > > 2013] [error] [client 108.162.231.120] File > > > "/home/nikos/public_html/cgi-bin/metrites.py", line 39, in [Wed > > > Aug 28 08:43:31 2013] [error] [client 108.162.231.120] print( > > > type(page), file=open('../err.out', 'w') ) [Wed Aug 28 08:43:31 2013] > > > [error] [client 108.162.231.120] PermissionError: [Errno 13] > > > \\u0386\\u03c1\\u03bd\\u03b7\\u03c3\\u03b7 > > > \\u03c0\\u03c1\\u03cc\\u03c3\\u03b2\\u03b1\\u03c3\\u03b7\\u03c2: > > > '../err.out' [Wed Aug 28 08:43:31 2013] [error] [client 108.162.231.120] > > > Premature end of script headers: metrites.py > > > > Then pick a different location, one where you have permission to write > > files! > > > > Honestly Nikos, I'm not trying to be rude, but if you cannot think of > > this on your own after all this time we've been helping you, perhaps > > programming is not the right vocation for you. Perhaps you need to > > consider a change of career. > > > > If you do decide to stay as a web developer, you need to start thinking: > > > > * What is this error message telling me? ("Permission denied") > > > > * How can I fix that? ("I need permission to read the file. Maybe try a > > different file?") > > > > > > > > > Also many times when i try to view the error_log by > > > > > > tail -F /usr/local/apache/logs/error_log & > > > > > > i get realtime scrolling of other joomla webistes pho errors and i have > > > hard time trackign my own webistes erros. > > > > > > is ther a work around for that? > > > > > > That's an apache logging issue, not Python, and I don't know enough about > > apache to do more than guess that there probably is a solution but I have > > no idea what it is. > > > > > > > > -- > > Steven As i have pointed out i as the owner of the accoutn have read and write perimssion bot at www/ and www/cgi-bin i also chnage the filename and still cant write to the file. if you as experts cannot help resolve this, how me as a newbiw can? From steve+comp.lang.python at pearwood.info Wed Aug 28 08:38:11 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 Aug 2013 12:38:11 GMT Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of References: <521cdce7$0$29986$c3e8da3$5496439d@news.astraweb.com> <3d299421-7c03-4c92-b860-15b111e75bc1@googlegroups.com> <521de8db$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <521def33$0$6599$c3e8da3$5496439d@news.astraweb.com> On Wed, 28 Aug 2013 05:17:34 -0700, Ferrous Cranus wrote: > As i have pointed out i as the owner of the accoutn have read and write > perimssion bot at www/ and www/cgi-bin i also chnage the filename and > still cant write to the file. If you type filenames as carelessly as you type requests for help, who knows what file name you are actually trying to open? Or to put it another way... If yuo tpye filnaems as carlesl yas y outype reqets for help, woh knwos wha tfile nmae oyu ar eactaly tying to oep n? Nikos, my Greek is completely non-existent, and I admire you for learning a second language. I know your English is good, because I have seen you write fluent, excellent English. But if you can't be bothered to type carefully and clearly when asking questions, then how can you expect others to be bothered to read and respond to your questions? > if you as experts cannot help resolve this, how me as a newbiw can? Just because your account has write permission to a location doesn't mean that the script being run by the webserver has write permission. You need to write to a location where the script has permission to write. First you need to find out what account the script is being run under -- it may be "apache", or possible "nobody", but I don't know. It shouldn't be "root", and almost certainly isn't "nikos". I'm not an expert, but a few seconds googling came up with a lot of useful looking hits. Google on "what user does apache run as" and do your research. This is off-topic though. This is not a "beginner's guide to apache" forum, if you can't solve this problem yourself you'll have to take it elsewhere. -- Steven From nikos.gr33k at gmail.com Wed Aug 28 08:48:24 2013 From: nikos.gr33k at gmail.com (Ferrous Cranus) Date: Wed, 28 Aug 2013 05:48:24 -0700 (PDT) Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: <521def33$0$6599$c3e8da3$5496439d@news.astraweb.com> References: <521cdce7$0$29986$c3e8da3$5496439d@news.astraweb.com> <3d299421-7c03-4c92-b860-15b111e75bc1@googlegroups.com> <521de8db$0$6599$c3e8da3$5496439d@news.astraweb.com> <521def33$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <9fc603a0-6ab9-4c05-b3df-86f07f09fa52@googlegroups.com> ?? ???????, 28 ????????? 2013 3:38:11 ?.?. UTC+3, ? ??????? Steven D'Aprano ??????: > On Wed, 28 Aug 2013 05:17:34 -0700, Ferrous Cranus wrote: > > > > > As i have pointed out i as the owner of the accoutn have read and write > > > perimssion bot at www/ and www/cgi-bin i also chnage the filename and > > > still cant write to the file. > > > > If you type filenames as carelessly as you type requests for help, who > > knows what file name you are actually trying to open? > > > > Or to put it another way... > > > > If yuo tpye filnaems as carlesl yas y outype reqets for help, woh knwos > > wha tfile nmae oyu ar eactaly tying to oep n? > > > > > > Nikos, my Greek is completely non-existent, and I admire you for learning > > a second language. I know your English is good, because I have seen you > > write fluent, excellent English. But if you can't be bothered to type > > carefully and clearly when asking questions, then how can you expect > > others to be bothered to read and respond to your questions? > > > > > > > if you as experts cannot help resolve this, how me as a newbiw can? > > > > Just because your account has write permission to a location doesn't mean > > that the script being run by the webserver has write permission. You need > > to write to a location where the script has permission to write. First > > you need to find out what account the script is being run under -- it may > > be "apache", or possible "nobody", but I don't know. It shouldn't be > > "root", and almost certainly isn't "nikos". I'm not an expert, but a few > > seconds googling came up with a lot of useful looking hits. Google on > > "what user does apache run as" and do your research. > > > > This is off-topic though. This is not a "beginner's guide to apache" > > forum, if you can't solve this problem yourself you'll have to take it > > elsewhere. > > > > > > > > -- > > Steven Hi steven , sorry for the typos. you are write my script is invoked by apache web server application which it runs under account 'nobody' Here is proof of that: nikos at superhost.gr [~]# ps aux | grep Apache nikos 8531 0.0 0.0 6372 680 pts/0 R+ 12:44 0:00 grep Apache nikos at superhost.gr [~]# ps aux | grep apache root 1101 0.0 0.2 65576 4168 ? Ss 08:30 0:01 /usr/local/apache/bin/httpd -k start -DSSL root 8448 0.0 0.1 65576 1700 ? S 12:42 0:00 /usr/local/apache/bin/httpd -k start -DSSL nobody 8449 0.0 0.2 65712 3228 ? S 12:42 0:00 /usr/local/apache/bin/httpd -k start -DSSL nobody 8450 0.0 0.2 65848 3348 ? S 12:42 0:00 /usr/local/apache/bin/httpd -k start -DSSL nobody 8451 0.0 0.2 65848 3360 ? S 12:42 0:00 /usr/local/apache/bin/httpd -k start -DSSL nobody 8452 0.0 0.2 65712 3340 ? S 12:42 0:00 /usr/local/apache/bin/httpd -k start -DSSL nobody 8453 0.0 0.2 65712 3260 ? S 12:42 0:00 /usr/local/apache/bin/httpd -k start -DSSL nobody 8467 0.0 0.1 65712 2356 ? S 12:43 0:00 /usr/local/apache/bin/httpd -k start -DSSL nobody 8468 0.0 0.1 65712 2344 ? S 12:43 0:00 /usr/local/apache/bin/httpd -k start -DSSL nobody 8519 0.0 0.1 65712 2344 ? S 12:43 0:00 /usr/local/apache/bin/httpd -k start -DSSL nikos 8537 0.0 0.0 6372 684 pts/0 R+ 12:44 0:00 grep apache My script were all workign in the previous VPS enviroment, the script is correct is just a few minor thing in the new VPS env that they need to be taken care of thus prohibit my script to run.. please don't give up helping me and tell me please what you want me to try. tahnk you very much. From nikos.gr33k at gmail.com Wed Aug 28 09:11:13 2013 From: nikos.gr33k at gmail.com (Ferrous Cranus) Date: Wed, 28 Aug 2013 06:11:13 -0700 (PDT) Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: <9fc603a0-6ab9-4c05-b3df-86f07f09fa52@googlegroups.com> References: <521cdce7$0$29986$c3e8da3$5496439d@news.astraweb.com> <3d299421-7c03-4c92-b860-15b111e75bc1@googlegroups.com> <521de8db$0$6599$c3e8da3$5496439d@news.astraweb.com> <521def33$0$6599$c3e8da3$5496439d@news.astraweb.com> <9fc603a0-6ab9-4c05-b3df-86f07f09fa52@googlegroups.com> Message-ID: <8d949d1b-9b92-44e8-9d16-ff7d9e5a3b2c@googlegroups.com> This si what iam tryign now since the function ishish proposed wont help me. try: #find the needed counter for the page URL if os.path.exists( path + page ) or os.path.exists( cgi_path + page ): cur.execute('''SELECT ID FROM counters WHERE url = %s''', page ) data = cur.fetchone() #URL is unique, so should only be one except: print( repr(e) ) [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120] Original exception was: [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120] Traceback (most recent call last): [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120] File "/home/nikos/public_html/cgi-bin/metrites.py", line 174, in [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120] cur.execute('''SELECT ID FROM counters WHERE url = %s''', page ) [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120] File "/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py", line 108, in execute [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120] query = query % escaped_args [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120] TypeError: unsupported operand type(s) for %: 'bytes' and 'str' [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120] [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120] During handling of the above exception, another exception occurred: [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120] [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120] Traceback (most recent call last): [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120] File "/home/nikos/public_html/cgi-bin/metrites.py", line 177, in [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120] print( repr(e) ) [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120] NameError: name 'e' is not defined From steve+comp.lang.python at pearwood.info Wed Aug 28 18:56:55 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 Aug 2013 22:56:55 GMT Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of References: <521cdce7$0$29986$c3e8da3$5496439d@news.astraweb.com> <3d299421-7c03-4c92-b860-15b111e75bc1@googlegroups.com> <521de8db$0$6599$c3e8da3$5496439d@news.astraweb.com> <521def33$0$6599$c3e8da3$5496439d@news.astraweb.com> <9fc603a0-6ab9-4c05-b3df-86f07f09fa52@googlegroups.com> <8d949d1b-9b92-44e8-9d16-ff7d9e5a3b2c@googlegroups.com> Message-ID: <521e8037$0$6599$c3e8da3$5496439d@news.astraweb.com> On Wed, 28 Aug 2013 06:11:13 -0700, Ferrous Cranus wrote: > This si what iam tryign now since the function ishish proposed wont help > me. I see that your apology for careless writing didn't last very long. [...] > except: > print( repr(e) ) What is the value of "e" here, and where is it defined? Let's look at the error in your log file: > [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120] print( > repr(e) ) [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120] > NameError: name 'e' is not defined Did you bother to read the error before asking for help? Variable 'e' is not defined. Perhaps you should define it? -- Steven From nikos.gr33k at gmail.com Thu Aug 29 00:20:01 2013 From: nikos.gr33k at gmail.com (Ferrous Cranus) Date: Wed, 28 Aug 2013 21:20:01 -0700 (PDT) Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: <521e8037$0$6599$c3e8da3$5496439d@news.astraweb.com> References: <521cdce7$0$29986$c3e8da3$5496439d@news.astraweb.com> <3d299421-7c03-4c92-b860-15b111e75bc1@googlegroups.com> <521de8db$0$6599$c3e8da3$5496439d@news.astraweb.com> <521def33$0$6599$c3e8da3$5496439d@news.astraweb.com> <9fc603a0-6ab9-4c05-b3df-86f07f09fa52@googlegroups.com> <8d949d1b-9b92-44e8-9d16-ff7d9e5a3b2c@googlegroups.com> <521e8037$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <7309054c-5af6-401c-8d1b-ebea48cb6c0a@googlegroups.com> ?? ??????, 29 ????????? 2013 1:56:55 ?.?. UTC+3, ? ??????? Steven D'Aprano ??????: > On Wed, 28 Aug 2013 06:11:13 -0700, Ferrous Cranus wrote: > > > > > This si what iam tryign now since the function ishish proposed wont help > > > me. > > > > I see that your apology for careless writing didn't last very long. > > > > [...] > > > except: > > > print( repr(e) ) > > > > What is the value of "e" here, and where is it defined? > > > > Let's look at the error in your log file: > > > > > > > [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120] print( > > > repr(e) ) [Wed Aug 28 13:08:27 2013] [error] [client 108.162.231.120] > > > NameError: name 'e' is not defined > > > > > > Did you bother to read the error before asking for help? > > > > Variable 'e' is not defined. Perhaps you should define it? Yes indeed , that was a a syntax error by me: try: #find the needed counter for the page URL if os.path.exists( path + page ) or os.path.exists( cgi_path + page ): cur.execute('''SELECT ID FROM counters WHERE url = %s''', page ) data = cur.fetchone() #URL is unique, so should only be one except Exception as e: con.rollback() #something failed, rollback the entire transaction print( repr(e) ) outputs: [Thu Aug 29 04:18:04 2013] [error] [client 108.162.229.127] File "/home/nikos/public_html/cgi-bin/metrites.py", line 206, in [Thu Aug 29 04:18:04 2013] [error] [client 108.162.229.127] cur.execute('''SELECT hits FROM counters WHERE url = %s''', page ) [Thu Aug 29 04:18:04 2013] [error] [client 108.162.229.127] File "/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py", line 108, in execute [Thu Aug 29 04:18:04 2013] [error] [client 108.162.229.127] query = query % escaped_args [Thu Aug 29 04:15:20 2013] [error] [client 141.101.98.154] File "/home/nikos/public_html/cgi-bin/metrites.py", line 206, in [Thu Aug 29 04:15:20 2013] [error] [client 141.101.98.154] cur.execute('''SELECT hits FROM counters WHERE url = %s''', page ) [Thu Aug 29 04:15:20 2013] [error] [client 141.101.98.154] File "/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py", line 108, in execute [Thu Aug 29 04:15:20 2013] [error] [client 141.101.98.154] query = query % escaped_args [Thu Aug 29 04:15:20 2013] [error] [client 141.101.98.154] TypeError: unsupported operand type(s) for %: 'bytes' and 'str' Now at least 'superhost.gr' displays the error when one by browser http://superhost.gr From steve at pearwood.info Thu Aug 29 02:30:52 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 29 Aug 2013 06:30:52 GMT Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of References: <521cdce7$0$29986$c3e8da3$5496439d@news.astraweb.com> <3d299421-7c03-4c92-b860-15b111e75bc1@googlegroups.com> <521de8db$0$6599$c3e8da3$5496439d@news.astraweb.com> <521def33$0$6599$c3e8da3$5496439d@news.astraweb.com> <9fc603a0-6ab9-4c05-b3df-86f07f09fa52@googlegroups.com> <8d949d1b-9b92-44e8-9d16-ff7d9e5a3b2c@googlegroups.com> <521e8037$0$6599$c3e8da3$5496439d@news.astraweb.com> <7309054c-5af6-401c-8d1b-ebea48cb6c0a@googlegroups.com> Message-ID: <521eea9c$0$2743$c3e8da3$76491128@news.astraweb.com> On Wed, 28 Aug 2013 21:20:01 -0700, Ferrous Cranus wrote: > [Thu Aug 29 04:18:04 2013] [error] [client 108.162.229.127] > File "/home/nikos/public_html/cgi-bin/metrites.py", line 206, in > > [Thu Aug 29 04:18:04 2013] [error] [client 108.162.229.127] > cur.execute('''SELECT hits FROM counters WHERE url = %s''', page ) [...] > TypeError: unsupported operand type(s) for %: 'bytes' and 'str' Oh look, it's exactly the same error you started with. Congratulations, you've made exactly no progress. As I asked you yesterday: And what result does form.getvalue return? What is its type? Is it a list, a tuple, a dict, a bytes object, a float, a str object, something else? Have you worked that out yet? If you can't work it out, you can guess, and then test your guess: page = form.getvalue('page') if isinstance(page, list): raise TypeError('guessed it was a list, it is a list') If your guess is wrong, try another guess. Keep going until you work out what type page is. If you can't write to a data file, you can write to the error log: page = form.getvalue('page') raise TypeError('type of page is %r' % type(page)) Keep going until you find out what type of data the variable 'page' is holding. Now read the error message again: unsupported operand type(s) for %: 'bytes' and 'str' Do you understand what that error means? Here is a hint: start a Python interactive session, and try things for yourself: steve at runes:~$ python3.3 Python 3.3.0rc3 (default, Sep 27 2012, 18:31:58) [GCC 4.4.5] on linux Type "help", "copyright", "credits" or "license" for more information. py> py> 'string%s' % 'string' 'stringstring' py> py> b'bytes%s' % b'bytes' Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for %: 'bytes' and 'bytes' Do you see a pattern yet? What do you think will happen if you run this? b'bytes%s' % 'string' Try it and see for yourself. Now look at the error message you are getting. Is it the same error message? Do you think that perhaps it has the same cause? How do you think you might solve this problem? Hint: if you have a data item that is type A, and you need it to be type B, you need to convert from A to B. -- Steven From cs at zip.com.au Wed Aug 28 21:01:06 2013 From: cs at zip.com.au (Cameron Simpson) Date: Thu, 29 Aug 2013 11:01:06 +1000 Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: <9fc603a0-6ab9-4c05-b3df-86f07f09fa52@googlegroups.com> References: <9fc603a0-6ab9-4c05-b3df-86f07f09fa52@googlegroups.com> Message-ID: <20130829010106.GA39248@cskk.homeip.net> On 28Aug2013 05:48, Nikos wrote: | Hi steven , sorry for the typos. | you are write my script is invoked by apache web server application which it runs under account 'nobody' [...] | nobody 8449 0.0 0.2 65712 3228 ? S 12:42 0:00 /usr/local/apache/bin/httpd -k start -DSSL [...] | My script were all workign in the previous VPS enviroment, the script is correct is just a few minor thing in the new VPS env that they need to be taken care of thus prohibit my script to run.. Your previous VPS used "suexec" in the Apache. This one does not. Cheers, -- Cameron Simpson Think positively, act positively, and never leave fingerprints. - Robert Sneddon From cs at zip.com.au Wed Aug 28 21:04:15 2013 From: cs at zip.com.au (Cameron Simpson) Date: Thu, 29 Aug 2013 11:04:15 +1000 Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: <521de8db$0$6599$c3e8da3$5496439d@news.astraweb.com> References: <521de8db$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <20130829010415.GA40322@cskk.homeip.net> On 28Aug2013 12:11, Steven D'Aprano wrote: | On Wed, 28 Aug 2013 01:46:01 -0700, Ferrous Cranus wrote: | > Also many times when i try to view the error_log by | > tail -F /usr/local/apache/logs/error_log & | > | > i get realtime scrolling of other joomla webistes pho errors and i have | > hard time trackign my own webistes erros. | > | > is ther a work around for that? | | That's an apache logging issue, not Python, and I don't know enough about | apache to do more than guess that there probably is a solution but I have | no idea what it is. Normal practice is to specify vhost specific log paths inside each website's VirtualHost clause, thus splitting the log for each website out into separate files. http://httpd.apache.org/docs/2.2/mod/core.html#virtualhost Notice the ErrorLog directive inside the clause. Cheers, -- Cameron Simpson "What the hell are we supposed to use, man -- harsh language?" "Flame units only." - _Aliens_ From nikos.gr33k at gmail.com Wed Aug 28 05:48:13 2013 From: nikos.gr33k at gmail.com (Ferrous Cranus) Date: Wed, 28 Aug 2013 02:48:13 -0700 (PDT) Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: References: Message-ID: <39be9227-c800-49a5-850d-f387c30d1e9d@googlegroups.com> ?? ?????, 27 ????????? 2013 6:22:32 ?.?. UTC+3, ? ??????? ishish ??????: > Am 27.08.2013 16:04, schrieb Ferrous Cranus: > > > ???? 27/8/2013 4:59 ??, ?/? ishish ??????: > > >>> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] Error > > >>> in > > >>> sys.excepthook: > > >>> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] > > >>> ValueError: underlying buffer has been detached > > >>> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] > > >>> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] > > >>> Original > > >>> exception was: > > >>> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] > > >>> Traceback > > >>> (most recent call last): > > >>> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] File > > >>> "/home/nikos/public_html/cgi-bin/metrites.py", line 169, in > > >>> > > >>> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] > > >>> cur.execute('''SELECT ID FROM counters WHERE url = %s''', page ) > > >>> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] File > > >>> > > >>> > > >>> "/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py", > > >>> line 108, in execute > > >>> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] > > >>> query > > >>> = query % escaped_args > > >>> [Tue Aug 27 13:02:57 2013] [error] [client 110.202.175.189] > > >>> TypeError: unsupported operand type(s) for %: 'bytes' and 'str' > > >> > > >> I quote from a Python 3 Guide > > >> [http://python.about.com/od/python30/ss/30_strings_3.htm]: > > >> > > >> The two data types, str and bytes, are mutually exclusive. One > > >> cannot > > >> legally combine them into one call. With the distinction between > > >> text > > >> and data, therefore, comes the need to convert between them. > > >> > > >> > > > > > > So, in this line: > > > > > > cur.execute('''SELECT ID FROM counters WHERE url = %s''', page ) > > > > > > the variable 'page' needs conversion to what? > > > > > > all that is stores is the location of a file > > > > > > path = '/home/nikos/public_html/' > > > > > > page = form.getvalue('page') > > > > > > if not page and os.path.exists( file ): > > > # it is an html template > > > page = file.replace( path, '' ) > > > > > > So chnage it to what? > > > -- > > > What is now proved was at first only imagined! > > > > > > The error occurs in file > > "/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py", > > line 108, in execute > > query = query % escaped_args > > > > You can check the actual values and data types using: > > > > print repr(query) > > print type(query) > > > > print repr(escaped_args) > > print type(escaped_args) > > > > Always useful is a proper exception handling using try:/except: hOW YOU MEAN PLEASE I TRY TO IMPLMENT WHAT YOU PROPOSE BUT I CANT. try: #find the needed counter for the page URL if os.path.exists( path + page ) or os.path.exists( cgi_path + page ): cur.execute('''SELECT ID FROM counters WHERE url = %s''', page ) data = cur.fetchone() #URL is unique, so should only be one print repr('''SELECT ID FROM counters WHERE url = %s''') print type('''SELECT ID FROM counters WHERE url = %s''') print repr(escaped_args) print type(escaped_args) I MEAN IF FAILS BEFORE IT TRIES TO GO INTO MY PRINT STAEMNT. HOW AM I GONNA CPATURE THE QUERY? From ishish at domhain.de Wed Aug 28 06:11:05 2013 From: ishish at domhain.de (ishish) Date: Wed, 28 Aug 2013 11:11:05 +0100 Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: <39be9227-c800-49a5-850d-f387c30d1e9d@googlegroups.com> References: <39be9227-c800-49a5-850d-f387c30d1e9d@googlegroups.com> Message-ID: <3f05e5c528e867f69a9d77cbfc241e5f@home.minuskel.de> Am 28.08.2013 10:48, schrieb Ferrous Cranus: >> >> I quote from a Python 3 Guide >> >> >> [http://python.about.com/od/python30/ss/30_strings_3.htm]: >> >> >> >> >> >> The two data types, str and bytes, are mutually exclusive. One >> >> >> cannot >> >> >> legally combine them into one call. With the distinction between >> >> >> text >> >> >> and data, therefore, comes the need to convert between them. >> >> >> >> >> >> >> >> > >> >> > So, in this line: >> >> > >> >> > cur.execute('''SELECT ID FROM counters WHERE url = %s''', page ) >> >> > >> >> > the variable 'page' needs conversion to what? >> >> > >> >> > all that is stores is the location of a file >> >> > >> >> > path = '/home/nikos/public_html/' >> >> > >> >> > page = form.getvalue('page') >> >> > >> >> > if not page and os.path.exists( file ): >> >> > # it is an html template >> >> > page = file.replace( path, '' ) >> >> > >> >> > So chnage it to what? >> >> > -- >> >> > What is now proved was at first only imagined! >> >> >> >> >> >> The error occurs in file >> >> >> "/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py", >> >> line 108, in execute >> >> query = query % escaped_args >> >> >> >> You can check the actual values and data types using: >> >> >> >> print repr(query) >> >> print type(query) >> >> >> >> print repr(escaped_args) >> >> print type(escaped_args) >> >> >> >> Always useful is a proper exception handling using try:/except: > > > hOW YOU MEAN PLEASE I TRY TO IMPLMENT WHAT YOU PROPOSE BUT I CANT. > > try: > #find the needed counter for the page URL > if os.path.exists( path + page ) or os.path.exists( cgi_path + page > ): > cur.execute('''SELECT ID FROM counters WHERE url = %s''', page ) > data = cur.fetchone() #URL is unique, so should only be one > > > print repr('''SELECT ID FROM counters WHERE url = %s''') > print type('''SELECT ID FROM counters WHERE url = %s''') > > print repr(escaped_args) > print type(escaped_args) > > I MEAN IF FAILS BEFORE IT TRIES TO GO INTO MY PRINT STAEMNT. > HOW AM I GONNA CPATURE THE QUERY? http://i.stack.imgur.com/jiFfM.jpg http://wiki.python.org/moin/HandlingExceptions From nikos.gr33k at gmail.com Wed Aug 28 06:43:08 2013 From: nikos.gr33k at gmail.com (Ferrous Cranus) Date: Wed, 28 Aug 2013 03:43:08 -0700 (PDT) Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: References: <39be9227-c800-49a5-850d-f387c30d1e9d@googlegroups.com> Message-ID: ?? ???????, 28 ????????? 2013 1:11:05 ?.?. UTC+3, ? ??????? ishish ??????: > http://wiki.python.org/moin/HandlingExceptions is this how you mean? try: #find the needed counter for the page URL if os.path.exists( path + page ) or os.path.exists( cgi_path + page ): cur.execute('''SELECT ID FROM counters WHERE url = %s''', page ) data = cur.fetchone() #URL is unique, so should only be one except: print repr(query) print type(query) print repr(escaped_args) print type(escaped_args) but i cannot see the error_log because of constant scrolling of error output. From nikos.gr33k at gmail.com Wed Aug 28 06:46:00 2013 From: nikos.gr33k at gmail.com (Ferrous Cranus) Date: Wed, 28 Aug 2013 03:46:00 -0700 (PDT) Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: References: <39be9227-c800-49a5-850d-f387c30d1e9d@googlegroups.com> Message-ID: ?? ???????, 28 ????????? 2013 1:43:08 ?.?. UTC+3, ? ??????? Ferrous Cranus ??????: > ?? ???????, 28 ????????? 2013 1:11:05 ?.?. UTC+3, ? ??????? ishish ??????: > > > > > http://wiki.python.org/moin/HandlingExceptions > > > > is this how you mean? > > > > try: > > #find the needed counter for the page URL > > if os.path.exists( path + page ) or os.path.exists( cgi_path + page ): > > cur.execute('''SELECT ID FROM counters WHERE url = %s''', page ) > > data = cur.fetchone() #URL is unique, so should only be one > > except: > > print repr(query) > > print type(query) > > > > print repr(escaped_args) > > print type(escaped_args) > > > > > > but i cannot see the error_log because of constant scrolling of error output. i try: tail -F /usr/local/apache/logs/error_log | grep nikos &' and see the follwing. if i dont grep at all i cannot stop the eroor output: ikos at superhost.gr [~]# [Wed Aug 28 10:44:22 2013] [error] [client 108.162.231.120] File "/home/nikos/public_html/cgi-bin/metrites.py", line 173, in [Wed Aug 28 10:44:22 2013] [error] [client 108.162.231.120] File "/home/nikos/public_html/cgi-bin/metrites.py", line 176, in [Wed Aug 28 10:44:22 2013] [error] [client 108.162.231.120] File does not exist: /home/nikos/public_html/500.shtml From nikos.gr33k at gmail.com Wed Aug 28 07:14:37 2013 From: nikos.gr33k at gmail.com (Ferrous Cranus) Date: Wed, 28 Aug 2013 04:14:37 -0700 (PDT) Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: References: <39be9227-c800-49a5-850d-f387c30d1e9d@googlegroups.com> Message-ID: <73c7b08d-5349-49ba-b945-6d87795088bf@googlegroups.com> Okey, continue trying and trying i came up with this: try: if os.path.exists( path + page ) or os.path.exists( cgi_path + page ): cur.execute('''SELECT ID FROM counters WHERE url = %s''', page ) data = cur.fetchone() except: with open("err.out", "a") as f: f.write( repr(query), type(query) ) f.write( repr(escaped_args), type(escaped_args) ) But i cannot test it without looking at the error log which is scrolling like hell and doesn't even quit with a ctrl+c How will i manage to troubleshoot? Please confirm the above is correct and is what you were propsoing i shoudl test. From davea at davea.name Wed Aug 28 07:32:44 2013 From: davea at davea.name (Dave Angel) Date: Wed, 28 Aug 2013 11:32:44 +0000 (UTC) Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of References: <39be9227-c800-49a5-850d-f387c30d1e9d@googlegroups.com> <73c7b08d-5349-49ba-b945-6d87795088bf@googlegroups.com> Message-ID: On 28/8/2013 07:14, Ferrous Cranus wrote: > > > But i cannot test it without looking at the error log which is scrolling like hell and doesn't even quit with a ctrl+c I take it this 'error log" is shared with other users, and you can't constrain them to cease and desist for a while? > > How will i manage to troubleshoot? > Please confirm the above is correct and is what you were propsoing i shoudl test. You really have no directory in which you have write permissions? If so, perhaps you'd better solve that first. -- DaveA From nikos.gr33k at gmail.com Wed Aug 28 07:35:47 2013 From: nikos.gr33k at gmail.com (Ferrous Cranus) Date: Wed, 28 Aug 2013 04:35:47 -0700 (PDT) Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: References: <39be9227-c800-49a5-850d-f387c30d1e9d@googlegroups.com> <73c7b08d-5349-49ba-b945-6d87795088bf@googlegroups.com> Message-ID: <92cad8d9-3d81-418a-89a1-cba0b35423e7@googlegroups.com> ?? ???????, 28 ????????? 2013 2:32:44 ?.?. UTC+3, ? ??????? Dave Angel ??????: > On 28/8/2013 07:14, Ferrous Cranus wrote: > > > > > > > > > > > But i cannot test it without looking at the error log which is scrolling like hell and doesn't even quit with a ctrl+c > > > > I take it this 'error log" is shared with other users, and you can't > > constrain them to cease and desist for a while? > > > > > > > > How will i manage to troubleshoot? > > > Please confirm the above is correct and is what you were propsoing i shoudl test. > > > > You really have no directory in which you have write permissions? If > > so, perhaps you'd better solve that first. > > > > > > -- > > DaveA [Wed Aug 28 11:34:36 2013] [error] [client 108.162.231.120] Error in sys.excepthook: [Wed Aug 28 11:34:36 2013] [error] [client 108.162.231.120] ValueError: underlying buffer has been detached [Wed Aug 28 11:34:36 2013] [error] [client 108.162.231.120] [Wed Aug 28 11:34:36 2013] [error] [client 108.162.231.120] Original exception was: [Wed Aug 28 11:34:36 2013] [error] [client 108.162.231.120] Traceback (most recent call last): [Wed Aug 28 11:34:36 2013] [error] [client 108.162.231.120] File "/home/nikos/public_html/cgi-bin/metrites. py", line 173, in [Wed Aug 28 11:34:36 2013] [error] [client 108.162.231.120] cur.execute('''SELECT ID FROM counters WHERE url = %s''', page ) [Wed Aug 28 11:34:36 2013] [error] [client 108.162.231.120] File "/usr/local/bin/python/lib/python3.3/site- packages/pymysql/cursors.py", line 108, in execute [Wed Aug 28 11:34:36 2013] [error] [client 108.162.231.120] query = query % escaped_args [Wed Aug 28 11:34:36 2013] [error] [client 108.162.231.120] TypeError: unsupported operand type(s) for %: 'by tes' and 'str' [Wed Aug 28 11:34:36 2013] [error] [client 108.162.231.120] [Wed Aug 28 11:34:36 2013] [error] [client 108.162.231.120] During handling of the above exception, another e xception occurred: [Wed Aug 28 11:34:36 2013] [error] [client 108.162.231.120] [Wed Aug 28 11:34:36 2013] [error] [client 108.162.231.120] Traceback (most recent call last): [Wed Aug 28 11:34:36 2013] [error] [client 108.162.231.120] File "/home/nikos/public_html/cgi-bin/metrites. py", line 176, in [Wed Aug 28 11:34:36 2013] [error] [client 108.162.231.120] with open("err.out", "a") as f: [Wed Aug 28 11:34:36 2013] [error] [client 108.162.231.120] PermissionError: [Errno 13] \\u0386\\u03c1\\u03bd \\u03b7\\u03c3\\u03b7 \\u03c0\\u03c1\\u03cc\\u03c3\\u03b2\\u03b1\\u03c3\\u03b7\\u03c2: 'err.out' [Wed Aug 28 11:34:36 2013] [error] [client 108.162.231.120] File does not exist: /home/nikos/public_html/500. shtml From nikos.gr33k at gmail.com Wed Aug 28 07:38:25 2013 From: nikos.gr33k at gmail.com (Ferrous Cranus) Date: Wed, 28 Aug 2013 04:38:25 -0700 (PDT) Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: References: <39be9227-c800-49a5-850d-f387c30d1e9d@googlegroups.com> <73c7b08d-5349-49ba-b945-6d87795088bf@googlegroups.com> Message-ID: ?? ???????, 28 ????????? 2013 2:32:44 ?.?. UTC+3, ? ??????? Dave Angel ??????: > On 28/8/2013 07:14, Ferrous Cranus wrote: > > > > > > > > > > > But i cannot test it without looking at the error log which is scrolling like hell and doesn't even quit with a ctrl+c > > > > I take it this 'error log" is shared with other users, and you can't > > constrain them to cease and desist for a while? > > > > > > > > How will i manage to troubleshoot? > > > Please confirm the above is correct and is what you were propsoing i shoudl test. > > > > You really have no directory in which you have write permissions? If > > so, perhaps you'd better solve that first. hello Dave, no this is the general error log apache produces for all the server. Is there a way to grep error logging info, pertainign only to my specific nikos account or my superhost.gr domain? From davea at davea.name Wed Aug 28 14:40:43 2013 From: davea at davea.name (Dave Angel) Date: Wed, 28 Aug 2013 18:40:43 +0000 (UTC) Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of References: <39be9227-c800-49a5-850d-f387c30d1e9d@googlegroups.com> <73c7b08d-5349-49ba-b945-6d87795088bf@googlegroups.com> Message-ID: On 28/8/2013 07:38, Ferrous Cranus wrote: > > no this is the general error log apache produces for all the server. > > Is there a way to grep error logging info, pertainign only to my specific nikos account or my superhost.gr domain? I now nothing about Apache logs, but how about grepping the client url ? [client 108.162.231.120] I still don't understand why you don't find or create a directory that "nobody" has write access to, so you can be more flexible about what information you log. Or strip the problem down to a simple "page" that displays what print emits in a simple form. -- DaveA From nikos.gr33k at gmail.com Wed Aug 28 07:55:54 2013 From: nikos.gr33k at gmail.com (Ferrous Cranus) Date: Wed, 28 Aug 2013 04:55:54 -0700 (PDT) Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: References: <39be9227-c800-49a5-850d-f387c30d1e9d@googlegroups.com> <73c7b08d-5349-49ba-b945-6d87795088bf@googlegroups.com> Message-ID: <85e059ef-9717-4101-910a-379a5ebd0522@googlegroups.com> ?? ???????, 28 ????????? 2013 2:32:44 ?.?. UTC+3, ? ??????? Dave Angel ??????: > On 28/8/2013 07:14, Ferrous Cranus wrote: > > > > > > > > > > > But i cannot test it without looking at the error log which is scrolling like hell and doesn't even quit with a ctrl+c > > > > I take it this 'error log" is shared with other users, and you can't > > constrain them to cease and desist for a while? > > > > > > > > How will i manage to troubleshoot? > > > Please confirm the above is correct and is what you were propsoing i shoudl test. > > > > You really have no directory in which you have write permissions? If > > so, perhaps you'd better solve that first. of cours ei ahve write permissions. Here: nikos at superhost.gr [~]# ls -ld www/ drwxr-x--- 4 nikos nobody 4096 Jul 13 10:33 www// nikos at superhost.gr [~]# ls -ld www/cgi-bin/ drwxr-xr-x 2 nikos nikos 4096 Aug 28 10:41 www/cgi-bin// whick make it a mysterya s to why with open("../err.out", "a") as f: fails to write the file. From ulrich.eckhardt at dominolaser.com Wed Aug 28 09:38:02 2013 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Wed, 28 Aug 2013 15:38:02 +0200 Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: <85e059ef-9717-4101-910a-379a5ebd0522@googlegroups.com> References: <39be9227-c800-49a5-850d-f387c30d1e9d@googlegroups.com> <73c7b08d-5349-49ba-b945-6d87795088bf@googlegroups.com> <85e059ef-9717-4101-910a-379a5ebd0522@googlegroups.com> Message-ID: Am 28.08.2013 13:55, schrieb Ferrous Cranus: > ?? ???????, 28 ????????? 2013 2:32:44 ?.?. UTC+3, ? ??????? Dave Angel ??????: >> You really have no directory in which you have write permissions? If >> so, perhaps you'd better solve that first. > > > of cours ei ahve write permissions. Here: > > nikos at superhost.gr [~]# ls -ld www/ > drwxr-x--- 4 nikos nobody 4096 Jul 13 10:33 www// > nikos at superhost.gr [~]# ls -ld www/cgi-bin/ > drwxr-xr-x 2 nikos nikos 4096 Aug 28 10:41 www/cgi-bin// > > > whick make it a mysterya s to why > with open("../err.out", "a") as f: > fails to write the file. ...maybe it's because the server is not running as user nikos? Uli From nikos.gr33k at gmail.com Wed Aug 28 10:58:37 2013 From: nikos.gr33k at gmail.com (Ferrous Cranus) Date: Wed, 28 Aug 2013 07:58:37 -0700 (PDT) Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: References: <39be9227-c800-49a5-850d-f387c30d1e9d@googlegroups.com> <73c7b08d-5349-49ba-b945-6d87795088bf@googlegroups.com> <85e059ef-9717-4101-910a-379a5ebd0522@googlegroups.com> Message-ID: <68f49226-7f00-489b-bfc0-267bd00aa356@googlegroups.com> ?? ???????, 28 ????????? 2013 4:38:02 ?.?. UTC+3, ? ??????? Ulrich Eckhardt ??????: > Am 28.08.2013 13:55, schrieb Ferrous Cranus: > > > ?? ???????, 28 ????????? 2013 2:32:44 ?.?. UTC+3, ? ??????? Dave Angel ??????: > > >> You really have no directory in which you have write permissions? If > > >> so, perhaps you'd better solve that first. > > > > > > > > > of cours ei ahve write permissions. Here: > > > > > > nikos at superhost.gr [~]# ls -ld www/ > > > drwxr-x--- 4 nikos nobody 4096 Jul 13 10:33 www// > > > nikos at superhost.gr [~]# ls -ld www/cgi-bin/ > > > drwxr-xr-x 2 nikos nikos 4096 Aug 28 10:41 www/cgi-bin// > > > > > > > > > whick make it a mysterya s to why > > > with open("../err.out", "a") as f: > > > fails to write the file. > > > > ...maybe it's because the server is not running as user nikos? > > > > Uli Yes Uli, the script metrits.py is being invoked by Apache Web Server which in turn runs under user Nobody. So, that mean that? user 'nobody' has no write permission to /home/nikos folder? From ramit.prasad at jpmorgan.com.dmarc.invalid Wed Aug 28 14:44:28 2013 From: ramit.prasad at jpmorgan.com.dmarc.invalid (Prasad, Ramit) Date: Wed, 28 Aug 2013 18:44:28 +0000 Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: <68f49226-7f00-489b-bfc0-267bd00aa356@googlegroups.com> References: <39be9227-c800-49a5-850d-f387c30d1e9d@googlegroups.com> <73c7b08d-5349-49ba-b945-6d87795088bf@googlegroups.com> <85e059ef-9717-4101-910a-379a5ebd0522@googlegroups.com> <68f49226-7f00-489b-bfc0-267bd00aa356@googlegroups.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF47418695494@SCACMX008.exchad.jpmchase.net> Ferrous Cranus wrote: > Yes Uli, the script metrits.py is being invoked by Apache Web Server which in turn runs under user > Nobody. > So, that mean that? user 'nobody' has no write permission to /home/nikos folder? Yes. You should make it group writable with "nobody" as the group. Use chmod and chown to change permissions and owners (i.e. groups). As a last resort (for testing purposes only!) you can set the directory "world writable", but then *anyone* with access to that host can write/delete/destroy the contents of that directory. This is obviously very insecure and not a good idea. But if you are only trying to get a quick error message then it might work for you. Normally I would have thought you would have a public_html or www directory in your home folder that would be readable/writable to the web server (and where you should write). ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From rosuav at gmail.com Wed Aug 28 18:55:34 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 29 Aug 2013 08:55:34 +1000 Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: <5B80DD153D7D744689F57F4FB69AF47418695494@SCACMX008.exchad.jpmchase.net> References: <39be9227-c800-49a5-850d-f387c30d1e9d@googlegroups.com> <73c7b08d-5349-49ba-b945-6d87795088bf@googlegroups.com> <85e059ef-9717-4101-910a-379a5ebd0522@googlegroups.com> <68f49226-7f00-489b-bfc0-267bd00aa356@googlegroups.com> <5B80DD153D7D744689F57F4FB69AF47418695494@SCACMX008.exchad.jpmchase.net> Message-ID: On Thu, Aug 29, 2013 at 4:44 AM, Prasad, Ramit wrote: > Normally I would have thought you would have a public_html or www directory in your > home folder that would be readable/writable to the web server (and where you should > write). No, a normal setup would have that world-readable but not writable. That way, even if an exploit is found in your web site that lets an attacker write files, s/he can't upload more files to the web server's directory and start running them. A directory writable by the web server might be /tmp. ChrisA From cs at zip.com.au Wed Aug 28 20:59:49 2013 From: cs at zip.com.au (Cameron Simpson) Date: Thu, 29 Aug 2013 10:59:49 +1000 Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: <5B80DD153D7D744689F57F4FB69AF47418695494@SCACMX008.exchad.jpmchase.net> References: <5B80DD153D7D744689F57F4FB69AF47418695494@SCACMX008.exchad.jpmchase.net> Message-ID: <20130829005949.GA36067@cskk.homeip.net> On 28Aug2013 18:44, Prasad, Ramit wrote: | Ferrous Cranus wrote: | > Yes Uli, the script metrits.py is being invoked by Apache Web Server which in turn runs under user | > Nobody. | > So, that mean that? user 'nobody' has no write permission to /home/nikos folder? As Ramit says, yes. Your own directory listing earlier showed "r-x" for the group "nobody"; that says "no write". The file was the same: "r--". | Yes. You should make it group writable with "nobody" as the group. Use chmod and chown | to change permissions and owners (i.e. groups). To be explicit: chgrp nobody the-file chmod g+w the-file | Normally I would have thought you would have a public_html or www directory in your | home folder that would be readable/writable to the web server (and where you should | write). Readable, yes. Writable? Generally mad. Apaches are often configured to run as "nobody" or "nofiles" specificly to ensure that it cannot write to stuff. The last thing you want is to have the website's files writable by the apache; it is asking for defacements and other hacks. Nikos should make a specific directory for these files and give ONLY THAT write permission for "nobody". And then use a full pathname to the directory in his CGI script - a relative path makes ill founded assumptions about the current working directory of the script. Cheers, -- Cameron Simpson A gentleman does not go motoring about after dark. - J. Lucas From nikos.gr33k at gmail.com Thu Aug 29 00:29:20 2013 From: nikos.gr33k at gmail.com (Ferrous Cranus) Date: Wed, 28 Aug 2013 21:29:20 -0700 (PDT) Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: References: <5B80DD153D7D744689F57F4FB69AF47418695494@SCACMX008.exchad.jpmchase.net> Message-ID: ?? ??????, 29 ????????? 2013 3:59:49 ?.?. UTC+3, ? ??????? Cameron Simpson ??????: > To be explicit: > > > > chgrp nobody the-file > > chmod g+w the-file Hello Cameron, nikos at superhost.gr [~/www]# touch err.out nikos at superhost.gr [~/www]# ls -l err.out -rw-rw-r-- 1 nikos nikos 0 Aug 29 04:26 err.out nikos at superhost.gr [~/www]# chgrp nobody err.out chgrp: changing group of `err.out': Operation not permitted As you can see the system does not let me alter even the groupname of a newly created text file. As, for the 'suexec' that you mentioned in another ;post of yours, old VPS had it enabled yes, the new one does not. Should i ask them to enable it back? Would it help if we enabled it back? From nikos at superhost.gr Thu Aug 29 00:49:51 2013 From: nikos at superhost.gr (Nick the Gr33k) Date: Thu, 29 Aug 2013 07:49:51 +0300 Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: References: <5B80DD153D7D744689F57F4FB69AF47418695494@SCACMX008.exchad.jpmchase.net> Message-ID: ???? 29/8/2013 7:29 ??, ?/? Ferrous Cranus ??????: > ?? ??????, 29 ????????? 2013 3:59:49 ?.?. UTC+3, ? ??????? Cameron Simpson ??????: > >> To be explicit: >> >> >> >> chgrp nobody the-file >> >> chmod g+w the-file > Hello Cameron, > > nikos at superhost.gr [~/www]# touch err.out > nikos at superhost.gr [~/www]# ls -l err.out > -rw-rw-r-- 1 nikos nikos 0 Aug 29 04:26 err.out > nikos at superhost.gr [~/www]# chgrp nobody err.out > chgrp: changing group of `err.out': Operation not permitted > > As you can see the system does not let me alter even the groupname of a newly created text file. > > As, for the 'suexec' that you mentioned in another ;post of yours, old VPS had it enabled yes, the new one does not. Should i ask them to enable it back? > Would it help if we enabled it back? Perhaps 'suexec' would allow Apache(running under user nobody) to switch to another more privileged user prior of writing to '/home/nikos/www/err.out' -- What is now proved was at first only imagined! From steve+comp.lang.python at pearwood.info Wed Aug 28 18:49:40 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 Aug 2013 22:49:40 GMT Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of References: <39be9227-c800-49a5-850d-f387c30d1e9d@googlegroups.com> <73c7b08d-5349-49ba-b945-6d87795088bf@googlegroups.com> <85e059ef-9717-4101-910a-379a5ebd0522@googlegroups.com> <68f49226-7f00-489b-bfc0-267bd00aa356@googlegroups.com> Message-ID: <521e7e84$0$6599$c3e8da3$5496439d@news.astraweb.com> On Wed, 28 Aug 2013 18:44:28 +0000, Prasad, Ramit wrote: > Normally I would have thought you would have a public_html or www > directory in your home folder that would be readable/writable to the web > server (and where you should write). I expect that he does. But Nikos has tried writing to the Nikos home directory, and to .. (the parent directory). I don't believe he has tried writing to . (the current directory), or to /tmp. I'm not an expert, but I expect that Apache should be able to write to one of those. And if not, he has root on his system, he can simply create a writable directory and write to that. I expect that there are security implications of having a directories writable to the webserver user, but temporarily creating one for a quick debugging aid should be okay. -- Steven From joel.goldstick at gmail.com Wed Aug 28 18:56:56 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Wed, 28 Aug 2013 18:56:56 -0400 Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: <521e7e84$0$6599$c3e8da3$5496439d@news.astraweb.com> References: <39be9227-c800-49a5-850d-f387c30d1e9d@googlegroups.com> <73c7b08d-5349-49ba-b945-6d87795088bf@googlegroups.com> <85e059ef-9717-4101-910a-379a5ebd0522@googlegroups.com> <68f49226-7f00-489b-bfc0-267bd00aa356@googlegroups.com> <521e7e84$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Wed, Aug 28, 2013 at 6:49 PM, Steven D'Aprano wrote: > On Wed, 28 Aug 2013 18:44:28 +0000, Prasad, Ramit wrote: > >> Normally I would have thought you would have a public_html or www >> directory in your home folder that would be readable/writable to the web >> server (and where you should write). > > I expect that he does. But Nikos has tried writing to the Nikos home > directory, and to .. (the parent directory). I don't believe he has tried > writing to . (the current directory), or to /tmp. I'm not an expert, but > I expect that Apache should be able to write to one of those. And if not, > he has root on his system, he can simply create a writable directory and > write to that. > > I expect that there are security implications of having a directories > writable to the webserver user, but temporarily creating one for a quick > debugging aid should be okay. > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list This thread follows well travelled grounds from several of the previous Nikos appearances here. He seems to have problems grasping permissions as they relate to apache. He seems to demand cook book answers as compared to understanding the issues. At any rate, isn't this stuff really something that the Web Server company should be helping him with? Its their server, they know how it is configured, and they can quickly look in his directories to figure out permissions related issues. -- Joel Goldstick http://joelgoldstick.com From steve+comp.lang.python at pearwood.info Wed Aug 28 19:36:39 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 Aug 2013 23:36:39 GMT Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of References: <39be9227-c800-49a5-850d-f387c30d1e9d@googlegroups.com> <73c7b08d-5349-49ba-b945-6d87795088bf@googlegroups.com> <85e059ef-9717-4101-910a-379a5ebd0522@googlegroups.com> <68f49226-7f00-489b-bfc0-267bd00aa356@googlegroups.com> <521e7e84$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <521e8987$0$6599$c3e8da3$5496439d@news.astraweb.com> On Wed, 28 Aug 2013 18:56:56 -0400, Joel Goldstick wrote: > At any rate, isn't > this stuff really something that the Web Server company should be > helping him with? Its their server, they know how it is configured, and > they can quickly look in his directories to figure out permissions > related issues. Perhaps. But Nikos is reporting that his log file shows entries from all the other websites on the server, which doesn't sound good to me. Surely any decent, competent web server company would be able to ensure that each customer sees only their own log entries? -- Steven From ishish at domhain.de Wed Aug 28 07:51:03 2013 From: ishish at domhain.de (ishish) Date: Wed, 28 Aug 2013 12:51:03 +0100 Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: <73c7b08d-5349-49ba-b945-6d87795088bf@googlegroups.com> References: <39be9227-c800-49a5-850d-f387c30d1e9d@googlegroups.com> <73c7b08d-5349-49ba-b945-6d87795088bf@googlegroups.com> Message-ID: <42669b3edb1f4d4cb0e62963da050b66@home.minuskel.de> Am 28.08.2013 12:14, schrieb Ferrous Cranus: > Okey, continue trying and trying i came up with this: > > > try: > if os.path.exists( path + page ) or os.path.exists( cgi_path + page > ): > cur.execute('''SELECT ID FROM counters WHERE url = %s''', page ) > data = cur.fetchone() > except: > with open("err.out", "a") as f: > f.write( repr(query), type(query) ) > f.write( repr(escaped_args), type(escaped_args) ) > > > But i cannot test it without looking at the error log which is > scrolling like hell and doesn't even quit with a ctrl+c > > How will i manage to troubleshoot? > Please confirm the above is correct and is what you were propsoing i > shoudl test. A simple way I always use if it comes to exception handling: import sys import traceback # Exception Identification def formatExceptionInfo(maxTBlevel=5): cla, exc, trbk = sys.exc_info() excName = cla.__name__ try: excArgs = exc.__dict__["args"] except KeyError: excArgs = "" excTb = traceback.format_tb(trbk, maxTBlevel) return (excName, excArgs, excTb) try: # Your code except: print formatExceptionInfo() From nikos.gr33k at gmail.com Wed Aug 28 08:03:12 2013 From: nikos.gr33k at gmail.com (Ferrous Cranus) Date: Wed, 28 Aug 2013 05:03:12 -0700 (PDT) Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: References: <39be9227-c800-49a5-850d-f387c30d1e9d@googlegroups.com> <73c7b08d-5349-49ba-b945-6d87795088bf@googlegroups.com> Message-ID: <8289446e-2703-4406-8a2f-47622c563978@googlegroups.com> ?? ???????, 28 ????????? 2013 2:51:03 ?.?. UTC+3, ? ??????? ishish ??????: > Am 28.08.2013 12:14, schrieb Ferrous Cranus: > > > Okey, continue trying and trying i came up with this: > > > > > > > > > try: > > > if os.path.exists( path + page ) or os.path.exists( cgi_path + page > > > ): > > > cur.execute('''SELECT ID FROM counters WHERE url = %s''', page ) > > > data = cur.fetchone() > > > except: > > > with open("err.out", "a") as f: > > > f.write( repr(query), type(query) ) > > > f.write( repr(escaped_args), type(escaped_args) ) > > > > > > > > > But i cannot test it without looking at the error log which is > > > scrolling like hell and doesn't even quit with a ctrl+c > > > > > > How will i manage to troubleshoot? > > > Please confirm the above is correct and is what you were propsoing i > > > shoudl test. > > > > A simple way I always use if it comes to exception handling: > > > > import sys > > import traceback > > > > # Exception Identification > > def formatExceptionInfo(maxTBlevel=5): > > cla, exc, trbk = sys.exc_info() > > excName = cla.__name__ > > try: > > excArgs = exc.__dict__["args"] > > except KeyError: > > excArgs = "" > > excTb = traceback.format_tb(trbk, maxTBlevel) > > return (excName, excArgs, excTb) > > > > try: > > # Your code > > except: > > print formatExceptionInfo() nikos at superhost.gr [~/www]# [Wed Aug 28 12:02:53 2013] [error] [client 108.162.231.120] malformed header from script. Bad header= 'index.html': metrites.py [Wed Aug 28 12:02:53 2013] [error] [client 108.162.231.120] Error in sys.excepthook: [Wed Aug 28 12:02:53 2013] [error] [client 108.162.231.120] ValueError: underlying buffer has been detached [Wed Aug 28 12:02:53 2013] [error] [client 108.162.231.120] [Wed Aug 28 12:02:53 2013] [error] [client 108.162.231.120] Original exception was: [Wed Aug 28 12:02:53 2013] [error] [client 108.162.231.120] Traceback (most recent call last): [Wed Aug 28 12:02:53 2013] [error] [client 108.162.231.120] File "/home/nikos/public_html/cgi-bin/metrites.py", line 191, in [Wed Aug 28 12:02:53 2013] [error] [client 108.162.231.120] if not data: [Wed Aug 28 12:02:53 2013] [error] [client 108.162.231.120] NameError: name 'data' is not defined [Wed Aug 28 12:02:53 2013] [error] [client 108.162.231.120] File does not exist: /home/nikos/public_html/500.shtml From ishish at domhain.de Wed Aug 28 08:16:15 2013 From: ishish at domhain.de (ishish) Date: Wed, 28 Aug 2013 13:16:15 +0100 Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: <8289446e-2703-4406-8a2f-47622c563978@googlegroups.com> References: <39be9227-c800-49a5-850d-f387c30d1e9d@googlegroups.com> <73c7b08d-5349-49ba-b945-6d87795088bf@googlegroups.com> <8289446e-2703-4406-8a2f-47622c563978@googlegroups.com> Message-ID: Well there you have it: File "/home/nikos/public_html/cgi-bin/metrites.py", line 191, in if not data: NameError: name 'data' is not defined From steve+comp.lang.python at pearwood.info Wed Aug 28 08:21:25 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 Aug 2013 12:21:25 GMT Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of References: <39be9227-c800-49a5-850d-f387c30d1e9d@googlegroups.com> Message-ID: <521deb44$0$6599$c3e8da3$5496439d@news.astraweb.com> On Wed, 28 Aug 2013 03:43:08 -0700, Ferrous Cranus wrote: > but i cannot see the error_log because of constant scrolling of error > output. Then don't use "tail -F", use "less". Or try "tail -s 60 -F" which will update only every 60 seconds. -- Steven From nikos.gr33k at gmail.com Wed Aug 28 08:36:39 2013 From: nikos.gr33k at gmail.com (Ferrous Cranus) Date: Wed, 28 Aug 2013 05:36:39 -0700 (PDT) Subject: [error] [client 178.59.111.223] (2)No such file or directory: exec of In-Reply-To: <521deb44$0$6599$c3e8da3$5496439d@news.astraweb.com> References: <39be9227-c800-49a5-850d-f387c30d1e9d@googlegroups.com> <521deb44$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <59c28319-f3ef-4204-9e1a-63b9a0af1a7c@googlegroups.com> ?? ???????, 28 ????????? 2013 3:21:25 ?.?. UTC+3, ? ??????? Steven D'Aprano ??????: > On Wed, 28 Aug 2013 03:43:08 -0700, Ferrous Cranus wrote: > > > > > but i cannot see the error_log because of constant scrolling of error > > > output. > > > > Then don't use "tail -F", use "less". > > > > Or try "tail -s 60 -F" which will update only every 60 seconds. > > > > > > > > -- > > Steven Very nice idea, than k you steven. Less work withiut scrilling like hell liek tail -F did. this is what i have now: # Exception Identification def formatExceptionInfo(maxTBlevel=5): cla, exc, trbk = sys.exc_info() excName = cla.__name__ try: excArgs = exc.__dict__["args"] except KeyError: excArgs = "" excTb = traceback.format_tb(trbk, maxTBlevel) return (excName, excArgs, excTb) try: #find the needed counter for the page URL if os.path.exists( path + page ) or os.path.exists( cgi_path + page ): cur.execute('''SELECT ID FROM counters WHERE url = %s''', page ) data = cur.fetchone() #URL is unique, so should only be one except: print( formatExceptionInfo() ) i see no error on what it used ot be isnteait comain at the follwing line which is: if not data: #first time for page; primary key is automatic, hit is defaulted cur.execute('''INSERT INTO counters (url) VALUES (%s)''', page ) cID = cur.lastrowid #get the primary key value of the new record else: #found the page, save primary key and use it to issue hit UPDATE cID = data[0] cur.execute('''UPDATE counters SET hits = hits + 1 WHERE ID = %s''', cID ) i jst dont follow..... What ishsi proposed supposed to give us the error in the query bit now it tells difefrent things. shoudl i remove the function since it want any helopfull? From mohsen at pahlevanzadeh.org Tue Aug 27 13:54:35 2013 From: mohsen at pahlevanzadeh.org (Mohsen Pahlevanzadeh) Date: Tue, 27 Aug 2013 22:24:35 +0430 Subject: which better for me?session.query or session.execute? Message-ID: <1377626075.6487.4.camel@debian> Dear all, Suppose I want to write a function for better using SQLAlchemly dynamically with the following specification: 1. It get field names and values. 2. It get operators such as = , <> , like , between and so on. 3. It get logical operators such as and , or and not. 4. .... 5. .... I need it for update query ,select ,join and delete. I read the following stackoverflow link: SQLAlchemy Flask filter query to combine results from two models SQLAlchemy Query and_/or_ Issue sqlalchemy filter multiple columns How do I specify a relation in SQLAlchemy where one condition requires a column to be null? In sqlalchemy, how can I combine two queries by having a column entry identical? My Question: I neeed to write a string and concatenite above params, which are better? session.query or set of function such as update, delete and so on that run by execute function? You'll make me happy if answer in http://stackoverflow.com : http://stackoverflow.com/questions/18472314/which-better-session-query-or-session-execute-python-sqlalchemy Yours, Mohsen From josiah.carlson at gmail.com Tue Aug 27 15:13:46 2013 From: josiah.carlson at gmail.com (Josiah Carlson) Date: Tue, 27 Aug 2013 12:13:46 -0700 Subject: ANN: rom 0.19 - Redis object mapper for Python Message-ID: Hey everyone, As time progresses, so does my Redis object mapper. The "rom" package is a Redis object mapper for Python. It sports an interface similar to Django's ORM, SQLAlchemy + Elixir, or Appengine's datastore. The changelog for recent releases can be seen below my signature. You can find the package at: https://www.github.com/josiahcarlson/rom https://pypi.python.org/pypi/rom And docs can be found at: http://pythonhosted.org/rom/ Please CC me on any replies if you have any questions or comments. Thank you, - Josiah #----------------------------------- 0.19 ------------------------------------ [fixed] Thanks to a bug report by https://github.com/MickeyKim , was notified of a bug when using unique indexes, which is now fixed and has a testcase. #----------------------------------- 0.18 ------------------------------------ [fixed] Thanks to a bug report by https://github.com/MickeyKim , was notified and received an interim patch for a bug that could cause deleted entities to be resurrected on session.commit() or session.flush() . This has now been fixed and a testcase has been added. #----------------------------------- 0.17 ------------------------------------ [added] LGPL 3 licensing option. #----------------------------------- 0.16 ------------------------------------ [added] Thanks to a feature request from https://github.com/jrsmith , Boolean column support is now available. [added] DateTime, Date, and Time column types because I was already adding Boolean columns. -------------- next part -------------- An HTML attachment was scrubbed... URL: From mukeshtiwari.iiitm at gmail.com Tue Aug 27 15:41:10 2013 From: mukeshtiwari.iiitm at gmail.com (mukesh tiwari) Date: Tue, 27 Aug 2013 12:41:10 -0700 (PDT) Subject: Improving the web page download code. Message-ID: Hello All, I am doing web stuff first time in python so I am looking for suggestions. I wrote this code to download the title of webpages using as much less resource ( server time, data download) as possible and should be fast enough. Initially I used BeautifulSoup for parsing but the person who is going to use this code asked me not to use this and use regular expressions ( The reason was BeautifulSoup is not fast enough ? ). Also initially, I was downloading the the whole page but finally I restricted to only 30000 characters to get the title of almost all the pages. Write now I can see only two shortcomings of this code, one when I kill the code by SIGINT ( ctrl-c ) then it dies instantly. I can modify this code to process all the elements in queue and let it die. The second is one IO call per iteration in download url function ( May be I can use async IO call but I am not sure ). I don't have much web programming experience so I am looking for suggestion to make it more robust. top-1m.csv is file downloaded from alexa[1]. Also some suggestions to write more idiomatic python code. -Mukesh Tiwari [1]http://www.alexa.com/topsites. import urllib2, os, socket, Queue, thread, signal, sys, re class Downloader(): def __init__( self ): self.q = Queue.Queue( 200 ) self.count = 0 def downloadurl( self ) : #open a file in append mode and write the result ( Improvement think of writing in chunks ) with open('titleoutput.dat', 'a+' ) as file : while True : try : url = self.q.get( ) data = urllib2.urlopen ( url , data = None , timeout = 10 ).read( 30000 ) regex = re.compile('(.*?)' , re.IGNORECASE) #Read data line by line and as soon you find the title go out of loop. #title = None #for r in data: # if not r : # raise StopIteration # else: # title = regex.search( r ) # if title is not None: break title = regex.search( data ) result = ', '.join ( [ url , title.group(1) ] ) #data.close() file.write(''.join( [ result , '\n' ] ) ) except urllib2.HTTPError as e: print ''.join ( [ url, ' ', str ( e ) ] ) except urllib2.URLError as e: print ''.join ( [ url, ' ', str ( e ) ] ) except Exception as e : print ''.join ( [ url, ' ', str( e ) ] ) #With block python calls file.close() automatically. def createurl ( self ) : #check if file exist. If not then create one with default value of 0 bytes read. if os.path.exists('bytesread.dat'): f = open ( 'bytesread.dat','r') self.count = int ( f.readline() ) else: f=open('bytesread.dat','w') f.write('0\n') f.close() #Reading data in chunks is fast but we can miss some sites due to reading the data in chunks( It's worth missing because reading is very fast) with open('top-1m.csv', 'r') as file: prefix = '' file.seek( self.count * 1024 ) #you will land into the middle of bytes so discard upto newline if ( self.count ): file.readline() for lines in iter ( lambda : file.read( 1024 ) , ''): l = lines.split('\n') n = len ( l ) l[0] = ''.join( [ prefix , l[0] ] ) for i in xrange ( n - 1 ) : self.q.put ( ''.join ( [ 'http://www.', l[i].split(',')[1] ] ) ) prefix = l[n-1] self.count += 1 #do graceful exit from here. def handleexception ( self , signal , frame) : with open('bytesread.dat', 'w') as file: print ''.join ( [ 'Number of bytes read ( probably unfinished ) ' , str ( self.count ) ] ) file.write ( ''.join ( [ str ( self.count ) , '\n' ] ) ) file.close() sys.exit(0) if __name__== '__main__': u = Downloader() signal.signal( signal.SIGINT , u.handleexception) thread.start_new_thread ( u.createurl , () ) for i in xrange ( 5 ) : thread.start_new_thread ( u.downloadurl , () ) while True : pass From python at mrabarnett.plus.com Tue Aug 27 16:19:59 2013 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 27 Aug 2013 21:19:59 +0100 Subject: Improving the web page download code. In-Reply-To: References: Message-ID: <521D09EF.5090303@mrabarnett.plus.com> On 27/08/2013 20:41, mukesh tiwari wrote: > Hello All, > I am doing web stuff first time in python so I am looking for suggestions. I wrote this code to download the title of webpages using as much less resource ( server time, data download) as possible and should be fast enough. Initially I used BeautifulSoup for parsing but the person who is going to use this code asked me not to use this and use regular expressions ( The reason was BeautifulSoup is not fast enough ? ). Also initially, I was downloading the the whole page but finally I restricted to only 30000 characters to get the title of almost all the pages. Write now I can see only two shortcomings of this code, one when I kill the code by SIGINT ( ctrl-c ) then it dies instantly. I can modify this code to process all the elements in queue and let it die. The second is one IO call per iteration in download url function ( May be I can use async IO call but I am not sure ). I don't have much web programming experience so I am looking for suggestion to make it more robust. top-1m.c sv > is file downloaded from alexa[1]. Also some suggestions to write more idiomatic python code. > > -Mukesh Tiwari > > [1]http://www.alexa.com/topsites. > > > import urllib2, os, socket, Queue, thread, signal, sys, re > > > class Downloader(): > > def __init__( self ): > self.q = Queue.Queue( 200 ) > self.count = 0 > > > > def downloadurl( self ) : > #open a file in append mode and write the result ( Improvement think of writing in chunks ) > with open('titleoutput.dat', 'a+' ) as file : > while True : > try : > url = self.q.get( ) > data = urllib2.urlopen ( url , data = None , timeout = 10 ).read( 30000 ) > regex = re.compile('(.*?)' , re.IGNORECASE) > #Read data line by line and as soon you find the title go out of loop. > #title = None > #for r in data: > # if not r : > # raise StopIteration > # else: > # title = regex.search( r ) > # if title is not None: break > > title = regex.search( data ) > result = ', '.join ( [ url , title.group(1) ] ) > #data.close() > file.write(''.join( [ result , '\n' ] ) ) > except urllib2.HTTPError as e: > print ''.join ( [ url, ' ', str ( e ) ] ) > except urllib2.URLError as e: > print ''.join ( [ url, ' ', str ( e ) ] ) > except Exception as e : > print ''.join ( [ url, ' ', str( e ) ] ) > #With block python calls file.close() automatically. > > > def createurl ( self ) : > > #check if file exist. If not then create one with default value of 0 bytes read. > if os.path.exists('bytesread.dat'): > f = open ( 'bytesread.dat','r') > self.count = int ( f.readline() ) > > else: > f=open('bytesread.dat','w') > f.write('0\n') > f.close() > > #Reading data in chunks is fast but we can miss some sites due to reading the data in chunks( It's worth missing because reading is very fast) > with open('top-1m.csv', 'r') as file: > prefix = '' > file.seek( self.count * 1024 ) > #you will land into the middle of bytes so discard upto newline > if ( self.count ): file.readline() > for lines in iter ( lambda : file.read( 1024 ) , ''): > l = lines.split('\n') > n = len ( l ) > l[0] = ''.join( [ prefix , l[0] ] ) > for i in xrange ( n - 1 ) : self.q.put ( ''.join ( [ 'http://www.', l[i].split(',')[1] ] ) ) > prefix = l[n-1] > self.count += 1 > > > #do graceful exit from here. > def handleexception ( self , signal , frame) : > with open('bytesread.dat', 'w') as file: > print ''.join ( [ 'Number of bytes read ( probably unfinished ) ' , str ( self.count ) ] ) > file.write ( ''.join ( [ str ( self.count ) , '\n' ] ) ) > file.close() > sys.exit(0) > > if __name__== '__main__': > u = Downloader() > signal.signal( signal.SIGINT , u.handleexception) > thread.start_new_thread ( u.createurl , () ) > for i in xrange ( 5 ) : > thread.start_new_thread ( u.downloadurl , () ) > while True : pass > > My preferred method when working with background threads is to put a sentinel such as None at the end and then when a worker gets an item from the queue and sees that it's the sentinel, it puts it back in the queue for the other workers to see, and then returns (terminates). The main thread can then call each worker thread's .join method to wait for it to finish. You currently have the main thread running in a 'busy loop', consuming processing time doing nothing! From mukeshtiwari.iiitm at gmail.com Tue Aug 27 16:53:30 2013 From: mukeshtiwari.iiitm at gmail.com (mukesh tiwari) Date: Tue, 27 Aug 2013 13:53:30 -0700 (PDT) Subject: Improving the web page download code. In-Reply-To: References: Message-ID: <3fff4758-65af-47ae-ab8f-d591679809b7@googlegroups.com> On Wednesday, 28 August 2013 01:49:59 UTC+5:30, MRAB wrote: > On 27/08/2013 20:41, mukesh tiwari wrote: > > > Hello All, > > > I am doing web stuff first time in python so I am looking for suggestions. I wrote this code to download the title of webpages using as much less resource ( server time, data download) as possible and should be fast enough. Initially I used BeautifulSoup for parsing but the person who is going to use this code asked me not to use this and use regular expressions ( The reason was BeautifulSoup is not fast enough ? ). Also initially, I was downloading the the whole page but finally I restricted to only 30000 characters to get the title of almost all the pages. Write now I can see only two shortcomings of this code, one when I kill the code by SIGINT ( ctrl-c ) then it dies instantly. I can modify this code to process all the elements in queue and let it die. The second is one IO call per iteration in download url function ( May be I can use async IO call but I am not sure ). I don't have much web programming experience so I am looking for suggestion to make it more robust. top-1m.c > > sv > > > is file downloaded from alexa[1]. Also some suggestions to write more idiomatic python code. > > > > > > -Mukesh Tiwari > > > > > > [1]http://www.alexa.com/topsites. > > > > > > > > > import urllib2, os, socket, Queue, thread, signal, sys, re > > > > > > > > > class Downloader(): > > > > > > def __init__( self ): > > > self.q = Queue.Queue( 200 ) > > > self.count = 0 > > > > > > > > > > > > def downloadurl( self ) : > > > #open a file in append mode and write the result ( Improvement think of writing in chunks ) > > > with open('titleoutput.dat', 'a+' ) as file : > > > while True : > > > try : > > > url = self.q.get( ) > > > data = urllib2.urlopen ( url , data = None , timeout = 10 ).read( 30000 ) > > > regex = re.compile('(.*?)' , re.IGNORECASE) > > > #Read data line by line and as soon you find the title go out of loop. > > > #title = None > > > #for r in data: > > > # if not r : > > > # raise StopIteration > > > # else: > > > # title = regex.search( r ) > > > # if title is not None: break > > > > > > title = regex.search( data ) > > > result = ', '.join ( [ url , title.group(1) ] ) > > > #data.close() > > > file.write(''.join( [ result , '\n' ] ) ) > > > except urllib2.HTTPError as e: > > > print ''.join ( [ url, ' ', str ( e ) ] ) > > > except urllib2.URLError as e: > > > print ''.join ( [ url, ' ', str ( e ) ] ) > > > except Exception as e : > > > print ''.join ( [ url, ' ', str( e ) ] ) > > > #With block python calls file.close() automatically. > > > > > > > > > def createurl ( self ) : > > > > > > #check if file exist. If not then create one with default value of 0 bytes read. > > > if os.path.exists('bytesread.dat'): > > > f = open ( 'bytesread.dat','r') > > > self.count = int ( f.readline() ) > > > > > > else: > > > f=open('bytesread.dat','w') > > > f.write('0\n') > > > f.close() > > > > > > #Reading data in chunks is fast but we can miss some sites due to reading the data in chunks( It's worth missing because reading is very fast) > > > with open('top-1m.csv', 'r') as file: > > > prefix = '' > > > file.seek( self.count * 1024 ) > > > #you will land into the middle of bytes so discard upto newline > > > if ( self.count ): file.readline() > > > for lines in iter ( lambda : file.read( 1024 ) , ''): > > > l = lines.split('\n') > > > n = len ( l ) > > > l[0] = ''.join( [ prefix , l[0] ] ) > > > for i in xrange ( n - 1 ) : self.q.put ( ''.join ( [ 'http://www.', l[i].split(',')[1] ] ) ) > > > prefix = l[n-1] > > > self.count += 1 > > > > > > > > > #do graceful exit from here. > > > def handleexception ( self , signal , frame) : > > > with open('bytesread.dat', 'w') as file: > > > print ''.join ( [ 'Number of bytes read ( probably unfinished ) ' , str ( self.count ) ] ) > > > file.write ( ''.join ( [ str ( self.count ) , '\n' ] ) ) > > > file.close() > > > sys.exit(0) > > > > > > if __name__== '__main__': > > > u = Downloader() > > > signal.signal( signal.SIGINT , u.handleexception) > > > thread.start_new_thread ( u.createurl , () ) > > > for i in xrange ( 5 ) : > > > thread.start_new_thread ( u.downloadurl , () ) > > > while True : pass > > > > > > > > My preferred method when working with background threads is to put a > > sentinel such as None at the end and then when a worker gets an item > > from the queue and sees that it's the sentinel, it puts it back in the > > queue for the other workers to see, and then returns (terminates). The > > main thread can then call each worker thread's .join method to wait for > > it to finish. You currently have the main thread running in a 'busy > > loop', consuming processing time doing nothing! Hi MRAB, Thank you for the reply. I wrote this while loop only because of there is no thread.join in thread[1] library but I got your point. I am simply running a while loop for doing nothing. So if somehow I can block the main without too much computation then it will great. -Mukesh Tiwari [1] http://docs.python.org/2/library/thread.html#module-thread From python at mrabarnett.plus.com Tue Aug 27 18:33:15 2013 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 27 Aug 2013 23:33:15 +0100 Subject: Improving the web page download code. In-Reply-To: <3fff4758-65af-47ae-ab8f-d591679809b7@googlegroups.com> References: <3fff4758-65af-47ae-ab8f-d591679809b7@googlegroups.com> Message-ID: <521D292B.5060400@mrabarnett.plus.com> On 27/08/2013 21:53, mukesh tiwari wrote: > On Wednesday, 28 August 2013 01:49:59 UTC+5:30, MRAB wrote: >> On 27/08/2013 20:41, mukesh tiwari wrote: >> [snip] >> > if __name__== '__main__': >> > u = Downloader() >> > signal.signal( signal.SIGINT , u.handleexception) >> > thread.start_new_thread ( u.createurl , () ) >> > for i in xrange ( 5 ) : >> > thread.start_new_thread ( u.downloadurl , () ) >> > while True : pass >> > >> > >> My preferred method when working with background threads is to put a >> sentinel such as None at the end and then when a worker gets an item >> from the queue and sees that it's the sentinel, it puts it back in >> the queue for the other workers to see, and then returns >> (terminates). The main thread can then call each worker thread's >> .join method to wait for it to finish. You currently have the main >> thread running in a 'busy loop', consuming processing time doing >> nothing! > > Hi MRAB, > Thank you for the reply. I wrote this while loop only because of > there is no thread.join in thread[1] library but I got your point. I > am simply running a while loop for doing nothing. So if somehow I can > block the main without too much computation then it will great. > Why don't you use the 'threading' module instead? creator = threading.Thread(target=u.createurl) workers = [] for i in xrange(5): workers.append(threading.Thread(target=u.downloadurl)) creator.start() for w in workers: w.start() creator.join() for w in workers: w.join() From mukeshtiwari.iiitm at gmail.com Wed Aug 28 02:23:31 2013 From: mukeshtiwari.iiitm at gmail.com (mukesh tiwari) Date: Tue, 27 Aug 2013 23:23:31 -0700 (PDT) Subject: Improving the web page download code. In-Reply-To: References: <3fff4758-65af-47ae-ab8f-d591679809b7@googlegroups.com> Message-ID: On Wednesday, 28 August 2013 04:03:15 UTC+5:30, MRAB wrote: > On 27/08/2013 21:53, mukesh tiwari wrote: > > > On Wednesday, 28 August 2013 01:49:59 UTC+5:30, MRAB wrote: > > >> On 27/08/2013 20:41, mukesh tiwari wrote: > > >> > > [snip] > > >> > if __name__== '__main__': > > >> > u = Downloader() > > >> > signal.signal( signal.SIGINT , u.handleexception) > > >> > thread.start_new_thread ( u.createurl , () ) > > >> > for i in xrange ( 5 ) : > > >> > thread.start_new_thread ( u.downloadurl , () ) > > >> > while True : pass > > >> > > > >> > > > >> My preferred method when working with background threads is to put a > > >> sentinel such as None at the end and then when a worker gets an item > > >> from the queue and sees that it's the sentinel, it puts it back in > > >> the queue for the other workers to see, and then returns > > >> (terminates). The main thread can then call each worker thread's > > >> .join method to wait for it to finish. You currently have the main > > >> thread running in a 'busy loop', consuming processing time doing > > >> nothing! > > > > > > Hi MRAB, > > > Thank you for the reply. I wrote this while loop only because of > > > there is no thread.join in thread[1] library but I got your point. I > > > am simply running a while loop for doing nothing. So if somehow I can > > > block the main without too much computation then it will great. > > > > > Why don't you use the 'threading' module instead? > > > > > > creator = threading.Thread(target=u.createurl) > > > > workers = [] > > for i in xrange(5): > > workers.append(threading.Thread(target=u.downloadurl)) > > > > creator.start() > > > > for w in workers: > > w.start() > > > > creator.join() > > > > for w in workers: > > w.join() Hi MRAB, Initially I blocked the main using raw_input('') and it was working fine. u = Downloader() signal.signal( signal.SIGINT , u.handleexception) thread.start_new_thread ( u.createurl , () ) for i in xrange ( 5 ) : thread.start_new_thread ( u.downloadurl , () ) #This is for blocking main raw_input('') When I pressed ctrl-c then it's responding fine but now after switching to threading module, I am not able to kill my program using SIGINT ( ctrl-c ). Any idea how to signal SIGINT to threads ? Now the changed code and I have to catch the SIGINT. u = Downloader() signal.signal( signal.SIGINT , u.handleexception) urlcreator = threading.Thread ( target = u.createurl ) workers = [] for i in xrange ( 5 ): workers.append ( threading.Thread( target = u.downloadurl ) ) urlcreator.start() for w in workers: w.start() urlcreator.join() for w in workers: w.join() -Mukesh Tiwari From python at mrabarnett.plus.com Wed Aug 28 11:12:40 2013 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 28 Aug 2013 16:12:40 +0100 Subject: Improving the web page download code. In-Reply-To: References: <3fff4758-65af-47ae-ab8f-d591679809b7@googlegroups.com> Message-ID: <521E1368.1020403@mrabarnett.plus.com> On 28/08/2013 07:23, mukesh tiwari wrote: [snip] > Initially I blocked the main using raw_input('') and it was working fine. > > u = Downloader() > signal.signal( signal.SIGINT , u.handleexception) > thread.start_new_thread ( u.createurl , () ) > for i in xrange ( 5 ) : > thread.start_new_thread ( u.downloadurl , () ) > #This is for blocking main > raw_input('') > When I pressed ctrl-c then it's responding fine but now after switching to threading module, I am not able to kill my program using SIGINT ( ctrl-c ). Any idea how to signal SIGINT to threads ? > Try making them daemon threads. A daemon thread is one that will be killed when the main thread terminates. > Now the changed code and I have to catch the SIGINT. > u = Downloader() > signal.signal( signal.SIGINT , u.handleexception) > urlcreator = threading.Thread ( target = u.createurl ) > > workers = [] > for i in xrange ( 5 ): > workers.append ( threading.Thread( target = u.downloadurl ) ) > urlcreator.daemon = True > urlcreator.start() > for w in workers: urlcreator.daemon = True w.daemon = True > w.start() > > urlcreator.join() > for w in workers: > w.join() > From alister.ware at ntlworld.com Wed Aug 28 04:58:21 2013 From: alister.ware at ntlworld.com (Alister) Date: Wed, 28 Aug 2013 08:58:21 GMT Subject: Improving the web page download code. References: Message-ID: On Tue, 27 Aug 2013 12:41:10 -0700, mukesh tiwari wrote: > Hello All, > I am doing web stuff first time in python so I am looking for > suggestions. I wrote this code to download the title of webpages using > as much less resource ( server time, data download) as possible and > should be fast enough. Initially I used BeautifulSoup for parsing but > the person who is going to use this code asked me not to use this and > use regular expressions ( The reason was BeautifulSoup is not fast > enough ? ). By the time you have written enough RE to reliably parse HTML(I ma not sure that that is even strictly possible) you will have re-inverted BeautifullSoup, Badly. unless you are looking for a very explicit section of data in the page this is not a good idea. From accessnewbie at gmail.com Tue Aug 27 16:45:27 2013 From: accessnewbie at gmail.com (accessnewbie at gmail.com) Date: Tue, 27 Aug 2013 13:45:27 -0700 (PDT) Subject: Adding new lines to word document using zipfile module within python 2.7? Message-ID: Writing text to a word document (word 2007) using the zipfile module via python coding (python 2.7). Below if the section of code I am using to do this. I can't figure out what character I need to use to get it to add new lines to the zipfile. if Count: blurb = "\r\nINSERT TABLE HERE\r\n\r\nSA* - South America\r\nNA** - North America\r\nCA*** - Central America" else: blurb = "" further down I also have the following code (a small snippet of actual code for clarification) replaceText = {"TEXTBLURB" : blurb} Right now "if Count" is true, the output in the word document looks like this: INSERT TABLE HERE SA* - South America NA** - North America CA*** - Central America I want it too look like this: INSERT TABLE HERE SA* - South America NA** - North America CA*** - Central America I have a MS Word template file called "Automated Response.docx" that has a TAG imbedded between two paragraphs called "TEXTBLURB". Using a python script, if a certain condition is met, I want to replace this tag with some text (see above "blurb") that contains newlines, otherwise, don't add the text between paragraphs. I can't figure out the python code needed to imbed newlines into the tag. Ideas as to how to add new lines via python coding? I tried \r, \n, \r\n, ^11 and . None worked. From auriocus at gmx.de Tue Aug 27 17:11:36 2013 From: auriocus at gmx.de (Christian Gollwitzer) Date: Tue, 27 Aug 2013 23:11:36 +0200 Subject: Adding new lines to word document using zipfile module within python 2.7? In-Reply-To: References: Message-ID: Am 27.08.13 22:45, schrieb accessnewbie at gmail.com: > Writing text to a word document (word 2007) using the zipfile module > via python coding (python 2.7). Below if the section of code I am > using to do this. I can't figure out what character I need to use to > get it to add new lines to the zipfile. Word files (2007) are zipped-up XML files. In XML, whitespace is ignored, so probably you need some special tag (like
in XHTML) to insert the line break. The easiest way to find this out - short of reading the specs - is to open the file in Word, add a line break, save it and then run a diff tool over the output and the original file. If you want to go the specs route instead, you can start here: http://en.wikipedia.org/wiki/Office_Open_XML Christian From kurt.alfred.mueller at gmail.com Wed Aug 28 04:32:14 2013 From: kurt.alfred.mueller at gmail.com (Kurt Mueller) Date: Wed, 28 Aug 2013 10:32:14 +0200 Subject: split lines from stdin into a list of unicode strings Message-ID: <521DB58E.5000102@gmail.com> This is a follow up to the Subject "right adjusted strings containing umlauts" For some text manipulation tasks I need a template to split lines from stdin into a list of strings the way shlex.split() does it. The encoding of the input can vary. For further processing in Python I need the list of strings to be in unicode. Here is template.py: ############################################################################################################## #!/usr/bin/env python # vim: set fileencoding=utf-8 : # split lines from stdin into a list of unicode strings # Muk 2013-08-23 # Python 2.7.3 from __future__ import print_function import sys import shlex import chardet bool_cmnt = True # shlex: skip comments bool_posx = True # shlex: posix mode (strings in quotes) for inpt_line in sys.stdin: print( 'inpt_line=' + repr( inpt_line ) ) enco_type = chardet.detect( inpt_line )[ 'encoding' ] # {'encoding': 'EUC-JP', 'confidence': 0.99} print( 'enco_type=' + repr( enco_type ) ) try: strg_inpt = shlex.split( inpt_line, bool_cmnt, bool_posx, ) # shlex does not work on unicode except Exception, errr: # usually 'No closing quotation' print( "error='%s' on inpt_line='%s'" % ( errr, inpt_line.rstrip(), ), file=sys.stderr, ) continue print( 'strg_inpt=' + repr( strg_inpt ) ) # list of strings strg_unic = [ strg.decode( enco_type ) for strg in strg_inpt ] # decode the strings into unicode print( 'strg_unic=' + repr( strg_unic ) ) # list of unicode strings ############################################################################################################## $ cat | template.py Comments are welcome. TIA -- Kurt Mueller From davea at davea.name Wed Aug 28 07:13:36 2013 From: davea at davea.name (Dave Angel) Date: Wed, 28 Aug 2013 11:13:36 +0000 (UTC) Subject: split lines from stdin into a list of unicode strings References: <521DB58E.5000102@gmail.com> Message-ID: On 28/8/2013 04:32, Kurt Mueller wrote: > This is a follow up to the Subject > "right adjusted strings containing umlauts" You started a new thread, with a new subject line. So presumably we're starting over with a clean slate. > > For some text manipulation tasks I need a template to split lines > from stdin into a list of strings the way shlex.split() does it. > The encoding of the input can vary. Does that mean it'll vary from one run of the program to the next, or it'll vary from one line to the next? Your code below assumes the latter. That can greatly increase the unreliability of the already dubious chardet algorithm. > For further processing in Python I need the list of strings to be in unicode. > > Here is template.py: > > ############################################################################################################## > #!/usr/bin/env python > # vim: set fileencoding=utf-8 : > # split lines from stdin into a list of unicode strings > # Muk 2013-08-23 > # Python 2.7.3 > > from __future__ import print_function > import sys > import shlex > import chardet Is this the one ? https://pypi.python.org/pypi/chardet > > bool_cmnt = True # shlex: skip comments > bool_posx = True # shlex: posix mode (strings in quotes) > > for inpt_line in sys.stdin: > print( 'inpt_line=' + repr( inpt_line ) ) > enco_type = chardet.detect( inpt_line )[ 'encoding' ] # {'encoding': 'EUC-JP', 'confidence': 0.99} > print( 'enco_type=' + repr( enco_type ) ) > try: > strg_inpt = shlex.split( inpt_line, bool_cmnt, bool_posx, ) # shlex does not work on unicode But shlex does, since you're using Python 2.7.3 > except Exception, errr: # usually 'No closing quotation' > print( "error='%s' on inpt_line='%s'" % ( errr, inpt_line.rstrip(), ), file=sys.stderr, ) > continue > print( 'strg_inpt=' + repr( strg_inpt ) ) # list of strings > strg_unic = [ strg.decode( enco_type ) for strg in strg_inpt ] # decode the strings into unicode > print( 'strg_unic=' + repr( strg_unic ) ) # list of unicode strings > ############################################################################################################## > > $ cat | template.py > Why not have a separate filter that converts from a (guessed) encoding into utf-8, and have the later stage(s) assume utf-8 ? That way, the filter could be fed clues by the user, or replaced entirely, without affecting the main code you're working on. Alternatively, just add a commandline argument with the encoding, and parse it into enco_type. -- DaveA From kurt.alfred.mueller at gmail.com Wed Aug 28 08:39:06 2013 From: kurt.alfred.mueller at gmail.com (kurt.alfred.mueller at gmail.com) Date: Wed, 28 Aug 2013 05:39:06 -0700 (PDT) Subject: split lines from stdin into a list of unicode strings In-Reply-To: References: <521DB58E.5000102@gmail.com> Message-ID: On Wednesday, August 28, 2013 1:13:36 PM UTC+2, Dave Angel wrote: > On 28/8/2013 04:32, Kurt Mueller wrote: > > For some text manipulation tasks I need a template to split lines > > from stdin into a list of strings the way shlex.split() does it. > > The encoding of the input can vary. > Does that mean it'll vary from one run of the program to the next, or > it'll vary from one line to the next? Your code below assumes the > latter. That can greatly increase the unreliability of the already > dubious chardet algorithm. The encoding only varies from one launch to the other. The reason I process each line is memory usage. Option to have a better reliability of chardet: I could read all of the input, save the input lines for further processing in a list, feed the lines into chardet.universaldetector.UniversalDetector.feed()/close()/result() and then decode and split/shlex the lines in the list. That way the chardet oracle would be more reliable, but roughly twice as much memory will be used. > > import chardet > Is this the one ? > https://pypi.python.org/pypi/chardet Yes. > > $ cat | template.py > Why not have a separate filter that converts from a (guessed) encoding > into utf-8, and have the later stage(s) assume utf-8 ? That way, the > filter could be fed clues by the user, or replaced entirely, without > affecting the main code you're working on. Working on UNIX-like systems (I am happy to work in a MSFZ) the processing pipe would be then: cat | recode2utf8 | splitlines.py memory usage 2 * ( plus chardet memory usage ) > Alternatively, just add a commandline argument with the encoding, and > parse it into enco_type. cat | splitlines.py -e latin9 memory usage 1 * or cat | splitlines.py -e $( codingdetect ) memory usage 1 * So, because memory usage is not primary, I think I will go with the option described above. -- Kurt M?ller From __peter__ at web.de Thu Aug 29 05:12:59 2013 From: __peter__ at web.de (Peter Otten) Date: Thu, 29 Aug 2013 11:12:59 +0200 Subject: split lines from stdin into a list of unicode strings References: <521DB58E.5000102@gmail.com> Message-ID: kurt.alfred.mueller at gmail.com wrote: > On Wednesday, August 28, 2013 1:13:36 PM UTC+2, Dave Angel wrote: >> On 28/8/2013 04:32, Kurt Mueller wrote: >> > For some text manipulation tasks I need a template to split lines >> > from stdin into a list of strings the way shlex.split() does it. >> > The encoding of the input can vary. > >> Does that mean it'll vary from one run of the program to the next, or >> it'll vary from one line to the next? Your code below assumes the >> latter. That can greatly increase the unreliability of the already >> dubious chardet algorithm. > > The encoding only varies from one launch to the other. > The reason I process each line is memory usage. > > Option to have a better reliability of chardet: > I could read all of the input, save the input lines for further > processing in a list, feed the lines into > chardet.universaldetector.UniversalDetector.feed()/close()/result() > and then decode and split/shlex the lines in the list. > That way the chardet oracle would be more reliable, but > roughly twice as much memory will be used. You can compromise and read ahead a limited number of lines. Here's my demo script (The interesting part is detect_encoding(), I got a bit distracted by unrelated stuff...). The script does one extra decode/encode cycle -- it should be easy to avoid that if you run into performance issues. #!/usr/bin/env python import sys import shlex import chardet from itertools import islice, chain def detect_encoding(instream, encoding, detect_lines): if encoding is None: encoding = instream.encoding if encoding is None: head = list(islice(instream, detect_lines)) encoding = chardet.detect("".join(head))["encoding"] instream = chain(head, instream) return encoding, instream def split_line(line, comments=True, posix=True): parts = shlex.split(line.encode("utf-8"), comments=comments, posix=posix) return [part.decode("utf-8") for part in parts] def to_int(s): """ >>> to_int(" 42") 42 >>> to_int("-1") is None True >>> to_int(" NONE ") is None True >>> to_int("none") is None True >>> to_int(" 0x400 ") 1024 """ s = s.lower().strip() if s in {"none", "-1"}: return None return int(s, 16 if s.startswith("0x") else 10) def main(): import argparse parser = argparse.ArgumentParser() parser.add_argument("-e", "--encoding") parser.add_argument( "-d", "--detect-lines", type=to_int, default=100, help=("number of lines used to determine encoding; " "'none' or -1 for whole file. (default: 100)")) args = parser.parse_args() encoding, instream = detect_encoding( sys.stdin, encoding=args.encoding, detect_lines=args.detect_lines) lines = (line.decode(encoding) for line in instream) for line in lines: try: parts = split_line(line) except ValueError as exc: print >> sys.stderr, exc else: print parts if __name__ == "__main__": main() From kurt.alfred.mueller at gmail.com Thu Aug 29 07:31:45 2013 From: kurt.alfred.mueller at gmail.com (Kurt Mueller) Date: Thu, 29 Aug 2013 13:31:45 +0200 Subject: split lines from stdin into a list of unicode strings In-Reply-To: References: <521DB58E.5000102@gmail.com> Message-ID: <521F3121.6000900@gmail.com> Am 29.08.2013 11:12, schrieb Peter Otten: > kurt.alfred.mueller at gmail.com wrote: >> On Wednesday, August 28, 2013 1:13:36 PM UTC+2, Dave Angel wrote: >>> On 28/8/2013 04:32, Kurt Mueller wrote: >>>> For some text manipulation tasks I need a template to split lines >>>> from stdin into a list of strings the way shlex.split() does it. >>>> The encoding of the input can vary. > You can compromise and read ahead a limited number of lines. Here's my demo > script (The interesting part is detect_encoding(), I got a bit distracted by > unrelated stuff...). The script does one extra decode/encode cycle -- it > should be easy to avoid that if you run into performance issues. Thanks Peter! I see the idea. It limits the buffersize/memory usage for the detection. I have to say that I am a bit disapointed by the chardet library. The encoding for the single character '?' is detected as {'confidence': 0.99, 'encoding': 'EUC-JP'}, whereas "file" says: $ echo "?" | file -i - /dev/stdin: text/plain; charset=utf-8 $ "?" is a character I use very often, as it is in my name: "M?ller":-) I try to use the "python-magic" library which has a similar functionality as chardet and is used by the "file" unix-command and it is expandable with a magicfile, see "man file". My magic_test script: ------------------------------------------------------------------- #!/usr/bin/env python # vim: set fileencoding=utf-8 : from __future__ import print_function import magic strg_chck = '?' magc_enco = magic.open( magic.MAGIC_MIME_ENCODING ) magc_enco.load() print( strg_chck + ' encoding=' + magc_enco.buffer( strg_chck ) ) magc_enco.close() ------------------------------------------------------------------- $ magic_test ? encoding=utf-8 python-magic seems to me a bit more reliable. Cheers -- Kurt Mueller From __peter__ at web.de Thu Aug 29 09:15:25 2013 From: __peter__ at web.de (Peter Otten) Date: Thu, 29 Aug 2013 15:15:25 +0200 Subject: split lines from stdin into a list of unicode strings References: <521DB58E.5000102@gmail.com> <521F3121.6000900@gmail.com> Message-ID: Kurt Mueller wrote: > I have to say that I am a bit disapointed by the chardet library. > The encoding for the single character '?' > is detected as {'confidence': 0.99, 'encoding': 'EUC-JP'}, > whereas "file" says: > $ echo "?" | file -i - > /dev/stdin: text/plain; charset=utf-8 > $ > > "?" is a character I use very often, as it is in my name: "M?ller":-) You cannot determine an encoding by a single letter. Why should "?" be more likely than "?"? The only thing you can blame chardet for is that its confidence rating is a flat out lie... For "M?ller" on the other side you could probably come up with a (simple) heuristic that "?" is more likely to be surrounded by ascii-letters than "?". From p at google-groups-2013.dobrogost.net Wed Aug 28 04:57:16 2013 From: p at google-groups-2013.dobrogost.net (Piotr Dobrogost) Date: Wed, 28 Aug 2013 01:57:16 -0700 (PDT) Subject: Why is str(None) == 'None' and not an empty string? Message-ID: <155b0796-147b-4132-adf0-e73c0e30969a@googlegroups.com> Hi! Having repr(None) == 'None' is sure the right thing but why does str(None) == 'None'? Wouldn't it be more correct if it was an empty string? Regards Piotr Dobrogost From tjreedy at udel.edu Wed Aug 28 06:33:04 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 28 Aug 2013 06:33:04 -0400 Subject: Why is str(None) == 'None' and not an empty string? In-Reply-To: <155b0796-147b-4132-adf0-e73c0e30969a@googlegroups.com> References: <155b0796-147b-4132-adf0-e73c0e30969a@googlegroups.com> Message-ID: On 8/28/2013 4:57 AM, Piotr Dobrogost wrote: > Having repr(None) == 'None' is sure the right thing but why does str(None) == 'None'? Wouldn't it be more correct if it was an empty string? No. There is no reason to be different. -- Terry Jan Reedy From steve+comp.lang.python at pearwood.info Wed Aug 28 08:21:37 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 Aug 2013 12:21:37 GMT Subject: Why is str(None) == 'None' and not an empty string? References: <155b0796-147b-4132-adf0-e73c0e30969a@googlegroups.com> Message-ID: <521deb50$0$6599$c3e8da3$5496439d@news.astraweb.com> On Wed, 28 Aug 2013 01:57:16 -0700, Piotr Dobrogost wrote: > Hi! > > Having repr(None) == 'None' is sure the right thing but why does > str(None) == 'None'? Wouldn't it be more correct if it was an empty > string? Why do you think an empty string is more correct? Would you expect str([]) or str(0.0) or str({}) to also give an empty string? I can't see any reason for str(None) to return the empty string. -- Steven From ian.g.kelly at gmail.com Thu Aug 29 06:43:16 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 29 Aug 2013 04:43:16 -0600 Subject: Why is str(None) == 'None' and not an empty string? In-Reply-To: <521deb50$0$6599$c3e8da3$5496439d@news.astraweb.com> References: <155b0796-147b-4132-adf0-e73c0e30969a@googlegroups.com> <521deb50$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Wed, Aug 28, 2013 at 6:21 AM, Steven D'Aprano wrote: > On Wed, 28 Aug 2013 01:57:16 -0700, Piotr Dobrogost wrote: > >> Hi! >> >> Having repr(None) == 'None' is sure the right thing but why does >> str(None) == 'None'? Wouldn't it be more correct if it was an empty >> string? > > > Why do you think an empty string is more correct? Would you expect > str([]) or str(0.0) or str({}) to also give an empty string? > > > I can't see any reason for str(None) to return the empty string. I've had many occasions where it would have been convenient for str(None) to return the empty string, e.g. when exporting tabular data that includes null values from a database to a spreadsheet. Generally it's safe to just call str() on the data, except that I'd rather empty cells just be empty rather than spamming the word "None" all over the place, so I end up having to do something like (str(value) if value is not None else '') instead. Not a major inconvenience, but enough to make me wonder if there could be a better way. I would not expect str([]) or str(0.0) or str({}) to return an empty string. I would expect these to return '[]', '0.0', and '{}' respectively, which are all consistent with how str operates on other values of their respective types. None is a singleton though, so it's not constrained by how other instances of NoneType behave. From timothy.c.delaney at gmail.com Thu Aug 29 16:59:51 2013 From: timothy.c.delaney at gmail.com (Tim Delaney) Date: Fri, 30 Aug 2013 06:59:51 +1000 Subject: Why is str(None) == 'None' and not an empty string? In-Reply-To: References: <155b0796-147b-4132-adf0-e73c0e30969a@googlegroups.com> <521deb50$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 29 August 2013 20:43, Ian Kelly wrote: > On Wed, Aug 28, 2013 at 6:21 AM, Steven D'Aprano > wrote: > > On Wed, 28 Aug 2013 01:57:16 -0700, Piotr Dobrogost wrote: > > > >> Hi! > >> > >> Having repr(None) == 'None' is sure the right thing but why does > >> str(None) == 'None'? Wouldn't it be more correct if it was an empty > >> string? > > > > > > Why do you think an empty string is more correct? Would you expect > > str([]) or str(0.0) or str({}) to also give an empty string? > > > > > > I can't see any reason for str(None) to return the empty string. > > I've had many occasions where it would have been convenient for > str(None) to return the empty string, e.g. when exporting tabular data > that includes null values from a database to a spreadsheet. Generally > it's safe to just call str() on the data, except that I'd rather empty > cells just be empty rather than spamming the word "None" all over the > place, so I end up having to do something like (str(value) if value is > not None else '') instead. Not a major inconvenience, but enough to > make me wonder if there could be a better way. > There is. def format(value): if value is None: return '' return str(value) print(format(value)) This also allows you to format other types differently e.g. only output 2 decimal places for non-integer numeric types. Tim Delaney -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Thu Aug 29 21:57:45 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 30 Aug 2013 01:57:45 GMT Subject: Why is str(None) == 'None' and not an empty string? References: <155b0796-147b-4132-adf0-e73c0e30969a@googlegroups.com> <521deb50$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <521ffc19$0$6599$c3e8da3$5496439d@news.astraweb.com> On Thu, 29 Aug 2013 04:43:16 -0600, Ian Kelly wrote: > I've had many occasions where it would have been convenient for > str(None) to return the empty string, e.g. when exporting tabular data > that includes null values from a database to a spreadsheet. Generally > it's safe to just call str() on the data, except that I'd rather empty > cells just be empty rather than spamming the word "None" all over the > place, so I end up having to do something like (str(value) if value is > not None else '') instead. Not a major inconvenience, but enough to > make me wonder if there could be a better way. Similarly, the interactive interpreter also special-cases None, and prints nothing at all if the result is None: py> None py> '' '' So it isn't that there are *no cases at all* where one might want None to display as empty. But then, there are also good use-cases for wanting 0 to display as empty too, which is why Excel and other spreadsheets allow you to create your own format strings controlling the display of +ve numbers, 0 and -ve numbers individually. The question is, what should str(None) give? I cannot see any reason to have str(None) return '' as the standard behaviour any more than str(0) ought to return '' as standard. > I would not expect str([]) or str(0.0) or str({}) to return an empty > string. I would expect these to return '[]', '0.0', and '{}' > respectively, which are all consistent with how str operates on other > values of their respective types. None is a singleton though, so it's > not constrained by how other instances of NoneType behave. "Other instances of NoneType" is irrelevant. NotImplemented is also a singleton. Would you expect str(NotImplemented ) to return the empty string, or perhaps some other arbitrary string like "?", or would you expect it to return 'NotImplemented'? Likewise for Ellipsis, which is another singleton. Custom classes aside, which of course could do *anything* no matter how silly, I can only think of two objects where str() returns the empty string: the empty byte string '' and the empty Unicode string u''. (In Python 3, there is only one, namely the empty Unicode string.) So while I don't doubt that it wouldn't be occasionally convenient to map None to '' rather than 'None', I think it would be surprising and, indeed, dangerous if it happened by default, since it would encourage people to build up SQL query strings by concatenation, as in the Original Poster's example. (And thank you for picking up on that.) -- Steven From steve+comp.lang.python at pearwood.info Thu Aug 29 22:44:28 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 30 Aug 2013 02:44:28 GMT Subject: Why is str(None) == 'None' and not an empty string? References: <155b0796-147b-4132-adf0-e73c0e30969a@googlegroups.com> <521deb50$0$6599$c3e8da3$5496439d@news.astraweb.com> <521ffc19$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5220070b$0$6599$c3e8da3$5496439d@news.astraweb.com> On Fri, 30 Aug 2013 01:57:45 +0000, Steven D'Aprano wrote: > So while I don't doubt that it wouldn't be occasionally convenient to > map None to '' rather than 'None', I think it would be surprising and, > indeed, dangerous if it happened by default, since it would encourage > people to build up SQL query strings by concatenation, as in the > Original Poster's example. Ah, apparently I'm smoking crack, since it wasn't the OP at all. My apologies. And worse, it was given as an example of what *not* to do. -- Steven From yanxiaopei199 at gmail.com Wed Aug 28 06:28:46 2013 From: yanxiaopei199 at gmail.com (Thanatos xiao) Date: Wed, 28 Aug 2013 18:28:46 +0800 Subject: about pyyaml questions! Message-ID: Hi Guys: Now I use pyyaml to load a yaml file, after I dump this load data,but I found an questions,before I load the yaml file,the file looks like: aaaa - -b -c - -e -x after I dump this data and write file, the file looks like: aaaa - -b -c - -e -x although when dump file, I have set default_flow_style=False Can the dumped file the same as original file....? ths..... -------------- next part -------------- An HTML attachment was scrubbed... URL: From fp2161 at gmail.com Wed Aug 28 07:42:57 2013 From: fp2161 at gmail.com (Fabrice POMBET) Date: Wed, 28 Aug 2013 13:42:57 +0200 Subject: =?iso-8859-1?Q?R=E9p_=3A_Why_is_str=28None=29_=3D=3D_=27None=27_?= =?iso-8859-1?Q?and_not_an_empty_string=3F?= In-Reply-To: References: Message-ID: <2C98ABD0-859D-4991-8DE3-CF68A24F136B@gmail.com> On 8/28/2013 4:57 AM, Piotr Dobrogost wrote: > Having repr(None) == 'None' is sure the right thing but why does str(None) == 'None'? Wouldn't it be more correct if it was an empty string? the point of str(obj) is to return a string containing the obj (a sequence of characters if it is unbound or not built-in, etc.)... If you set the rule str(None)=="", then you will cause plenty of problems. For instance, if you want to build a string like request="SELECT X"+"IN Y"+"WHERE B="+String(B) to prepare a sequel request, and the field B happens to be sometimes "None", you would automatically end up with """SELECT X IN Y WHERE B=''""" instead of """SELECT X IN Y WHERE B='None'""", and your sql request will fall into limbos... From ian.g.kelly at gmail.com Thu Aug 29 06:55:36 2013 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 29 Aug 2013 04:55:36 -0600 Subject: =?ISO-8859-1?Q?Re=3A_R=E9p_=3A_Why_is_str=28None=29_=3D=3D_=27None=27_and_not_an_emp?= =?ISO-8859-1?Q?ty_string=3F?= In-Reply-To: <2C98ABD0-859D-4991-8DE3-CF68A24F136B@gmail.com> References: <2C98ABD0-859D-4991-8DE3-CF68A24F136B@gmail.com> Message-ID: On Wed, Aug 28, 2013 at 5:42 AM, Fabrice POMBET wrote: > > On 8/28/2013 4:57 AM, Piotr Dobrogost wrote: > >> Having repr(None) == 'None' is sure the right thing but why does str(None) == 'None'? Wouldn't it be more correct if it was an empty string? > > the point of str(obj) is to return a string containing the obj (a sequence of characters if it is unbound or not built-in, etc.)... > > If you set the rule str(None)=="", then you will cause plenty of problems. > > For instance, if you want to build a string like request="SELECT X"+"IN Y"+"WHERE B="+String(B) > to prepare a sequel request, and the field B happens to be sometimes "None", you would automatically end up with """SELECT X IN Y WHERE B=''""" instead of """SELECT X IN Y WHERE B='None'""", > and your sql request will fall into limbos... The proper way to pass values into a SQL query is by using bind parameters. Inserting them into the query string by concatenation is error-prone and an excellent way to write code that is vulnerable to SQL injection attacks. The DB API guarantees that the object None will map to the database value NULL when passed directly as a parameter. The value returned by str(None) is irrelevant in this context. From fp2161 at gmail.com Thu Aug 29 16:09:19 2013 From: fp2161 at gmail.com (fp2161 at gmail.com) Date: Thu, 29 Aug 2013 13:09:19 -0700 (PDT) Subject: =?ISO-8859-1?Q?Re=3A_R=E9p_=3A_Why_is_str=28None=29_=3D=3D_=27None=27_and_not_an_emp?= =?ISO-8859-1?Q?ty_string=3F?= In-Reply-To: References: <2C98ABD0-859D-4991-8DE3-CF68A24F136B@gmail.com> Message-ID: On Thursday, August 29, 2013 12:55:36 PM UTC+2, Ian wrote: > On Wed, Aug 28, 2013 at 5:42 AM, Fabrice POMBET wrote: > > > > > > On 8/28/2013 4:57 AM, Piotr Dobrogost wrote: > > > > > >> Having repr(None) == 'None' is sure the right thing but why does str(None) == 'None'? Wouldn't it be more correct if it was an empty string? > > > > > > the point of str(obj) is to return a string containing the obj (a sequence of characters if it is unbound or not built-in, etc.)... > > > > > > If you set the rule str(None)=="", then you will cause plenty of problems. > > > > > > For instance, if you want to build a string like request="SELECT X"+"IN Y"+"WHERE B="+String(B) > > > to prepare a sequel request, and the field B happens to be sometimes "None", you would automatically end up with """SELECT X IN Y WHERE B=''""" instead of """SELECT X IN Y WHERE B='None'""", > > > and your sql request will fall into limbos... > > > > The proper way to pass values into a SQL query is by using bind > > parameters. Inserting them into the query string by concatenation is > > error-prone and an excellent way to write code that is vulnerable to > > SQL injection attacks. > > > > The DB API guarantees that the object None will map to the database > > value NULL when passed directly as a parameter. The value returned by > > str(None) is irrelevant in this context. I could not agree more with you. The purpose of my post, however, was only to give a simple illustration of how such a generic change would make everything awkward, not to give any proper, precise or general directions on how to code a safe SQL request for a DB when you are online. Thank you however for your corrections. From adamkalinski at gmail.com Wed Aug 28 08:52:46 2013 From: adamkalinski at gmail.com (AdamKal) Date: Wed, 28 Aug 2013 05:52:46 -0700 (PDT) Subject: Is there a function that applies list of functions to a value? Message-ID: Hi, >From time to time I have to apply a series of functions to a value in such a way: func4(func3(func2(func1(myval)))) I was wondering if there is a function in standard library that would take a list of functions and a initial value and do the above like this: func_im_looking_for([func1, func2, func3, func4], myval) I looked in itertools but nothing seamed to do the job. This seams like something vary obvious that was needed many times elsewhere so maybe you could help me? From python.list at tim.thechases.com Wed Aug 28 09:11:34 2013 From: python.list at tim.thechases.com (Tim Chase) Date: Wed, 28 Aug 2013 08:11:34 -0500 Subject: Is there a function that applies list of functions to a value? In-Reply-To: References: Message-ID: <20130828081134.3fd96fd4@bigbox.christie.dr> On 2013-08-28 05:52, AdamKal wrote: > From time to time I have to apply a series of functions to a value > in such a way: > > func4(func3(func2(func1(myval)))) > > I was wondering if there is a function in standard library that > would take a list of functions and a initial value and do the above > like this: > > func_im_looking_for([func1, func2, func3, func4], myval) At least in Py2.x you can use reduce: reduce(lambda value, fn: fn(value), [list_of_functions], myval) I believe it was moved into functools.reduce() in Py3.x meaning you might want to do something like try: reduce # see if it's already in __builtins__ except NameError: # running Py3... from functools import reduce or try: from functools import reduce except ImportError: pass # it should already be in __builtins__ for pre-2.6 at the top to make sure it's in your global namespace. -tkc From jpiitula at ling.helsinki.fi Wed Aug 28 09:10:49 2013 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 28 Aug 2013 16:10:49 +0300 Subject: Is there a function that applies list of functions to a value? References: Message-ID: AdamKal writes: > Hi, > > From time to time I have to apply a series of functions to a value > in such a way: > > func4(func3(func2(func1(myval)))) > > I was wondering if there is a function in standard library that > would take a list of functions and a initial value and do the above > like this: > > func_im_looking_for([func1, func2, func3, func4], myval) > > I looked in itertools but nothing seamed to do the job. This seams > like something vary obvious that was needed many times elsewhere so > maybe you could help me? If you can have things backwards, or have func_im_looking_for reverse things first, you can do it this way: from functools import reduce def funcall(arg, fun): return fun(arg) def func1(arg): return 'f1({})'.format(arg) def func2(arg): return 'f2({})'.format(arg) def func3(arg): return 'f3({})'.format(arg) # prints: f1(f2(f3(3.14))) print(reduce(funcall, (func3, func2, func1), 3.14)) From fp2161 at gmail.com Thu Aug 29 18:48:15 2013 From: fp2161 at gmail.com (fp2161 at gmail.com) Date: Thu, 29 Aug 2013 15:48:15 -0700 (PDT) Subject: Is there a function that applies list of functions to a value? In-Reply-To: References: Message-ID: On Wednesday, August 28, 2013 3:10:49 PM UTC+2, Jussi Piitulainen wrote: > AdamKal writes: > > > > > Hi, > > > > > > From time to time I have to apply a series of functions to a value > > > in such a way: > > > > > > func4(func3(func2(func1(myval)))) > > > > > > I was wondering if there is a function in standard library that > > > would take a list of functions and a initial value and do the above > > > like this: > > > > > > func_im_looking_for([func1, func2, func3, func4], myval) > > > > > > I looked in itertools but nothing seamed to do the job. This seams > > > like something vary obvious that was needed many times elsewhere so > > > maybe you could help me? > > > > If you can have things backwards, or have func_im_looking_for reverse > > things first, you can do it this way: > > > > from functools import reduce > > > > def funcall(arg, fun): return fun(arg) > > > > def func1(arg): return 'f1({})'.format(arg) > > def func2(arg): return 'f2({})'.format(arg) > > def func3(arg): return 'f3({})'.format(arg) > > > > # prints: f1(f2(f3(3.14))) > > print(reduce(funcall, (func3, func2, func1), 3.14)) I had never thought about the "f({0})".format(arg) trick, that is excellent, thank you!!! From ishish at domhain.de Wed Aug 28 09:17:45 2013 From: ishish at domhain.de (ishish) Date: Wed, 28 Aug 2013 14:17:45 +0100 Subject: Is there a function that applies list of functions to a =?UTF-8?Q?value=3F?= In-Reply-To: References: Message-ID: Am 28.08.2013 13:52, schrieb AdamKal: > Hi, > > From time to time I have to apply a series of functions to a value in > such a way: > > func4(func3(func2(func1(myval)))) > > I was wondering if there is a function in standard library that would > take a list of functions and a initial value and do the above like > this: > > func_im_looking_for([func1, func2, func3, func4], myval) > > I looked in itertools but nothing seamed to do the job. This seams > like something vary obvious that was needed many times elsewhere so > maybe you could help me? You could try somthing like: myval = 'whatever' for i in range(1,4): print eval("func%s(%s)" % (i, myval)) From jpiitula at ling.helsinki.fi Wed Aug 28 09:19:50 2013 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 28 Aug 2013 16:19:50 +0300 Subject: Is there a function that applies list of functions to a value? References: Message-ID: Tim Chase writes: > On 2013-08-28 05:52, AdamKal wrote: > > From time to time I have to apply a series of functions to a value > > in such a way: > > > > func4(func3(func2(func1(myval)))) > > > > I was wondering if there is a function in standard library that > > would take a list of functions and a initial value and do the above > > like this: > > > > func_im_looking_for([func1, func2, func3, func4], myval) > > At least in Py2.x you can use reduce: > > reduce(lambda value, fn: fn(value), [list_of_functions], myval) I notice now that I didn't notice, when I just sent the same solution, that the function list was already backwards to its intended order of application, so yes, this should work as wanted. Sorry. From adamkalinski at gmail.com Wed Aug 28 09:23:47 2013 From: adamkalinski at gmail.com (AdamKal) Date: Wed, 28 Aug 2013 06:23:47 -0700 (PDT) Subject: Is there a function that applies list of functions to a value? In-Reply-To: References: Message-ID: <21b63d71-14ce-4a59-9e5a-092156832d0b@googlegroups.com> Thanks! I guess this is as simple as it gets then. I was just looking for the "one obvious way to do it". W dniu ?roda, 28 sierpnia 2013 15:11:34 UTC+2 u?ytkownik Tim Chase napisa?: > On 2013-08-28 05:52, AdamKal wrote: > > > From time to time I have to apply a series of functions to a value > > > in such a way: > > > > > > func4(func3(func2(func1(myval)))) > > > > > > I was wondering if there is a function in standard library that > > > would take a list of functions and a initial value and do the above > > > like this: > > > > > > func_im_looking_for([func1, func2, func3, func4], myval) > > > > At least in Py2.x you can use reduce: > > > > reduce(lambda value, fn: fn(value), [list_of_functions], myval) > > > > I believe it was moved into functools.reduce() in Py3.x meaning you > > might want to do something like > > > > try: > > reduce # see if it's already in __builtins__ > > except NameError: # running Py3... > > from functools import reduce > > > > or > > > > try: > > from functools import reduce > > except ImportError: > > pass # it should already be in __builtins__ for pre-2.6 > > > > at the top to make sure it's in your global namespace. > > > > -tkc From rosuav at gmail.com Wed Aug 28 09:26:42 2013 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 28 Aug 2013 23:26:42 +1000 Subject: Is there a function that applies list of functions to a value? In-Reply-To: <21b63d71-14ce-4a59-9e5a-092156832d0b@googlegroups.com> References: <21b63d71-14ce-4a59-9e5a-092156832d0b@googlegroups.com> Message-ID: On Wed, Aug 28, 2013 at 11:23 PM, AdamKal wrote: > I guess this is as simple as it gets then. I was just looking for the "one obvious way to do it". The one obvious way to do some things is to post on python-list and see what comes back :) I love reading over these sorts of threads, they're good fun. ChrisA From python.list at tim.thechases.com Wed Aug 28 09:43:39 2013 From: python.list at tim.thechases.com (Tim Chase) Date: Wed, 28 Aug 2013 08:43:39 -0500 Subject: Is there a function that applies list of functions to a value? In-Reply-To: <21b63d71-14ce-4a59-9e5a-092156832d0b@googlegroups.com> References: <21b63d71-14ce-4a59-9e5a-092156832d0b@googlegroups.com> Message-ID: <20130828084339.578e5116@bigbox.christie.dr> On 2013-08-28 06:23, AdamKal wrote: > Thanks! > > I guess this is as simple as it gets then. I was just looking for > the "one obvious way to do it". When 3 replies from 3 people all arrive within minutes, each suggesting reduce(), I'd figure it's the "one obvious way to do it" :-) -tkc From adamkalinski at gmail.com Wed Aug 28 09:50:37 2013 From: adamkalinski at gmail.com (AdamKal) Date: Wed, 28 Aug 2013 06:50:37 -0700 (PDT) Subject: Is there a function that applies list of functions to a value? In-Reply-To: References: <21b63d71-14ce-4a59-9e5a-092156832d0b@googlegroups.com> Message-ID: <72faeeaa-d1aa-4514-bde6-23b943ea8e6e@googlegroups.com> W dniu ?roda, 28 sierpnia 2013 15:43:39 UTC+2 u?ytkownik Tim Chase napisa?: > When 3 replies from 3 people all arrive within minutes, each > suggesting reduce(), I'd figure it's the "one obvious way to do > it" :-) I guess it's at least a good hint ;) Thanks to all! :) From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Wed Aug 28 09:09:58 2013 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Wed, 28 Aug 2013 15:09:58 +0200 Subject: Is there a function that applies list of functions to a value? In-Reply-To: References: Message-ID: Am 2013-08-28 14:52 schrieb AdamKal: > Hi, > > From time to time I have to apply a series of functions to a value in such a way: > > func4(func3(func2(func1(myval)))) > > I was wondering if there is a function in standard library that would take a list of functions and a initial value and do the above like this: > > func_im_looking_for([func1, func2, func3, func4], myval) > > I looked in itertools but nothing seamed to do the job. This seams like something vary obvious that was needed many times elsewhere so maybe you could help me? reduce(lambda x, f: f(x), funcs, myval) would do the job. Thomas From Joshua.R.English at gmail.com Wed Aug 28 14:50:53 2013 From: Joshua.R.English at gmail.com (Josh English) Date: Wed, 28 Aug 2013 11:50:53 -0700 (PDT) Subject: Is there a function that applies list of functions to a value? In-Reply-To: References: Message-ID: Reduce tricks are nice, but I prefer clarity sometimes: def double(x): return x*2 def add3(x): return x+3 def compose(*funcs): for func in funcs: if not callable(func): raise ValueError('Must pass callable functions') def inner(value): for func in funcs: value = func(value) return value return inner add_then_double = compose(add3, double) double_then_add = compose(double, add3) print add_then_double(1) # prints 8 print double_then_add(1) # prints 5 From fp2161 at gmail.com Fri Aug 30 02:17:19 2013 From: fp2161 at gmail.com (fp2161 at gmail.com) Date: Thu, 29 Aug 2013 23:17:19 -0700 (PDT) Subject: Is there a function that applies list of functions to a value? In-Reply-To: References: Message-ID: <7867a7cb-e419-4f2e-8d3d-30862de93014@googlegroups.com> On Wednesday, August 28, 2013 8:50:53 PM UTC+2, Josh English wrote: > Reduce tricks are nice, but I prefer clarity sometimes: > > > > def double(x): > > return x*2 > > > > def add3(x): > > return x+3 > > > > > > def compose(*funcs): > > for func in funcs: > > if not callable(func): > > raise ValueError('Must pass callable functions') > > > > def inner(value): > > for func in funcs: > > value = func(value) > > return value > > > > return inner > > > > > > add_then_double = compose(add3, double) > > double_then_add = compose(double, add3) > > > > print add_then_double(1) # prints 8 > > print double_then_add(1) # prints 5 This is my favourite design, simple, clear, straightforward, very pythonic imho. So great that I actually dod not notice it, and wrote it again after you! Imho still, the ValueError you are raising is not that important in this context, it would raise an Error anyway. From wuwei23 at gmail.com Fri Aug 30 02:36:40 2013 From: wuwei23 at gmail.com (alex23) Date: Fri, 30 Aug 2013 16:36:40 +1000 Subject: Is there a function that applies list of functions to a value? In-Reply-To: <7867a7cb-e419-4f2e-8d3d-30862de93014@googlegroups.com> References: <7867a7cb-e419-4f2e-8d3d-30862de93014@googlegroups.com> Message-ID: On 30/08/2013 4:17 PM, fp2161 at gmail.com wrote: > On Wednesday, August 28, 2013 8:50:53 PM UTC+2, Josh English wrote: >> def compose(*funcs): >> for func in funcs: >> if not callable(func): >> raise ValueError('Must pass callable functions') > Imho still, the ValueError you are raising is not that important in this context, it would raise an Error anyway. The main advantage with Josh's approach is that it fails at the point of composition, not when the composed function is first used. It'd be even more useful if it aggregated a list of the failing functions and returned their names as part of the error. Personally, I'd go with an assertion: assert all(map(callable, funcs)), "Must pass callable functions" I find that it makes it more obvious that this is part of the function contract rather than the actual body. From fp2161 at gmail.com Fri Aug 30 04:11:50 2013 From: fp2161 at gmail.com (Fabrice Pombet) Date: Fri, 30 Aug 2013 01:11:50 -0700 (PDT) Subject: Is there a function that applies list of functions to a value? In-Reply-To: References: <7867a7cb-e419-4f2e-8d3d-30862de93014@googlegroups.com> Message-ID: On Friday, August 30, 2013 8:36:40 AM UTC+2, alex23 wrote: > On 30/08/2013 4:17 PM, fp2161 at gmail.com wrote: > > > On Wednesday, August 28, 2013 8:50:53 PM UTC+2, Josh English wrote: > > >> def compose(*funcs): > > >> for func in funcs: > > >> if not callable(func): > > >> raise ValueError('Must pass callable functions') > > > > > Imho still, the ValueError you are raising is not that important in this context, it would raise an Error anyway. > > > > The main advantage with Josh's approach is that it fails at the point of > > composition, not when the composed function is first used. It'd be even > > more useful if it aggregated a list of the failing functions and > > returned their names as part of the error. > > > > Personally, I'd go with an assertion: > > > > assert all(map(callable, funcs)), "Must pass callable functions" > > > > I find that it makes it more obvious that this is part of the function > > contract rather than the actual body. it is a valid point, but I would contend that it makes this quick and easy code a little bit heavy just for the sake of ensuring that you are composing composable functions... The assertion is definitely better. From fp2161 at gmail.com Thu Aug 29 16:50:39 2013 From: fp2161 at gmail.com (fp2161 at gmail.com) Date: Thu, 29 Aug 2013 13:50:39 -0700 (PDT) Subject: Is there a function that applies list of functions to a value? In-Reply-To: References: Message-ID: <90afb50e-7932-4e05-a90d-7005e1de91b8@googlegroups.com> On Wednesday, August 28, 2013 2:52:46 PM UTC+2, AdamKal wrote: > Hi, > > > > From time to time I have to apply a series of functions to a value in such a way: > > > > func4(func3(func2(func1(myval)))) > > > > I was wondering if there is a function in standard library that would take a list of functions and a initial value and do the above like this: > > > > func_im_looking_for([func1, func2, func3, func4], myval) > > > > I looked in itertools but nothing seamed to do the job. This seams like something vary obvious that was needed many times elsewhere so maybe you could help me? My way is so obvious that it may not be that interesting... def func4(f1,f2,f3,f4): def anon(x): f1(f2(f3(f4(x)))) return anon From rosuav at gmail.com Thu Aug 29 17:05:38 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 30 Aug 2013 07:05:38 +1000 Subject: Is there a function that applies list of functions to a value? In-Reply-To: <90afb50e-7932-4e05-a90d-7005e1de91b8@googlegroups.com> References: <90afb50e-7932-4e05-a90d-7005e1de91b8@googlegroups.com> Message-ID: On Fri, Aug 30, 2013 at 6:50 AM, wrote: > My way is so obvious that it may not be that interesting... > > def func4(f1,f2,f3,f4): > def anon(x): > f1(f2(f3(f4(x)))) > return anon Or have it return the result of f1. And then, since it's an anonymous function that simply returns an expression, I'd write it as: def func4(f1,f2,f3,f4): return lambda x: f1(f2(f3(f4(x)))) Of course, that's still restricted to precisely four args. Extending this concept to a variable number of arguments is, uhh, left as an exercise to the reader. Which will probably end up going back to reduce(). :) ChrisA From fp2161 at gmail.com Thu Aug 29 17:27:23 2013 From: fp2161 at gmail.com (fp2161 at gmail.com) Date: Thu, 29 Aug 2013 14:27:23 -0700 (PDT) Subject: Is there a function that applies list of functions to a value? In-Reply-To: References: <90afb50e-7932-4e05-a90d-7005e1de91b8@googlegroups.com> Message-ID: <6e80de10-56c8-4be9-ac88-bc072c409a76@googlegroups.com> On Thursday, August 29, 2013 11:05:38 PM UTC+2, Chris Angelico wrote: > On Fri, Aug 30, 2013 at 6:50 AM, wrote: > > > My way is so obvious that it may not be that interesting... > > > > > > def func4(f1,f2,f3,f4): > > > def anon(x): > > > f1(f2(f3(f4(x)))) > > > return anon > > > > Or have it return the result of f1. And then, since it's an anonymous > > function that simply returns an expression, I'd write it as: > > > > def func4(f1,f2,f3,f4): > > return lambda x: f1(f2(f3(f4(x)))) > > > > Of course, that's still restricted to precisely four args. Extending > > this concept to a variable number of arguments is, uhh, left as an > > exercise to the reader. Which will probably end up going back to > > reduce(). :) > > > > ChrisA Chris, call me a snob, but I resent using lambdas (aren't they usually considered odd/bad practice in python?) From rosuav at gmail.com Thu Aug 29 17:35:39 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 30 Aug 2013 07:35:39 +1000 Subject: Is there a function that applies list of functions to a value? In-Reply-To: <6e80de10-56c8-4be9-ac88-bc072c409a76@googlegroups.com> References: <90afb50e-7932-4e05-a90d-7005e1de91b8@googlegroups.com> <6e80de10-56c8-4be9-ac88-bc072c409a76@googlegroups.com> Message-ID: On Fri, Aug 30, 2013 at 7:27 AM, wrote: > Chris, call me a snob, but I resent using lambdas (aren't they usually considered odd/bad practice in python?) They're not bad practice; all they are is a function without a name, that's restricted to returning a single expression. So they're perfectly suited to this task, and ill-suited to some others. Like everything, lambda's a tool that can be used or misused. ChrisA From steve+comp.lang.python at pearwood.info Thu Aug 29 22:18:58 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 30 Aug 2013 02:18:58 GMT Subject: Is there a function that applies list of functions to a value? References: <90afb50e-7932-4e05-a90d-7005e1de91b8@googlegroups.com> <6e80de10-56c8-4be9-ac88-bc072c409a76@googlegroups.com> Message-ID: <52200111$0$6599$c3e8da3$5496439d@news.astraweb.com> On Thu, 29 Aug 2013 14:27:23 -0700, fp2161 wrote: > Chris, call me a snob, but I resent using lambdas (aren't they usually > considered odd/bad practice in python?) Only among people who dislike functional programming idioms. Like GvR. It is true that lambda functions are slightly restricted compared to "normal" functions: they are limited to a single expression, and they all share the same name '', which if you have a lot of them can make debugging annoying. So *overuse* of lambdas is considered bad form. But for callbacks and such, they're fine. It is frowned upon to *directly* bind a lambda to a name, as in this: plusOne = lambda x: x+1 instead of: def plusOne(x): return x+1 which is fair enough for production code, but at the interactive interpreter (and throwaway code) I'll continue to feel free to assign anonymous functions to names just as I assign anonymous ints and anonymous lists to names :-) I say "directly" because this of course is allowed: funcs = [lambda x: x+1, lambda x: x*2, lambda x: x**3] for func in func: ... and *much* better than having to predefine plusOne, timesTwo, powerThree functions. -- Steven From fp2161 at gmail.com Fri Aug 30 02:14:09 2013 From: fp2161 at gmail.com (fp2161 at gmail.com) Date: Thu, 29 Aug 2013 23:14:09 -0700 (PDT) Subject: Is there a function that applies list of functions to a value? In-Reply-To: References: <90afb50e-7932-4e05-a90d-7005e1de91b8@googlegroups.com> <6e80de10-56c8-4be9-ac88-bc072c409a76@googlegroups.com> Message-ID: <50764540-6edd-4d65-b44c-9fbd1daf9043@googlegroups.com> On Thursday, August 29, 2013 11:35:39 PM UTC+2, Chris Angelico wrote: > On Fri, Aug 30, 2013 at 7:27 AM, wrote: > > > Chris, call me a snob, but I resent using lambdas (aren't they usually considered odd/bad practice in python?) > > > > They're not bad practice; all they are is a function without a name, > > that's restricted to returning a single expression. So they're > > perfectly suited to this task, and ill-suited to some others. > > > > Like everything, lambda's a tool that can be used or misused. > > > > ChrisA For this purpose however, I suspect that a named function with a proper docstring that can be imported and reused over and over again is probably more appropriate than a lambda (the first post is telling us about something happening from time to time...) From wuwei23 at gmail.com Fri Aug 30 02:23:44 2013 From: wuwei23 at gmail.com (alex23) Date: Fri, 30 Aug 2013 16:23:44 +1000 Subject: Is there a function that applies list of functions to a value? In-Reply-To: <50764540-6edd-4d65-b44c-9fbd1daf9043@googlegroups.com> References: <90afb50e-7932-4e05-a90d-7005e1de91b8@googlegroups.com> <6e80de10-56c8-4be9-ac88-bc072c409a76@googlegroups.com> <50764540-6edd-4d65-b44c-9fbd1daf9043@googlegroups.com> Message-ID: On 30/08/2013 4:14 PM, fp2161 at gmail.com wrote: > For this purpose however, I suspect that a named function with a proper docstring that can be imported and reused over and over again is probably more appropriate than a lambda Given that in Chris' example the lambda was returned from a factory, importing the inner function by name is never going to be a concern. It's also possible to assign to both __name__ and __doc__ for a lambda, which can be useful at times. From fp2161 at gmail.com Fri Aug 30 04:06:00 2013 From: fp2161 at gmail.com (Fabrice Pombet) Date: Fri, 30 Aug 2013 01:06:00 -0700 (PDT) Subject: Is there a function that applies list of functions to a value? In-Reply-To: References: <90afb50e-7932-4e05-a90d-7005e1de91b8@googlegroups.com> <6e80de10-56c8-4be9-ac88-bc072c409a76@googlegroups.com> <50764540-6edd-4d65-b44c-9fbd1daf9043@googlegroups.com> Message-ID: <06f5bce9-bfae-4700-b81e-195e84e54454@googlegroups.com> On Friday, August 30, 2013 8:23:44 AM UTC+2, alex23 wrote: > On 30/08/2013 4:14 PM, fp2161 at gmail.com wrote: > > > For this purpose however, I suspect that a named function with a proper docstring that can be imported and reused over and over again is probably more appropriate than a lambda > > > > Given that in Chris' example the lambda was returned from a factory, > > importing the inner function by name is never going to be a concern. > > > > It's also possible to assign to both __name__ and __doc__ for a lambda, > > which can be useful at times. I am sorry, I can't see any of Chris' code in this thread, where is it??? From fp2161 at gmail.com Thu Aug 29 17:48:12 2013 From: fp2161 at gmail.com (fp2161 at gmail.com) Date: Thu, 29 Aug 2013 14:48:12 -0700 (PDT) Subject: Is there a function that applies list of functions to a value? In-Reply-To: References: <90afb50e-7932-4e05-a90d-7005e1de91b8@googlegroups.com> Message-ID: <4544ac21-e36f-4d9e-8972-8f6a41e629ef@googlegroups.com> On Thursday, August 29, 2013 11:05:38 PM UTC+2, Chris Angelico wrote: > On Fri, Aug 30, 2013 at 6:50 AM, wrote: > > > My way is so obvious that it may not be that interesting... > > > > > > def func4(f1,f2,f3,f4): > > > def anon(x): > > > f1(f2(f3(f4(x)))) > > > return anon > > > > Or have it return the result of f1. And then, since it's an anonymous > > function that simply returns an expression, I'd write it as: > > > > def func4(f1,f2,f3,f4): > > return lambda x: f1(f2(f3(f4(x)))) > > > > Of course, that's still restricted to precisely four args. Extending > > this concept to a variable number of arguments is, uhh, left as an > > exercise to the reader. Which will probably end up going back to > > reduce(). :) > > > > ChrisA Here is the generalisable version: def comp(*func): def anon(x): res=x for f in func: res=f(res) return res return anon you can then compose your function in __main__: f4=comp(f5,f6,f7) and call it: print(x) ... (I hope that Chris is happy now...) From tjreedy at udel.edu Fri Aug 30 00:38:55 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 30 Aug 2013 00:38:55 -0400 Subject: Is there a function that applies list of functions to a value? In-Reply-To: <4544ac21-e36f-4d9e-8972-8f6a41e629ef@googlegroups.com> References: <90afb50e-7932-4e05-a90d-7005e1de91b8@googlegroups.com> <4544ac21-e36f-4d9e-8972-8f6a41e629ef@googlegroups.com> Message-ID: On 8/29/2013 5:48 PM, fp2161 at gmail.com wrote: > Here is the generalisable version: > > def comp(*func): > def anon(x): > res=x > for f in func: > res=f(res) > return res > return anon With a bit more work, one can set the __name__ and __qualname__ attributes. import math as m def comp(*func, name=''): def anon(x): res=x for f in func: res=f(res) return res if name: anon.__name__ = name q = anon.__qualname__.rsplit('.', maxsplit=1) q[1] = name anon.__qualname__ = '.'.join(q) return anon esincos = comp(m.exp, m.sin, m.cos, name='esincos') print(esincos) # .esincos at 0x00000000033107B8> -- Terry Jan Reedy From steve+comp.lang.python at pearwood.info Thu Aug 29 22:09:45 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 30 Aug 2013 02:09:45 GMT Subject: Is there a function that applies list of functions to a value? References: <90afb50e-7932-4e05-a90d-7005e1de91b8@googlegroups.com> Message-ID: <521ffee9$0$6599$c3e8da3$5496439d@news.astraweb.com> On Thu, 29 Aug 2013 13:50:39 -0700, fp2161 wrote: > My way is so obvious that it may not be that interesting... > > def func4(f1,f2,f3,f4): > def anon(x): > f1(f2(f3(f4(x)))) > return anon I don't think "obvious" is quite the right description. Well, perhaps "obviously wrong" :-) You also need to define func1 (trivial), func2, func3, func5, func6, func7, func8, ..., func2147483647, plus another master function to choose between them, depending on the number of functions provided as argument. I assume that the maximum number of arguments given is 2**31-1. Python may not actually have that limitation, in which case you would need to define additional functions. Or... you would have to come up with an implementation which doesn't hard- code the number of functions used. -- Steven From fp2161 at gmail.com Fri Aug 30 02:06:49 2013 From: fp2161 at gmail.com (fp2161 at gmail.com) Date: Thu, 29 Aug 2013 23:06:49 -0700 (PDT) Subject: Is there a function that applies list of functions to a value? In-Reply-To: <521ffee9$0$6599$c3e8da3$5496439d@news.astraweb.com> References: <90afb50e-7932-4e05-a90d-7005e1de91b8@googlegroups.com> <521ffee9$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Friday, August 30, 2013 4:09:45 AM UTC+2, Steven D'Aprano wrote: > On Thu, 29 Aug 2013 13:50:39 -0700, fp2161 wrote: > > > > > My way is so obvious that it may not be that interesting... > > > > > > def func4(f1,f2,f3,f4): > > > def anon(x): > > > f1(f2(f3(f4(x)))) > > > return anon > I don't think "obvious" is quite the right description. Well, perhaps > > "obviously wrong" :-) > You also need to define func1 (trivial), func2, func3, func5, func6, > > func7, func8, ..., func2147483647, plus another master function to choose > > between them, depending on the number of functions provided as argument. > I assume that the maximum number of arguments given is 2**31-1. Python > > may not actually have that limitation, in which case you would need to > > define additional functions. > Or... you would have to come up with an implementation which doesn't hard- > > code the number of functions used. > > Steven I got the generalisation criticism before yours, and generalised it accordingly. Unfortunately it was wrong essentially because it was so obvious that Josh Englsih posted essentially the same one before me... From baruchel at gmx.com Wed Aug 28 09:02:47 2013 From: baruchel at gmx.com (Thomas Baruchel) Date: 28 Aug 2013 13:02:47 GMT Subject: My attempts in playing with tail-recursion in python Message-ID: <521df4f7$0$2551$426a74cc@news.free.fr> Hi, I would like to share some of my recent attempts concerning recursivity in python, more precisely recursivity with lambda functions. I know that the title of my thread with the "tail-recursion" words may wake up some long and old war; please don't take it as such. I am not claiming anything about what programming languages should be or not; I enjoy using python for many purposes and I perfectly know how to handle loops for achieving some tasks, but sometimes I like rather think with recursivity because I have been using various dialects of Lisp for a long time. I had a very great time studying the Y-combinator; then I wrote various pieces of code for remapping recursive calls to loop iterations. The following functions are fully usable; I hope someone will enjoy using them. If you are not interested by the explanations, just jump to the end of the message and take my functions for using them. Here is a recursive function: fac = lambda f: lambda n: 1 if n==0 else n*f(n-1) The Y-combinator is well known: Y = lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args))) Now, you can use the recursive function: rf = Y(fac) rf(5) Now we write fac as a tail-recursive function: fac = lambda f: lambda a,b: b if a==0 else f(a-1,a*b) rf = Y(fac) rf(5,1) but of course, the recursivity is handled like previously. In fact, it is very easy to build some effcient wrapper; for instance: (lambda f: f(lambda *args: ("callback",args))) will act in the following way: (lambda f: f(lambda *args: ("callback",args)))(fac)(5,1) --> ('callback', (4, 5)) (lambda f: f(lambda *args: ("callback",args)))(fac)(4,5) --> ('callback', (3, 20)) (lambda f: f(lambda *args: ("callback",args)))(fac)(3,20)) --> ('callback', (2, 60)) (lambda f: f(lambda *args: ("callback",args)))(fac)(2,60) --> ('callback', (1, 120)) (lambda f: f(lambda *args: ("callback",args)))(fac)(1,120) --> ('callback', (0, 120)) (lambda f: f(lambda *args: ("callback",args)))(fac)(0,120) --> 120 Here, the "callback" string is arbitrary chosen in order to make the loop know that a next call is needed. Now, the recursivity is handled as successive calls, which is what I was looking for. A first proposal could be: def with_tail_recursion(func): x = (lambda f: f(lambda *args: ("callback",args)))(func) def wrapper(*args): out = ("callback",args) while type(out)==tuple and out[0]=="callback": out = x(*out[1]) return out return wrapper Now, the very same function can be used as recursive or inside a loop: fac = lambda f: lambda a,b: b if a==0 else f(a-1,a*b) rf = Y(fac) tr = with_tail_recursion(fac) rf(5,1) --> 120 tr(5,1) --> 120 Of course, if most of your job relies on writing functions returning tuples with the string "callback" being the first argument, you should use another keyword in the previous code ;-) You can inspect the stack by raising an exception: f2 = lambda f: lambda n: 1/n if n==0 else f(n-1) and see the differences between Y(f2)(5) and with_tail_recursion(f2)(5) But I was not fully happy with the use of a tuple, efficient but a little too tricky to my eyes. For that reason, I took the Y combinator again and slightly modified it: lambda f: (lambda x: x(x))(lambda y: f(lambda *args: lambda: y(y)(*args))) Now, the function doesn't call itself, but returns a function calling itself with the relevant arguments. I like this new function better than the previous one, but it is now more difficult for the wrapper to detect when it is time to leave the loop. Right now, I use the following test: checking for the __call__ string in the dir() list. This means ONE IMPORTANT THING: this second wrapper can't handle tail-recursive functions returning functions, (which is actually more likely to happen than tail-recursive functions returning tuples with the string "callback" being the first argument ;-) ). But I like it that way: def B(func): x = (lambda f: (lambda x: x(x))(lambda y: f(lambda *args: lambda: y(y)(*args))))(func) def wrapper(*args): out = x(*args) while '__call__' in dir(out): out = out() return out return wrapper tr2 = B(fac) tr2(5,1) --> 120 Now, you can write functions with a huge depth of recusivity with no problem : B(lambda f: lambda n: "ok" if n==0 else f(n-1))(5000) (don't try with the Y combinator instead...) Best regards, -- Thomas Baruchel From baruchel at gmx.com Wed Aug 28 16:16:18 2013 From: baruchel at gmx.com (Thomas Baruchel) Date: 28 Aug 2013 20:16:18 GMT Subject: My attempts in playing with tail-recursion in python References: <521df4f7$0$2551$426a74cc@news.free.fr> Message-ID: <521e5a92$0$3734$426a74cc@news.free.fr> Le 28-08-2013, Thomas Baruchel a ?crit?: > The following functions are fully usable; I hope someone will enjoy using > them. > > If you are not interested by the explanations, just jump to the end of the > message and take my functions for using them. Despite the very short size of my function, I made a module of it as a convenience for my own use. I share here my "recursion.py" file in which I also added some docstrings. ----------------- module "recursion.py" ------------------------------- """ The recursion module provides convenient functions for handling recursion with lambda functions. The famous Y-combinator is provided as a convenience, but the most distinctive function of the module is the B function for handling tail-recursion. Written by Thomas Baruchel """ Y = lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args))) def B(func): """ B(lambda) -> lambda Return a usable function from a lambda expression written with a tail-recursion style. The new function will be evaluated inside a loop with no recursive calls, allowing high depths of recursion. Since the internal loop evaluates the return of the function as long as it is callable, the function does not work with functions returning functions (at the end of the recursive calls); there is no restriction otherwise. E.g.: fac = lambda f: lambda a,b: b if a==0 else f(a-1,a*b) func = B(fac) func(5,1) --> 120 or more shortly: B(lambda f: lambda a,b: b if a==0 else f(a-1,a*b))(5,1) --> 120 """ x = (lambda f: (lambda x: x(x))(lambda y: f(lambda *args: lambda: y(y)(*args))))(func) def wrapper(*args): out = x(*args) while callable(out): out = out() return out return wrapper From ndbecker2 at gmail.com Wed Aug 28 11:15:52 2013 From: ndbecker2 at gmail.com (Neal Becker) Date: Wed, 28 Aug 2013 11:15:52 -0400 Subject: python3 integer division debugging Message-ID: The change in integer division seems to be the most insidious source of silent errors in porting code from python2 - since it changes the behaviour or valid code silently. I wish the interpreter had an instrumented mode to detect and report such problems. From oscar.j.benjamin at gmail.com Wed Aug 28 11:21:09 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Wed, 28 Aug 2013 16:21:09 +0100 Subject: python3 integer division debugging In-Reply-To: References: Message-ID: On 28 August 2013 16:15, Neal Becker wrote: > The change in integer division seems to be the most insidious source of silent > errors in porting code from python2 - since it changes the behaviour or valid > code silently. > > I wish the interpreter had an instrumented mode to detect and report such > problems. Is that a joke? Run the code under Python 2.6/2.7 with the -3 flag: $ cat test.py print(10 / 7) $ python -3 test.py test.py:2: DeprecationWarning: classic int division print(10 / 7) 1 Oscar From rosuav at gmail.com Wed Aug 28 11:26:47 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 29 Aug 2013 01:26:47 +1000 Subject: python3 integer division debugging In-Reply-To: References: Message-ID: On Thu, Aug 29, 2013 at 1:21 AM, Oscar Benjamin wrote: > On 28 August 2013 16:15, Neal Becker wrote: >> The change in integer division seems to be the most insidious source of silent >> errors in porting code from python2 - since it changes the behaviour or valid >> code silently. >> >> I wish the interpreter had an instrumented mode to detect and report such >> problems. > > Is that a joke? > > Run the code under Python 2.6/2.7 with the -3 flag: I wouldn't say that's a joke, it's just another demonstration of Guido's time machine :) ChrisA From ndbecker2 at gmail.com Wed Aug 28 11:29:23 2013 From: ndbecker2 at gmail.com (Neal Becker) Date: Wed, 28 Aug 2013 11:29:23 -0400 Subject: python3 integer division debugging References: Message-ID: Chris Angelico wrote: > On Thu, Aug 29, 2013 at 1:21 AM, Oscar Benjamin > wrote: >> On 28 August 2013 16:15, Neal Becker wrote: >>> The change in integer division seems to be the most insidious source of >>> silent errors in porting code from python2 - since it changes the behaviour >>> or valid code silently. >>> >>> I wish the interpreter had an instrumented mode to detect and report such >>> problems. >> >> Is that a joke? >> >> Run the code under Python 2.6/2.7 with the -3 flag: > > I wouldn't say that's a joke, it's just another demonstration of > Guido's time machine :) > > ChrisA Ah! I looked for it in python3. Funny it isn't there as well. Thanks! From random832 at fastmail.us Wed Aug 28 11:24:03 2013 From: random832 at fastmail.us (random832 at fastmail.us) Date: Wed, 28 Aug 2013 11:24:03 -0400 Subject: python3 integer division debugging In-Reply-To: References: Message-ID: <1377703443.20490.15167249.3FDD2E91@webmail.messagingengine.com> On Wed, Aug 28, 2013, at 11:15, Neal Becker wrote: > The change in integer division seems to be the most insidious source of > silent > errors in porting code from python2 - since it changes the behaviour or > valid > code silently. > > I wish the interpreter had an instrumented mode to detect and report such > problems. There is such a mode in python2 from version 2.2 through 2.7: -Q warn. From tjreedy at udel.edu Wed Aug 28 15:23:45 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 28 Aug 2013 15:23:45 -0400 Subject: python3 integer division debugging In-Reply-To: References: Message-ID: On 8/28/2013 11:15 AM, Neal Becker wrote: > The change in integer division seems to be the most insidious source of silent > errors in porting code from python2 - since it changes the behaviour or valid > code silently. In Python since 2.??, put 'from __future__ import integer_division' (sp?) at the top of your code -- Terry Jan Reedy From johnl at iecc.com Wed Aug 28 12:44:53 2013 From: johnl at iecc.com (John Levine) Date: Wed, 28 Aug 2013 16:44:53 +0000 (UTC) Subject: String splitting with exceptions Message-ID: I have a crufty old DNS provisioning system that I'm rewriting and I hope improving in python. (It's based on tinydns if you know what that is.) The record formats are, in the worst case, like this: foo.[DOM]::[IP6::4361:6368:6574]:600:: What I would like to do is to split this string into a list like this: [ 'foo.[DOM]','','[IP6::4361:6368:6574]','600','' ] Colons are separators except when they're inside square brackets. I have been messing around with re.split() and re.findall() and haven't been able to come up with either a working separator pattern for split() or a working field pattern for findall(). I came pretty close with findall() but can't get it to reliably match the nothing between two adjacent colons not inside brackets. Any suggestions? I realize I could do it in a loop where I pick stuff off the front of the string, but yuck. This is in python 2.7.5. -- Regards, John Levine, johnl at iecc.com, Primary Perpetrator of "The Internet for Dummies", Please consider the environment before reading this e-mail. http://jl.ly From skip at pobox.com Wed Aug 28 12:55:31 2013 From: skip at pobox.com (Skip Montanaro) Date: Wed, 28 Aug 2013 11:55:31 -0500 Subject: String splitting with exceptions In-Reply-To: References: Message-ID: > The record formats are, in the worst case, like this: > > foo.[DOM]::[IP6::4361:6368:6574]:600:: > Any suggestions? Write a little parser that can handle the record format? Skip From random832 at fastmail.us Wed Aug 28 13:14:03 2013 From: random832 at fastmail.us (random832 at fastmail.us) Date: Wed, 28 Aug 2013 13:14:03 -0400 Subject: String splitting with exceptions In-Reply-To: References: Message-ID: <1377710043.22605.15211369.55EB0B33@webmail.messagingengine.com> On Wed, Aug 28, 2013, at 12:44, John Levine wrote: > I have a crufty old DNS provisioning system that I'm rewriting and I > hope improving in python. (It's based on tinydns if you know what > that is.) > > The record formats are, in the worst case, like this: > > foo.[DOM]::[IP6::4361:6368:6574]:600:: > > What I would like to do is to split this string into a list like this: > > [ 'foo.[DOM]','','[IP6::4361:6368:6574]','600','' ] > > Colons are separators except when they're inside square brackets. I > have been messing around with re.split() and re.findall() and haven't > been able to come up with either a working separator pattern for > split() or a working field pattern for findall(). I came pretty > close with findall() but can't get it to reliably match the > nothing between two adjacent colons not inside brackets. > > Any suggestions? I realize I could do it in a loop where I pick stuff > off the front of the string, but yuck. > > This is in python 2.7.5. Can you have brackets within brackets? If so, this is impossible to deal with within a regex. Otherwise: >>> re.findall('((?:[^[:]|\[[^]]*\])*):?',s) ['foo.[DOM]', '', '[IP6::4361:6368:6574]', '600', '', ''] I'm not sure why _your_ list only has one empty string at the end. Is the record always terminated by a colon that is not meant to imply an empty field after it? If so, remove the question mark: >>> re.findall('((?:[^[:]|\[[^]]*\])*):',s) ['foo.[DOM]', '', '[IP6::4361:6368:6574]', '600', ''] I've done this kind of thing (for validation, not capturing) for email addresses (there are some obscure bits of email address syntax that need it) before, so it came to mind immediately. From python.list at tim.thechases.com Wed Aug 28 13:32:46 2013 From: python.list at tim.thechases.com (Tim Chase) Date: Wed, 28 Aug 2013 12:32:46 -0500 Subject: String splitting with exceptions In-Reply-To: <1377710043.22605.15211369.55EB0B33@webmail.messagingengine.com> References: <1377710043.22605.15211369.55EB0B33@webmail.messagingengine.com> Message-ID: <20130828123246.665d2d39@bigbox.christie.dr> On 2013-08-28 13:14, random832 at fastmail.us wrote: > On Wed, Aug 28, 2013, at 12:44, John Levine wrote: > > I have a crufty old DNS provisioning system that I'm rewriting > > and I hope improving in python. (It's based on tinydns if you > > know what that is.) > > > > The record formats are, in the worst case, like this: > > > > foo.[DOM]::[IP6::4361:6368:6574]:600:: > > Otherwise: > >>> re.findall('((?:[^[:]|\[[^]]*\])*):?',s) > ['foo.[DOM]', '', '[IP6::4361:6368:6574]', '600', '', ''] > > I'm not sure why _your_ list only has one empty string at the end. I wondered that. I also wondered about bracketed quoting that doesn't start at the beginning of a field: foo.[one:two]::[IP6::1234:5678:9101]:600:: ^ This might be bogus, or one might want to catch this case. -tkc From neilc at norwich.edu Wed Aug 28 14:18:11 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 28 Aug 2013 18:18:11 GMT Subject: String splitting with exceptions References: <1377710043.22605.15211369.55EB0B33@webmail.messagingengine.com> Message-ID: On 2013-08-28, Tim Chase wrote: > On 2013-08-28 13:14, random832 at fastmail.us wrote: >> On Wed, Aug 28, 2013, at 12:44, John Levine wrote: >> > I have a crufty old DNS provisioning system that I'm rewriting >> > and I hope improving in python. (It's based on tinydns if you >> > know what that is.) >> > >> > The record formats are, in the worst case, like this: >> > >> > foo.[DOM]::[IP6::4361:6368:6574]:600:: >> >> Otherwise: >> >>> re.findall('((?:[^[:]|\[[^]]*\])*):?',s) >> ['foo.[DOM]', '', '[IP6::4361:6368:6574]', '600', '', ''] >> >> I'm not sure why _your_ list only has one empty string at the end. > > I wondered that. Good point. My little parser fails on that, too. It'll miss *all* final fields. My parser needs "if s: yield s[b:]" at the end, to operate like str.split, where the empty string is special. -- Neil Cerutti From neilc at norwich.edu Wed Aug 28 14:08:11 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 28 Aug 2013 18:08:11 GMT Subject: String splitting with exceptions References: Message-ID: On 2013-08-28, John Levine wrote: > I have a crufty old DNS provisioning system that I'm rewriting and I > hope improving in python. (It's based on tinydns if you know what > that is.) > > The record formats are, in the worst case, like this: > > foo.[DOM]::[IP6::4361:6368:6574]:600:: > > What I would like to do is to split this string into a list like this: > > [ 'foo.[DOM]','','[IP6::4361:6368:6574]','600','' ] > > Colons are separators except when they're inside square > brackets. I have been messing around with re.split() and > re.findall() and haven't been able to come up with either a > working separator pattern for split() or a working field > pattern for findall(). I came pretty close with findall() but > can't get it to reliably match the nothing between two adjacent > colons not inside brackets. > > Any suggestions? I realize I could do it in a loop where I pick > stuff off the front of the string, but yuck. A little parser, as Skip suggested, is a good way to go. The brackets make your string context-sensitive, a difficult concept to cleanly parse with a regex. I initially hoped a csv module dialect could work, but the quote character is (currently) hard-coded to be a single, simple character, i.e., I can't tell it to treat [xxx] as "xxx". What about Skip's suggestion? A little parser. It might seem crass or something, but it really is easier than musceling a regex into a context sensitive grammer. def dns_split(s): in_brackets = False b = 0 # index of beginning of current string for i, c in enumerate(s): if not in_brackets: if c == "[": in_brackets = True elif c == ':': yield s[b:i] b = i+1 elif c == "]": in_brackets = False >>> print(list(dns_split(s))) ['foo.[DOM]', '', '[IP6::4361:6368:6574]', '600', ''] It'll gag on nested brackets (fixable with a counter) and has no error handling (requires thought), but it's a start. -- Neil Cerutti From __peter__ at web.de Wed Aug 28 14:31:17 2013 From: __peter__ at web.de (Peter Otten) Date: Wed, 28 Aug 2013 20:31:17 +0200 Subject: String splitting with exceptions References: Message-ID: Neil Cerutti wrote: > On 2013-08-28, John Levine wrote: >> I have a crufty old DNS provisioning system that I'm rewriting and I >> hope improving in python. (It's based on tinydns if you know what >> that is.) >> >> The record formats are, in the worst case, like this: >> >> foo.[DOM]::[IP6::4361:6368:6574]:600:: >> >> What I would like to do is to split this string into a list like this: >> >> [ 'foo.[DOM]','','[IP6::4361:6368:6574]','600','' ] >> >> Colons are separators except when they're inside square >> brackets. I have been messing around with re.split() and >> re.findall() and haven't been able to come up with either a >> working separator pattern for split() or a working field >> pattern for findall(). I came pretty close with findall() but >> can't get it to reliably match the nothing between two adjacent >> colons not inside brackets. >> >> Any suggestions? I realize I could do it in a loop where I pick >> stuff off the front of the string, but yuck. > > A little parser, as Skip suggested, is a good way to go. > > The brackets make your string context-sensitive, a difficult > concept to cleanly parse with a regex. > > I initially hoped a csv module dialect could work, but the quote > character is (currently) hard-coded to be a single, simple > character, i.e., I can't tell it to treat [xxx] as "xxx". > > What about Skip's suggestion? A little parser. It might seem > crass or something, but it really is easier than musceling a > regex into a context sensitive grammer. > > def dns_split(s): > in_brackets = False > b = 0 # index of beginning of current string > for i, c in enumerate(s): > if not in_brackets: > if c == "[": > in_brackets = True > elif c == ':': > yield s[b:i] > b = i+1 > elif c == "]": > in_brackets = False I think you need one more yield outside the loop. >>>> print(list(dns_split(s))) > ['foo.[DOM]', '', '[IP6::4361:6368:6574]', '600', ''] > > It'll gag on nested brackets (fixable with a counter) and has no > error handling (requires thought), but it's a start. Something similar on top of regex: >>> def split(s): ... start = level = 0 ... for m in re.compile(r"[[:\]]").finditer(s): ... if m.group() == "[": level += 1 ... elif m.group() == "]": ... assert level ... level -= 1 ... elif level == 0: ... yield s[start:m.start()] ... start = m.end() ... yield s[start:] ... >>> list(split("a[b:c:]:d")) ['a[b:c:]', 'd'] >>> list(split("a[b:c[:]]:d")) ['a[b:c[:]]', 'd'] >>> list(split("")) [''] >>> list(split(":")) ['', ''] >>> list(split(":x")) ['', 'x'] >>> list(split("[:x]")) ['[:x]'] >>> list(split(":[:x]")) ['', '[:x]'] >>> list(split(":[:[:]:x]")) ['', '[:[:]:x]'] >>> list(split("[:::]")) ['[:::]'] >>> s = "foo.[DOM]::[IP6::4361:6368:6574]:600::" >>> list(split(s)) ['foo.[DOM]', '', '[IP6::4361:6368:6574]', '600', '', ''] Note that there is one more empty string which I believe the OP forgot. From johnl at iecc.com Wed Aug 28 17:35:16 2013 From: johnl at iecc.com (John Levine) Date: Wed, 28 Aug 2013 21:35:16 +0000 (UTC) Subject: String splitting with exceptions References: Message-ID: >Can you have brackets within brackets? If so, this is impossible to deal >with within a regex. Nope. It's a regular language, not a CFL. >Otherwise: >>>> re.findall('((?:[^[:]|\[[^]]*\])*):?',s) >['foo.[DOM]', '', '[IP6::4361:6368:6574]', '600', '', ''] That seems to do it, thanks. -- Regards, John Levine, johnl at iecc.com, Primary Perpetrator of "The Internet for Dummies", Please consider the environment before reading this e-mail. http://jl.ly From wxjmfauth at gmail.com Thu Aug 29 03:26:26 2013 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Thu, 29 Aug 2013 00:26:26 -0700 (PDT) Subject: String splitting with exceptions In-Reply-To: References: Message-ID: Le mercredi 28 ao?t 2013 18:44:53 UTC+2, John Levine a ?crit?: > I have a crufty old DNS provisioning system that I'm rewriting and I > > hope improving in python. (It's based on tinydns if you know what > > that is.) > > > > The record formats are, in the worst case, like this: > > > > foo.[DOM]::[IP6::4361:6368:6574]:600:: > > > > What I would like to do is to split this string into a list like this: > > > > [ 'foo.[DOM]','','[IP6::4361:6368:6574]','600','' ] > > > > Colons are separators except when they're inside square brackets. I > > have been messing around with re.split() and re.findall() and haven't > > been able to come up with either a working separator pattern for > > split() or a working field pattern for findall(). I came pretty > > close with findall() but can't get it to reliably match the > > nothing between two adjacent colons not inside brackets. > > > > Any suggestions? I realize I could do it in a loop where I pick stuff > > off the front of the string, but yuck. > > > > This is in python 2.7.5. > > > > -- > > Regards, > > John Levine, johnl at iecc.com, Primary Perpetrator of "The Internet for Dummies", > > Please consider the environment before reading this e-mail. http://jl.ly ---------- Basic idea: protect -> split -> unprotect >>> s = 'foo.[DOM]::[IP6::4361:6368:6574]:600::' >>> r = s.replace('[IP6::', '***') >>> a = r.split('::') >>> a ['foo.[DOM]', '***4361:6368:6574]:600', ''] >>> a[1] = a[1].replace('***', '[IP6::') >>> a ['foo.[DOM]', '[IP6::4361:6368:6574]:600', ''] jmf From ecazs.net at gmail.com Wed Aug 28 16:14:39 2013 From: ecazs.net at gmail.com (ecazs.net at gmail.com) Date: Wed, 28 Aug 2013 13:14:39 -0700 (PDT) Subject: Moving to Python for web Message-ID: <52bbd0bc-7c3e-4c85-bfe1-7fd4912945be@googlegroups.com> So, I have been working in PHP for several years but I want to learn something new. That something new is Python. But since I'm a web developer I want to build stuff for the web. I don't want to use Django because it's too bloated, it seem to do everything for you. I don't like that. I want to do the "plumbing". But at the same time I have no idea how I would write something that could handle cookies, sessions, post, get etc etc; so I assume I have to use some kind of web framework. So, can you recommend a minimal web framework that still allows me to write in Python? Furthermore, do I even need one? I am currently running NGINX and I have UWSGI installed. I'm just worried about how I would handle form submissions, get requests and sessions/cookies. Any advice would be greatly appreciated. From joel.goldstick at gmail.com Wed Aug 28 17:08:52 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Wed, 28 Aug 2013 17:08:52 -0400 Subject: Moving to Python for web In-Reply-To: <52bbd0bc-7c3e-4c85-bfe1-7fd4912945be@googlegroups.com> References: <52bbd0bc-7c3e-4c85-bfe1-7fd4912945be@googlegroups.com> Message-ID: On Wed, Aug 28, 2013 at 4:14 PM, wrote: > So, I have been working in PHP for several years but I want to learn something new. That something new is Python. But since I'm a web developer I want to build stuff for the web. > > I don't want to use Django because it's too bloated, it seem to do everything for you. I don't like that. I want to do the "plumbing". But at the same time I have no idea how I would write something that could handle cookies, sessions, post, get etc etc; so I assume I have to use some kind of web framework. > > So, can you recommend a minimal web framework that still allows me to write in Python? Furthermore, do I even need one? I am currently running NGINX and I have UWSGI installed. I'm just worried about how I would handle form submissions, get requests and sessions/cookies. > > Any advice would be greatly appreciated. I like django, but you may want to google "python micro web framework" There are a handful with some following so that there are communities, docs, etc available As to get/cookies/sessions check out the requests module (3rd party - well received) http://docs.python-requests.org/en/latest/ > -- > http://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com From ecazs.net at gmail.com Wed Aug 28 17:25:44 2013 From: ecazs.net at gmail.com (Andreas Ecaz) Date: Wed, 28 Aug 2013 14:25:44 -0700 (PDT) Subject: Moving to Python for web In-Reply-To: References: <52bbd0bc-7c3e-4c85-bfe1-7fd4912945be@googlegroups.com> Message-ID: On Wednesday, August 28, 2013 11:08:52 PM UTC+2, Joel Goldstick wrote: > On Wed, Aug 28, 2013 at 4:14 PM, wrote: > > > So, I have been working in PHP for several years but I want to learn something new. That something new is Python. But since I'm a web developer I want to build stuff for the web. > > > > > > I don't want to use Django because it's too bloated, it seem to do everything for you. I don't like that. I want to do the "plumbing". But at the same time I have no idea how I would write something that could handle cookies, sessions, post, get etc etc; so I assume I have to use some kind of web framework. > > > > > > So, can you recommend a minimal web framework that still allows me to write in Python? Furthermore, do I even need one? I am currently running NGINX and I have UWSGI installed. I'm just worried about how I would handle form submissions, get requests and sessions/cookies. > > > > > > Any advice would be greatly appreciated. > > > > I like django, but you may want to google "python micro web framework" > > > > There are a handful with some following so that there are communities, > > docs, etc available > > > > As to get/cookies/sessions check out the requests module (3rd party - > > well received) http://docs.python-requests.org/en/latest/ > > > -- > > > http://mail.python.org/mailman/listinfo/python-list > > > > > > > > -- > > Joel Goldstick > > http://joelgoldstick.com I've looked at Flask, Bottle and Web.py. I quite like the look of Bottle. I'll keep looking for some other microframeworks, maybe I can find something else that interests me. Thank you. From ecazs.net at gmail.com Wed Aug 28 17:36:01 2013 From: ecazs.net at gmail.com (Andreas Ecaz) Date: Wed, 28 Aug 2013 14:36:01 -0700 (PDT) Subject: Moving to Python for web In-Reply-To: References: <52bbd0bc-7c3e-4c85-bfe1-7fd4912945be@googlegroups.com> Message-ID: <909c9bca-9e52-482c-adfb-044aa2c91a20@googlegroups.com> On Wednesday, August 28, 2013 11:25:44 PM UTC+2, Andreas Ecaz wrote: > I've looked at Flask, Bottle and Web.py. I quite like the look of Bottle. I'll keep looking for some other microframeworks, maybe I can find something else that interests me. > > > > Thank you. At the moment I'm worried about writing more framework than python (if that makes sense) which seems unlikely with MOST microframeworks. From sfourman at gmail.com Wed Aug 28 22:18:50 2013 From: sfourman at gmail.com (Sam Fourman Jr.) Date: Wed, 28 Aug 2013 22:18:50 -0400 Subject: Moving to Python for web In-Reply-To: <52bbd0bc-7c3e-4c85-bfe1-7fd4912945be@googlegroups.com> References: <52bbd0bc-7c3e-4c85-bfe1-7fd4912945be@googlegroups.com> Message-ID: On Wed, Aug 28, 2013 at 4:14 PM, wrote: > So, I have been working in PHP for several years but I want to learn > something new. That something new is Python. But since I'm a web developer > I want to build stuff for the web. > > I don't want to use Django because it's too bloated, it seem to do > everything for you. I don't like that. I want to do the "plumbing". But at > the same time I have no idea how I would write something that could handle > cookies, sessions, post, get etc etc; so I assume I have to use some kind > of web framework. > > So, can you recommend a minimal web framework that still allows me to > write in Python? Furthermore, do I even need one? I am currently running > NGINX and I have UWSGI installed. I'm just worried about how I would handle > form submissions, get requests and sessions/cookies. > > Any advice would be greatly appreciated. > -- > http://mail.python.org/mailman/listinfo/python-list > I am in the same boat as you, I need to rid myself of php, and ruby doesn't "feel" right.. I like python for system admin stuff.. it seems everything has python bindings :) there are MANY micro frameworks, I have been following these guys for a few years http://www.pocoo.org/ specifically jinja2 and flask looks to be the best choice out of all the options out there -- Sam Fourman Jr. -------------- next part -------------- An HTML attachment was scrubbed... URL: From kwpolska at gmail.com Thu Aug 29 05:05:04 2013 From: kwpolska at gmail.com (=?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?=) Date: Thu, 29 Aug 2013 11:05:04 +0200 Subject: Moving to Python for web In-Reply-To: References: <52bbd0bc-7c3e-4c85-bfe1-7fd4912945be@googlegroups.com> Message-ID: On Thu, Aug 29, 2013 at 4:18 AM, Sam Fourman Jr. wrote: > there are MANY micro frameworks, I have been following these guys for a few > years > http://www.pocoo.org/ +1. The Pocoo team makes many awesome things. > specifically jinja2 and flask looks to be the best choice out of all the > options out there Jinja2 is a standalone templating engine, something like Smarty for PHP. And Flask is a microframework, which adds on top of Jinja2 (not mandatory) and Werkzeug (another Pocoo project) to make a nice and easy webdevelopment base. IMO it is the best choice in terms of microframeworks. But microframeworks are, as the name states, micro. The big frameworks include tons of abstractions and fancy features. Some people may like them, others may not. (Those features can obviously be implemented in the microframeworks on one?s own or through existing open-source code.) -- Chris ?Kwpolska? Warrick PGP: 5EAAEA16 stop html mail | always bottom-post | only UTF-8 makes sense From sepatan at sibmail.com Wed Aug 28 22:36:41 2013 From: sepatan at sibmail.com (sepatan at sibmail.com) Date: Thu, 29 Aug 2013 09:36:41 +0700 (NOVT) Subject: Moving to Python for web In-Reply-To: <52bbd0bc-7c3e-4c85-bfe1-7fd4912945be@googlegroups.com> References: <52bbd0bc-7c3e-4c85-bfe1-7fd4912945be@googlegroups.com> Message-ID: <50022.83.172.33.173.1377743801.squirrel@www.sibmail.com> Here, for example, take a look: http://www.clips.ua.ac.be/pages/pattern > So, I have been working in PHP for several years but I want to learn > something new. That something new is Python. But since I'm a web developer > I want to build stuff for the web. > > I don't want to use Django because it's too bloated, it seem to do > everything for you. I don't like that. I want to do the "plumbing". But at > the same time I have no idea how I would write something that could handle > cookies, sessions, post, get etc etc; so I assume I have to use some kind > of web framework. > > So, can you recommend a minimal web framework that still allows me to > write in Python? Furthermore, do I even need one? I am currently running > NGINX and I have UWSGI installed. I'm just worried about how I would > handle form submissions, get requests and sessions/cookies. > > Any advice would be greatly appreciated. > -- > http://mail.python.org/mailman/listinfo/python-list > From ecazs.net at gmail.com Thu Aug 29 08:45:49 2013 From: ecazs.net at gmail.com (Andreas Ecaz) Date: Thu, 29 Aug 2013 05:45:49 -0700 (PDT) Subject: Moving to Python for web In-Reply-To: <52bbd0bc-7c3e-4c85-bfe1-7fd4912945be@googlegroups.com> References: <52bbd0bc-7c3e-4c85-bfe1-7fd4912945be@googlegroups.com> Message-ID: <05537ea8-4993-4ad2-baa7-4ccdf5268c5d@googlegroups.com> I've decided to go with Flask! It's now running on UWSGI with NGINX. Hopefully I can get some stuff done :) @Chris ?Kwpolska? Warrick I just don't like the big frameworks, for me there is too much magic going on. From kwpolska at gmail.com Thu Aug 29 09:10:03 2013 From: kwpolska at gmail.com (=?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?=) Date: Thu, 29 Aug 2013 15:10:03 +0200 Subject: Moving to Python for web In-Reply-To: <05537ea8-4993-4ad2-baa7-4ccdf5268c5d@googlegroups.com> References: <52bbd0bc-7c3e-4c85-bfe1-7fd4912945be@googlegroups.com> <05537ea8-4993-4ad2-baa7-4ccdf5268c5d@googlegroups.com> Message-ID: On Thu, Aug 29, 2013 at 2:45 PM, Andreas Ecaz wrote: > I've decided to go with Flask! It's now running on UWSGI with NGINX. Hopefully I can get some stuff done :) How are you running uWSGI? On sane (non-Windows) OSes, I recommend using the uWSGI Emperor, which will protect you from your website going down when something crashes. You run the Emperor through your OS?s init system (e.g. upstart in Ubuntu, systemd in many others). -- Chris ?Kwpolska? Warrick PGP: 5EAAEA16 stop html mail | always bottom-post | only UTF-8 makes sense From ecazs.net at gmail.com Thu Aug 29 10:18:28 2013 From: ecazs.net at gmail.com (Andreas Ecaz) Date: Thu, 29 Aug 2013 07:18:28 -0700 (PDT) Subject: Moving to Python for web In-Reply-To: References: <52bbd0bc-7c3e-4c85-bfe1-7fd4912945be@googlegroups.com> <05537ea8-4993-4ad2-baa7-4ccdf5268c5d@googlegroups.com> Message-ID: On Thursday, August 29, 2013 3:10:03 PM UTC+2, Chris ?Kwpolska? Warrick wrote: > On Thu, Aug 29, 2013 at 2:45 PM, Andreas Ecaz wrote: > > > I've decided to go with Flask! It's now running on UWSGI with NGINX. Hopefully I can get some stuff done :) > > > > How are you running uWSGI? On sane (non-Windows) OSes, I recommend > > using the uWSGI Emperor, which will protect you from your website > > going down when something crashes. You run the Emperor through your > > OS?s init system (e.g. upstart in Ubuntu, systemd in many others). > > > > -- > > Chris ?Kwpolska? Warrick > > PGP: 5EAAEA16 > > stop html mail | always bottom-post | only UTF-8 makes sense Well, I know I'm not using Emperor. I'm fairly new to managing servers myself but I'll take a look at how to set that up. From joe.fbs.junior at gmail.com Wed Aug 28 17:09:22 2013 From: joe.fbs.junior at gmail.com (Joe Junior) Date: Wed, 28 Aug 2013 18:09:22 -0300 Subject: Interface and duck typing woes Message-ID: While designing a simple library, I found myself asking a philosophical question: to check or not to check the parameter's interface? I think that, considering it is Python, the usual answer would be "no", but here is the situation that got me thinking: class Flock: def __init__(self): self.ducks= [] def do_stuff(self): for duck in self.ducks: duck.quack() class Duck: def quack(self): #quack-quack pass f = Flock() d = Duck() f.ducks.append(d) f.do_stuff() Ok, no big deal there, the problem is if the user forgets to implement the quack() method. The stack trace would complain that "duck.quack()" is wrong, but that can happen hundreds of lines after the user actually added the object to the Flock, and it can be hard to find out what is happening and which object is wrong. Of course I don't want to check isistance(), I like duck typing, but should I check if hasattr() and callable() before adding to the container? What is the pythonic way to deal with it? Am I worrying too much ;-)? Thanks, Joe From tjreedy at udel.edu Wed Aug 28 17:54:20 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 28 Aug 2013 17:54:20 -0400 Subject: Interface and duck typing woes In-Reply-To: References: Message-ID: On 8/28/2013 5:09 PM, Joe Junior wrote: > While designing a simple library, I found myself asking a > philosophical question: to check or not to check the parameter's > interface? > > I think that, considering it is Python, the usual answer would be > "no", but here is the situation that got me thinking: > > class Flock: > > def __init__(self): > self.ducks= [] > > def do_stuff(self): > for duck in self.ducks: > duck.quack() > > class Duck: > > def quack(self): > #quack-quack > pass > > f = Flock() > d = Duck() > f.ducks.append(d) > f.do_stuff() > > Ok, no big deal there, the problem is if the user forgets to implement > the quack() method. The stack trace would complain that "duck.quack()" > is wrong, but that can happen hundreds of lines after the user > actually added the object to the Flock, and it can be hard to find out > what is happening and which object is wrong. > > Of course I don't want to check isistance(), I like duck typing, but > should I check if hasattr() and callable() before adding to the > container? What is the pythonic way to deal with it? Am I worrying too > much ;-)? You could underscore '_ducks' and add a .add_duck method with the checks you suggest. Or wrap 'duck.quack()' in try-except and log or warn (or even raise) on AttributeError or TypeError (not callable) with an informative message. Grepping for 'ducks.append(' will find all locations where a non-duck might have been added. Depending on who the users will be, I might just not worry about it until an exception is raised. If you try to protect against everything that you might do wrong, you are on the road to madness, as the protection code might also be buggy. (Too much testing has the same problem ;-). -- Terry Jan Reedy From rosuav at gmail.com Wed Aug 28 19:17:21 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 29 Aug 2013 09:17:21 +1000 Subject: Interface and duck typing woes In-Reply-To: References: Message-ID: On Thu, Aug 29, 2013 at 7:54 AM, Terry Reedy wrote: > Depending on who the users will be, I might just not worry about it until an > exception is raised. If you try to protect against everything that you might > do wrong, you are on the road to madness, as the protection code might also > be buggy. (Too much testing has the same problem ;-). I'd go further. Do you believe that you can write code to catch every bug you might make? If so, you are naive and probably haven't spent much time programming yet :) And if not, then you must acknowledge that bugs WILL happen; therefore you will need to cope with them after the event. So rather than trying to prevent them all, just improve your means of coping, and you'll accomplish the same end with much less trouble. At this point I could go off into a lengthy discussion of philosophy and original sin (not "original SYN", which is a different thing altogether), but anyone who's ever written bug-handling code will understand what I mean already :) ChrisA From cs at zip.com.au Wed Aug 28 21:20:20 2013 From: cs at zip.com.au (Cameron Simpson) Date: Thu, 29 Aug 2013 11:20:20 +1000 Subject: Interface and duck typing woes In-Reply-To: References: Message-ID: <20130829012020.GA47225@cskk.homeip.net> On 29Aug2013 09:17, Chris Angelico wrote: | On Thu, Aug 29, 2013 at 7:54 AM, Terry Reedy wrote: | > Depending on who the users will be, I might just not worry about it until an | > exception is raised. If you try to protect against everything that you might | > do wrong, you are on the road to madness, as the protection code might also | > be buggy. (Too much testing has the same problem ;-). | | I'd go further. | | Do you believe that you can write code to catch every bug you might | make? If so, you are naive and probably haven't spent much time | programming yet :) And if not, then you must acknowledge that bugs | WILL happen; therefore you will need to cope with them after the | event. So rather than trying to prevent them all, just improve your | means of coping, and you'll accomplish the same end with much less | trouble. I'm not so extreme. Yes, of course certain things will only show at runtime and you should be prepared to have to deal with that. However, when working in Java its type strictness caught a great many simple brainfart logic errors by checking function signatures; typically calling the wrong function/method or mangling arguments. Getting this stuff up front was handy. Of course there's a price there in terms of flexibility and all that wordy stuff defining the functions in the first place. As an aside, you can also get a lot of this checking in C with aggressive linting and making a bunch of macros like: #define CNULL ((char *)NULL) #define CPNULL ((char **)NULL) and so forth as needed - lint can then catch a lot of otherwise unchecked comparisons. Anyway, I digress. My point is that there are plusses to having signature/type checking at coding time. It is not the Python Way, but I surely cannot be alone in sometimes being frustrated chasing a deeply nested runtime error that static type checking might have found up front. Cheers, -- Cameron Simpson "waste cycles drawing trendy 3D junk" - Mac Eudora v3 config option From rosuav at gmail.com Wed Aug 28 21:39:25 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 29 Aug 2013 11:39:25 +1000 Subject: Interface and duck typing woes In-Reply-To: <20130829012020.GA47225@cskk.homeip.net> References: <20130829012020.GA47225@cskk.homeip.net> Message-ID: On Thu, Aug 29, 2013 at 11:20 AM, Cameron Simpson wrote: > However, when working in Java its type strictness caught a great > many simple brainfart logic errors by checking function signatures; > typically calling the wrong function/method or mangling arguments. > Getting this stuff up front was handy. It certainly is useful, which is why I like a language with declared types. But it's not everything, and it's certainly far from sufficient. So once you've decided that run-time errors are normal, you write code with that in mind (in a web server, for instance, you'd have some code around the outside that catches and logs errors and returns a 500 to the client), and then you don't stress about them. Any you can catch early, do so, but don't put more effort into catching errors early than you save by not having them later. The novice thinks his primary job is to stop the program from crashing. The expert knows that a crash is just another way for things to go wrong, and one of the easiest to deal with. ChrisA From steve+comp.lang.python at pearwood.info Wed Aug 28 22:48:25 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 29 Aug 2013 02:48:25 GMT Subject: Interface and duck typing woes References: <20130829012020.GA47225@cskk.homeip.net> Message-ID: <521eb679$0$6599$c3e8da3$5496439d@news.astraweb.com> On Thu, 29 Aug 2013 11:39:25 +1000, Chris Angelico wrote: > The novice thinks his primary job is to stop the program from crashing. > The expert knows that a crash is just another way for things to go > wrong, and one of the easiest to deal with. "I find it amusing when novice programmers believe their main job is preventing programs from crashing. ... More experienced programmers realize that correct code is great, code that crashes could use improvement, but incorrect code that doesn?t crash is a horrible nightmare." http://cdsmith.wordpress.com/2011/01/09/an-old-article-i-wrote/ Anyone who hasn't already done so, you should read the whole article. It's about static versus dynamic typing, testing, proving correctness, and how they all fit in together. -- Steven From roy at panix.com Wed Aug 28 21:27:34 2013 From: roy at panix.com (Roy Smith) Date: Wed, 28 Aug 2013 21:27:34 -0400 Subject: Interface and duck typing woes References: Message-ID: In article , Cameron Simpson wrote: > Anyway, I digress. My point is that there are plusses to having > signature/type checking at coding time. It is not the Python Way, > but I surely cannot be alone in sometimes being frustrated chasing > a deeply nested runtime error that static type checking might have > found up front. One time when I really do miss static typing (and often emulate by sprinkling isinstance() assertions into my functions) is when I do a big refactoring. I'll change (to pick a real example) Song.get(id) from taking an integer to taking an ObjectId. I'll hunt through my source code to find all the places where I call Song.get(), but usually I'll miss a few. The assertions will help me catch that quickly because I'll get an easy to understand exception thrown right at the first point of contact, rather than something cryptic happening 12 stack frames down, deep in the database layer. From steve+comp.lang.python at pearwood.info Wed Aug 28 19:24:58 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 Aug 2013 23:24:58 GMT Subject: Interface and duck typing woes References: Message-ID: <521e86c9$0$6599$c3e8da3$5496439d@news.astraweb.com> On Wed, 28 Aug 2013 18:09:22 -0300, Joe Junior wrote: > While designing a simple library, I found myself asking a philosophical > question: to check or not to check the parameter's interface? The only correct answer to that is, "Yes no maybe". :-) > I think that, considering it is Python, the usual answer would be "no", > but here is the situation that got me thinking: > > class Flock: > > def __init__(self): > self.ducks= [] > > def do_stuff(self): > for duck in self.ducks: > duck.quack() > > class Duck: > > def quack(self): > #quack-quack > pass > > f = Flock() > d = Duck() > f.ducks.append(d) > f.do_stuff() > > Ok, no big deal there, the problem is if the user forgets to implement > the quack() method. The stack trace would complain that "duck.quack()" > is wrong, but that can happen hundreds of lines after the user actually > added the object to the Flock, and it can be hard to find out what is > happening and which object is wrong. True, but that's a good case for improving the error message, or using a debugger. Here is Flock.do_stuff re-written to give a more verbose/useful error message: def do_stuff(self): for i, duck in enumerate(self.ducks): try: duck.quack() except AttributeError: raise DuckError( 'object %r at index %d has no quack' % (duck, i) ) Okay, seeing the index is useful. But we would have got nearly as much information from the AttributeError traceback, minus the index: py> (42).quack() Traceback (most recent call last): File "", line 1, in AttributeError: 'int' object has no attribute 'quack' So how much extra work are you prepared to put in to make a rare occurrence (passing a magpie instead of a duck) easier to debug? Since errors are presumably rare, maybe the answer is "not a lot of extra work". But if the consequence of an error is catastrophic ("for want of a duck quacking, the nuclear reactor explodes, making the northern hemisphere uninhabitable"), maybe the answer is "as much as it takes". Other questions: what happens if duck.quack is buggy and raises AttributeError? A good question, but just how far should we go in worrying about things like this? What happens if duck.quack is buggy and raises StopIteration? Sometimes the right reaction is to deal with it if and when it actually occurs. In other words, wait for the bug report before trying to fix it. ("Fix it" may mean telling people "don't do that!".) > Of course I don't want to check isistance(), I like duck typing, but > should I check if hasattr() and callable() before adding to the > container? What is the pythonic way to deal with it? Am I worrying too > much ;-)? Yes :-) Except in the (rare?) case that you aren't worrying enough, in which case you can check hasattr and callable up front, or do whatever other tests you feel the need to check. It depends on the specific code you are writing. -- Steven From nobody at nowhere.com Thu Aug 29 05:41:39 2013 From: nobody at nowhere.com (Nobody) Date: Thu, 29 Aug 2013 10:41:39 +0100 Subject: Interface and duck typing woes References: Message-ID: On Wed, 28 Aug 2013 18:09:22 -0300, Joe Junior wrote: > Of course I don't want to check isistance(), I like duck typing, but > should I check if hasattr() and callable() before adding to the container? That won't tell you if the object has a quack() method but with incompatible semantics (e.g. wrong number or types of arguments). > What is the pythonic way to deal with it? Ignore it. If you want early type checking, use a statically-typed language. From joe.fbs.junior at gmail.com Thu Aug 29 08:40:32 2013 From: joe.fbs.junior at gmail.com (Joe Junior) Date: Thu, 29 Aug 2013 09:40:32 -0300 Subject: Interface and duck typing woes In-Reply-To: References: Message-ID: Well, the main reason for me asking this question here was because of the Java/C#/Whatever developer in me craving for an Interface for the container's items, and I noticed that I'm not alone in this. But I was actually expecting the "We're all consenting adults, here", I guess I just needed the confirmation :-) Another reason for this question is that I read some people saying they wouldn't use python for large projects, and they always point at the lack of Interfaces as a concern. I actually disagree, but I can see their point. What do you think? @Nobody >> Of course I don't want to check isistance(), I like duck typing, but >> should I check if hasattr() and callable() before adding to the container? >That won't tell you if the object has a quack() method but with >incompatible semantics (e.g. wrong number or types of arguments). Yeah, didn't think about that, it's kinda swimming upstream! Definitely it is more hassle than it is worth. @ChrisA >Do you believe that you can write code to catch every bug you might >make? If so, you are naive and probably haven't spent much time >programming yet :) And if not, then you must acknowledge that bugs >WILL happen; therefore you will need to cope with them after the >event. So rather than trying to prevent them all, just improve your >means of coping, and you'll accomplish the same end with much less >trouble. Oh, no! I'm not that presumptuous (or naive)! But what do you mean by "improve means of coping"? Do you know any article on the subject you could point me? @Steven >> Of course I don't want to check isistance(), I like duck typing, but >> should I check if hasattr() and callable() before adding to the >> container? What is the pythonic way to deal with it? Am I worrying too >> much ;-)? >Yes :-) Well, thanks! :-) And thanks for the article. From rosuav at gmail.com Thu Aug 29 09:07:24 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 29 Aug 2013 23:07:24 +1000 Subject: Interface and duck typing woes In-Reply-To: References: Message-ID: On Thu, Aug 29, 2013 at 10:40 PM, Joe Junior wrote: > @ChrisA >>Do you believe that you can write code to catch every bug you might >>make? If so, you are naive and probably haven't spent much time >>programming yet :) And if not, then you must acknowledge that bugs >>WILL happen; therefore you will need to cope with them after the >>event. So rather than trying to prevent them all, just improve your >>means of coping, and you'll accomplish the same end with much less >>trouble. > > Oh, no! I'm not that presumptuous (or naive)! But what do you mean by > "improve means of coping"? Do you know any article on the subject you > could point me? Hmm. l don't know of any good articles off-hand. But what I'm talking about is simply developing the skill of reading exceptions, plus a few simple things like knowing where it's appropriate to catch-and-log; sometimes, what that means is actually writing some code to (for example) email you whenever there's an exception, but more likely it means writing no code at all, and just looking at STDERR of your live usage. Works really well for >95% of Python scripts. The most important thing to consider is: What happens if my code doesn't run all the way through? Is it safe for this to run part way, then bomb with an exception? For many scripts, it's pretty easy: fix the problem and rerun the script, and it'll completely rewrite its output file. For others, this is a good reason for putting all your "real data" into a transactional database - you begin a transaction at the top, don't commit till the end, and if an exception kills your script, your transaction will be rolled back. I have a system for patching our database based on a script (written in Pike, not Python, but the same applies); if I have any sort of critical failure in the patch script, it'll bomb out as soon as I test it - but since I use PostgreSQL, all that DDL (eg "ALTER TABLE") is covered by transactional integrity (which it isn't with MySQL - another reason to be wary of MySQL), so my patch will be backed out, and I can fix it and start over. I don't need to have a Look Before You Leap approach to database changes - I can simply do stuff, and if it crashes, all's well. (That same script also has a system for catching errors at a mid-level point that means that the process doesn't terminate when there's an error; it supports full code reload, so once I fix the patch, I send the process a SIGHUP and it fetches from disk again.) *That* is error handling the safe way. ChrisA From joe.fbs.junior at gmail.com Thu Aug 29 10:11:38 2013 From: joe.fbs.junior at gmail.com (Joe Junior) Date: Thu, 29 Aug 2013 11:11:38 -0300 Subject: Interface and duck typing woes In-Reply-To: References: Message-ID: On 29 August 2013 10:07, Chris Angelico wrote: > Hmm. l don't know of any good articles off-hand. But what I'm talking > about is simply developing the skill of reading exceptions, plus a few > simple things like knowing where it's appropriate to catch-and-log; > sometimes, what that means is actually writing some code to (for > example) email you whenever there's an exception, but more likely it > means writing no code at all, and just looking at STDERR of your live > usage. Works really well for >95% of Python scripts. > > The most important thing to consider is: What happens if my code > doesn't run all the way through? Is it safe for this to run part way, > then bomb with an exception? For many scripts, it's pretty easy: fix > the problem and rerun the script, and it'll completely rewrite its > output file. For others, this is a good reason for putting all your > "real data" into a transactional database - you begin a transaction at > the top, don't commit till the end, and if an exception kills your > script, your transaction will be rolled back. I have a system for > patching our database based on a script (written in Pike, not Python, > but the same applies); if I have any sort of critical failure in the > patch script, it'll bomb out as soon as I test it - but since I use > PostgreSQL, all that DDL (eg "ALTER TABLE") is covered by > transactional integrity (which it isn't with MySQL - another reason to > be wary of MySQL), so my patch will be backed out, and I can fix it > and start over. I don't need to have a Look Before You Leap approach > to database changes - I can simply do stuff, and if it crashes, all's > well. (That same script also has a system for catching errors at a > mid-level point that means that the process doesn't terminate when > there's an error; it supports full code reload, so once I fix the > patch, I send the process a SIGHUP and it fetches from disk again.) > *That* is error handling the safe way. > Oh, I get it! Thanks. From wuwei23 at gmail.com Thu Aug 29 20:14:23 2013 From: wuwei23 at gmail.com (alex23) Date: Fri, 30 Aug 2013 10:14:23 +1000 Subject: Interface and duck typing woes In-Reply-To: References: Message-ID: On 29/08/2013 10:40 PM, Joe Junior wrote: > Another reason for this question is that I read some people saying > they wouldn't use python for large projects, and they always point at > the lack of Interfaces as a concern. I actually disagree, but I can > see their point. What do you think? Having worked on large Python projects both with & without interfaces, it's definitely possible with either approach. It certainly isn't lacking in for support for them: http://docs.zope.org/zope.interface/ From steve+comp.lang.python at pearwood.info Thu Aug 29 22:42:33 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 30 Aug 2013 02:42:33 GMT Subject: Interface and duck typing woes References: Message-ID: <52200699$0$6599$c3e8da3$5496439d@news.astraweb.com> On Thu, 29 Aug 2013 09:40:32 -0300, Joe Junior wrote: > Well, the main reason for me asking this question here was because of > the Java/C#/Whatever developer in me craving for an Interface for the > container's items, and I noticed that I'm not alone in this. But I was > actually expecting the "We're all consenting adults, here", I guess I > just needed the confirmation :-) > > Another reason for this question is that I read some people saying they > wouldn't use python for large projects, and they always point at the > lack of Interfaces as a concern. I actually disagree, but I can see > their point. What do you think? Interfaces aren't a built-in part of the language, but big frameworks like Zope and Twisted include them. See for example discussion here: http://dirtsimple.org/2004/12/python-interfaces-are-not-java.html In a more ad-hoc manner, there are recipes for interface-like functionality. For example, from the Python Cookbook, we have this: http://code.activestate.com/recipes/52291 It's a myth that Python is entirely opposed to type-checking. Many built- ins do it. Way back in Python 1.5, Python's creator Guido van Rossum wrote an essay describing a way to implement Eiffel-like pre- and post- condition checking: http://www.python.org/doc/essays/metaclasses/ These days, it would be relatively simple to implement pre- and post- condition checking using decorators, and indeed one of the motivating use- cases for function annotations in Python 3 is to allow such things. http://www.python.org/dev/peps/pep-3107/ (Function annotations are perhaps the best Python feature that nobody uses.) -- Steven From roy at panix.com Fri Aug 30 06:35:47 2013 From: roy at panix.com (Roy Smith) Date: Fri, 30 Aug 2013 06:35:47 -0400 Subject: Interface and duck typing woes References: <52200699$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <52200699$0$6599$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > These days, it would be relatively simple to implement pre- and post- > condition checking using decorators, and indeed one of the motivating use- > cases for function annotations in Python 3 is to allow such things. > > http://www.python.org/dev/peps/pep-3107/ > > (Function annotations are perhaps the best Python feature that nobody > uses.) This is awesome. From steve+comp.lang.python at pearwood.info Fri Aug 30 20:13:32 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 31 Aug 2013 00:13:32 GMT Subject: Interface and duck typing woes References: <52200699$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5221352b$0$6599$c3e8da3$5496439d@news.astraweb.com> On Fri, 30 Aug 2013 06:35:47 -0400, Roy Smith wrote: > In article <52200699$0$6599$c3e8da3$5496439d at news.astraweb.com>, > Steven D'Aprano wrote: > >> These days, it would be relatively simple to implement pre- and post- >> condition checking using decorators, and indeed one of the motivating >> use- cases for function annotations in Python 3 is to allow such >> things. >> >> http://www.python.org/dev/peps/pep-3107/ >> >> (Function annotations are perhaps the best Python feature that nobody >> uses.) > > This is awesome. Heh, everybody has one of two reactions: "This is awesome!" "You'll add type checking to my Python code over my dead body!!!" But I'm still to see a practical use for annotations in real world code. Or indeed to think of a use for them other than type checking. -- Steven From ned at nedbatchelder.com Fri Aug 30 20:45:01 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Fri, 30 Aug 2013 20:45:01 -0400 Subject: Interface and duck typing woes In-Reply-To: <5221352b$0$6599$c3e8da3$5496439d@news.astraweb.com> References: <52200699$0$6599$c3e8da3$5496439d@news.astraweb.com> <5221352b$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <52213C8D.9070906@nedbatchelder.com> On 8/30/13 8:13 PM, Steven D'Aprano wrote: > On Fri, 30 Aug 2013 06:35:47 -0400, Roy Smith wrote: > >> In article <52200699$0$6599$c3e8da3$5496439d at news.astraweb.com>, >> Steven D'Aprano wrote: >> >>> These days, it would be relatively simple to implement pre- and post- >>> condition checking using decorators, and indeed one of the motivating >>> use- cases for function annotations in Python 3 is to allow such >>> things. >>> >>> http://www.python.org/dev/peps/pep-3107/ >>> >>> (Function annotations are perhaps the best Python feature that nobody >>> uses.) >> This is awesome. > > Heh, everybody has one of two reactions: > > "This is awesome!" > > "You'll add type checking to my Python code over my dead body!!!" > > But I'm still to see a practical use for annotations in real world code. > Or indeed to think of a use for them other than type checking. > > At PyCon 2007 (I think), Guido was giving a keynote about the features coming in Py3k, and he couldn't remember the name "function annotations." He said, "what are they called, the things that aren't type declarations." --Ned. From joshua at landau.ws Sat Aug 31 19:18:54 2013 From: joshua at landau.ws (Joshua Landau) Date: Sun, 1 Sep 2013 00:18:54 +0100 Subject: Interface and duck typing woes In-Reply-To: <5221352b$0$6599$c3e8da3$5496439d@news.astraweb.com> References: <52200699$0$6599$c3e8da3$5496439d@news.astraweb.com> <5221352b$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 31 August 2013 01:13, Steven D'Aprano wrote: > On Fri, 30 Aug 2013 06:35:47 -0400, Roy Smith wrote: > >> In article <52200699$0$6599$c3e8da3$5496439d at news.astraweb.com>, >> Steven D'Aprano wrote: >> >>> These days, it would be relatively simple to implement pre- and post- >>> condition checking using decorators, and indeed one of the motivating >>> use- cases for function annotations in Python 3 is to allow such >>> things. >>> >>> http://www.python.org/dev/peps/pep-3107/ >>> >>> (Function annotations are perhaps the best Python feature that nobody >>> uses.) >> >> This is awesome. > > Heh, everybody has one of two reactions: > > "This is awesome!" > > "You'll add type checking to my Python code over my dead body!!!" > > But I'm still to see a practical use for annotations in real world code. > Or indeed to think of a use for them other than type checking. I occasionally use them for documentation. I think that there some are cases where the return type (encoded as a string) is as good an indicator of functionality as a short docstring, so use both. From roy at panix.com Sat Aug 31 20:52:52 2013 From: roy at panix.com (Roy Smith) Date: Sat, 31 Aug 2013 20:52:52 -0400 Subject: Interface and duck typing woes References: <52200699$0$6599$c3e8da3$5496439d@news.astraweb.com> <5221352b$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <5221352b$0$6599$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > Heh, everybody has one of two reactions: > > "This is awesome!" [[i.e. what I said]] > > "You'll add type checking to my Python code over my dead body!!!" Duck typing is a funny thing. Sure, I don't have to give you a Duck, I just have to give you something that quacks like a Duck. But, at some point, you and I need to agree on what that means. If you're expecting a https://en.wikipedia.org/wiki/Duck and I give you a https://en.wikipedia.org/wiki/DUKW, we've had a failure to communicate. To take a quasi-realistic example, let's say I've got this musical masterpiece in my database: { "_id" : ObjectId("4ccb7052e5f37551d479add6"), "album" : "My World", "album_comp_id" : NumberLong(34732133), "artist" : "Justin Bieber", "avail_date_aac" : ISODate("1970-01-01T00:00:00Z"), "avail_date_mp3" : ISODate("1970-01-01T00:00:00Z"), "duration" : 192, "genre" : "Pop", "mn_comp_id" : NumberLong(34732147), "seq_num" : 1297, "song_id" : 6544798, "title" : "Love Me", "track" : -1 } If I want to ask you, "Please return to me a url from which I can stream this song as an mp3", I could look at your Song class and find: @staticmethod def get_stream_url(song): [some code goes here] but that would leave me wondering what you mean by "song". You could legitimately mean 6544798, "4ccb7052e5f37551d479add6", ObjectId("4ccb7052e5f37551d479add6"), an instance of the Song class itself, or possibly even 34732147. Depending on the context, any of those might very well be the right answer. And, we haven't even gotten to describing what I should expect to get back. So, to clear things up, you had to go and write something in the doc string: @staticmethod def get_stream_url(song): """Song is an instance of class Song. This will return an absolute URL as a string.""" But, why not just embed that information in some way which is both compact and machine readable? Of course, when I say, "Song is an instance of class Song", what I really (in full duck typing glory) mean is, "Song is something which has a "mn_comp_id" attribute whose value is something which I can pass to str() and get back a properly formatted decimal integer string. So, this would work, wouldn't it? class Platypus: def __getattr__(self, name): return 34732147 duck = Platypus() Song.get_stream_url(duck) Hey, it met the description you gave me, didn't it? And, sure enough, if you do with duck what I expect you will, we will soon hear Justin Bieber's, "Love Me" coming out the speaker. But, in reality, I suspect we would quickly get into an argument about just what exactly did you mean when you said "Duck". From jussi.santti at ard.fi Fri Aug 30 01:37:39 2013 From: jussi.santti at ard.fi (jussi.santti at ard.fi) Date: Thu, 29 Aug 2013 22:37:39 -0700 (PDT) Subject: Interface and duck typing woes In-Reply-To: References: Message-ID: On Thursday, August 29, 2013 12:09:22 AM UTC+3, Joe Junior wrote: > While designing a simple library, I found myself asking a > > philosophical question: to check or not to check the parameter's > > interface? > Design by contract discipline says: do not. > > > I think that, considering it is Python, the usual answer would be > > "no", but here is the situation that got me thinking: > > > > class Flock: > > > > def __init__(self): > > self.ducks= [] > > > > def do_stuff(self): > > for duck in self.ducks: > > duck.quack() > > > > class Duck: > > > > def quack(self): > > #quack-quack > > pass > > > > f = Flock() > > d = Duck() > > f.ducks.append(d) > > f.do_stuff() > > > > Ok, no big deal there, the problem is if the user forgets to implement > > the quack() method. The stack trace would complain that "duck.quack()" > > is wrong, but that can happen hundreds of lines after the user > > actually added the object to the Flock, and it can be hard to find out > > what is happening and which object is wrong. > > > > Of course I don't want to check isistance(), I like duck typing, but > > should I check if hasattr() and callable() before adding to the > > container? What is the pythonic way to deal with it? Am I worrying too > > much ;-)? > > > > Thanks, > > > > Joe From fp2161 at gmail.com Thu Aug 29 02:31:25 2013 From: fp2161 at gmail.com (Fabrice POMBET) Date: Thu, 29 Aug 2013 08:31:25 +0200 Subject: Interface and duck typing woes In-Reply-To: References: Message-ID: <49A42AD7-2F16-4EB5-9963-1889576698BD@gmail.com> Le 29 ao?t 2013 ? 00:56, python-list-request at python.org a ?crit : """While designing a simple library, I found myself asking a philosophical question: to check or not to check the parameter's interface? I think that, considering it is Python, the usual answer would be "no", but here is the situation that got me thinking: class Flock: def __init__(self): self.ducks= [] def do_stuff(self): for duck in self.ducks: duck.quack() class Duck: def quack(self): #quack-quack pass f = Flock() d = Duck() f.ducks.append(d) f.do_stuff() Ok, no big deal there, the problem is if the user forgets to implement the quack() method. The stack trace would complain that "duck.quack()" is wrong, but that can happen hundreds of lines after the user actually added the object to the Flock, and it can be hard to find out what is happening and which object is wrong. Of course I don't want to check isistance(), I like duck typing, but should I check if hasattr() and callable() before adding to the container? What is the pythonic way to deal with it? Am I worrying too much ;-)? Thanks, Joe""" Hey Joe, I am no depository of the pythonic way to think(tm) but I would create flock and inherit Duck from flock, or possibly set Flock as a method of ducks. that would look like this: class Flock(): def __init__(self, flock): self.flock=flock class Duck(Flock): def __init(self, flock): super().__init__(flock) then you only need to create some functions for any object to display the lists and or dicts that you will create outside these classes, in the main or in another function... you just instantiate them like that: Donald=Duck('Donald') or (rather): flock=[] flock.append(Duck('Donald')) one big advantage with this method is, you can creat plenty of other bird classes and append them to a list. Alternatively, you could have just one class Flock and then set duck as an attribute of flock, and set a list of your flock as a private attribute (the self.duck thing in your code... Well... Could be handled in a better way...) but that is another story, the one before is way simpler for your purpose. From rosuav at gmail.com Thu Aug 29 03:16:57 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 29 Aug 2013 17:16:57 +1000 Subject: Interface and duck typing woes In-Reply-To: <49A42AD7-2F16-4EB5-9963-1889576698BD@gmail.com> References: <49A42AD7-2F16-4EB5-9963-1889576698BD@gmail.com> Message-ID: On Thu, Aug 29, 2013 at 4:31 PM, Fabrice POMBET wrote: > I am no depository of the pythonic way to think(tm) but I would create flock and inherit Duck from flock, or possibly set Flock as a method of ducks. > Why should a Duck _be_ a Flock? They are quite different. No, a flock _has_ a duck (or rather, a number of ducks). ChrisA From steve at pearwood.info Thu Aug 29 03:53:10 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 29 Aug 2013 07:53:10 GMT Subject: Interface and duck typing woes References: Message-ID: <521efde6$0$2743$c3e8da3$76491128@news.astraweb.com> On Thu, 29 Aug 2013 08:31:25 +0200, Fabrice POMBET wrote: > I am no depository of the pythonic way to think(tm) but I would create > flock and inherit Duck from flock, or possibly set Flock as a method of > ducks. Neither of those are good design. Donald is an individual Duck, he is not a flock of ducks. isinstance(donald, Flock) should return False, not True. Likewise Donald has wings, a beak, even a sailor suit (but no pants). He does not have a Flock. hasattr(donald, 'Flock') should return False. > that would look like this: > > class Flock(): > def __init__(self, flock): > self.flock=flock This puts all the responsibility for creating the flock outside the class. As you show below, you have to do this: flock=[] flock.append(Duck('Donald')) before calling Flock(flock). In this case, what is the point of the Flock class? It doesn't do anything. You might as well just work with the list. > class Duck(Flock): > def __init(self, flock): > super().__init__(flock) This design makes no sense. Presumably Duck should have an attribute "name". That's how you seem to be using it below, when you create Duck("Donald"). But then you pass the name of the individual Duck up to the parent Flock. If Donald, Daffy and Daisy join a flock, why should the Flock be named "Donald"? Why should it have a name at all? > then you only need to create some functions for any object to display > the lists and or dicts that you will create outside these classes, in > the main or in another function... > > you just instantiate them like that: > > Donald=Duck('Donald') > or (rather): > flock=[] > flock.append(Duck('Donald')) I think, rather, a better approach is: class Flock: def __init__(self, *birds): self.birds = [] # no up-front type checking self.birds.extend(birds) # --- or if you prefer Look Before You Leap --- for bird in birds: if isinstance(Duck, bird): self.birds.append(bird) def quack_all(self): for bird in self.birds: bird.quack() class Duck: def __init__(self, name): self.name = name def quack(self): print "%s quacks" % self.name -- Steven From mohsen at pahlevanzadeh.org Wed Aug 28 20:18:26 2013 From: mohsen at pahlevanzadeh.org (Mohsen Pahlevanzadeh) Date: Thu, 29 Aug 2013 04:48:26 +0430 Subject: semicolon at end of python's statements Message-ID: <1377735506.18906.15.camel@debian> Dear all, I'm C++ programmer and unfortunately put semicolon at end of my statements in python. Quesion: What's really defferences between putting semicolon and don't put? Yours, Mohsen From rosuav at gmail.com Wed Aug 28 20:31:17 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 29 Aug 2013 10:31:17 +1000 Subject: semicolon at end of python's statements In-Reply-To: <1377735506.18906.15.camel@debian> References: <1377735506.18906.15.camel@debian> Message-ID: On Thu, Aug 29, 2013 at 10:18 AM, Mohsen Pahlevanzadeh wrote: > Dear all, > > I'm C++ programmer and unfortunately put semicolon at end of my > statements in python. > > Quesion: > What's really defferences between putting semicolon and don't put? Very little. Putting the semicolon makes you look like a C programmer who's new to Python; omitting it makes you look like you actually understand Python :) As a C and C++ programmer myself, I know where you're coming from, but putting semicolons at the ends of Python statements is as useless as putting ((((lots of (((irritating (((superfluous (((parentheses))))))))))))) in your C++ code. The parser won't mind, but subsequent programmers will wonder what these unnecessary syntactic elements are for. ChrisA From python.list at tim.thechases.com Wed Aug 28 20:37:16 2013 From: python.list at tim.thechases.com (Tim Chase) Date: Wed, 28 Aug 2013 19:37:16 -0500 Subject: semicolon at end of python's statements In-Reply-To: References: <1377735506.18906.15.camel@debian> Message-ID: <20130828193716.07bad258@bigbox.christie.dr> On 2013-08-29 10:31, Chris Angelico wrote: > but putting semicolons at the ends of Python statements is as > useless as putting ((((lots of (((irritating (((superfluous > (((parentheses))))))))))))) in your C++ code. The parser won't mind, > but subsequent programmers will wonder what these unnecessary > syntactic elements are for. That would would be a lisp programmer writing C++ code? :-) -tkc From rosuav at gmail.com Wed Aug 28 20:39:51 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 29 Aug 2013 10:39:51 +1000 Subject: semicolon at end of python's statements In-Reply-To: <20130828193716.07bad258@bigbox.christie.dr> References: <1377735506.18906.15.camel@debian> <20130828193716.07bad258@bigbox.christie.dr> Message-ID: On Thu, Aug 29, 2013 at 10:37 AM, Tim Chase wrote: > On 2013-08-29 10:31, Chris Angelico wrote: >> but putting semicolons at the ends of Python statements is as >> useless as putting ((((lots of (((irritating (((superfluous >> (((parentheses))))))))))))) in your C++ code. The parser won't mind, >> but subsequent programmers will wonder what these unnecessary >> syntactic elements are for. > > That would would be a lisp programmer writing C++ code? :-) Isn't that what LISP stands for? Lots of Irritating Superfluous Parentheses? :) I shouldn't talk, though. Never used LISP for anything serious - nearest I've done is to use Scheme to tinker with GNU Lilypond. ChrisA From python.list at tim.thechases.com Wed Aug 28 20:35:55 2013 From: python.list at tim.thechases.com (Tim Chase) Date: Wed, 28 Aug 2013 19:35:55 -0500 Subject: semicolon at end of python's statements In-Reply-To: <1377735506.18906.15.camel@debian> References: <1377735506.18906.15.camel@debian> Message-ID: <20130828193555.06e96b63@bigbox.christie.dr> On 2013-08-29 04:48, Mohsen Pahlevanzadeh wrote: > I'm C++ programmer and unfortunately put semicolon at end of my > statements in python. > > Quesion: > What's really defferences between putting semicolon and don't put? From a technical standpoint, nothing (see below). From a "readability on the part of other programmers" standpoint, it's bad practice. So if you're coding for yourself, do whichever makes you happy. If you want to interact with other Python developers and don't want to make them grumpy, remove them. -tkc >>> def with_semis(): ... print 1; ... print 2; ... print 3; ... >>> def without_semis(): ... print 1 ... print 2 ... print 3 ... >>> import dis >>> dis.dis(with_semis) 2 0 LOAD_CONST 1 (1) 3 PRINT_ITEM 4 PRINT_NEWLINE 3 5 LOAD_CONST 2 (2) 8 PRINT_ITEM 9 PRINT_NEWLINE 4 10 LOAD_CONST 3 (3) 13 PRINT_ITEM 14 PRINT_NEWLINE 15 LOAD_CONST 0 (None) 18 RETURN_VALUE >>> dis.dis(without_semis) 2 0 LOAD_CONST 1 (1) 3 PRINT_ITEM 4 PRINT_NEWLINE 3 5 LOAD_CONST 2 (2) 8 PRINT_ITEM 9 PRINT_NEWLINE 4 10 LOAD_CONST 3 (3) 13 PRINT_ITEM 14 PRINT_NEWLINE 15 LOAD_CONST 0 (None) 18 RETURN_VALUE From roy at panix.com Wed Aug 28 20:33:11 2013 From: roy at panix.com (Roy Smith) Date: Wed, 28 Aug 2013 20:33:11 -0400 Subject: semicolon at end of python's statements References: Message-ID: In article , Mohsen Pahlevanzadeh wrote: > Dear all, > > I'm C++ programmer and unfortunately put semicolon at end of my > statements in python. > > Quesion: > What's really defferences between putting semicolon and don't put? In theory, nothing. In practice, all the real Python programmers will make fun of you. Somewhat more seriously, every language has its own way of doing things. There's the set of things the language allows you to do, and the somewhat smaller set of things that have become accepted as the "proper" way to do things in that language. For Python, putting semicolons at the ends of statements falls into the first set but not the second. From rosuav at gmail.com Wed Aug 28 20:47:46 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 29 Aug 2013 10:47:46 +1000 Subject: semicolon at end of python's statements In-Reply-To: References: Message-ID: On Thu, Aug 29, 2013 at 10:33 AM, Roy Smith wrote: > In article , > Mohsen Pahlevanzadeh wrote: > >> Dear all, >> >> I'm C++ programmer and unfortunately put semicolon at end of my >> statements in python. >> >> Quesion: >> What's really defferences between putting semicolon and don't put? > > In theory, nothing. In practice, all the real Python programmers will > make fun of you. > > Somewhat more seriously, every language has its own way of doing things. > There's the set of things the language allows you to do, and the > somewhat smaller set of things that have become accepted as the "proper" > way to do things in that language. For Python, putting semicolons at > the ends of statements falls into the first set but not the second. This is about Perl, but may be of interest. http://www.perl.com/pub/2007/12/06/soto-11.html One of his main points is that languages differ primarily in what they force you to say - not what you're able to express. C forces you to declare the ends of statements with semicolons. Python forces you to be consistent with indentation. Smalltalk (if I have this correct) forces you to put parentheses in expressions like a+b*c to declare order of operations. German forces you to match das/die/der to the noun it's referring to. Doing what a completely different programming language forces you to do is like speaking with the wrong grammar ("Murdered in the bed we will be! One day we will in bed find ourselves stone dead!"[1]); native speakers will understand you, but it doesn't sound fluent. ChrisA [1] Mit der knife in der chess! Sun on the Stubble. Great book. From roy at panix.com Wed Aug 28 21:04:37 2013 From: roy at panix.com (Roy Smith) Date: Wed, 28 Aug 2013 21:04:37 -0400 Subject: semicolon at end of python's statements References: Message-ID: In article , Chris Angelico wrote: > This is about Perl, but may be of interest. > > http://www.perl.com/pub/2007/12/06/soto-11.html I got about halfway through, then raised an uncaught TLDNR Exception. But I did like what he had to say about Tcl. Tcl is under-appreciated. A few gigs back, I did a lot of work in Tcl. We were writing a network management tool (long since subsumed into IBM Tivoli via multiple corporate mergers and put out to pasture: http://tinyurl.com/qxd4kw9). The dev team consisted mostly of people who were networking subject matter experts and not real programmers. Tcl turned out to be an excellent tool to let the SME's express their networking knowledge in executable form without having to learn C++, Java, or even Python. Tcl is also ridiculously easy to embed. It's literally one line of C code and you've got an embedded Tcl interpreter running. From steve at pearwood.info Thu Aug 29 00:27:30 2013 From: steve at pearwood.info (Steven D'Aprano) Date: 29 Aug 2013 04:27:30 GMT Subject: semicolon at end of python's statements References: Message-ID: <521ecdb1$0$11100$c3e8da3@news.astraweb.com> On Thu, 29 Aug 2013 04:48:26 +0430, Mohsen Pahlevanzadeh wrote: > Dear all, > > I'm C++ programmer and unfortunately put semicolon at end of my > statements in python. > > Quesion: > What's really defferences between putting semicolon and don't put? It's about the same as writing this: x = 1 pass y = 2 pass z = x**2 + y**2 pass print z pass except you save three keystrokes per LOC. -- Steven From ned at nedbatchelder.com Wed Aug 28 20:38:38 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Wed, 28 Aug 2013 20:38:38 -0400 Subject: semicolon at end of python's statements In-Reply-To: <1377735506.18906.15.camel@debian> References: <1377735506.18906.15.camel@debian> Message-ID: <521E980E.3030403@nedbatchelder.com> On 8/28/13 8:18 PM, Mohsen Pahlevanzadeh wrote: > Dear all, > > I'm C++ programmer and unfortunately put semicolon at end of my > statements in python. > > Quesion: > What's really defferences between putting semicolon and don't put? There is no difference. The semicolon is unnecessary in Python. If you include them, people will know that you are still thinking in another language. Full disclosure: you can separate statements on a line with semicolons, but this practice is strongly discouraged. --Ned. > Yours, > Mohsen > From sfourman at gmail.com Wed Aug 28 22:10:16 2013 From: sfourman at gmail.com (Sam Fourman Jr.) Date: Wed, 28 Aug 2013 22:10:16 -0400 Subject: semicolon at end of python's statements In-Reply-To: <1377735506.18906.15.camel@debian> References: <1377735506.18906.15.camel@debian> Message-ID: On Wed, Aug 28, 2013 at 8:18 PM, Mohsen Pahlevanzadeh < mohsen at pahlevanzadeh.org> wrote: > Dear all, > > I'm C++ programmer and unfortunately put semicolon at end of my > statements in python. > > Quesion: > What's really defferences between putting semicolon and don't put? > > Yours, > Mohsen I totally understand where you are coming from, but I have found that the thing I can't get used to is the "indent thing" Python is a great language, but I always secretly find myself wishing I could somehow use python, and not deal with the mandatory "indents" Sam Fourman Jr. -------------- next part -------------- An HTML attachment was scrubbed... URL: From gherron at digipen.edu Thu Aug 29 03:50:40 2013 From: gherron at digipen.edu (Gary Herron) Date: Thu, 29 Aug 2013 00:50:40 -0700 Subject: semicolon at end of python's statements In-Reply-To: References: <1377735506.18906.15.camel@debian> Message-ID: <521EFD50.9060900@digipen.edu> On 08/28/2013 07:10 PM, Sam Fourman Jr. wrote: > > On Wed, Aug 28, 2013 at 8:18 PM, Mohsen Pahlevanzadeh > > wrote: > > Dear all, > > I'm C++ programmer and unfortunately put semicolon at end of my > statements in python. > > Quesion: > What's really defferences between putting semicolon and don't put? > > Yours, > Mohsen > > > I totally understand where you are coming from, but > I have found that the thing I can't get used to is the "indent thing" > > Python is a great language, but I always secretly find myself > wishing I could somehow use python, and not deal with the mandatory > "indents" > > > Sam Fourman Jr. > What an odd thing to say. In what sense do you consider indents to be non-mandatory in other languages? In *any* programming language, if you write code without indents, it is absolutely unreadable, absolutely unmaintainable, and you will be absolutely fired! Other languages force you (and your editor) to maintain two indications of nesting, (1) indentation, so you can read it, and (2) braces/begin/end/whatever, so the compiler can read it. Neither one is truly optional, and woe to you if the two don't match. On the other hand, Python's use of indentation as the (only) indication of nesting is readable by both humans and parsers. Looked at that way, other languages seem foolish and burdensome and Python's indentation is the only sensible way. Whether you follow that argument or not, I hope you come to like Python's indentation eventually. Many (most?) of the rest of here have. Gary Herron -- Dr. Gary Herron Department of Computer Science DigiPen Institute of Technology (425) 895-4418 -------------- next part -------------- An HTML attachment was scrubbed... URL: From antoon.pardon at rece.vub.ac.be Thu Aug 29 06:11:41 2013 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Thu, 29 Aug 2013 12:11:41 +0200 Subject: semicolon at end of python's statements In-Reply-To: <521EFD50.9060900@digipen.edu> References: <1377735506.18906.15.camel@debian> <521EFD50.9060900@digipen.edu> Message-ID: <521F1E5D.5000307@rece.vub.ac.be> Op 29-08-13 09:50, Gary Herron schreef: > On 08/28/2013 07:10 PM, Sam Fourman Jr. wrote: >> >> On Wed, Aug 28, 2013 at 8:18 PM, Mohsen Pahlevanzadeh >> > wrote: >> >> Dear all, >> >> I'm C++ programmer and unfortunately put semicolon at end of my >> statements in python. >> >> Quesion: >> What's really defferences between putting semicolon and don't put? >> >> Yours, >> Mohsen >> >> >> I totally understand where you are coming from, but >> I have found that the thing I can't get used to is the "indent thing" >> >> Python is a great language, but I always secretly find myself >> wishing I could somehow use python, and not deal with the mandatory >> "indents" >> >> >> Sam Fourman Jr. >> > > What an odd thing to say. In what sense do you consider indents to be > non-mandatory in other languages? In *any* programming language, if you > write code without indents, it is absolutely unreadable, absolutely > unmaintainable, and you will be absolutely fired! Sure but the most readable indentation is not always the indentation that follows the formal structure. Also IIRC it stopped the introduction of a number of language features because the feature didn't seem to fit pythons obligated indentation. Without the forced indentation, one could introduce the feature and have people experiment with what kind of indentation works best. Now that wasn't an option. > Other languages force you (and your editor) to maintain two indications > of nesting, (1) indentation, so you can read it, and (2) > braces/begin/end/whatever, so the compiler can read it. Neither one is > truly optional, and woe to you if the two don't match. On the other > hand, Python's use of indentation as the (only) indication of nesting is > readable by both humans and parsers. Sure but it means you lose redundancy and so sometimes that means it is harder to find the problem. I remember the time that I somehow ended up copying part of a function lower in the file. Now because there was no need of a right brace or end or whatever this file executed without a problem but redefined the function into an incomple one. Should some kind of end marker be needed, the problem would have been found much easier, because if would have produced a syntax error. > Looked at that way, other languages seem foolish and burdensome and > Python's indentation is the only sensible way. I don't agree. -- Antoon Pardon From alister.ware at ntlworld.com Thu Aug 29 05:39:09 2013 From: alister.ware at ntlworld.com (Alister) Date: Thu, 29 Aug 2013 09:39:09 GMT Subject: semicolon at end of python's statements References: <1377735506.18906.15.camel@debian> Message-ID: <1FETt.52607$Mw4.14965@fx15.am4> On Wed, 28 Aug 2013 22:10:16 -0400, Sam Fourman Jr. wrote: > On Wed, Aug 28, 2013 at 8:18 PM, Mohsen Pahlevanzadeh < > mohsen at pahlevanzadeh.org> wrote: > >> Dear all, >> >> I'm C++ programmer and unfortunately put semicolon at end of my >> statements in python. >> >> Quesion: >> What's really defferences between putting semicolon and don't put? >> >> Yours, >> Mohsen > > > I totally understand where you are coming from, but I have found that > the thing I can't get used to is the "indent thing" > > Python is a great language, but I always secretly find myself wishing I > could somehow use python, and not deal with the mandatory "indents" > > Yet no doubt you voluntarily indent your c code to make it readable? it wont take long before you find you don't even think about indentation and actually like it -- All the really good ideas I ever had came to me while I was milking a cow. -- Grant Wood From rosuav at gmail.com Thu Aug 29 05:52:19 2013 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 29 Aug 2013 19:52:19 +1000 Subject: semicolon at end of python's statements In-Reply-To: <1FETt.52607$Mw4.14965@fx15.am4> References: <1377735506.18906.15.camel@debian> <1FETt.52607$Mw4.14965@fx15.am4> Message-ID: On Thu, Aug 29, 2013 at 7:39 PM, Alister wrote: > Yet no doubt you voluntarily indent your c code to make it readable? > it wont take long before you find you don't even think about indentation > and actually like it Most of the time, in any language, I will indeed have indentation matching the syntactic structure of the code (which is what Python demands). But I like the flexibility of having the indentation under _my_ control, not under the language's, because my rule is that it should match the *logical*, not syntactic, structure. For instance, here's how code looks that iterates over an array: foreach (some_array, mixed val) { //do something with val } And here's how it looks if I want to iterate over a filtered version of the array: foreach (some_array, mixed val) if (some_condition) { //do something with val } There's no language support required. It reads like a Python list-comp, simply adding a condition as part of the loop header. But it technically breaks the indentation rules, and Python doesn't allow this sort of thing. In Python, this particular example is probably best done with a generator expression in the for loop, but there are other cases where I combine two physical structural levels into a single line and indent level. ChrisA From fabiosantosart at gmail.com Thu Aug 29 06:02:04 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Thu, 29 Aug 2013 11:02:04 +0100 Subject: semicolon at end of python's statements In-Reply-To: References: <1377735506.18906.15.camel@debian> <1FETt.52607$Mw4.14965@fx15.am4> Message-ID: On 29 Aug 2013 10:54, "Chris Angelico" wrote: > > foreach (some_array, mixed val) if (some_condition) > { > //do something with val > } > I hope that you don't mind that I have started to use this in JavaScript back at work. Its foreach loop requires you to do a filter all the time. It is a shame that this is not possible in python. for..if exists in comprehensions and not in regular loops but that would be nice sometimes. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ben+python at benfinney.id.au Thu Aug 29 18:17:51 2013 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 30 Aug 2013 08:17:51 +1000 Subject: semicolon at end of python's statements References: <1377735506.18906.15.camel@debian> <1FETt.52607$Mw4.14965@fx15.am4> Message-ID: <7wob8gywds.fsf@benfinney.id.au> F?bio Santos writes: > It is a shame that this is not possible in python. for..if exists in > comprehensions and not in regular loops but that would be nice > sometimes. So you use it in a generator expression, and iterate over the generator: for foo in (spam for spam in sequence if predicate(spam)): process(spam) That way, there's no need for new syntax. -- \ ?bash awk grep perl sed, df du, du-du du-du, vi troff su fsck | `\ rm * halt LART LART LART!? ?The Swedish BOFH, | _o__) alt.sysadmin.recovery | Ben Finney From rosuav at gmail.com Thu Aug 29 18:50:12 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 30 Aug 2013 08:50:12 +1000 Subject: semicolon at end of python's statements In-Reply-To: <7wob8gywds.fsf@benfinney.id.au> References: <1377735506.18906.15.camel@debian> <1FETt.52607$Mw4.14965@fx15.am4> <7wob8gywds.fsf@benfinney.id.au> Message-ID: On Fri, Aug 30, 2013 at 8:17 AM, Ben Finney wrote: > F?bio Santos writes: > >> It is a shame that this is not possible in python. for..if exists in >> comprehensions and not in regular loops but that would be nice >> sometimes. > > So you use it in a generator expression, and iterate over the generator: > > for foo in (spam for spam in sequence if predicate(spam)): > process(spam) > > That way, there's no need for new syntax. That works for the specific example of a for loop (and proves that it's logical and sensible to iterate over part of a loop). But how many times has something similar come up - like the while-loop-that-retains-its-value suggestion a while ago? Sometimes, the only "new syntax" required is a weakening of the rule that one thing goes on one line, and then the syntax is simply two block constructs forming a single loop header. Also, the genexp version you have above seems to repeat itself a lot. I'd write that as simply: for spam in sequence: if predicate(spam): process(spam) though of course, if the processing is a single function call, there are other ways to lay it out. But assume that the processing is a full suite. ChrisA From ben+python at benfinney.id.au Fri Aug 30 00:55:37 2013 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 30 Aug 2013 14:55:37 +1000 Subject: semicolon at end of python's statements References: <1377735506.18906.15.camel@debian> <1FETt.52607$Mw4.14965@fx15.am4> <7wob8gywds.fsf@benfinney.id.au> Message-ID: <7wfvtrkcae.fsf@benfinney.id.au> Ben Finney writes: > F?bio Santos writes: > > > It is a shame that this is not possible in python. for..if exists in > > comprehensions and not in regular loops but that would be nice > > sometimes. > for foo in (spam for spam in sequence if predicate(spam)): ? Better: for foo in filter(predicate, sequence): process(foo) -- \ ?Corporation, n. An ingenious device for obtaining individual | `\ profit without individual responsibility.? ?Ambrose Bierce, | _o__) _The Devil's Dictionary_, 1906 | Ben Finney From antoon.pardon at rece.vub.ac.be Fri Aug 30 03:15:26 2013 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Fri, 30 Aug 2013 09:15:26 +0200 Subject: semicolon at end of python's statements In-Reply-To: <7wfvtrkcae.fsf@benfinney.id.au> References: <1377735506.18906.15.camel@debian> <1FETt.52607$Mw4.14965@fx15.am4> <7wob8gywds.fsf@benfinney.id.au> <7wfvtrkcae.fsf@benfinney.id.au> Message-ID: <5220468E.4070407@rece.vub.ac.be> Op 30-08-13 06:55, Ben Finney schreef: > Ben Finney writes: > >> F?bio Santos writes: >> >>> It is a shame that this is not possible in python. for..if exists in >>> comprehensions and not in regular loops but that would be nice >>> sometimes. >> for foo in (spam for spam in sequence if predicate(spam)): ? > > Better: > > for foo in filter(predicate, sequence): > process(foo) Well better in what way? You now have to translate a predicate expression into a predicate function. Which AFAIU was one of the reasons to move away from map/filter to list comprehension. As I understand it, python made a move away from map and filter towards list comprehension. Chris seems to want some of the possibilities that came with that incorporated into the for statement. And your suggestion is to go back to the old kind of filter way. -- Antoon Pardon From rosuav at gmail.com Fri Aug 30 03:25:37 2013 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 30 Aug 2013 17:25:37 +1000 Subject: semicolon at end of python's statements In-Reply-To: <5220468E.4070407@rece.vub.ac.be> References: <1377735506.18906.15.camel@debian> <1FETt.52607$Mw4.14965@fx15.am4> <7wob8gywds.fsf@benfinney.id.au> <7wfvtrkcae.fsf@benfinney.id.au> <5220468E.4070407@rece.vub.ac.be> Message-ID: On Fri, Aug 30, 2013 at 5:15 PM, Antoon Pardon wrote: > Op 30-08-13 06:55, Ben Finney schreef: >> Ben Finney writes: >> >>> F?bio Santos writes: >>> >>>> It is a shame that this is not possible in python. for..if exists in >>>> comprehensions and not in regular loops but that would be nice >>>> sometimes. >>> for foo in (spam for spam in sequence if predicate(spam)): ? >> >> Better: >> >> for foo in filter(predicate, sequence): >> process(foo) > > Well better in what way? You now have to translate a predicate > expression into a predicate function. Which AFAIU was one of > the reasons to move away from map/filter to list comprehension. > > As I understand it, python made a move away from map and filter > towards list comprehension. Chris seems to want some of the > possibilities that came with that incorporated into the for > statement. And your suggestion is to go back to the old kind > of filter way. No, actually Ben's quite right - assuming the predicate is a simple function, of course (Python's lambda notation is a bit clunky for comparisons); as of Python 3, filter() is lazy and is pretty much what I'm doing here. However, that's still a specific answer to a specific (albeit common) instance of wanting to merge control structures. ChrisA From antoon.pardon at rece.vub.ac.be Fri Aug 30 03:48:10 2013 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Fri, 30 Aug 2013 09:48:10 +0200 Subject: semicolon at end of python's statements In-Reply-To: References: <1377735506.18906.15.camel@debian> <1FETt.52607$Mw4.14965@fx15.am4> <7wob8gywds.fsf@benfinney.id.au> <7wfvtrkcae.fsf@benfinney.id.au> <5220468E.4070407@rece.vub.ac.be> Message-ID: <52204E3A.7080807@rece.vub.ac.be> Op 30-08-13 09:25, Chris Angelico schreef: > On Fri, Aug 30, 2013 at 5:15 PM, Antoon Pardon > wrote: >> Op 30-08-13 06:55, Ben Finney schreef: >>> Ben Finney writes: >>> >>>> F?bio Santos writes: >>>> >>>>> It is a shame that this is not possible in python. for..if exists in >>>>> comprehensions and not in regular loops but that would be nice >>>>> sometimes. >>>> for foo in (spam for spam in sequence if predicate(spam)): ? >>> >>> Better: >>> >>> for foo in filter(predicate, sequence): >>> process(foo) >> >> Well better in what way? You now have to translate a predicate >> expression into a predicate function. Which AFAIU was one of >> the reasons to move away from map/filter to list comprehension. >> >> As I understand it, python made a move away from map and filter >> towards list comprehension. Chris seems to want some of the >> possibilities that came with that incorporated into the for >> statement. And your suggestion is to go back to the old kind >> of filter way. > > No, actually Ben's quite right - assuming the predicate is a simple > function, But why should we assume that? Suppose I would like to process all odd items in a list. A comprehension kind of notation would be | for item in lst if item % 2: | process items Which we would have to turn into | for item in filter(lambda nr: nr % 2, lst): | process items But AFAIR, one of the driving forces behind the introduction to list comprehension, and thus a move away from map and filter was to get rid of the lambda's in this kind of situations. > of course (Python's lambda notation is a bit clunky for > comparisons); as of Python 3, filter() is lazy and is pretty much what > I'm doing here. Lazy or not, is AFAICS, not a point here. -- Antoon Pardon From fabiosantosart at gmail.com Fri Aug 30 06:32:17 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Fri, 30 Aug 2013 11:32:17 +0100 Subject: semicolon at end of python's statements In-Reply-To: <7wob8gywds.fsf@benfinney.id.au> References: <1377735506.18906.15.camel@debian> <1FETt.52607$Mw4.14965@fx15.am4> <7wob8gywds.fsf@benfinney.id.au> Message-ID: On 29 Aug 2013 23:20, "Ben Finney" wrote: > > F?bio Santos writes: > > > It is a shame that this is not possible in python. for..if exists in > > comprehensions and not in regular loops but that would be nice > > sometimes. > > So you use it in a generator expression, and iterate over the generator: > > for foo in (spam for spam in sequence if predicate(spam)): > process(spam) > > That way, there's no need for new syntax. The problem I have with that strategy is that it is repetitive and hinders readability. You wrote "for" and "in" twice, and spam (a pretty useless intermediate variable) thrice! While it does its job, it hides the true intent for filtering beneath a lot of (pun intended) spam. The "if" particle is nigh undetectable there. To get around this, I often declare a generator. But I still find it a bit awkward to have to look up the definition elsewhere, and to waste lines over something so simple. I can't say I understand why we don't merge the for loops' syntax with the comprehension syntax. Even after following the for..while discussion. -------------- next part -------------- An HTML attachment was scrubbed... URL: From roy at panix.com Fri Aug 30 06:53:27 2013 From: roy at panix.com (Roy Smith) Date: Fri, 30 Aug 2013 06:53:27 -0400 Subject: semicolon at end of python's statements References: <1377735506.18906.15.camel@debian> <1FETt.52607$Mw4.14965@fx15.am4> <7wob8gywds.fsf@benfinney.id.au> Message-ID: In article , F?bio Santos wrote: > On 29 Aug 2013 23:20, "Ben Finney" wrote: > > > > F?bio Santos writes: > > > > > It is a shame that this is not possible in python. for..if exists in > > > comprehensions and not in regular loops but that would be nice > > > sometimes. > > > > So you use it in a generator expression, and iterate over the generator: > > > > for foo in (spam for spam in sequence if predicate(spam)): > > process(spam) > > > > That way, there's no need for new syntax. > > The problem I have with that strategy is that it is repetitive and hinders > readability. You wrote "for" and "in" twice, and spam (a pretty useless > intermediate variable) thrice! While it does its job, it hides the true > intent for filtering beneath a lot of (pun intended) spam. The "if" > particle is nigh undetectable there. > > To get around this, I often declare a generator. But I still find it a bit > awkward to have to look up the definition elsewhere, and to waste lines > over something so simple. > > I can't say I understand why we don't merge the for loops' syntax with the > comprehension syntax. Even after following the for..while discussion. +1 on loop comprehensions, for all the reasons F?bios states. From antoon.pardon at rece.vub.ac.be Fri Aug 30 10:14:39 2013 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Fri, 30 Aug 2013 16:14:39 +0200 Subject: semicolon at end of python's statements In-Reply-To: References: <1377735506.18906.15.camel@debian> <1FETt.52607$Mw4.14965@fx15.am4> <7wob8gywds.fsf@benfinney.id.au> Message-ID: <5220A8CF.4000302@rece.vub.ac.be> Op 30-08-13 12:53, Roy Smith schreef: > In article , > F?bio Santos wrote: > >> On 29 Aug 2013 23:20, "Ben Finney" wrote: >>> >>> F?bio Santos writes: >>> >>>> It is a shame that this is not possible in python. for..if exists in >>>> comprehensions and not in regular loops but that would be nice >>>> sometimes. >>> >>> So you use it in a generator expression, and iterate over the generator: >>> >>> for foo in (spam for spam in sequence if predicate(spam)): >>> process(spam) >>> >>> That way, there's no need for new syntax. >> >> The problem I have with that strategy is that it is repetitive and hinders >> readability. You wrote "for" and "in" twice, and spam (a pretty useless >> intermediate variable) thrice! While it does its job, it hides the true >> intent for filtering beneath a lot of (pun intended) spam. The "if" >> particle is nigh undetectable there. >> >> To get around this, I often declare a generator. But I still find it a bit >> awkward to have to look up the definition elsewhere, and to waste lines >> over something so simple. >> >> I can't say I understand why we don't merge the for loops' syntax with the >> comprehension syntax. Even after following the for..while discussion. > > +1 on loop comprehensions, for all the reasons F?bios states. Maybe python should just allow more than one control structure on one line and then considers the end of the suite the end of both controls. In that case we could just write the following: for a in lst: if a % 2: treat a process a some more after loop things. It technically is not loop comprehension but just nested controls but by allowing them on the same line it looks close enough like loop comprehension. Plus if people have a problem that would best be solved by a while loop comprehension that would be just as easy. -- Antoon Pardon From rosuav at gmail.com Fri Aug 30 18:18:51 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 31 Aug 2013 08:18:51 +1000 Subject: semicolon at end of python's statements In-Reply-To: <5220A8CF.4000302@rece.vub.ac.be> References: <1377735506.18906.15.camel@debian> <1FETt.52607$Mw4.14965@fx15.am4> <7wob8gywds.fsf@benfinney.id.au> <5220A8CF.4000302@rece.vub.ac.be> Message-ID: On Sat, Aug 31, 2013 at 12:14 AM, Antoon Pardon wrote: > Maybe python should just allow more than one control structure on one > line and then considers the end of the suite the end of both controls. > In that case we could just write the following: > > for a in lst: if a % 2: > treat a > process a some more > after loop things. Exactly what I suggested a while ago :) This is how we got onto this subject - a discussion of how physical structure and logical structure can differ. ChrisA From steve+comp.lang.python at pearwood.info Fri Aug 30 20:09:26 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 31 Aug 2013 00:09:26 GMT Subject: semicolon at end of python's statements References: <1377735506.18906.15.camel@debian> <1FETt.52607$Mw4.14965@fx15.am4> <7wob8gywds.fsf@benfinney.id.au> Message-ID: <52213435$0$6599$c3e8da3$5496439d@news.astraweb.com> On Fri, 30 Aug 2013 11:32:17 +0100, F?bio Santos wrote: > On 29 Aug 2013 23:20, "Ben Finney" wrote: >> >> F?bio Santos writes: >> >> > It is a shame that this is not possible in python. for..if exists in >> > comprehensions and not in regular loops but that would be nice >> > sometimes. >> >> So you use it in a generator expression, and iterate over the >> generator: >> >> for foo in (spam for spam in sequence if predicate(spam)): >> process(spam) >> >> That way, there's no need for new syntax. > > The problem I have with that strategy is that it is repetitive and > hinders readability. You wrote "for" and "in" twice, and spam (a pretty > useless intermediate variable) thrice! There is no need for spam to be "intermediate", and the fact that it shouldn't be is demonstrated by Ben's error in referring to "process (spam)" instead of "process(foo)". We really are spoiled for choice here. We can write any of these: # Option 1 for spam in sequence: if predicate(spam): process(spam) # Option 2 for spam in filter(predicate, sequence): process(spam) # Option 3 for spam in (spam for spam in sequence if predicate(spam)): process(spam) Adding a fourth option: for spam in sequence if predicate(spam): process(spam) saves absolutely nothing except a line and an indent level, neither of which are in short supply, and gains nothing in readability over Option 1. > While it does its job, it hides > the true intent for filtering beneath a lot of (pun intended) spam. The > "if" particle is nigh undetectable there. > > To get around this, I often declare a generator. But I still find it a > bit awkward to have to look up the definition elsewhere, and to waste > lines over something so simple. No need to look up a definition elsewhere, you can put the generator right next to the loop: gen = (spam for spam in sequence if predicate(spam)) for spam in gen: process(spam) But of all the options shown, including the hypothetical for...if, I still prefer Option 1, or Option 2 as my second option. -- Steven From tjreedy at udel.edu Sat Aug 31 01:03:35 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 31 Aug 2013 01:03:35 -0400 Subject: semicolon at end of python's statements In-Reply-To: <52213435$0$6599$c3e8da3$5496439d@news.astraweb.com> References: <1377735506.18906.15.camel@debian> <1FETt.52607$Mw4.14965@fx15.am4> <7wob8gywds.fsf@benfinney.id.au> <52213435$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 8/30/2013 8:09 PM, Steven D'Aprano wrote: > We really are spoiled for choice here. We can write any of these: > > # Option 1 > for spam in sequence: > if predicate(spam): > process(spam) > > # Option 2 > for spam in filter(predicate, sequence): > process(spam) > > # Option 3 > for spam in (spam for spam in sequence if predicate(spam)): > process(spam) > > > Adding a fourth option: > > for spam in sequence if predicate(spam): > process(spam) > > saves absolutely nothing except a line and an indent level, neither of > which are in short supply, and gains nothing in readability over Option 1. Which is why it has been rejected. > But of all the options shown, including the hypothetical for...if, I > still prefer Option 1, or Option 2 as my second option. Ditto. I think people would be better off spending more time learning more about how to use this incredibly powerful tool we have and less arguing for rejected redundant alternatives to what we do have. Just a few days ago, after 16 years of Python, I learned something really neat about function attributes of instances that made a certain testing problem disappear. -- Terry Jan Reedy From jpiitula at ling.helsinki.fi Sat Aug 31 03:47:05 2013 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 31 Aug 2013 10:47:05 +0300 Subject: semicolon at end of python's statements References: <1377735506.18906.15.camel@debian> <1FETt.52607$Mw4.14965@fx15.am4> <7wob8gywds.fsf@benfinney.id.au> <52213435$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven D'Aprano writes: > We really are spoiled for choice here. We can write any of these: > > # Option 1 > for spam in sequence: > if predicate(spam): > process(spam) # Option 1.5 for spam in sequence: if not predicate(spam): continue process(spam) This saves an indent level. > # Option 2 > for spam in filter(predicate, sequence): > process(spam) > > # Option 3 > for spam in (spam for spam in sequence if predicate(spam)): > process(spam) > > > Adding a fourth option: > > for spam in sequence if predicate(spam): > process(spam) > > saves absolutely nothing except a line and an indent level, neither > of which are in short supply, and gains nothing in readability over > Option 1. From paul.nospam at rudin.co.uk Sat Aug 31 04:00:50 2013 From: paul.nospam at rudin.co.uk (Paul Rudin) Date: Sat, 31 Aug 2013 09:00:50 +0100 Subject: semicolon at end of python's statements References: <1377735506.18906.15.camel@debian> <1FETt.52607$Mw4.14965@fx15.am4> <7wob8gywds.fsf@benfinney.id.au> <52213435$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <87vc2mz3v1.fsf@no-fixed-abode.cable.virginmedia.net> Jussi Piitulainen writes: > # Option 1.5 > for spam in sequence: > if not predicate(spam): continue > process(spam) > > This saves an indent level. Just out of interest: is saving an indent level a useful thing? I wouldn't lay out my code like that just because if you're coming back to it later and reading through quickly it's (to my mind at least) easier to miss what's going on. From jpiitula at ling.helsinki.fi Sat Aug 31 06:28:09 2013 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 31 Aug 2013 13:28:09 +0300 Subject: semicolon at end of python's statements References: <1377735506.18906.15.camel@debian> <1FETt.52607$Mw4.14965@fx15.am4> <7wob8gywds.fsf@benfinney.id.au> <52213435$0$6599$c3e8da3$5496439d@news.astraweb.com> <87vc2mz3v1.fsf@no-fixed-abode.cable.virginmedia.net> Message-ID: Paul Rudin writes: > Jussi Piitulainen writes: > > > # Option 1.5 > > for spam in sequence: > > if not predicate(spam): continue > > process(spam) > > > > This saves an indent level. > > Just out of interest: is saving an indent level a useful thing? It might be if process(spam) is a more complex statement. > I wouldn't lay out my code like that just because if you're coming > back to it later and reading through quickly it's (to my mind at > least) easier to miss what's going on. I agree with the general principle but I have actually done the above (once or twice) precisely because I found it to be the clearer choice in a particular situation. From i.sheeha at gmail.com Thu Aug 29 03:28:05 2013 From: i.sheeha at gmail.com (i.sheeha at gmail.com) Date: Thu, 29 Aug 2013 00:28:05 -0700 (PDT) Subject: Question about XMLRPC Message-ID: Hello, I should write a python script(s) that listens to an existing XMLRPC service on my company's dev server.My utility should take some data from that XMLRPC, send it to an online xml service provider(it's something about hotel accomodation, where they have xml patterns for different requests), then they return their response to me, also in xml data interchange format.then i should parse that and return to the existing XML-RPC, or write the parsed data to the Posgresql database.The thing is, i know something about XML parsing, i know also something about XMLRPC, but i'm not permitted to edit the existing XMLRPC service.Can anyone just tell me what is the shortest and the best solution for this problem? From greg.ewing at canterbury.ac.nz Thu Aug 29 07:54:42 2013 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Thu, 29 Aug 2013 23:54:42 +1200 Subject: Question about XMLRPC In-Reply-To: References: Message-ID: i.sheeha at gmail.com wrote: > I should write a python script(s) that listens to an existing XMLRPC > service on my company's dev server. > then i should parse that and return to the existing XML-RPC, > or write the parsed data to the Posgresql database. > but i'm not permitted to edit the existing XMLRPC service. It's not clear exactly what you mean by this. Are you trying to intercept XMLRPC requests sent to an existing service and do something different with them? To do that without modifying the existing service, you would need to change your web server's configuration to redirect the url of the service to a handler of your own. Are you able to do that, or get your administrators to do it? -- Greg From nikos at superhost.gr Thu Aug 29 08:35:38 2013 From: nikos at superhost.gr (Ferrous Cranus) Date: Thu, 29 Aug 2013 15:35:38 +0300 Subject: Question about XMLRPC In-Reply-To: References: Message-ID: ???? 29/8/2013 2:54 ??, ?/? Gregory Ewing ??????: > i.sheeha at gmail.com wrote: > >> I should write a python script(s) that listens to an existing XMLRPC >> service on my company's dev server. > >> then i should parse that and return to the existing XML-RPC, > > or write the parsed data to the Posgresql database. > >> but i'm not permitted to edit the existing XMLRPC service. > > It's not clear exactly what you mean by this. Are you trying > to intercept XMLRPC requests sent to an existing service and > do something different with them? > > To do that without modifying the existing service, you would > need to change your web server's configuration to redirect > the url of the service to a handler of your own. Are you > able to do that, or get your administrators to do it? > test -- Webhost From nikos at superhost.gr Thu Aug 29 11:30:06 2013 From: nikos at superhost.gr (Ferrous Cranus) Date: Thu, 29 Aug 2013 18:30:06 +0300 Subject: Question about XMLRPC In-Reply-To: References: Message-ID: ???? 29/8/2013 3:35 ??, ?/? Ferrous Cranus ??????: > ???? 29/8/2013 2:54 ??, ?/? Gregory Ewing ??????: >> i.sheeha at gmail.com wrote: >> >>> I should write a python script(s) that listens to an existing XMLRPC >>> service on my company's dev server. >> >>> then i should parse that and return to the existing XML-RPC, >> > or write the parsed data to the Posgresql database. >> >>> but i'm not permitted to edit the existing XMLRPC service. >> >> It's not clear exactly what you mean by this. Are you trying >> to intercept XMLRPC requests sent to an existing service and >> do something different with them? >> >> To do that without modifying the existing service, you would >> need to change your web server's configuration to redirect >> the url of the service to a handler of your own. Are you >> able to do that, or get your administrators to do it? >> > test > test -- Webhost From nikos at superhost.gr Fri Aug 30 09:29:55 2013 From: nikos at superhost.gr (Ferrous Cranus) Date: Fri, 30 Aug 2013 16:29:55 +0300 Subject: Question about XMLRPC In-Reply-To: References: Message-ID: ???? 29/8/2013 6:30 ??, ?/? Ferrous Cranus ??????: > ???? 29/8/2013 3:35 ??, ?/? Ferrous Cranus ??????: >> ???? 29/8/2013 2:54 ??, ?/? Gregory Ewing ??????: >>> i.sheeha at gmail.com wrote: >>> >>>> I should write a python script(s) that listens to an existing XMLRPC >>>> service on my company's dev server. >>> >>>> then i should parse that and return to the existing XML-RPC, >>> > or write the parsed data to the Posgresql database. >>> >>>> but i'm not permitted to edit the existing XMLRPC service. >>> >>> It's not clear exactly what you mean by this. Are you trying >>> to intercept XMLRPC requests sent to an existing service and >>> do something different with them? >>> >>> To do that without modifying the existing service, you would >>> need to change your web server's configuration to redirect >>> the url of the service to a handler of your own. Are you >>> able to do that, or get your administrators to do it? >>> >> test >> > test > test -- Webhost From james.harris.1 at gmail.com Thu Aug 29 04:12:31 2013 From: james.harris.1 at gmail.com (James Harris) Date: Thu, 29 Aug 2013 09:12:31 +0100 Subject: Python TUI that will work on DOS/Windows and Unix/Linux Message-ID: Am looking for a TUI (textual user interface) mechanism to allow a Python program to create and update a display in text mode. For example, if a command prompt was sized 80x25 it would be made up of 80 x 25 = 2000 characters. The Python program would need to be able to write to any of those 2000 characters at any time though in practice the display would normally be arranged by dividing it up into non-overlapping rectangular regions. I have seen that there are various libraries: urwid, newt, console, dialog etc. But they seem to be either for Unix or for DOS, not for both. I am looking for a library that will run under either. Furthermore, some libraries are complex, providing widgets of all kinds. I am looking for something much simpler and the lighter-weight it is the better. At least at this stage I pretty much just want to divide the screen up into panels. Input from keyboard would be essential. Input from a mouse would be nice to have. Especially if you have had a similar requirement in the past but even if not, is there any cross-platform system you would recommend? James From philip at gn.apc.org Thu Aug 29 05:43:29 2013 From: philip at gn.apc.org (Philip Inglesant) Date: Thu, 29 Aug 2013 10:43:29 +0100 Subject: Download Visual Studio Express 2008 now In-Reply-To: References: Message-ID: <521F17C1.60200@gn.apc.org> Hi Martyn, Thanks for the good advice to download VS 2008 before M$ delete it from their download servers. Unfortunately they have already done this so many Python modules now can't be compiled correctly on Windows! Best regards, Philip From wingusr at gmail.com Thu Aug 29 06:07:29 2013 From: wingusr at gmail.com (TP) Date: Thu, 29 Aug 2013 03:07:29 -0700 Subject: Download Visual Studio Express 2008 now In-Reply-To: <521F17C1.60200@gn.apc.org> References: <521F17C1.60200@gn.apc.org> Message-ID: On Thu, Aug 29, 2013 at 2:43 AM, Philip Inglesant wrote: > Hi Martyn, > > Thanks for the good advice to download VS 2008 before M$ delete it from > their download servers. > > Unfortunately they have already done this so many Python modules now can't > be compiled correctly on Windows! > > Best regards, > > Philip > -- > http://mail.python.org/**mailman/listinfo/python-list > Not true. See http://stackoverflow.com/questions/15318560/visual-c-2008-express-download-link-dead for still live link to VS2008 Express SP1 iso at http://download.microsoft.com/download/E/8/E/E8EEB394-7F42-4963-A2D8-29559B738298/VS2008ExpressWithSP1ENUX1504728.iso -------------- next part -------------- An HTML attachment was scrubbed... URL: From tim at akwebsoft.com Thu Aug 29 14:34:18 2013 From: tim at akwebsoft.com (Tim Johnson) Date: Thu, 29 Aug 2013 10:34:18 -0800 Subject: subprocess.Popen instance hangs Message-ID: <20130829183418.GE414@mail.akwebsoft.com> using Python 2.7.1 on OS X 10.7.5 I'm managing a process of drush using an instance of subprocess.Popen The process has a '--verbose' option. When that option is passed as part of the initializer `args' argument, the process will hang. It should be no surprise as drush output with the --verbose option can be _extremely_ verbose, and I can do without it, but I would like to learn how to handle it. I've googled this topic, but my poor little brain is yet to sort out all of the content found. ## my relevant code follows : p = subprocess.Popen(args,stderr=subprocess.STDOUT,stdout=subprocess.PIPE) ## wait() is 'forever' if '--verbose' used exit_status = p.wait() output = p.stdout.read() ## done I 'suspect' that using a tempfile may be the solution, if so, I could use some examples. thanks -- Tim tim at tee jay forty nine dot com or akwebsoft dot com http://www.akwebsoft.com From python at mrabarnett.plus.com Thu Aug 29 15:01:56 2013 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 29 Aug 2013 20:01:56 +0100 Subject: subprocess.Popen instance hangs In-Reply-To: <20130829183418.GE414@mail.akwebsoft.com> References: <20130829183418.GE414@mail.akwebsoft.com> Message-ID: <521F9AA4.1050406@mrabarnett.plus.com> On 29/08/2013 19:34, Tim Johnson wrote: > using Python 2.7.1 on OS X 10.7.5 > > I'm managing a process of drush using an instance of subprocess.Popen > > The process has a '--verbose' option. When that option is passed as > part of the initializer `args' argument, the process will hang. > > It should be no surprise as drush output with the --verbose option > can be _extremely_ verbose, and I can do without it, but I would > like to learn how to handle it. I've googled this topic, but my poor > little brain is yet to sort out all of the content found. > > ## my relevant code follows : > p = subprocess.Popen(args,stderr=subprocess.STDOUT,stdout=subprocess.PIPE) > ## wait() is 'forever' if '--verbose' used > exit_status = p.wait() > output = p.stdout.read() > ## done > > I 'suspect' that using a tempfile may be the solution, if so, I > could use some examples. > The subprocess will terminate when it has finished writing its output, but because you're not consuming any of the output (you're waiting for it to finish), the buffer fills up and blocks the subprocess. Try reading the output or using the .communicate method. Alternatively, pass an open file as the stdout argument. From tim at akwebsoft.com Thu Aug 29 18:56:21 2013 From: tim at akwebsoft.com (Tim Johnson) Date: Thu, 29 Aug 2013 14:56:21 -0800 Subject: subprocess.Popen instance hangs In-Reply-To: <521F9AA4.1050406@mrabarnett.plus.com> References: <20130829183418.GE414@mail.akwebsoft.com> <521F9AA4.1050406@mrabarnett.plus.com> Message-ID: <20130829225621.GF414@mail.akwebsoft.com> * MRAB [130829 11:04]: > On 29/08/2013 19:34, Tim Johnson wrote: > >could use some examples. > > > The subprocess will terminate when it has finished writing its output, > but because you're not consuming any of the output (you're waiting for > it to finish), the buffer fills up and blocks the subprocess. > > Try reading the output or using the .communicate method. > > Alternatively, pass an open file as the stdout argument. > Kudos to all for the replies. Here is some code to review: ## execute process and read output p = subprocess.Popen(args,stderr=subprocess.STDOUT,stdout=subprocess.PIPE) while 1 : output = p.stdout.read() if output : print(output) else : break ## Check for errors exit_status = p.wait() if exit_status : if p.stderr : self.__err('Process terminated with exit status: %s' % (str(exit_status)), 'Following error message was found:' p.stderr.read()) else : self.__err('Process terminated with exit status: %s' % (str(exit_status))) Without any error from the drush process, this works fine. I can't quite figure out how to simulate/cause an error from drush, so I would welcome comments on the error handling thanks again -- Tim tim at tee jay forty nine dot com or akwebsoft dot com http://www.akwebsoft.com From ramit.prasad at jpmorgan.com.dmarc.invalid Thu Aug 29 14:49:47 2013 From: ramit.prasad at jpmorgan.com.dmarc.invalid (Prasad, Ramit) Date: Thu, 29 Aug 2013 18:49:47 +0000 Subject: subprocess.Popen instance hangs In-Reply-To: <20130829183418.GE414@mail.akwebsoft.com> References: <20130829183418.GE414@mail.akwebsoft.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF4741869A781@SCACMX008.exchad.jpmchase.net> Tim Johnson > using Python 2.7.1 on OS X 10.7.5 > > I'm managing a process of drush using an instance of subprocess.Popen > > The process has a '--verbose' option. When that option is passed as > part of the initializer `args' argument, the process will hang. > > It should be no surprise as drush output with the --verbose option > can be _extremely_ verbose, and I can do without it, but I would > like to learn how to handle it. I've googled this topic, but my poor > little brain is yet to sort out all of the content found. > > ## my relevant code follows : > p = subprocess.Popen(args,stderr=subprocess.STDOUT,stdout=subprocess.PIPE) > ## wait() is 'forever' if '--verbose' used > exit_status = p.wait() > output = p.stdout.read() > ## done > > I 'suspect' that using a tempfile may be the solution, if so, I > could use some examples. > > thanks > -- > Tim > tim at tee jay forty nine dot com or akwebsoft dot com > http://www.akwebsoft.com > -- I think the documentation covers your issue. """ Popen.wait() Wait for child process to terminate. Set and return returncode attribute. Warning: will deadlock when using stdout=PIPE and/or stderr=PIPE and the child process generates enough output to a pipe such that it blocks waiting for the OS pipe buffer to accept more data. Use [Popen.]communicate() to avoid that. """ From http://docs.python.org/2/library/subprocess.html#subprocess.Popen.wait Does switching to `p.communicate()` solve your problem? ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From thudfoo at gmail.com Thu Aug 29 16:26:24 2013 From: thudfoo at gmail.com (xDog Walker) Date: Thu, 29 Aug 2013 13:26:24 -0700 Subject: subprocess.Popen instance hangs In-Reply-To: <20130829183418.GE414@mail.akwebsoft.com> References: <20130829183418.GE414@mail.akwebsoft.com> Message-ID: <201308291326.24412.thudfoo@gmail.com> On Thursday 2013 August 29 11:34, Tim Johnson wrote: > using Python 2.7.1 on OS X 10.7.5 > > I'm managing a process of drush using an instance of subprocess.Popen > > The process has a '--verbose' option. When that option is passed as > part of the initializer `args' argument, the process will hang. > > It should be no surprise as drush output with the --verbose option > can be _extremely_ verbose, and I can do without it, but I would > like to learn how to handle it. I've googled this topic, but my poor > little brain is yet to sort out all of the content found. > > ## my relevant code follows : > p = subprocess.Popen(args,stderr=subprocess.STDOUT,stdout=subprocess.PIPE) > ## wait() is 'forever' if '--verbose' used > exit_status = p.wait() > output = p.stdout.read() > ## done p = subprocess.Popen(args,stderr=subprocess.STDOUT,stdout=subprocess.PIPE) for line in p.stdout: do_something(line) p.wait() -- Yonder nor sorghum stenches shut ladle gulls stopper torque wet strainers. From tim at akwebsoft.com Thu Aug 29 21:00:21 2013 From: tim at akwebsoft.com (Tim Johnson) Date: Thu, 29 Aug 2013 17:00:21 -0800 Subject: subprocess.Popen instance hangs In-Reply-To: <20130829183418.GE414@mail.akwebsoft.com> References: <20130829183418.GE414@mail.akwebsoft.com> Message-ID: <20130830010021.GA400@mail.akwebsoft.com> * Tim Johnson [130829 10:51]: > using Python 2.7.1 on OS X 10.7.5 > > I'm managing a process of drush using an instance of subprocess.Popen <...> ## This appears to be what works. def __exec(self,args) : """Run the process with arguments""" p = subprocess.Popen(args,stderr=subprocess.PIPE,stdout=subprocess.PIPE) while 1 : output = p.stdout.read() if output : print(output) else : break errmsg = p.communicate()[1] if errmsg : self.__err('Process terminated with error:',errmsg) ## Thanks again, gnarly one for me. I am eternally a noob! -- Tim tim at tee jay forty nine dot com or akwebsoft dot com http://www.akwebsoft.com From nobody at nowhere.com Fri Aug 30 10:47:27 2013 From: nobody at nowhere.com (Nobody) Date: Fri, 30 Aug 2013 15:47:27 +0100 Subject: subprocess.Popen instance hangs References: <20130829183418.GE414@mail.akwebsoft.com> Message-ID: On Thu, 29 Aug 2013 17:00:21 -0800, Tim Johnson wrote: > ## This appears to be what works. > def __exec(self,args) : > """Run the process with arguments""" > p = > subprocess.Popen(args,stderr=subprocess.PIPE,stdout=subprocess.PIPE) > while 1 : > output = p.stdout.read() If the process tries to write more than a pipe's worth of data to stderr, before closing stdout, it will block indefinitely. If you want to process both stdout and stderr, you have to be able to consume the data in whatever order the process generates it, which means either using multiple threads or (on Unix) select/poll or non-blocking I/O. This is what the .communicate() method does (threads on Windows, select/poll on Unix). The alternative is to merge both streams with stderr=subprocess.STDOUT, or redirect one of them to a file (or /dev/null, etc). From tim at akwebsoft.com Fri Aug 30 11:32:29 2013 From: tim at akwebsoft.com (Tim Johnson) Date: Fri, 30 Aug 2013 07:32:29 -0800 Subject: subprocess.Popen instance hangs In-Reply-To: References: <20130829183418.GE414@mail.akwebsoft.com> Message-ID: <20130830153229.GC400@mail.akwebsoft.com> * Nobody [130830 06:55]: > On Thu, 29 Aug 2013 17:00:21 -0800, Tim Johnson wrote: > > > ## This appears to be what works. > > def __exec(self,args) : > > """Run the process with arguments""" > > p = > > subprocess.Popen(args,stderr=subprocess.PIPE,stdout=subprocess.PIPE) > > while 1 : > > output = p.stdout.read() > > If the process tries to write more than a pipe's worth of data to stderr, > before closing stdout, it will block indefinitely. > > If you want to process both stdout and stderr, you have to be able to > consume the data in whatever order the process generates it, which means > either using multiple threads or (on Unix) select/poll or non-blocking > I/O. This is what the .communicate() method does (threads on Windows, > select/poll on Unix). > > The alternative is to merge both streams with stderr=subprocess.STDOUT, or > redirect one of them to a file (or /dev/null, etc). In earlier code I, I was merging them... :) Like I said: gnarly! What if I were to do something like: ## code while 1: output = p.stout.read() err = p.stderr.read() ## trapping for AttributeError, etc.. .... ## /code break'ing if either no output or value in `err' ?? The objective is to display all output, but to also separate error messages from normal output. thank you -- Tim tim at tee jay forty nine dot com or akwebsoft dot com http://www.akwebsoft.com From malaclypse2 at gmail.com Fri Aug 30 11:38:58 2013 From: malaclypse2 at gmail.com (Jerry Hill) Date: Fri, 30 Aug 2013 11:38:58 -0400 Subject: subprocess.Popen instance hangs In-Reply-To: <20130830153229.GC400@mail.akwebsoft.com> References: <20130829183418.GE414@mail.akwebsoft.com> <20130830153229.GC400@mail.akwebsoft.com> Message-ID: On Fri, Aug 30, 2013 at 11:32 AM, Tim Johnson wrote: > The objective is to display all output, but to also separate error > messages from normal output. I still think you want to use communicate(). Like this: p = subprocess.Popen(args,stderr=subprocess.PIPE,stdout=subprocess.PIPE) output, err = p.communicate() That's it. No need for a loop, or manually handling the fact that stderr and/or stdout could end up with a full buffer and start to block. -- Jerry From tim at akwebsoft.com Fri Aug 30 14:43:57 2013 From: tim at akwebsoft.com (Tim Johnson) Date: Fri, 30 Aug 2013 10:43:57 -0800 Subject: subprocess.Popen instance hangs In-Reply-To: References: <20130829183418.GE414@mail.akwebsoft.com> <20130830153229.GC400@mail.akwebsoft.com> Message-ID: <20130830184357.GD400@mail.akwebsoft.com> * Jerry Hill [130830 07:48]: > On Fri, Aug 30, 2013 at 11:32 AM, Tim Johnson wrote: > > The objective is to display all output, but to also separate error > > messages from normal output. > > I still think you want to use communicate(). Like this: > > p = subprocess.Popen(args,stderr=subprocess.PIPE,stdout=subprocess.PIPE) > output, err = p.communicate() > > That's it. No need for a loop, or manually handling the fact that > stderr and/or stdout could end up with a full buffer and start to > block. The following code : p = subprocess.Popen(args,stderr=subprocess.PIPE,stdout=subprocess.PIPE) errmsg,output = p.communicate() ... Hangs this code : p = subprocess.Popen(args,stderr=subprocess.PIPE,stdout=subprocess.PIPE) while 1 : output = p.stdout.read() if output : print(output) else : break ... works -- Tim tim at tee jay forty nine dot com or akwebsoft dot com http://www.akwebsoft.com From contact at stephane-klein.info Thu Aug 29 16:47:50 2013 From: contact at stephane-klein.info (=?windows-1252?Q?St=E9phane_Klein?=) Date: Thu, 29 Aug 2013 22:47:50 +0200 Subject: =?windows-1252?Q?The_Python_Skeleton_Builder_Tools_=3A?= =?windows-1252?Q?_mr=2Ebob=2C_Cookiecutter=85?= Message-ID: Hi, this is a list with Python Skeleton Builder Tools : http://wiki.python.org/moin/SkeletonBuilderTools If some tools missing, you can complete the list. Best regards, St?phane -- St?phane Klein blog: http://stephane-klein.info Twitter: http://twitter.com/klein_stephane cv: http://cv.stephane-klein.info From tbaror at gmail.com Fri Aug 30 01:43:24 2013 From: tbaror at gmail.com (tbaror at gmail.com) Date: Thu, 29 Aug 2013 22:43:24 -0700 (PDT) Subject: Looking for WINCAP library with Python 3 Message-ID: <0754b4c2-9618-4d50-907d-8c08dca40532@googlegroups.com> Hello Group I need to write a utility that will control wincap under Windows 7 using Pyhton 3.x So i can control which interface stop start capture and dump to file. It will be most appreciated if someone could suggest me such library to use Please advice Thanks From david.dsch at gmail.com Fri Aug 30 06:53:05 2013 From: david.dsch at gmail.com (david.dsch at gmail.com) Date: Fri, 30 Aug 2013 03:53:05 -0700 (PDT) Subject: python script to gather file links into textfile Message-ID: <3cd251f2-f37b-4aa6-a385-26163fa5d62b@googlegroups.com> Hi, im looking for someone who can make a script that gathers all file links from an url into a textfile, like this : http://pastebin.com/jfD31r1x From neilc at norwich.edu Fri Aug 30 09:01:52 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 30 Aug 2013 13:01:52 GMT Subject: python script to gather file links into textfile References: <3cd251f2-f37b-4aa6-a385-26163fa5d62b@googlegroups.com> Message-ID: On 2013-08-30, david.dsch at gmail.com wrote: > Hi, im looking for someone who can make a script that gathers > all file links from an url into a textfile, like this : > http://pastebin.com/jfD31r1x Michael Jackson advises you to start with the man in the mirror. -- Neil Cerutti From b.amiel at evs.com Fri Aug 30 09:09:16 2013 From: b.amiel at evs.com (Bastien Amiel) Date: Fri, 30 Aug 2013 15:09:16 +0200 Subject: python script to gather file links into textfile In-Reply-To: References: <3cd251f2-f37b-4aa6-a385-26163fa5d62b@googlegroups.com> Message-ID: <5220997C.6030802@opencubetech.com> Le 30/08/2013 15:01, Neil Cerutti a ?crit : > On 2013-08-30, david.dsch at gmail.com wrote: >> Hi, im looking for someone who can make a script that gathers >> all file links from an url into a textfile, like this : >> http://pastebin.com/jfD31r1x 1. Read the file with urls http://stackoverflow.com/questions/3925614/how-do-you-read-a-file-into-a-list-in-python 2. Download each url http://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python > Michael Jackson advises you to start with the man in the mirror. :D From joel.goldstick at gmail.com Fri Aug 30 10:56:43 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Fri, 30 Aug 2013 10:56:43 -0400 Subject: python script to gather file links into textfile In-Reply-To: <5220997C.6030802@opencubetech.com> References: <3cd251f2-f37b-4aa6-a385-26163fa5d62b@googlegroups.com> <5220997C.6030802@opencubetech.com> Message-ID: > >> On 2013-08-30, david.dsch at gmail.com wrote: >>> >>> Hi, im looking for someone who can make a script that gathers >>> all file links from an url into a textfile, like this : >>> http://pastebin.com/jfD31r1x > Sometimes its good to look in the closets or under the beds. People forget what they put in those places sometimes. -- Joel Goldstick http://joelgoldstick.com From joel.goldstick at gmail.com Fri Aug 30 12:13:34 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Fri, 30 Aug 2013 12:13:34 -0400 Subject: python script to gather file links into textfile In-Reply-To: References: <3cd251f2-f37b-4aa6-a385-26163fa5d62b@googlegroups.com> <5220997C.6030802@opencubetech.com> Message-ID: David, actually, what is the url? I read about you a little and it looks like you are into music but not a software guy. You might be better off finding someone local to write the code for you. People here generally help people with actual coding problems, or discuss aspects of python, but if you want to hire someone you will need to give more specific details of what you want done On Fri, Aug 30, 2013 at 10:56 AM, Joel Goldstick wrote: >> >>> On 2013-08-30, david.dsch at gmail.com wrote: >>>> >>>> Hi, im looking for someone who can make a script that gathers >>>> all file links from an url into a textfile, like this : >>>> http://pastebin.com/jfD31r1x >> > Sometimes its good to look in the closets or under the beds. People > forget what they put in those places sometimes. > > > > -- > Joel Goldstick > http://joelgoldstick.com -- Joel Goldstick http://joelgoldstick.com From steve+comp.lang.python at pearwood.info Fri Aug 30 20:34:21 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 31 Aug 2013 00:34:21 GMT Subject: python script to gather file links into textfile References: <3cd251f2-f37b-4aa6-a385-26163fa5d62b@googlegroups.com> Message-ID: <52213a0d$0$6599$c3e8da3$5496439d@news.astraweb.com> On Fri, 30 Aug 2013 03:53:05 -0700, david.dsch wrote: > Hi, im looking for someone who can make a script that gathers all file > links from an url into a textfile, like this : > http://pastebin.com/jfD31r1x You've come to the right place! My rate is AUD$100 an hour. Contact me if you are interested. -- Steven From hossamalagmy at gmail.com Fri Aug 30 10:02:53 2013 From: hossamalagmy at gmail.com (23alagmy) Date: Fri, 30 Aug 2013 07:02:53 -0700 (PDT) Subject: =?windows-1256?B?1d3NyuTHINrh7CDH4d3t0yDI5t8gx9TK0d8gxw==?= =?windows-1256?B?4cfkIMfOyMfR7eUg5srR3e3lIObV5tE=?= Message-ID: <9cdb8f67-101b-4864-ae2e-30c2479eadf6@googlegroups.com> ?????? ??? ????? ??? ????? ???? ??????? ?????? ???? 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 hermanmu at gmail.com Fri Aug 30 11:19:04 2013 From: hermanmu at gmail.com (Michael Herman) Date: Fri, 30 Aug 2013 08:19:04 -0700 (PDT) Subject: Python Weekend Challenge - $$ Message-ID: https://gist.github.com/mjhea0/6390724 Check it out!:) Have a great labor day weekend. From badouglas at gmail.com Fri Aug 30 13:13:33 2013 From: badouglas at gmail.com (bruce) Date: Fri, 30 Aug 2013 13:13:33 -0400 Subject: web2py - running on fedora Message-ID: Hi. I know this is a python list, but hoping that I can find someone to help get a base install of web2py running. I've got an older version of fedora, running py 2.6.4 running apache v2.2 I'm simply trying to get web2py up/running, and then to interface it with apache, so that the existing webapps (php apps) don't get screwed up. Thanks -bruce From joel.goldstick at gmail.com Fri Aug 30 13:25:44 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Fri, 30 Aug 2013 13:25:44 -0400 Subject: web2py - running on fedora In-Reply-To: References: Message-ID: On Fri, Aug 30, 2013 at 1:13 PM, bruce wrote: > Hi. > > I know this is a python list, but hoping that I can find someone to > help get a base install of web2py running. > > I've got an older version of fedora, running py 2.6.4 running apache v2.2 > > I'm simply trying to get web2py up/running, and then to interface it > with apache, so that the existing webapps (php apps) don't get screwed > up. > > Thanks > > -bruce > -- > http://mail.python.org/mailman/listinfo/python-list Do you know about google? I just seached this: 'installing web2py apache' and I got whole lot of information. Why don't you try to get it running and come back with a specific question if you get stuck? -- Joel Goldstick http://joelgoldstick.com From neilc at norwich.edu Fri Aug 30 13:20:16 2013 From: neilc at norwich.edu (Neil Cerutti) Date: 30 Aug 2013 17:20:16 GMT Subject: sax.handler.Contenthandler.__init__ Message-ID: This code is from The Python Cookbook, 2nd edition, 12.2 Counting Tags in a Document: from xml.sax.handler import ContentHandler import xml.sax class countHandler(ContentHandler): def __init__(self): self.tags={} def startElement(self, name, attr): self.tags[name] = 1 + self.tags.get(name, 0) Isn't overriding __init__ a risky thing to do? The docs don't mention it as a method I should override, and also don't define what's in there or if I'd need to call the base class __init__. Moreover, startDocument is provided for parser setup. As it happens, ContentHandler.__init__ isn't empty, so the above code could fail if the parser isn't prepared for _locator to be undefined. Is the above code is an acceptable idiom? -- Neil Cerutti From ramit.prasad at jpmorgan.com.dmarc.invalid Fri Aug 30 13:35:42 2013 From: ramit.prasad at jpmorgan.com.dmarc.invalid (Prasad, Ramit) Date: Fri, 30 Aug 2013 17:35:42 +0000 Subject: sax.handler.Contenthandler.__init__ In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF4741869DE05@SCACMX008.exchad.jpmchase.net> Neil Cerutti wrote: > This code is from The Python Cookbook, 2nd edition, 12.2 Counting > Tags in a Document: > > from xml.sax.handler import ContentHandler > import xml.sax > class countHandler(ContentHandler): > def __init__(self): > self.tags={} > def startElement(self, name, attr): > self.tags[name] = 1 + self.tags.get(name, 0) > > Isn't overriding __init__ a risky thing to do? The docs don't > mention it as a method I should override, and also don't define > what's in there or if I'd need to call the base class __init__. > Moreover, startDocument is provided for parser setup. > > As it happens, ContentHandler.__init__ isn't empty, so the above > code could fail if the parser isn't prepared for _locator to be > undefined. > > Is the above code is an acceptable idiom? > > -- > Neil Cerutti > -- I think this is a bad idea unless you want to avoid the parent class __init__ specifically (in which case a comment stating why is mandatory). I do not like that this recipe shows behavior that might be fine in this instance, but is not a good general practice. def __init__(self): super(ContentHandler, self).__init__() #OR ContentHandler.__init__(self) self.tags={} I personally think the super() line is better of the two options. ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From vinay_sajip at yahoo.co.uk Fri Aug 30 13:39:17 2013 From: vinay_sajip at yahoo.co.uk (Vinay Sajip) Date: Fri, 30 Aug 2013 10:39:17 -0700 (PDT) Subject: ANN: A new version (0.3.5) of python-gnupg has been released. Message-ID: <302be43d-b4b1-444d-af45-1cc2111d3955@googlegroups.com> A new version of the Python module which wraps GnuPG has been released. What Changed? ============= This is a minor enhancement and bug-fix release. See the project website ( http://code.google.com/p/python-gnupg/ ) for more information. Summary: Added improved shell quoting to guard against shell injection attacks. Added search_keys() and send_keys() methods to interact with keyservers. A symmetric cipher algorithm can now be specified when encrypting. UTF-8 encoding is used as a fall back when no other encoding can be determined. The key length now defaults to 2048 bits. A default Name-Comment field is no longer provided during key generation. What Does It Do? ================ The gnupg module allows Python programs to make use of the functionality provided by the Gnu Privacy Guard (abbreviated GPG or GnuPG). Using this module, Python programs can encrypt and decrypt data, digitally sign documents and verify digital signatures, manage (generate, list and delete) encryption keys, using proven Public Key Infrastructure (PKI) encryption technology based on OpenPGP. This module is expected to be used with Python versions >= 2.4, as it makes use of the subprocess module which appeared in that version of Python. This module is a newer version derived from earlier work by Andrew Kuchling, Richard Jones and Steve Traugott. A test suite using unittest is included with the source distribution. Simple usage: >>> import gnupg >>> gpg = gnupg.GPG(gnupghome='/path/to/keyring/directory') >>> gpg.list_keys() [{ ... 'fingerprint': 'F819EE7705497D73E3CCEE65197D5DAC68F1AAB2', 'keyid': '197D5DAC68F1AAB2', 'length': '1024', 'type': 'pub', 'uids': ['', 'Gary Gross (A test user) ']}, { ... 'fingerprint': '37F24DD4B918CC264D4F31D60C5FEFA7A921FC4A', 'keyid': '0C5FEFA7A921FC4A', 'length': '1024', ... 'uids': ['', 'Danny Davis (A test user) ']}] >>> encrypted = gpg.encrypt("Hello, world!", ['0C5FEFA7A921FC4A']) >>> str(encrypted) '-----BEGIN PGP MESSAGE-----\nVersion: GnuPG v1.4.9 (GNU/Linux)\n \nhQIOA/6NHMDTXUwcEAf ... -----END PGP MESSAGE-----\n' >>> decrypted = gpg.decrypt(str(encrypted), passphrase='secret') >>> str(decrypted) 'Hello, world!' >>> signed = gpg.sign("Goodbye, world!", passphrase='secret') >>> verified = gpg.verify(str(signed)) >>> print "Verified" if verified else "Not verified" 'Verified' For more information, visit http://code.google.com/p/python-gnupg/ - as always, your feedback is most welcome (especially bug reports, patches and suggestions for improvement). Enjoy! Cheers Vinay Sajip Red Dove Consultants Ltd. From niubao56 at gmail.com Fri Aug 30 14:04:28 2013 From: niubao56 at gmail.com (niubao56 at gmail.com) Date: Fri, 30 Aug 2013 11:04:28 -0700 (PDT) Subject: Best practice for generalizing and documenting each method's behaviour Message-ID: I'm starting a small project coding in Python as I learn the ropes. As the project grows bigger, there are more and more overlapping and even redundant methods. For example, several classes have a checkAndClean_obj_state() method. If just one or two such classes, it is easy to analyze the behaviour of them and design the optimal interaction for all objects. However, when there are many of such classes, exactly at what point to invoke check and clean behaviour becomes a little blurred. There is a desperate need for generalizing and documenting the behaviour of each such class, preferably in a flowchart. I'm currently doing the flowchart manually but the job becomes a bit overwhelming. I wonder what Python pros are using for analyzing and documenting classes/functions behaviours and interactions? Is UML the only way? Personally I found UML is a bit overkill for a one person project, but I'm not sure if it is the right direction. I'd appreciate any insight. Many thanks. From fabiosantosart at gmail.com Sat Aug 31 22:31:10 2013 From: fabiosantosart at gmail.com (=?ISO-8859-1?Q?F=E1bio_Santos?=) Date: Sun, 1 Sep 2013 03:31:10 +0100 Subject: Best practice for generalizing and documenting each method's behaviour In-Reply-To: References: Message-ID: On 30 Aug 2013 19:07, wrote: > > I'm starting a small project coding in Python as I learn the ropes. As the project grows bigger, there are more and more overlapping and even redundant methods. For example, several classes have a checkAndClean_obj_state() method. If just one or two such classes, it is easy to analyze the behaviour of them and design the optimal interaction for all objects. However, when there are many of such classes, exactly at what point to invoke check and clean behaviour becomes a little blurred. There is a desperate need for generalizing and documenting the behaviour of each such class, preferably in a flowchart. I'm currently doing the flowchart manually but the job becomes a bit overwhelming. > > I wonder what Python pros are using for analyzing and documenting classes/functions behaviours and interactions? Is UML the only way? Personally I found UML is a bit overkill for a one person project, but I'm not sure if it is the right direction. I'd appreciate any insight. Many thanks. I can't say I have ever used any tool for documenting python automatically. Human written documentation is always best. As for graphs and all, YMMV, but I've found that paper is best for expressing your code structure. I have looked into tools which generate graphs of your imports, but I found none of them satisfactory. There are a lot of problems parsing python due to its dynamic nature. In the end I decided to sit and read my codebase as I drew. -------------- next part -------------- An HTML attachment was scrubbed... URL: From allisson at gmail.com Fri Aug 30 19:23:04 2013 From: allisson at gmail.com (Allisson Azevedo) Date: Fri, 30 Aug 2013 16:23:04 -0700 (PDT) Subject: Gunstar - Another python web framework. Message-ID: It's a recent project, check this out: http://github.com/allisson/gunstar From nikos at superhost.gr Sat Aug 31 02:41:27 2013 From: nikos at superhost.gr (Ferrous Cranus) Date: Sat, 31 Aug 2013 09:41:27 +0300 Subject: UnicodeDecodeError issue Message-ID: Suddenly my webiste superhost.gr running my main python script presents me with this error: Code: UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef \xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1, 'invalid start byte') Does anyone know what this means? -- Webhost From rosuav at gmail.com Sat Aug 31 02:53:33 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 31 Aug 2013 16:53:33 +1000 Subject: UnicodeDecodeError issue In-Reply-To: References: Message-ID: On Sat, Aug 31, 2013 at 4:41 PM, Ferrous Cranus wrote: > Suddenly my webiste superhost.gr running my main python script presents me > with this error: > > Code: > UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef > \xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1, > 'invalid start byte') > > > Does anyone know what this means? Yes. It means that 0xB6 is an invalid start byte in UTF-8. If that seems unhelpful, it's because your question was; there is not enough information for us to be able to give any further information. ChrisA From nikos at superhost.gr Sat Aug 31 03:02:25 2013 From: nikos at superhost.gr (Ferrous Cranus) Date: Sat, 31 Aug 2013 10:02:25 +0300 Subject: UnicodeDecodeError issue In-Reply-To: References: Message-ID: ???? 31/8/2013 9:53 ??, ?/? Chris Angelico ??????: > On Sat, Aug 31, 2013 at 4:41 PM, Ferrous Cranus wrote: >> Suddenly my webiste superhost.gr running my main python script presents me >> with this error: >> >> Code: >> UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef >> \xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1, >> 'invalid start byte') >> >> >> Does anyone know what this means? > > Yes. It means that 0xB6 is an invalid start byte in UTF-8. If that > seems unhelpful, it's because your question was; there is not enough > information for us to be able to give any further information. Hello Chris, I provided you with only that info because that only info is displayed at http://superhost.gr at the top left. If you please tell me what to try to provide you guys with more info about this problem. -- Webhost From nikos at superhost.gr Sat Aug 31 03:18:33 2013 From: nikos at superhost.gr (Ferrous Cranus) Date: Sat, 31 Aug 2013 10:18:33 +0300 Subject: UnicodeDecodeError issue In-Reply-To: References: Message-ID: ???? 31/8/2013 10:02 ??, ?/? Ferrous Cranus ??????: > ???? 31/8/2013 9:53 ??, ?/? Chris Angelico ??????: >> On Sat, Aug 31, 2013 at 4:41 PM, Ferrous Cranus >> wrote: >>> Suddenly my webiste superhost.gr running my main python script >>> presents me >>> with this error: >>> >>> Code: >>> UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef >>> \xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1, >>> 'invalid start byte') >>> >>> >>> Does anyone know what this means? >> >> Yes. It means that 0xB6 is an invalid start byte in UTF-8. If that >> seems unhelpful, it's because your question was; there is not enough >> information for us to be able to give any further information. > > > Hello Chris, > > I provided you with only that info because that only info is displayed > at http://superhost.gr at the top left. > > If you please tell me what to try to provide you guys with more info > about this problem. > > And the wird thign is that few the last days this error was not there, just only today it started dissaperaing. -- Webhost From __peter__ at web.de Sat Aug 31 03:25:30 2013 From: __peter__ at web.de (Peter Otten) Date: Sat, 31 Aug 2013 09:25:30 +0200 Subject: UnicodeDecodeError issue References: Message-ID: Ferrous Cranus wrote: > Suddenly my webiste superhost.gr running my main python script presents > me with this error: > Code: > UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef > \xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1, > 'invalid start byte') > Does anyone know what this means? >>> b'\xb6\xe3\xed\xf9\xf3\xf4\xef\xfc\xed\xef\xec\xe1\xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2'.decode("iso-8859-7") 'Unknown hostname' From nikos at superhost.gr Sat Aug 31 03:58:43 2013 From: nikos at superhost.gr (Ferrous Cranus) Date: Sat, 31 Aug 2013 10:58:43 +0300 Subject: UnicodeDecodeError issue In-Reply-To: References: Message-ID: ???? 31/8/2013 10:25 ??, ?/? Peter Otten ??????: > Ferrous Cranus wrote: > >> Suddenly my webiste superhost.gr running my main python script presents >> me with this error: > >> Code: >> UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef >> \xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1, >> 'invalid start byte') > >> Does anyone know what this means? > >>>> b'\xb6\xe3\xed\xf9\xf3\xf4\xef\xfc\xed\xef\xec\xe1\xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2'.decode("iso-8859-7") > 'Unknown hostname' > > Thanks you decoded in greek-iso, coll thinking! But llok: Why can the host varibale be unset since iam using this: try: gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat') city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] ) or socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] ) or '??????? ?????????' except Exception as e: err = repr(e) ================== it gets set to string '??????? ?????????' if in case visitor's remote ip address cannot be resolved. So that error should arise at all. -- Webhost From nikos at superhost.gr Sat Aug 31 04:31:13 2013 From: nikos at superhost.gr (Ferrous Cranus) Date: Sat, 31 Aug 2013 11:31:13 +0300 Subject: UnicodeDecodeError issue In-Reply-To: References: Message-ID: Here is the code inside files.py: try: gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat') city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] ) or socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] ) or os.environ['REMOTE_ADDR'] except Exception as e: print( repr(e), file=open( '/tmp/err.out', 'w' ) ) that produces this: [Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116] ValueError: underlying buffer has been detached, referer: http://superhost.gr/ [Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116] , referer: http://superhost.gr/ [Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116] Original exception was:, referer: http://superhost.gr/ [Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116] Traceback (most recent call last):, referer: http://superhost.gr/ [Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116] File "/home/nikos/public_html/cgi-bin/files.py", line 135, in , referer: http://superhost.gr/ [Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116] cur.execute('''INSERT INTO files (url, host, city, lastvisit) VALUES (%s, %s, %s, %s)''', (filename, host, city, lastvisit) ), referer: http://superhost.gr/ [Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116] NameError: name 'host' is not defined, referer: http://superhost.gr/ But 'host' defaults to an ip address if it cannot resolve the hostname. Why the errro says its undefined? From steve+comp.lang.python at pearwood.info Sat Aug 31 07:28:52 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 31 Aug 2013 11:28:52 GMT Subject: UnicodeDecodeError issue References: Message-ID: <5221d373$0$6599$c3e8da3$5496439d@news.astraweb.com> On Sat, 31 Aug 2013 11:31:13 +0300, Ferrous Cranus wrote: > Here is the code inside files.py: The code you show is not the ENTIRE code inside of files.py, is it? You are only showing us a small piece, correct? The code you show: > try: > gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat') > city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or > gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) > host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] ) or > socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] ) or > os.environ['REMOTE_ADDR'] > except Exception as e: > print( repr(e), file=open( '/tmp/err.out', 'w' ) ) does not contain a call to cur.execute. And here is your error: > [error] [client 108.162.229.116] Traceback (most recent call last):, > referer: http://superhost.gr/ [Sat Aug 31 08:29:33 2013] [error] [client > 108.162.229.116] File "/home/nikos/public_html/cgi-bin/files.py", line > 135, in , referer: http://superhost.gr/ > [Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116] > cur.execute('''INSERT INTO files (url, host, city, lastvisit) VALUES > (%s, %s, %s, %s)''', (filename, host, city, lastvisit) ), referer: > http://superhost.gr/ > [Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116] NameError: > name 'host' is not defined, referer: http://superhost.gr/ Extracting out the critical part: line 135 cur.execute('''INSERT INTO files (url, host, city, lastvisit) VALUES (%s, %s, %s, %s)''', (filename, host, city, lastvisit) ), Where is the call to cur.execute in the code snippet you show above? > But 'host' defaults to an ip address if it cannot resolve the hostname. > Why the errro says its undefined? Because it is is undefined. Python is not lying to you. If Python tells you there is an error, BELIEVE IT. Resolving the hostname is irrelevant. print(host) ### ERROR OCCURS HERE BECAUSE HOST IS UNDEFINED ### host = socket.gethostbyaddr(addr) The second line is too late, the error has already occurred. -- Steven From nikos at superhost.gr Sat Aug 31 08:58:11 2013 From: nikos at superhost.gr (Ferrous Cranus) Date: Sat, 31 Aug 2013 15:58:11 +0300 Subject: UnicodeDecodeError issue In-Reply-To: <5221d373$0$6599$c3e8da3$5496439d@news.astraweb.com> References: <5221d373$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: ???? 31/8/2013 2:28 ??, ?/? Steven D'Aprano ??????: > On Sat, 31 Aug 2013 11:31:13 +0300, Ferrous Cranus wrote: > >> Here is the code inside files.py: > > The code you show is not the ENTIRE code inside of files.py, is it? You > are only showing us a small piece, correct? > > The code you show: > >> try: >> gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat') >> city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or >> gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) >> host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] ) or >> socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] ) or >> os.environ['REMOTE_ADDR'] >> except Exception as e: >> print( repr(e), file=open( '/tmp/err.out', 'w' ) ) > > > does not contain a call to cur.execute. And here is your error: > > >> [error] [client 108.162.229.116] Traceback (most recent call last):, >> referer: http://superhost.gr/ [Sat Aug 31 08:29:33 2013] [error] [client >> 108.162.229.116] File "/home/nikos/public_html/cgi-bin/files.py", line >> 135, in , referer: http://superhost.gr/ >> [Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116] >> cur.execute('''INSERT INTO files (url, host, city, lastvisit) VALUES >> (%s, %s, %s, %s)''', (filename, host, city, lastvisit) ), referer: >> http://superhost.gr/ >> [Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116] NameError: >> name 'host' is not defined, referer: http://superhost.gr/ > > > Extracting out the critical part: > > line 135 > cur.execute('''INSERT INTO files (url, host, city, lastvisit) VALUES > (%s, %s, %s, %s)''', (filename, host, city, lastvisit) ), > > Where is the call to cur.execute in the code snippet you show above? > > > >> But 'host' defaults to an ip address if it cannot resolve the hostname. >> Why the errro says its undefined? > > Because it is is undefined. Python is not lying to you. If Python tells > you there is an error, BELIEVE IT. Resolving the hostname is irrelevant. > > print(host) ### ERROR OCCURS HERE BECAUSE HOST IS UNDEFINED ### > host = socket.gethostbyaddr(addr) I'm not saying Python is lying of course it does not. But how is this possible since: host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] ) or socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] ) or os.environ['REMOTE_ADDR'] it must have a value by defaulting to something. The cur.execute fails because it make use of 'host' which is undefined. # Try to insert the file into the database cur.execute('''INSERT INTO files (url, host, city, lastvisit) VALUES (%s, %s, %s, %s)''', (filename, host, city, lastvisit) ) And the question remain as to why 'host' is undefined. -- Webhost From nikos at superhost.gr Sat Aug 31 09:07:01 2013 From: nikos at superhost.gr (Ferrous Cranus) Date: Sat, 31 Aug 2013 16:07:01 +0300 Subject: UnicodeDecodeError issue In-Reply-To: References: <5221d373$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: ???? 31/8/2013 3:58 ??, ?/? Ferrous Cranus ??????: > ???? 31/8/2013 2:28 ??, ?/? Steven D'Aprano ??????: >> On Sat, 31 Aug 2013 11:31:13 +0300, Ferrous Cranus wrote: >> >>> Here is the code inside files.py: >> >> The code you show is not the ENTIRE code inside of files.py, is it? You >> are only showing us a small piece, correct? >> >> The code you show: >> >>> try: >>> gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat') >>> city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or >>> gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] ) >>> host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] ) or >>> socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] ) or >>> os.environ['REMOTE_ADDR'] >>> except Exception as e: >>> print( repr(e), file=open( '/tmp/err.out', 'w' ) ) >> >> >> does not contain a call to cur.execute. And here is your error: >> >> >>> [error] [client 108.162.229.116] Traceback (most recent call last):, >>> referer: http://superhost.gr/ [Sat Aug 31 08:29:33 2013] [error] [client >>> 108.162.229.116] File "/home/nikos/public_html/cgi-bin/files.py", line >>> 135, in , referer: http://superhost.gr/ >>> [Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116] >>> cur.execute('''INSERT INTO files (url, host, city, lastvisit) VALUES >>> (%s, %s, %s, %s)''', (filename, host, city, lastvisit) ), referer: >>> http://superhost.gr/ >>> [Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116] NameError: >>> name 'host' is not defined, referer: http://superhost.gr/ >> >> >> Extracting out the critical part: >> >> line 135 >> cur.execute('''INSERT INTO files (url, host, city, lastvisit) VALUES >> (%s, %s, %s, %s)''', (filename, host, city, lastvisit) ), >> >> Where is the call to cur.execute in the code snippet you show above? >> >> >> >>> But 'host' defaults to an ip address if it cannot resolve the hostname. >>> Why the errro says its undefined? >> >> Because it is is undefined. Python is not lying to you. If Python tells >> you there is an error, BELIEVE IT. Resolving the hostname is irrelevant. >> >> print(host) ### ERROR OCCURS HERE BECAUSE HOST IS UNDEFINED ### >> host = socket.gethostbyaddr(addr) > I'm not saying Python is lying of course it does not. > > But how is this possible since: > > > host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] ) or > socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] ) or > os.environ['REMOTE_ADDR'] > > > it must have a value by defaulting to something. > > The cur.execute fails because it make use of 'host' which is undefined. > > # Try to insert the file into the database > cur.execute('''INSERT INTO files (url, host, city, lastvisit) > VALUES (%s, %s, %s, %s)''', (filename, host, city, lastvisit) ) > > > And the question remain as to why 'host' is undefined. Thsi fails too: host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or "Unkknown host" -- Webhost From steve+comp.lang.python at pearwood.info Sat Aug 31 11:44:34 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 31 Aug 2013 15:44:34 GMT Subject: UnicodeDecodeError issue References: <5221d373$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <52220f61$0$6599$c3e8da3$5496439d@news.astraweb.com> On Sat, 31 Aug 2013 15:58:11 +0300, Ferrous Cranus wrote: Failure is here, line 135: >> cur.execute('''INSERT INTO files (url, host, city, lastvisit) VALUES >> (%s, %s, %s, %s)''', (filename, host, city, lastvisit) ), [...] > But how is this possible since: > > > host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] ) or > socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] ) or > os.environ['REMOTE_ADDR'] What's the line number of that line of code? My guess is that it is AFTER line 135, which is where the error occurs. Or possibly it is inside a function that hasn't been called. Here is the same failure: print(host) host = "this is too late" Here is the same failure again: if 0: host = "this never gets called" print(host) Here is the same failure again: data = [] for x in data: host = "This never happens" print(host) And again, a trickier one this time: host = "something" del host # and now it is gone print(host) And again: def func(): global host print(host) func() host = "Too late, the error has already occurred" One last one: def set_host(): host = "This is a local variable" set_host() print(host) Study all these examples. Now read your own code. host has not been defined at the time the cur.execute line is reached. You have to work out which of my examples best matches your code. > it must have a value by defaulting to something. No, there is no default value for variables. How long have you been programming in Python? Six months? A year? > The cur.execute fails because it make use of 'host' which is undefined. Correct. > # Try to insert the file into the database cur.execute ('''INSERT INTO > files (url, host, city, lastvisit) VALUES > (%s, %s, %s, %s)''', (filename, host, city, lastvisit) ) > > > And the question remain as to why 'host' is undefined. Because you haven't defined it BEFORE you try to use it. There is no point defining it AFTER you use it, the error has already occurred. You have to make the coffee before you drink it: # This fails too drink(coffee) coffee = make_coffee() # This doesn't help at all drink(coffee) coffee = make_coffee() or prepare_coffee() # This is also useless drink(coffee) coffee = make_coffee() or prepare_coffee() or "coffee" # But this works coffee = make_coffee() drink(coffee) -- Steven From candide at free.invalid Sat Aug 31 04:17:23 2013 From: candide at free.invalid (candide) Date: Sat, 31 Aug 2013 10:17:23 +0200 Subject: print function and unwanted trailing space Message-ID: <5221a693$0$2059$426a74cc@news.free.fr> What is the equivalent in Python 3 to the following Python 2 code: # ----------------------------- for i in range(5): print i, # ----------------------------- ? Be careful that the above code doesn't add a trailing space after the last number in the list, hence the following Python 3 code isn't strictly equivalent: # ----------------------------- for i in range(5): print(i, end=' ') # <- The last ' ' is unwanted print() # ----------------------------- From andipersti at gmail.com Sat Aug 31 04:43:55 2013 From: andipersti at gmail.com (Andreas Perstinger) Date: Sat, 31 Aug 2013 10:43:55 +0200 Subject: print function and unwanted trailing space In-Reply-To: <5221a693$0$2059$426a74cc@news.free.fr> References: <5221a693$0$2059$426a74cc@news.free.fr> Message-ID: <5221ACCB.4030608@gmail.com> On 31.08.2013 10:17, candide wrote: > > What is the equivalent in Python 3 to the following Python 2 code: > > # ----------------------------- > for i in range(5): > print i, > # ----------------------------- > > ? How about >>> print(" ".join(str(i) for i in range(5))) 0 1 2 3 4 Bye, Andreas From candide at free.invalid Sat Aug 31 05:25:31 2013 From: candide at free.invalid (candide) Date: Sat, 31 Aug 2013 11:25:31 +0200 Subject: print function and unwanted trailing space In-Reply-To: References: <5221a693$0$2059$426a74cc@news.free.fr> Message-ID: <5221b68b$0$2280$426a74cc@news.free.fr> Le 31/08/2013 10:43, Andreas Perstinger a ?crit : > How about > > >>> print(" ".join(str(i) for i in range(5))) > 0 1 2 3 4 > Thanks for your answer. The output is stricly the same but the code doesn't suit my needs : 1) I'm porting to Python 3 a Python 2 full beginner course : the learners are not aware of the join method nor the str type nor generators stuff; 2) Your code introduce a (sometimes) useless conversion to str (consider a string instead of range(5)). From __peter__ at web.de Sat Aug 31 06:31:32 2013 From: __peter__ at web.de (Peter Otten) Date: Sat, 31 Aug 2013 12:31:32 +0200 Subject: print function and unwanted trailing space References: <5221a693$0$2059$426a74cc@news.free.fr> <5221b68b$0$2280$426a74cc@news.free.fr> Message-ID: candide wrote: > Le 31/08/2013 10:43, Andreas Perstinger a ?crit : > > > How about > > > > >>> print(" ".join(str(i) for i in range(5))) > > 0 1 2 3 4 > > > > > Thanks for your answer. The output is stricly the same but the code > doesn't suit my needs : > > 1) I'm porting to Python 3 a Python 2 full beginner course : the > learners are not aware of the join method nor the str type nor > generators stuff; > 2) Your code introduce a (sometimes) useless conversion to str (consider > a string instead of range(5)). You are out of luck, the softspace mechanism, roughly softspace = False for i in range(5): if softspace: print(end=" ") print(i, end="") softspace = True print() with `softspace` saved as a file attribute, is gone in Python3. But I don't think that someone who doesn't know it existed will miss the feature. Maybe you can allow for /some/ magic and introduce your students to print(*range(5)) early-on. From candide at free.invalid Sat Aug 31 09:33:35 2013 From: candide at free.invalid (candide) Date: Sat, 31 Aug 2013 15:33:35 +0200 Subject: print function and unwanted trailing space In-Reply-To: References: <5221a693$0$2059$426a74cc@news.free.fr> <5221b68b$0$2280$426a74cc@news.free.fr> Message-ID: <5221f0af$0$2412$426a34cc@news.free.fr> Le 31/08/2013 12:31, Peter Otten a ?crit : > softspace = False > for i in range(5): > if softspace: > print(end=" ") > print(i, end="") > softspace = True > print() The if instruction imposes useless testing (we know in advance the problem to occur at the very end of the loop) and useless writing (writing ''). The following is clearer # ------------------------- n=5 for i in range(n-1): print(i, end=' ') print(n-1) # ------------------------- but doesn't solve all the cases (imagine a string or an iterator). From __peter__ at web.de Sat Aug 31 09:59:21 2013 From: __peter__ at web.de (Peter Otten) Date: Sat, 31 Aug 2013 15:59:21 +0200 Subject: print function and unwanted trailing space References: <5221a693$0$2059$426a74cc@news.free.fr> <5221b68b$0$2280$426a74cc@news.free.fr> <5221f0af$0$2412$426a34cc@news.free.fr> Message-ID: candide wrote: > Le 31/08/2013 12:31, Peter Otten a ?crit : > > softspace = False > > for i in range(5): > > if softspace: > > print(end=" ") > > print(i, end="") > > softspace = True > > print() > > > The if instruction imposes useless testing (we know in advance the > problem to occur at the very end of the loop) and useless writing > (writing ''). To make it crystal clear, the above was to illustrate the algorithm used in Python 2, not a suggestion. Python 2 uses that "useless testing" -- which is cheap compared to actual I/O. > The following is clearer > > # ------------------------- > n=5 > for i in range(n-1): > print(i, end=' ') > print(n-1) > # ------------------------- > > > but doesn't solve all the cases (imagine a string or an iterator). I still think you should live with a trailing space or go with my actual suggestion print(*range(5)) From rosuav at gmail.com Sat Aug 31 11:30:48 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 1 Sep 2013 01:30:48 +1000 Subject: print function and unwanted trailing space In-Reply-To: <5221f0af$0$2412$426a34cc@news.free.fr> References: <5221a693$0$2059$426a74cc@news.free.fr> <5221b68b$0$2280$426a74cc@news.free.fr> <5221f0af$0$2412$426a34cc@news.free.fr> Message-ID: On Sat, Aug 31, 2013 at 11:33 PM, candide wrote: > The if instruction imposes useless testing (we know in advance the problem > to occur at the very end of the loop) and useless writing (writing ''). > > The following is clearer > > # ------------------------- > n=5 > for i in range(n-1): > print(i, end=' ') > print(n-1) > # ------------------------- > > > but doesn't solve all the cases (imagine a string or an iterator). Similar but maybe simpler, and copes with more arbitrary iterables: it=iter(range(5)) print(next(it), end='') for i in it: print('',i, end='') Also guarantees to use 'sep' between the elements, fwiw. ChrisA From oscar.j.benjamin at gmail.com Sat Aug 31 11:43:59 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Sat, 31 Aug 2013 16:43:59 +0100 Subject: print function and unwanted trailing space In-Reply-To: References: <5221a693$0$2059$426a74cc@news.free.fr> <5221b68b$0$2280$426a74cc@news.free.fr> <5221f0af$0$2412$426a34cc@news.free.fr> Message-ID: On 31 August 2013 16:30, Chris Angelico wrote: >> >> but doesn't solve all the cases (imagine a string or an iterator). > > Similar but maybe simpler, and copes with more arbitrary iterables: > > it=iter(range(5)) > print(next(it), end='') > for i in it: > print('',i, end='') If you want to work with arbitrary iterables then you'll want it = iter(iterable) try: val = next(it) except StopIteration: pass # Or raise or something? else: print(val, end='') for i in it: print('', i, end='') Oscar From rosuav at gmail.com Sat Aug 31 18:08:04 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 1 Sep 2013 08:08:04 +1000 Subject: print function and unwanted trailing space In-Reply-To: References: <5221a693$0$2059$426a74cc@news.free.fr> <5221b68b$0$2280$426a74cc@news.free.fr> <5221f0af$0$2412$426a34cc@news.free.fr> Message-ID: On Sun, Sep 1, 2013 at 1:43 AM, Oscar Benjamin wrote: > On 31 August 2013 16:30, Chris Angelico wrote: >>> >>> but doesn't solve all the cases (imagine a string or an iterator). >> >> Similar but maybe simpler, and copes with more arbitrary iterables: >> >> it=iter(range(5)) >> print(next(it), end='') >> for i in it: >> print('',i, end='') > > If you want to work with arbitrary iterables then you'll want > > it = iter(iterable) > try: > val = next(it) > except StopIteration: > pass # Or raise or something? > else: > print(val, end='') > for i in it: > print('', i, end='') I went with this version: except StopIteration: raise In other words, if it's going to bomb, let it bomb :) ChrisA From joshua at landau.ws Sat Aug 31 19:15:22 2013 From: joshua at landau.ws (Joshua Landau) Date: Sun, 1 Sep 2013 00:15:22 +0100 Subject: print function and unwanted trailing space In-Reply-To: References: <5221a693$0$2059$426a74cc@news.free.fr> <5221b68b$0$2280$426a74cc@news.free.fr> <5221f0af$0$2412$426a34cc@news.free.fr> Message-ID: On 31 August 2013 23:08, Chris Angelico wrote: > On Sun, Sep 1, 2013 at 1:43 AM, Oscar Benjamin > wrote: >> On 31 August 2013 16:30, Chris Angelico wrote: >>>> >>>> but doesn't solve all the cases (imagine a string or an iterator). >>> >>> Similar but maybe simpler, and copes with more arbitrary iterables: >>> >>> it=iter(range(5)) >>> print(next(it), end='') >>> for i in it: >>> print('',i, end='') >> >> If you want to work with arbitrary iterables then you'll want >> >> it = iter(iterable) >> try: >> val = next(it) >> except StopIteration: >> pass # Or raise or something? >> else: >> print(val, end='') >> for i in it: >> print('', i, end='') > > I went with this version: > > except StopIteration: > raise > > In other words, if it's going to bomb, let it bomb :) I think the point is that StopIteration is an unsafe error to raise. From tjreedy at udel.edu Sat Aug 31 19:57:44 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 31 Aug 2013 19:57:44 -0400 Subject: print function and unwanted trailing space In-Reply-To: References: <5221a693$0$2059$426a74cc@news.free.fr> <5221b68b$0$2280$426a74cc@news.free.fr> <5221f0af$0$2412$426a34cc@news.free.fr> Message-ID: On 8/31/2013 7:15 PM, Joshua Landau wrote: > On 31 August 2013 23:08, Chris Angelico wrote: >> On Sun, Sep 1, 2013 at 1:43 AM, Oscar Benjamin >> wrote: >>> On 31 August 2013 16:30, Chris Angelico wrote: >>>>> >>>>> but doesn't solve all the cases (imagine a string or an iterator). >>>> >>>> Similar but maybe simpler, and copes with more arbitrary iterables: >>>> >>>> it=iter(range(5)) >>>> print(next(it), end='') >>>> for i in it: >>>> print('',i, end='') >>> >>> If you want to work with arbitrary iterables then you'll want >>> >>> it = iter(iterable) >>> try: >>> val = next(it) >>> except StopIteration: >>> pass # Or raise or something? >>> else: >>> print(val, end='') >>> for i in it: >>> print('', i, end='') >> >> I went with this version: >> >> except StopIteration: >> raise >> >> In other words, if it's going to bomb, let it bomb :) > > I think the point is that StopIteration is an unsafe error to raise. It should only be raised by iterator.__next__ method and caught by iterator user and not re-raised. Raise something like "ValueError('empty iterable') from None" instead. -- Terry Jan Reedy From candide at free.invalid Sat Aug 31 11:58:54 2013 From: candide at free.invalid (candide) Date: Sat, 31 Aug 2013 17:58:54 +0200 Subject: print function and unwanted trailing space In-Reply-To: References: <5221a693$0$2059$426a74cc@news.free.fr> <5221b68b$0$2280$426a74cc@news.free.fr> <5221f0af$0$2412$426a34cc@news.free.fr> Message-ID: <522212bf$0$2323$426a74cc@news.free.fr> Le 31/08/2013 15:59, Peter Otten a ?crit : > > To make it crystal clear, the above was to illustrate the algorithm used in > Python 2, not a suggestion. Ok sorry, I misinterpreted. > I still think you should live with a trailing space Are you sure ? The following code #---------------------------------- import io output = io.StringIO() n=5 for i in range(n-1): print(i, end=' ', file=output) print(n-1, file=output) print(output.getvalue().count(' ')) #---------------------------------- outputs 4, the correct number of space character. > or go with my actual > suggestion > > print(*range(5)) > It's a very good suggestion (the best one in fact) but rather complicated to explain to pure novices in programming. From candide at free.invalid Sat Aug 31 11:51:19 2013 From: candide at free.invalid (candide) Date: Sat, 31 Aug 2013 17:51:19 +0200 Subject: print function and unwanted trailing space In-Reply-To: References: <5221a693$0$2059$426a74cc@news.free.fr> <5221b68b$0$2280$426a74cc@news.free.fr> Message-ID: <522210f8$0$13987$426a74cc@news.free.fr> Le 31/08/2013 12:31, Peter Otten a ?crit : > with `softspace` saved as a file attribute, is gone in Python3. After reading http://docs.python.org/3.0/whatsnew/3.0.html#print-is-a-function I understand what you meant by "softspace". Thanks. From steve+comp.lang.python at pearwood.info Sat Aug 31 07:16:33 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 31 Aug 2013 11:16:33 GMT Subject: print function and unwanted trailing space References: <5221a693$0$2059$426a74cc@news.free.fr> Message-ID: <5221d090$0$6599$c3e8da3$5496439d@news.astraweb.com> On Sat, 31 Aug 2013 10:17:23 +0200, candide wrote: > What is the equivalent in Python 3 to the following Python 2 code: > > # ----------------------------- > for i in range(5): > print i, > # ----------------------------- > > ? > > Be careful that the above code doesn't add a trailing space after the > last number in the list, Of course it does. Have you actually tried it? The interactive interpreter is tricky, because you cannot directly follow a for-loop with another statement. If you try, the interactive interpreter gives you an indentation error. But we can work around it by sticking everything inside an if block, like so: py> if True: ... for i in range(5): ... print i, ... # could be pages of code here ... print "FINISHED" ... 0 1 2 3 4 FINISHED Or you could stick the code inside an exec, which doesn't have the same limitation as the interactive interpreter. This mimics the behaviour of code in a file: py> exec """for i in range(5): ... print i, ... print "FINISHED" ... """ 0 1 2 3 4 FINISHED The same results occur with any other Python 2.x, and indeed all the way back to Python 1.5 and older. > hence the following Python 3 code isn't strictly equivalent: > > > # ----------------------------- > for i in range(5): > print(i, end=' ') # <- The last ' ' is unwanted > print() The last space is exactly the same as you get in Python 2. But really, who cares about an extra invisible space? In non-interactive mode, the two are exactly the same (ignoring the extra print() call outside the loop), but even at the interactive interpreter, I'd like to see the code where an extra space makes a real difference. -- Steven From __peter__ at web.de Sat Aug 31 08:27:39 2013 From: __peter__ at web.de (Peter Otten) Date: Sat, 31 Aug 2013 14:27:39 +0200 Subject: print function and unwanted trailing space References: <5221a693$0$2059$426a74cc@news.free.fr> <5221d090$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Sat, 31 Aug 2013 10:17:23 +0200, candide wrote: > >> What is the equivalent in Python 3 to the following Python 2 code: >> >> # ----------------------------- >> for i in range(5): >> print i, >> # ----------------------------- >> >> ? >> >> Be careful that the above code doesn't add a trailing space after the >> last number in the list, > > Of course it does. Have you actually tried it? The interactive > interpreter is tricky, because you cannot directly follow a for-loop with > another statement. If you try, the interactive interpreter gives you an > indentation error. But we can work around it by sticking everything > inside an if block, like so: > > py> if True: > ... for i in range(5): > ... print i, > ... # could be pages of code here > ... print "FINISHED" > ... > 0 1 2 3 4 FINISHED > > > Or you could stick the code inside an exec, which doesn't have the same > limitation as the interactive interpreter. This mimics the behaviour of > code in a file: > > py> exec """for i in range(5): > ... print i, > ... print "FINISHED" > ... """ > 0 1 2 3 4 FINISHED > > > The same results occur with any other Python 2.x, and indeed all the way > back to Python 1.5 and older. Your test is flawed. The softspace mechanism ensures that there is a space *between* all printed items, but not *after* the last printed item. print "FINISHED" will add a space while print will not. Compare: >>> with open("tmp.txt", "w") as f: ... for i in range(3): print >> f, i, ... print >> f ... >>> open("tmp.txt").read() '0 1 2\n' >>> with open("tmp.txt", "w") as f: ... for i in range(3): print >> f, i, ... print >> f, "FINISHED" ... >>> open("tmp.txt").read() '0 1 2 FINISHED\n' From oscar.j.benjamin at gmail.com Sat Aug 31 08:37:58 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Sat, 31 Aug 2013 13:37:58 +0100 Subject: print function and unwanted trailing space In-Reply-To: <5221d090$0$6599$c3e8da3$5496439d@news.astraweb.com> References: <5221a693$0$2059$426a74cc@news.free.fr> <5221d090$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 31 August 2013 12:16, Steven D'Aprano wrote: > On Sat, 31 Aug 2013 10:17:23 +0200, candide wrote: > >> What is the equivalent in Python 3 to the following Python 2 code: >> >> # ----------------------------- >> for i in range(5): >> print i, >> # ----------------------------- >> >> ? >> >> Be careful that the above code doesn't add a trailing space after the >> last number in the list, > > Of course it does. Have you actually tried it? The interactive > interpreter is tricky, because you cannot directly follow a for-loop with > another statement. If you try, the interactive interpreter gives you an > indentation error. But we can work around it by sticking everything > inside an if block, like so: > > py> if True: > ... for i in range(5): > ... print i, > ... # could be pages of code here > ... print "FINISHED" > ... > 0 1 2 3 4 FINISHED The space is added by the final print statement, not the last one in the loop. Here's a demo that shows this: $ cat print.py for i in range(5): print i, print $ cat print3.py for i in range(5): print(i, end=' ') print() $ py -2.7 print.py | cat -A 0 1 2 3 4^M$ $ py -3.3 print3.py | cat -A 0 1 2 3 4 ^M$ (Notice the space between the 4 and ^M (which is cat's way of saying '\r'). [snip] > >> hence the following Python 3 code isn't strictly equivalent: >> >> >> # ----------------------------- >> for i in range(5): >> print(i, end=' ') # <- The last ' ' is unwanted >> print() > > The last space is exactly the same as you get in Python 2. But really, > who cares about an extra invisible space? In non-interactive mode, the > two are exactly the same (ignoring the extra print() call outside the > loop), but even at the interactive interpreter, I'd like to see the code > where an extra space makes a real difference. I seem to remember it breaking some unit test or doc test something when I first tried to port something using 2to3 (this is the replacement that 2to3 uses for print with a trailing comma). It's not so important for interactive terminal output but when you do 'python script.py > output.dat' the unwanted space shouldn't be there. The soft-space feature is useful but stateful as Peter says and defies the normal concept of what happens when calling a function so it was removed when print became a function. Oscar From candide at free.invalid Sat Aug 31 08:59:17 2013 From: candide at free.invalid (candide) Date: Sat, 31 Aug 2013 14:59:17 +0200 Subject: print function and unwanted trailing space In-Reply-To: <5221d090$0$6599$c3e8da3$5496439d@news.astraweb.com> References: <5221a693$0$2059$426a74cc@news.free.fr> <5221d090$0$6599$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5221e8a5$0$2105$426a74cc@news.free.fr> Le 31/08/2013 13:16, Steven D'Aprano a ?crit : > > Of course it does. Have you actually tried it? Of course I did, redirecting the output to a file in order to spot an eventually trailing space. I did the same for the Python 3 code. From steve+comp.lang.python at pearwood.info Sat Aug 31 11:18:02 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 31 Aug 2013 15:18:02 GMT Subject: print function and unwanted trailing space References: <5221a693$0$2059$426a74cc@news.free.fr> <5221d090$0$6599$c3e8da3$5496439d@news.astraweb.com> <5221e8a5$0$2105$426a74cc@news.free.fr> Message-ID: <5222092a$0$6599$c3e8da3$5496439d@news.astraweb.com> On Sat, 31 Aug 2013 14:59:17 +0200, candide wrote: > Le 31/08/2013 13:16, Steven D'Aprano a ?crit : > > >> Of course it does. Have you actually tried it? > > > Of course I did, redirecting the output to a file in order to spot an > eventually trailing space. I did the same for the Python 3 code. Fair enough. Seems like the print statement implementation in Python 2 is uglier than I imagined, keeping hidden state between invocations. -- Steven From ned at nedbatchelder.com Sat Aug 31 07:24:54 2013 From: ned at nedbatchelder.com (Ned Batchelder) Date: Sat, 31 Aug 2013 07:24:54 -0400 Subject: print function and unwanted trailing space In-Reply-To: <5221a693$0$2059$426a74cc@news.free.fr> References: <5221a693$0$2059$426a74cc@news.free.fr> Message-ID: <5221D286.3020202@nedbatchelder.com> On 8/31/13 4:17 AM, candide wrote: > > What is the equivalent in Python 3 to the following Python 2 code: > > # ----------------------------- > for i in range(5): > print i, > # ----------------------------- > > ? > > Be careful that the above code doesn't add a trailing space after the > last number in the list, hence the following Python 3 code isn't > strictly equivalent: > > > # ----------------------------- > for i in range(5): > print(i, end=' ') # <- The last ' ' is unwanted > print() > # ----------------------------- For a beginner course, the trailing space is fine, use this code. They'll never notice the trailing space (I wouldn't have!) and to explain why the comma leaves off the last one takes a really advanced understanding of obscure details anyway. --Ned. From candide at free.invalid Sat Aug 31 09:51:15 2013 From: candide at free.invalid (candide) Date: Sat, 31 Aug 2013 15:51:15 +0200 Subject: print function and unwanted trailing space In-Reply-To: References: <5221a693$0$2059$426a74cc@news.free.fr> Message-ID: <5221f4d3$0$2051$426a74cc@news.free.fr> Le 31/08/2013 13:24, Ned Batchelder a ?crit : > For a beginner course, the trailing space is fine, use this code. I was really expecting there was a trick but I'll follow your advice, after all the trailing space is invisible! Nevertheless, this can be quite annoying. For instance, some automated program testing (cf. http://www.spoj.com/, http://acm.timus.ru/, etc) expect the exact output to get accepted, no byte more or a byte less. From cantinaderecuerdos at yahoo.com Sat Aug 31 04:38:02 2013 From: cantinaderecuerdos at yahoo.com (John Smith) Date: Sat, 31 Aug 2013 16:38:02 +0800 (SGT) Subject: MySQLdb Problem Message-ID: <1377938282.78528.YahooMailBasic@web194904.mail.sg3.yahoo.com> Hi; Since there is no list for MySQLdb, I'm hoping you can help me. I have installed, de-installed and reinstalled this s/w and MySQL itself on my Win8 box. However, when I go to use it from a script, I get the following error: "C:\Python27\lib\site-packages\MySQLdb\__init.py__", line 27 in import _mysql ImportError: DLL load failed: %1 is not valid Win32 application. Well, I'm running Win8 which is 64-bit, but I'm pretty sure it runs 32 as well, right? Python 27, MySQL 5.5. TIA, John From rosuav at gmail.com Sat Aug 31 11:18:51 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 1 Sep 2013 01:18:51 +1000 Subject: MySQLdb Problem In-Reply-To: <1377938282.78528.YahooMailBasic@web194904.mail.sg3.yahoo.com> References: <1377938282.78528.YahooMailBasic@web194904.mail.sg3.yahoo.com> Message-ID: On Sat, Aug 31, 2013 at 6:38 PM, John Smith wrote: > Hi; > Since there is no list for MySQLdb, I'm hoping you can help me. I have installed, de-installed and reinstalled this s/w and MySQL itself on my Win8 box. However, when I go to use it from a script, I get the following error: > > "C:\Python27\lib\site-packages\MySQLdb\__init.py__", line 27 in > import _mysql > > ImportError: DLL load failed: %1 is not valid Win32 application. > > Well, I'm running Win8 which is 64-bit, but I'm pretty sure it runs 32 as well, right? Python 27, MySQL 5.5. Do your Python and your MySQLdb match? I haven't confirmed, but I'm pretty sure you'll have problems if you have a 32-bit Python with a 64-bit mysqldb DLL, or vice versa. ChrisA From cantinaderecuerdos at yahoo.com Sat Aug 31 18:17:18 2013 From: cantinaderecuerdos at yahoo.com (John Smith) Date: Sun, 1 Sep 2013 06:17:18 +0800 (SGT) Subject: MySQLdb Problem In-Reply-To: Message-ID: <1377987438.23663.YahooMailBasic@web194906.mail.sg3.yahoo.com> -------------------------------------------- On Sat, 31/8/13, Chris Angelico wrote: Subject: Re: MySQLdb Problem To: python-list at python.org Date: Saturday, 31 August, 2013, 4:18 PM > Do your Python and your MySQLdb match? I haven't confirmed, > but I'm > pretty sure you'll have problems if you have a 32-bit Python > with a > 64-bit mysqldb DLL, or vice versa. Yeah, that turned out to be the problem. What's strange is that it worked before. Thanks, John From rosuav at gmail.com Sat Aug 31 18:20:07 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 1 Sep 2013 08:20:07 +1000 Subject: MySQLdb Problem In-Reply-To: <1377987438.23663.YahooMailBasic@web194906.mail.sg3.yahoo.com> References: <1377987438.23663.YahooMailBasic@web194906.mail.sg3.yahoo.com> Message-ID: On Sun, Sep 1, 2013 at 8:17 AM, John Smith wrote: > > -------------------------------------------- > On Sat, 31/8/13, Chris Angelico wrote: > > Subject: Re: MySQLdb Problem > To: python-list at python.org > Date: Saturday, 31 August, 2013, 4:18 PM > >> Do your Python and your MySQLdb match? I haven't confirmed, >> but I'm >> pretty sure you'll have problems if you have a 32-bit Python >> with a >> 64-bit mysqldb DLL, or vice versa. > > Yeah, that turned out to be the problem. What's strange is that it worked before. Possibly you upgraded to a new version of Python (even from 2.7.3 to 2.7.5 or something, which wouldn't otherwise break stuff) and downloaded the other form of executable? Anyway, glad it's sorted. ChrisA From sergio7654 at gmail.com Sat Aug 31 10:24:48 2013 From: sergio7654 at gmail.com (sergio7654 at gmail.com) Date: Sat, 31 Aug 2013 07:24:48 -0700 (PDT) Subject: HMAC encription issue Message-ID: <3479e08e-d435-492b-b2a0-a1c18678f6ab@googlegroups.com> Hi, I need to create a HMAC in Python just the way the following Javascript code does: var credentials = "admin:amin"; key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A3035" var shaObj = new jsSHA(credentials, "ASCII"); var hash = shaObj.getHMAC(key, "HEX", "HEX"); ---- in this case the hash is: 8347ce7126c1068aa183fbfdafe2c17a9cc86734 This javascript library can be tested here: http://caligatio.github.io/jsSHA/ So I'm trying to do the same in Python by: key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A303$ credentials = "admin:admin" hash = hmac.new(key, credentials, hashlib.sha1).hexdigest() ----- which gives me hash= 2c7e849a0eaa8cd0cc1120f6f156913755b674b6 Something's is wrong obviously. Maybe it has probably something to do with the encoding of the key or credentials but I'm lost on this. I tried to convert key to hex using the following function def toHex(s): lst = [] for ch in s: hv = hex(ord(ch)).replace('0x', '') if len(hv) == 1: hv = '0'+hv lst.append(hv) return reduce(lambda x,y:x+y, lst) But this didn't work either. Any help would be appreciated! From roy at panix.com Sat Aug 31 10:39:08 2013 From: roy at panix.com (Roy Smith) Date: Sat, 31 Aug 2013 10:39:08 -0400 Subject: HMAC encription issue References: <3479e08e-d435-492b-b2a0-a1c18678f6ab@googlegroups.com> Message-ID: In article <3479e08e-d435-492b-b2a0-a1c18678f6ab at googlegroups.com>, sergio7654 at gmail.com wrote: > key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A3035" > key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A303$ To start with, your keys are not the same (the JS one ends in "35", the Python one ends in "3$", and I don't know what to make of the fact that your Python key isn't terminated with a quote. From sergio7654 at gmail.com Sat Aug 31 10:48:53 2013 From: sergio7654 at gmail.com (Sergio Sanchez) Date: Sat, 31 Aug 2013 07:48:53 -0700 (PDT) Subject: HMAC encription issue In-Reply-To: References: <3479e08e-d435-492b-b2a0-a1c18678f6ab@googlegroups.com> Message-ID: El s?bado, 31 de agosto de 2013 16:39:08 UTC+2, Roy Smith escribi?: > In article <3479e08e-d435-492b-b2a0-a1c18678f6ab at googlegroups.com>, > > sergio7654 at gmail.com wrote: > > > > > key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A3035" > > > key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A303$ > > > > To start with, your keys are not the same (the JS one ends in "35", the Python one ends in "3$", > > and I don't know what to make of the fact that your Python key isn't terminated with a quote. Sorry, that was a problem with the cut & paste. The two sentences key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A3035" are just the same in Python and Javascript. From roy at panix.com Sat Aug 31 11:01:01 2013 From: roy at panix.com (Roy Smith) Date: Sat, 31 Aug 2013 11:01:01 -0400 Subject: HMAC encription issue References: <3479e08e-d435-492b-b2a0-a1c18678f6ab@googlegroups.com> Message-ID: In article , Sergio Sanchez wrote: > > To start with, your keys are not the same > > Sorry, that was a problem with the cut & paste. And your credentials are different too: var credentials = "admin:amin"; credentials = "admin:admin" Is this also a cut & paste error? Please, post EXACTlY the code you are actually running. Otherwise, it really is impossible for anybody to figure out what you're doing wrong. This is double ultra especially true with crypto, since small differences in the inputs result (by design) in huge differences in the output. From sergio7654 at gmail.com Sat Aug 31 11:16:24 2013 From: sergio7654 at gmail.com (Sergio Sanchez) Date: Sat, 31 Aug 2013 08:16:24 -0700 (PDT) Subject: HMAC encription issue In-Reply-To: References: <3479e08e-d435-492b-b2a0-a1c18678f6ab@googlegroups.com> Message-ID: El s?bado, 31 de agosto de 2013 16:48:53 UTC+2, Sergio Sanchez escribi?: > El s?bado, 31 de agosto de 2013 16:39:08 UTC+2, Roy Smith escribi?: > > > In article <3479e08e-d435-492b-b2a0-a1c18678f6ab at googlegroups.com>, > > > > > > sergio7654 at gmail.com wrote: > > > > > > > > > > > > > key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A3035" > > > > > > > key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A303$ > > > > > > > > > > > > To start with, your keys are not the same (the JS one ends in "35", the Python one ends in "3$", > > > > > > and I don't know what to make of the fact that your Python key isn't terminated with a quote. > > > > > > Sorry, that was a problem with the cut & paste. The two sentences key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A3035" are just the same in Python and Javascript. Solved: key must be converted to binary with the function unhexlify(hexstr) Thanks, From !nospam!astrochelonian at gmail.com Sat Aug 31 13:11:38 2013 From: !nospam!astrochelonian at gmail.com (Eduardo Alvarez) Date: Sat, 31 Aug 2013 13:11:38 -0400 Subject: argparse - specify order of argument parsing? Message-ID: When using argparse, is there a way to specify in what order arguments get parsed? I am writing a script whose parameters can be modified in the following order: Defaults -> config file -> command-line switches. However, I want to give the option of specifying a config file using a command line switch as well, so logically, that file should be parsed before any other arguments are applied. However, it seems that parse_args() parses arguments in the order they're given, so if the config file switch is not given first, the config file will overwrite whatever was in the command-line switches, which should have higher priority. Thank you in advance, -- Eduardo Alvarez From python.list at tim.thechases.com Sat Aug 31 13:50:16 2013 From: python.list at tim.thechases.com (Tim Chase) Date: Sat, 31 Aug 2013 12:50:16 -0500 Subject: argparse - specify order of argument parsing? In-Reply-To: References: Message-ID: <20130831125016.116c2f18@bigbox.christie.dr> On 2013-08-31 13:11, Eduardo Alvarez wrote: > When using argparse, is there a way to specify in what order > arguments get parsed? I am writing a script whose parameters can be > modified in the following order: > > Defaults -> config file -> command-line switches. > > However, I want to give the option of specifying a config file > using a command line switch as well, so logically, that file should > be parsed before any other arguments are applied. However, it seems > that parse_args() parses arguments in the order they're given, so > if the config file switch is not given first, the config file will > overwrite whatever was in the command-line switches, which should > have higher priority. While I haven't come up with a good solution using argparse/optparse alone, I've found that it's easier (for processing) to specify the config file as an environment variable. So rather than doing my_prog.py -c /path/to/config.ini arg1 arg2 ... I just do MY_PROG_CONF=/path/to/config.ini my_prog.py arg1 arg2 ... ...at least on *nix systems; on Win32, it's c:\temp> set MY_PROG_CONF=c:\path\to\config.ini c:\temp> python my_prog.py arg1 arg2 ... Then you just intercept the config-file name from os.environ: config_file = os.environ.get( "MY_PROG_CONF", default_ini_location) -tkc From tjreedy at udel.edu Sat Aug 31 14:13:17 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 31 Aug 2013 14:13:17 -0400 Subject: argparse - specify order of argument parsing? In-Reply-To: References: Message-ID: On 8/31/2013 1:11 PM, Eduardo Alvarez wrote: > When using argparse, is there a way to specify in what order arguments > get parsed? I expect argparse to forward iterate the sequence of arguments that it receives. > I am writing a script whose parameters can be modified in > the following order: > > Defaults -> config file -> command-line switches. > > However, I want to give the option of specifying a config file using a > command line switch as well, so logically, that file should be parsed > before any other arguments are applied. However, it seems that > parse_args() parses arguments in the order they're given, Right. > so if the > config file switch is not given first, the config file will overwrite > whatever was in the command-line switches, which should have higher > priority. So just document that a config file has to be given first to work as expected. Order dependence among arguments is common. -- Terry Jan Reedy From tjreedy at udel.edu Sat Aug 31 14:17:33 2013 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 31 Aug 2013 14:17:33 -0400 Subject: argparse - specify order of argument parsing? In-Reply-To: References: Message-ID: On 8/31/2013 2:13 PM, Terry Reedy wrote: > On 8/31/2013 1:11 PM, Eduardo Alvarez wrote: >> When using argparse, is there a way to specify in what order arguments >> get parsed? > > I expect argparse to forward iterate the sequence of arguments that it > receives. Aside from the environment variable solution, you could search sys.argv for 'config=filename' and remove it and process it *before* you invoke argparse. -- Terry Jan Reedy From cs at zip.com.au Sat Aug 31 19:44:56 2013 From: cs at zip.com.au (Cameron Simpson) Date: Sun, 1 Sep 2013 09:44:56 +1000 Subject: argparse - specify order of argument parsing? In-Reply-To: References: Message-ID: <20130831234456.GA98629@cskk.homeip.net> On 31Aug2013 14:17, Terry Reedy wrote: | On 8/31/2013 2:13 PM, Terry Reedy wrote: | >On 8/31/2013 1:11 PM, Eduardo Alvarez wrote: | >>When using argparse, is there a way to specify in what order arguments | >>get parsed? | > | >I expect argparse to forward iterate the sequence of arguments that it | >receives. | | Aside from the environment variable solution, you could search | sys.argv for 'config=filename' and remove it and process it *before* | you invoke argparse. Although, speaking for myself, I like options to have effect as encountered: some-cmd -a --no-foo Would turn on "all" options then turn off "foo". Hard to do if you look for "more important" options and then less important options. I'm personally against out-of-order option handling; it confuses things for the user. If you (as the user) need the config file to be known first, put it first. Cheers, -- Cameron Simpson You wouldn't... ...but you KNOW you could. - Original V65 Commercial From papillion at gmail.com Sat Aug 31 19:44:04 2013 From: papillion at gmail.com (Anthony Papillion) Date: Sat, 31 Aug 2013 18:44:04 -0500 Subject: How do I process this using Python? Message-ID: <52227FC4.1060208@gmail.com> Hello Everyone, I'm writing a processor for Bitmessage messages and I am needing to parse the following returned JSON string: {u'inboxMessages': [{u'fromAddress': u'BM-2DBYkhiBZCyrBa8J7gFRGrFRSGqtHgPtMvwQ', u'toAddress': u'BM-2DC7SCTj2gzgrGgMvUCARdrfrsgLyz3iMyN3', u'read': 0, u'msgid': u'36659a4453e12a085d8fbfeefc58da8fb23f38bfb0984c2983e0ddc31c776038', u'receivedTime': u'1377986524', u'message': u'dGVzdGluZyAxIDIgMw0KDQotLQ0KSm9obiBQZXJyeQ0KDQo=\n', u'encodingType': 2, u'subject': u'bWVzc2FnZSAx\n'}, {u'fromAddress': u'BM-2DBYkhiBZCyrBa8J7gNBrngtgttHgPtMvwQ', u'toAddress': u'BM-2DC7SCTj2gzgrGgMvUCARdCrfthyz3iMyN3', u'read': 0, u'msgid': u'2ebe10c788ed47c6c122e3b43ae6642cb15077536c7056ed5088ab2d339c4630', u'receivedTime': u'1377986557', u'message': u'VGhpcyBpcyB0aGUgbmV4dCB0ZXN0DQoNCi0tDQpKb2huIFBlcnJ5DQoNCg==\n', u'encodingType': 2, u'subject': u'dGVzdGluZyAzIDQgNQ==\n'}, {u'fromAddress': u'BM-2DBYkhithgyhyrBa8J7gNBrnSGqtHgPtMvwQ', u'toAddress': u'BM-2DC7SCTj2gzgrtgtgMvUCARdCogLyz3iMyN3', u'read': 0, u'msgid': u'91dffd421c898aab0ffc43a363869a580abec6fa851aa6cf7cefe98263f96c81', u'receivedTime': u'1377986599', u'message': u'VGhpcyBpcyB0aGUgM3JkIHRlc3QNCg0hjj0NCkpvaG4gUGVycnkNCg0K\n', u'encodingType': 2, u'subject': u'dGhpcyBpcyB0aGUgM3Jk\n'}]} I tried using the following code: data = json.loads(api.getAllInboxMessages) # This is the API call for messageSender in data['inboxMessages']['fromAddress'] print messageSender For some reason (probably obvious reasons) isn't working. I'm trying to loop through the JSON and return all of the fromAddress fields. Can anyone offer suggestions? Thanks, Anthony From rosuav at gmail.com Sat Aug 31 19:48:55 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 1 Sep 2013 09:48:55 +1000 Subject: How do I process this using Python? In-Reply-To: <52227FC4.1060208@gmail.com> References: <52227FC4.1060208@gmail.com> Message-ID: On Sun, Sep 1, 2013 at 9:44 AM, Anthony Papillion wrote: > I'm writing a processor for Bitmessage messages and I am needing to > parse the following returned JSON string: > > {u'inboxMessages': Does the JSON string really have those u prefixes and apostrophes? That's not valid JSON. You may be able to use ast.literal_eval() on it - I was able to with the example data - but not a JSON parser. Can you sort out your transmission end? ChrisA From papillion at gmail.com Sat Aug 31 20:19:40 2013 From: papillion at gmail.com (Anthony Papillion) Date: Sat, 31 Aug 2013 19:19:40 -0500 Subject: How do I process this using Python? In-Reply-To: References: <52227FC4.1060208@gmail.com> Message-ID: <5222881C.8060205@gmail.com> On 08/31/2013 06:48 PM, Chris Angelico wrote: > On Sun, Sep 1, 2013 at 9:44 AM, Anthony Papillion wrote: >> I'm writing a processor for Bitmessage messages and I am needing to >> parse the following returned JSON string: >> >> {u'inboxMessages': > > Does the JSON string really have those u prefixes and apostrophes? > That's not valid JSON. You may be able to use ast.literal_eval() on it > - I was able to with the example data - but not a JSON parser. Can you > sort out your transmission end? > > ChrisA I think I remembered what the 'u' prefix is. It indicates that the data following is a unicode string. So could that be valid JSON data wrapped up in unicode? Anthony From rosuav at gmail.com Sat Aug 31 20:32:04 2013 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 1 Sep 2013 10:32:04 +1000 Subject: How do I process this using Python? In-Reply-To: <5222881C.8060205@gmail.com> References: <52227FC4.1060208@gmail.com> <5222881C.8060205@gmail.com> Message-ID: On Sun, Sep 1, 2013 at 10:19 AM, Anthony Papillion wrote: > On 08/31/2013 06:48 PM, Chris Angelico wrote: >> On Sun, Sep 1, 2013 at 9:44 AM, Anthony Papillion wrote: >>> I'm writing a processor for Bitmessage messages and I am needing to >>> parse the following returned JSON string: >>> >>> {u'inboxMessages': >> >> Does the JSON string really have those u prefixes and apostrophes? >> That's not valid JSON. You may be able to use ast.literal_eval() on it >> - I was able to with the example data - but not a JSON parser. Can you >> sort out your transmission end? >> >> ChrisA > > I think I remembered what the 'u' prefix is. It indicates that the data > following is a unicode string. So could that be valid JSON data wrapped > up in unicode? No; JSON already supports Unicode. What you may have is an incorrect JSON encoder that uses Python's repr() to shortcut its work - but it's wrong JSON. But bitmessage seems to be written in Python. Can you simply access the objects it's giving you, rather than going via text strings? ChrisA From papillion at gmail.com Sat Aug 31 20:58:43 2013 From: papillion at gmail.com (Anthony Papillion) Date: Sat, 31 Aug 2013 19:58:43 -0500 Subject: How do I process this using Python? (SOLVED) In-Reply-To: References: <52227FC4.1060208@gmail.com> <5222881C.8060205@gmail.com> Message-ID: <52229143.5080203@gmail.com> On 08/31/2013 07:32 PM, Chris Angelico wrote: > On Sun, Sep 1, 2013 at 10:19 AM, Anthony Papillion wrote: >> On 08/31/2013 06:48 PM, Chris Angelico wrote: >>> On Sun, Sep 1, 2013 at 9:44 AM, Anthony Papillion wrote: >>>> I'm writing a processor for Bitmessage messages and I am needing to >>>> parse the following returned JSON string: >>>> >>>> {u'inboxMessages': >>> >>> Does the JSON string really have those u prefixes and apostrophes? >>> That's not valid JSON. You may be able to use ast.literal_eval() on it >>> - I was able to with the example data - but not a JSON parser. Can you >>> sort out your transmission end? >>> >>> ChrisA >> >> I think I remembered what the 'u' prefix is. It indicates that the data >> following is a unicode string. So could that be valid JSON data wrapped >> up in unicode? > > No; JSON already supports Unicode. What you may have is an incorrect > JSON encoder that uses Python's repr() to shortcut its work - but it's > wrong JSON. > > But bitmessage seems to be written in Python. Can you simply access > the objects it's giving you, rather than going via text strings? > > ChrisA Once I looked at the data in a better structured way and saw it in the proper type it became clear. Here is my solution: data = json.loads(api.getAllInboxMessages()) for message in data['inboxmessages']: print message['fromAddress'] Yep, it was that simple. Thanks for the help guys! I appreciate how fast everyone responded. Anthony From cs at zip.com.au Sat Aug 31 20:32:24 2013 From: cs at zip.com.au (Cameron Simpson) Date: Sun, 1 Sep 2013 10:32:24 +1000 Subject: How do I process this using Python? In-Reply-To: <5222881C.8060205@gmail.com> References: <5222881C.8060205@gmail.com> Message-ID: <20130901003224.GA32086@cskk.homeip.net> On 31Aug2013 19:19, Anthony Papillion wrote: | On 08/31/2013 06:48 PM, Chris Angelico wrote: | > On Sun, Sep 1, 2013 at 9:44 AM, Anthony Papillion wrote: | >> I'm writing a processor for Bitmessage messages and I am needing to | >> parse the following returned JSON string: | >> | >> {u'inboxMessages': | > | > Does the JSON string really have those u prefixes and apostrophes? | > That's not valid JSON. You may be able to use ast.literal_eval() on it | > - I was able to with the example data - but not a JSON parser. Can you | > sort out your transmission end? | | I think I remembered what the 'u' prefix is. It indicates that the data | following is a unicode string. So could that be valid JSON data wrapped | up in unicode? How sure are you that it is JSON? It looks to me like a message that might once have been JSON, but has already been passed through json.loads() for you. What does type(the_json) say? What does repr(the_json) say? Print both. I would guess it has already been parsed. Cheers, -- Cameron Simpson Sattinger's Second Law: Even though you've plugged it in, it still won't work until you switch it on. From papillion at gmail.com Sat Aug 31 20:45:44 2013 From: papillion at gmail.com (Anthony Papillion) Date: Sat, 31 Aug 2013 19:45:44 -0500 Subject: How do I process this using Python? In-Reply-To: <20130901003224.GA32086@cskk.homeip.net> References: <5222881C.8060205@gmail.com> <20130901003224.GA32086@cskk.homeip.net> Message-ID: <52228E38.20403@gmail.com> On 08/31/2013 07:32 PM, Cameron Simpson wrote: > On 31Aug2013 19:19, Anthony Papillion wrote: > | On 08/31/2013 06:48 PM, Chris Angelico wrote: > | > On Sun, Sep 1, 2013 at 9:44 AM, Anthony Papillion wrote: > | >> I'm writing a processor for Bitmessage messages and I am needing to > | >> parse the following returned JSON string: > | >> > | >> {u'inboxMessages': > | > > | > Does the JSON string really have those u prefixes and apostrophes? > | > That's not valid JSON. You may be able to use ast.literal_eval() on it > | > - I was able to with the example data - but not a JSON parser. Can you > | > sort out your transmission end? > | > | I think I remembered what the 'u' prefix is. It indicates that the data > | following is a unicode string. So could that be valid JSON data wrapped > | up in unicode? > > How sure are you that it is JSON? It looks to me like a message > that might once have been JSON, but has already been passed through > json.loads() for you. > > What does type(the_json) say? What does repr(the_json) say? > Print both. > > I would guess it has already been parsed. > > Cheers, And you would be right! It's actually a dictionary! Anthony From steve+comp.lang.python at pearwood.info Sat Aug 31 21:15:29 2013 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 01 Sep 2013 01:15:29 GMT Subject: How do I process this using Python? References: Message-ID: <52229531$0$6599$c3e8da3$5496439d@news.astraweb.com> On Sat, 31 Aug 2013 18:44:04 -0500, Anthony Papillion wrote: > Hello Everyone, > > I'm writing a processor for Bitmessage messages and I am needing to > parse the following returned JSON string: [...] > For some reason (probably obvious reasons) isn't working. I'm trying to > loop through the JSON and return all of the fromAddress fields. > > Can anyone offer suggestions? Yes. Don't assume that "isn't working" is obvious. What do you mean "isn't working"? What part isn't working? What does it do? Does it print the wrong results, or does it raise an exception? If so, please copy and paste the entire traceback, do not simplify it, summarise it, or retype it from memory. -- Steven From anntzer.lee at gmail.com Sat Aug 31 20:03:41 2013 From: anntzer.lee at gmail.com (anntzer.lee at gmail.com) Date: Sat, 31 Aug 2013 17:03:41 -0700 (PDT) Subject: gethostbyname_ex(hostname) extremely slow (crossposted from stackoverflow) Message-ID: Hi, At startup, IPython (qtconsole) calls "socket.gethostbyname_ex(socket.gethostname())[2]" to find a list of IP addresses that point to the machine. On a Linux server that I manage this call is extremely slow (>20s)... which I have trouble understanding as "ip addr show" seems to give the same information nearly instantaneously. Is there anything I can do to make this faster? Can this be a network configuration issue (I am behind a router)? This issue is independent of IPython: $ time python -c 'import socket; print(socket.gethostbyname_ex(socket.gethostname())[2])' ['192.168.0.102'] python -c 0.07s user 0.02s system 0% cpu 28.190 total Thanks. Antony From roy at panix.com Sat Aug 31 21:01:00 2013 From: roy at panix.com (Roy Smith) Date: Sat, 31 Aug 2013 21:01:00 -0400 Subject: gethostbyname_ex(hostname) extremely slow (crossposted from stackoverflow) References: Message-ID: In article , anntzer.lee at gmail.com wrote: > Hi, > > At startup, IPython (qtconsole) calls > "socket.gethostbyname_ex(socket.gethostname())[2]" to find a list of IP > addresses that point to the machine. On a Linux server that I manage this > call is extremely slow (>20s)... which I have trouble understanding as "ip > addr show" seems to give the same information nearly instantaneously. Is > there anything I can do to make this faster? Can this be a network > configuration issue (I am behind a router)? It's almost certainly some sort of configuration issue which is causing some DNS query to timeout. The first step, is to figure out which part is taking so long. Open up a python shell and run: >>> name = socket.gethostname() see how long that takes and what it returns. Then, assuming it returns a string containing your hostname (massive handwave about what that actually means), try >>> socket.gethostbyname_ex(name) and see how long that takes and what it returns. At least at that point you'll have cut the problem in half. If I had to guess, you've got a missing PTR record, because that's what most people screw up. From roy at panix.com Sat Aug 31 21:23:00 2013 From: roy at panix.com (Roy Smith) Date: Sat, 31 Aug 2013 21:23:00 -0400 Subject: Not getting my head around pandas Message-ID: I've got a pandas DataFrame that looks like: Int64Index: 12960 entries, 0 to 12959 Data columns (total 2 columns): date 12960 non-null values ms 12960 non-null values dtypes: datetime64[ns](1), float64(1) date ms 12955 2013-08-30 23:20:00 96.868491 12956 2013-08-30 23:30:00 96.857826 12957 2013-08-30 23:40:00 92.624406 12958 2013-08-30 23:50:00 85.402094 12959 2013-08-31 00:00:00 93.870912 This is samples taken every 10 minutes going back several months. I want to find the mean and variance for all the points that happened at the same time each day. In other words, I want to group by date.time(). I can certainly do this by pulling all the data out of the DataFrame and working on it as normal Python data. But, I suspect there's some easy way to do this in pandas and I'm just not seeing it.