From ralf at schoenian-online.de Thu Oct 1 00:15:20 2009 From: ralf at schoenian-online.de (Ralf Schoenian) Date: Thu, 01 Oct 2009 06:15:20 +0200 Subject: Python book In-Reply-To: References: Message-ID: <4ac42a67$0$31878$9b4e6d93@newsspool3.arcor-online.net> lallous wrote: > Hello > > Can anyone suggest a good book Python book for advancing from beginner > level? > > (I started with Learning Python 3rd ed) > > Regards, > Elias Hi Elias, welcome to Python. I have learned Python with the official tutorial and with the outstanding book: Beginning Python, From Novice to Professional by Magnus Lie Hetland. This book provides a gently introduction into Python and 10 chapters with "complete" projects. Ralf From lan.rogers.book at gmail.com Thu Oct 1 01:22:19 2009 From: lan.rogers.book at gmail.com (Lanny) Date: Wed, 30 Sep 2009 22:22:19 -0700 (PDT) Subject: Python RPG Codebase Message-ID: <54ae24ff-7ad2-4075-b28d-784a51344e5a@f18g2000prf.googlegroups.com> I've been thinking about putting together a text based RPG written fully in Python, possibly expanding to a MUD system. I'd like to know if anyone feels any kind of need for this thing or if I'd be wasting my time, and also if anyone would be interested in participating, because of the highly modular nature of any RPG it should make for easy cooperation. From jackdied at gmail.com Thu Oct 1 01:46:16 2009 From: jackdied at gmail.com (Jack Diederich) Date: Thu, 1 Oct 2009 01:46:16 -0400 Subject: Python RPG Codebase In-Reply-To: <54ae24ff-7ad2-4075-b28d-784a51344e5a@f18g2000prf.googlegroups.com> References: <54ae24ff-7ad2-4075-b28d-784a51344e5a@f18g2000prf.googlegroups.com> Message-ID: On Thu, Oct 1, 2009 at 1:22 AM, Lanny wrote: > I've been thinking about putting together a text based RPG written > fully in Python, possibly expanding to a MUD system. I'd like to know > if anyone feels any kind of need for this thing or if I'd be wasting > my time, and also if anyone would be interested in participating, > because of the highly modular nature of any RPG it should make for > easy cooperation. You might not be aware that twisted (the popular asynchronous TCP/IP server) was started as a MUD project. It grew to do many other things but Gylph still hacks on his MUD code in fits and starts (interesting stuff too, not your typical LPC/Mush/MOO/Diku codebase). There are also a couple pure-python MUD clients if you're into that kind of thing. I still follow this stuff in passing because I learned more in undergrad hacking LPC than I did in my classes proper (probably because I spent hundreds of hours in class but thousands of hours writing LPC). -Jack From tim_grove at sil.org Thu Oct 1 02:35:03 2009 From: tim_grove at sil.org (Timothy W. Grove) Date: Thu, 01 Oct 2009 07:35:03 +0100 Subject: Python and lost files Message-ID: <4AC44D97.6060500@sil.org> Recently I purchased some software to recover some files which I had lost. (A python project, incidentally! Yes, I should have kept better backups!) They were nowhere to found in the file system, nor in the recycle bin, but this software was able to locate them and restore them. I was just wondering if there was a way using python to view and recover files from the hard drive which would otherwise remain lost forever? Best regards, Tim From tibor.beck at hotmail.com Thu Oct 1 02:49:05 2009 From: tibor.beck at hotmail.com (gentlestone) Date: Wed, 30 Sep 2009 23:49:05 -0700 (PDT) Subject: unicode issue References: <9a67ac5b-0bec-4a95-a55f-fe44b6ed8d62@a21g2000yqc.googlegroups.com> Message-ID: >save in utf-8 the coding declaration also has to be utf-8 ok, I understand, but what's the problem? Unfortunately seems to be the Python interactive mode doesn't have unicode support. It recognize the latin-1 encoding only. So I have 2 options, how to write doctest: 1. Replace native charaters with their encoded representation like u"\u017dabovit\xe1 zmie\u0161an\xe1 ka\u0161a" instead of u"?abovit? zmie?an? ka?a" 2. Use latin-1 encoding, where the file is saved in utf-8 The first is bad because doctest is a great documenttion tool and it is propably the main reason I use python. And something like u"\u017dabovit\xe1 zmie\u0161an\xe1 ka\u0161a" is not a best documentation style. But the tests work. The second is bad, because the declaration is incorrect and if I use it in Django model declaration for example I got bad data in the application. So what is the solution? Back to Java? :-) From list at qtrac.plus.com Thu Oct 1 02:55:25 2009 From: list at qtrac.plus.com (Mark Summerfield) Date: Wed, 30 Sep 2009 23:55:25 -0700 (PDT) Subject: New Book: Programming in Python 3 (Second Edition) Message-ID: <3927d890-afdd-4bf6-8e7a-65b66e9f905b@x37g2000yqj.googlegroups.com> Hi, A new edition of my Python 3 book will be available in the U.S. next month, and elsewhere in December or January: "Programming in Python 3 (Second Edition): A Complete Introduction to the Python Language" ISBN 0321680561 http://www.qtrac.eu/py3book.html The book is aimed at a wide audience, but assumes some programming experience (not necessarily Python, not necessarily object-oriented). It teaches solid procedural style programming, then builds on that to teach solid object-oriented programming, and then goes on to more advanced topics (e.g., including a nice way to create validated attributes by combining class decorators with descriptors). But even newcomers to Python 3 should be able to write useful (although small and basic) programs after reading chapter 1, and then go on to create larger and more sophisticated programs as they work through the chapters. The book has been fully revised and updated and now covers Python 3.0 and 3.1, and has been extended with new chapters on debugging, testing, and profiling, and on parsing (including coverage of the PyParsing and PLY modules), as well as a new section on coroutines in the advanced chapter. I was motivated to produce a second edition so soon after the first because it seems to me that Python's core developers would rather people switched to the 3.1 series and bypass 3.0 altogether and I wanted to support that. From lallous at lgwm.org Thu Oct 1 03:21:30 2009 From: lallous at lgwm.org (lallous) Date: Thu, 1 Oct 2009 09:21:30 +0200 Subject: Python and lost files References: Message-ID: Hello Timothy, "Timothy W. Grove" wrote in message news:mailman.726.1254378947.2807.python-list at python.org... > Recently I purchased some software to recover some files which I had lost. > (A python project, incidentally! Yes, I should have kept better backups!) > They were nowhere to found in the file system, nor in the recycle bin, but > this software was able to locate them and restore them. I was just > wondering if there was a way using python to view and recover files from > the hard drive which would otherwise remain lost forever? > To recover lost or deleted files you need a specialized tools (such as data recovery programs). I don't see a way to recover files using python itself but perhaps using a data recovery program written in Python. -- Elias From hjtoi-better-remove-before-reply at comcast.net Thu Oct 1 03:23:41 2009 From: hjtoi-better-remove-before-reply at comcast.net (Heikki Toivonen) Date: Thu, 01 Oct 2009 00:23:41 -0700 Subject: M2Crypto 0.20.1 won't build on Red Hat Linux References: <4ac39492$0$1642$742ec2ed@news.sonic.net> Message-ID: John Nagle wrote: > M2Crypto, from > > http://pypi.python.org/packages/source/M/M2Crypto/M2Crypto-0.20.1.tar.gz > > won't build on Red Hat Linux / 386. The error is > > It's some incompatibility between Red Hat include file packaging and > M2Crypto. Yup, all Fedora Core-based systems actually. > There was at one time a note on how to deal with this problem, but it's > been deleted. It's still in Google's cache, though. Not deleted, but my hosting provider made some changes that put my blog and a number of sites down, without advance notice, while I was on vacation :( Blog is back up, so the URL works again: http://www.heikkitoivonen.net/blog/2009/02/09/m2crypto-build-wrapper-for-fedora-core-based-distributions/ I've also added this to the FAQ: http://chandlerproject.org/Projects/MeTooCrypto#Code -- Heikki Toivonen - http://heikkitoivonen.net From hendrik at microcorp.co.za Thu Oct 1 03:40:47 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Thu, 1 Oct 2009 09:40:47 +0200 Subject: Most "active" coroutine library project? In-Reply-To: References: <64183b73-9cb0-4ce1-98d0-f47e13a818e4@f37g2000yqn.googlegroups.com> Message-ID: <200910010940.47519.hendrik@microcorp.co.za> On Thursday, 1 October 2009 00:27:02 Rhodri James wrote: > I was going to say, you want 256 bytes of RAM, you profligate > so-and-so? Here, have 32 bytes of data space and stop your > whining :-) My multi tasking is coming on nicely, but I am struggling a bit with the garbage collection. The Trash bin gets a bit tight at times, so the extra bytes help a lot when I run more than 500 independent tasks. :-) - Hendrik From jef.mangelschots at gmail.com Thu Oct 1 04:18:14 2009 From: jef.mangelschots at gmail.com (jefm) Date: Thu, 1 Oct 2009 01:18:14 -0700 (PDT) Subject: PyOpenGL and graphics card support Message-ID: OpenGL newbie alert!!! Do I need to do anything special to use OpenGL capabilities of my graphics card ? I have the impression PyOpenG is using the Windows emulation. when I execute the following code: print "glGetString - GL_VENDOR: ", glGetString (GL_VENDOR) print "glGetString - GL_RENDERER: ", glGetString (GL_RENDERER) print "glGetString - GL_SHADING_LANGUAGE_VERSION:", glGetString (GL_SHADING_LANGUAGE_VERSION) print "glGetString - GL_EXTENSIONS: ", glGetString (GL_EXTENSIONS) print "gluGetString - GLU_VERSION: ", gluGetString (GLU_VERSION) print "gluGetString - GLU_EXTENSIONS: ", gluGetString (GLU_EXTENSIONS) I get the following results: glGetString - GL_VENDOR: None glGetString - GL_RENDERER: None glGetString - GL_SHADING_LANGUAGE_VERSION: None glGetString - GL_EXTENSIONS: None gluGetString - GLU_VERSION: 1.2.2.0 Microsoft Corporation gluGetString - GLU_EXTENSIONS: I am using Python 2.6.2 and PyOpenGL3.0.1a3 on Windows 7 Enterprise My graphics card is NVIDIA GeForce 9500 GT card with driver date = 7/14/2009 and version = 8.15.11.9038 From jef.mangelschots at gmail.com Thu Oct 1 04:27:03 2009 From: jef.mangelschots at gmail.com (jefm) Date: Thu, 1 Oct 2009 01:27:03 -0700 (PDT) Subject: PyOpenGL and graphics card support References: Message-ID: <8241f105-aa73-44fc-96fb-a09bf4f2d5e1@v15g2000prn.googlegroups.com> these are the imports I use: from OpenGL.GL import * from OpenGL.GLUT import * from OpenGL.GLU import * From hrgzsl at gmail.com Thu Oct 1 04:29:30 2009 From: hrgzsl at gmail.com (hrgzsl at gmail.com) Date: Thu, 1 Oct 2009 08:29:30 +0000 Subject: A new Internet-search website written in Python Message-ID: <30240d070910010129hb2b9788oa471c486d7bb1305@mail.gmail.com> Dear Python-list subscribers, The purpose of this email is to inform the Python-list mailing-list subscribers of an Internet-search website that is run by software written in Python. The website has been in development for several months, and although it is not in a very polished state as of now, its core functions, such as categorizing search engines, are working, and hence it was decided that its existence would be publicly announced. The website allows its users to search the Internet using existing search engines and search functions of existing websites, such as - Google, Yahoo!, and Bing, for general-purpose web searches; - search functions of Wikipedia and Dictionary.com, for searches within specific referential works; and - Google Images and the search function of YouTube, for multimedia-searches. The examples mentioned above are a small portion of supported engines/websites, and currently more than 100 are supported. The website is currently accessible via the following address: http://www.search-aggregator.com/ If you require help using the website, or encounter a problem with the website, please send an e-mail to the following e-mail address: hrgzsl at gmail.com Help e-mails will be welcome, and problem reports will be appreciated. From http Thu Oct 1 04:34:29 2009 From: http (Paul Rubin) Date: 01 Oct 2009 01:34:29 -0700 Subject: A new Internet-search website written in Python References: Message-ID: <7xocor1pii.fsf@ruckus.brouhaha.com> "hrgzsl at gmail.com" writes: > The purpose of this email is to inform the Python-list mailing-list > subscribers of an Internet-search website that is run by software > written in Python. Is the software downloadable? If not, why should anyone here care what language it is written in? From hrgzsl at gmail.com Thu Oct 1 05:22:50 2009 From: hrgzsl at gmail.com (hrgzsl at gmail.com) Date: Thu, 1 Oct 2009 09:22:50 +0000 Subject: A new Internet-search website written in Python In-Reply-To: <7xocor1pii.fsf@ruckus.brouhaha.com> References: <7xocor1pii.fsf@ruckus.brouhaha.com> Message-ID: <30240d070910010222k71dac717jea220134d05c241e@mail.gmail.com> On 10/1/09, Paul Rubin wrote: > "hrgzsl at gmail.com" writes: >> The purpose of this email is to inform the Python-list mailing-list >> subscribers of an Internet-search website that is run by software >> written in Python. > > Is the software downloadable? If not, why should anyone here care > what language it is written in? Dear Paul, I am afraid the software is currently not downloadable. But the fact that it is written in Python might be of interest to some people, and that was the reason for the sending of the previous e-mail. By the way, I have noticed that the address in the "from" field in your e-mail is set to "http://phr.cx at nospam.invalid". Is this supposed to imply that my previous e-mail is considered to be spam? Taro Taniguchi From sean.gillies at gmail.com Thu Oct 1 05:26:26 2009 From: sean.gillies at gmail.com (sean.gillies at gmail.com) Date: Thu, 1 Oct 2009 02:26:26 -0700 (PDT) Subject: Unofficial Python GIS SIG Message-ID: <2edb4c08-5853-4d21-9bcc-5895c431250b@l13g2000yqb.googlegroups.com> There's growing interest among GIS users of Python for a discussion group, so I've started an unofficial Python GIS SIG at: http://groups.google.com/group/python-gis-sig. Please join if you're interested in improving Python's GIS story. At any rate, the group should (hopefully) reduce the number of GIS questions that get mistakenly posted to Web-SIG ("GIS" is commonly known as "SIG" outside the US/UK). Regards, -- Sean Gillies Programmer Institute for the Study of the Ancient World New York University From nospam at nospam.com Thu Oct 1 05:28:09 2009 From: nospam at nospam.com (Gilles Ganault) Date: Thu, 01 Oct 2009 11:28:09 +0200 Subject: Concurrent threads to pull web pages? Message-ID: Hello I recently asked how to pull companies' ID from an SQLite database, have multiple instances of a Python script download each company's web page from a remote server, eg. www.acme.com/company.php?id=1, and use regexes to extract some information from each page. I need to run multiple instances to save time, since each page takes about 10 seconds to be returned to the script/browser. Since I've never written a multi-threaded Python script before, to save time investigating, I was wondering if someone already had a script that downloads web pages and save some information into a database. Thank you for any tip. From sion at viridian.paintbox Thu Oct 1 05:35:00 2009 From: sion at viridian.paintbox (Sion Arrowsmith) Date: Thu, 01 Oct 2009 09:35:00 GMT Subject: Simple if-else question References: <4AC2314E.4040204@stoneleaf.us> Message-ID: <8J_wm.421662$RV1.5934@newsfe26.ams2> MRAB wrote: >> [ for ... else ] >The example that makes it clearest for me is searching through a list >for a certain item and breaking out of the 'for' loop if I find it. If I >get to the end of the list and still haven't broken out then I haven't >found the item, and that's when the else statement takes effect: What works for me is thinking about the while ... else construct and comparing it to if ... else: if x: print "x is True" else: print "x is False" while x: print "x is True" x -= 1 # or something # A break here exits the loop with x True else: print "x is False" then the step from while to for is obvious. -- \S under construction From wuwei23 at gmail.com Thu Oct 1 05:56:12 2009 From: wuwei23 at gmail.com (alex23) Date: Thu, 1 Oct 2009 02:56:12 -0700 (PDT) Subject: Python and lost files References: Message-ID: "Timothy W. Grove" wrote: > I was just wondering if there was a way using python to view and recover > files from the hard drive which would otherwise remain lost forever? I'm not familiar with any Python-based tools for data recovery, but you might be interested in mercurial[1], which _is_ (primarily) written in Python ;) 1: http://mercurial.selenic.com/ From olof.bjarnason at gmail.com Thu Oct 1 06:00:36 2009 From: olof.bjarnason at gmail.com (Olof Bjarnason) Date: Thu, 1 Oct 2009 12:00:36 +0200 Subject: Simple if-else question In-Reply-To: <8J_wm.421662$RV1.5934@newsfe26.ams2> References: <4AC2314E.4040204@stoneleaf.us> <8J_wm.421662$RV1.5934@newsfe26.ams2> Message-ID: 2009/10/1 Sion Arrowsmith : > MRAB ? wrote: >>> [ for ... else ] >>The example that makes it clearest for me is searching through a list >>for a certain item and breaking out of the 'for' loop if I find it. If I >>get to the end of the list and still haven't broken out then I haven't >>found the item, and that's when the else statement takes effect: > > What works for me is thinking about the while ... else construct > and comparing it to if ... else: > > if x: > ? ?print "x is True" > else: > ? ?print "x is False" > > while x: > ? ?print "x is True" > ? ?x -= 1 # or something > ? ?# A break here exits the loop with x True > else: > ? ?print "x is False" > > then the step from while to for is obvious. Am I grokking for..else: for i in range(10): if fn(i): break nobreak: # let 'nobreak' mean 'else' print('break did not happen') > > -- > \S > > ? under construction > > -- > http://mail.python.org/mailman/listinfo/python-list > -- twitter.com/olofb olofb.wordpress.com olofb.wordpress.com/tag/english From davea at ieee.org Thu Oct 1 06:50:21 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 01 Oct 2009 06:50:21 -0400 Subject: unicode issue In-Reply-To: References: <9a67ac5b-0bec-4a95-a55f-fe44b6ed8d62@a21g2000yqc.googlegroups.com> Message-ID: <4AC4896D.7080208@ieee.org> gentlestone wrote: >> save in utf-8 the coding declaration also has to be utf-8 >> > > ok, I understand, but what's the problem? Unfortunately seems to be > the Python interactive > mode doesn't have unicode support. It recognize the latin-1 encoding > only. > > So I have 2 options, how to write doctest: > 1. Replace native charaters with their encoded representation like > u"\u017dabovit\xe1 zmie\u0161an\xe1 ka\u0161a" instead of u"?abovit? > zmie?an? ka?a" > 2. Use latin-1 encoding, where the file is saved in utf-8 > > The first is bad because doctest is a great documenttion tool and it > is propably the main reason I use python. And something like > u"\u017dabovit\xe1 zmie\u0161an\xe1 ka\u0161a" is not a best > documentation style. But the tests work. > > The second is bad, because the declaration is incorrect and if I use > it in Django model declaration for example I got bad data in the > application. > > So what is the solution? Back to Java? :-) > > Wait -- don't give up yet. Since I'm one of the ones who (partially) steered you wrong, let me try to help. Key variable here is how your text editor behaves. Since I've never taken my (programming) text editor out of ASCII mode before this week, it took some experimenting (and more importantly a message from Piet on this thread) to make sense of things. I think I now know how to make my own editor (Komodo IDE) behave in this environment, and you probably can do as well or better. In fact, judging from your messages, you probably are doing much better on the editor front. When I tried this morning to re-open that test file from yesterday, many of the characters were all messed up. I was okay as long as the project was still open, but not today. The editor itself apparently looks to that encoding declaration when it's deciding how to interpret the bytes on disk. So I did the following, using Komodo IDE. I created a new file in the project. Before saving it, I used Edit->CurrentFileSettings->Properties->Encoding to set it to UTF-8. *NOW* I pasted the stuff from your email message. And added the #-*- coding: utf-8 -*- as the second line of the file. Notice it's *NOT* latin-1. At this point I save and run the file, and it seems to work fine. My guess is that I could set these as default settings in Komodo, if I were doing UTF-8 very often, and it would become painless. I know I have certain stuff in my python template, and could add that encoding line as well. Anyway, that gets us to the step of running the doctest. The trick here seems to be that we need to define the docstring as a Unicode docstring to have it interpreted correctly. Try adding the u in front of the triple quote as follows: def downcode(name): u""" >>> downcode(u"?abovit? zmie?an? ka?a") u'Zabovita zmiesana kasa' """ for key, value in _MAP.iteritems(): name = name.replace(key, value) return name Now, if the doctest passes, we seem to be in good shape. There's another problem, that hopefully somebody else can help with. That's if doctest needs to report an error. When I deliberately changed the "expect" string I get an error like the following. UnicodeEncodeError: 'ascii' codec can't encode character u'\u017d' in position 1 50: ordinal not in range(128) I get a similar error if running the -v option on doctest. (Note that I do *NOT* get the error when running inside Komodo. And what I've read implies that the same would be true if running inside IDLE.) The problem is similar to the one you'd have doing a simple: print u"\u017d" I think these are avoided if sys.stdout.encoding (and maybe sys.stderr.encoding) are set to utf-8. On my system they're set to None, which says to use "the system default encoding." On my system that would be ASCII, so I get the error. But perhaps yours is already something better. I found links: http://drj11.wordpress.com/2007/05/14/python-how-is-sysstdoutencoding-chosen/ http://wiki.python.org/moin/PrintFails http://lists.macromates.com/textmate/2008-June/025735.html which indicate you may want to try: set LC_CTYPE=en_GB.utf-8 python at the command prompt before running python. This could be system specific; it didn't work for me on XP. The workaround that works for me (so far) is: if __name__ == "__main__": import sys, codecs sys.stdout = codecs.getwriter('utf8')(sys.stdout) print u"?abovit? zmie?an? ka?a" import doctest doctest.testmod() The codecs line tells python that stdout should use utf-8. That doesn't make the characters look good on my console, but at least it avoids the errors. I'm guessing that on my system I should use latin1 here instead of utf8. But I don't want to confuse things. HTH DaveA From John at nospam.net Thu Oct 1 07:19:04 2009 From: John at nospam.net (John) Date: Thu, 01 Oct 2009 07:19:04 -0400 Subject: "cx_Freeze.freezer.ConfigError: no initscript named Console" Message-ID: cx_freeze v4.01 Python 2.6 Ubuntu Jaunty Following the example of 'cx-freeze hello.py', I'm getting the error message below. I put all of the error keywords into google and found no hits. Some people in various posts have said to use Python 2.5 but a lot of my code is using Python 2.6 features. Can you telling what I'm doing wrong? ./cxfreeze hello.py Traceback (most recent call last): File "./cxfreeze", line 5, in main() File "/root/cx_Freeze-4.1/cx_Freeze/main.py", line 178, in main silent = options.silent) File "/root/cx_Freeze-4.1/cx_Freeze/freezer.py", line 85, in __init__ self._VerifyConfiguration() File "/root/cx_Freeze-4.1/cx_Freeze/freezer.py", line 325, in _VerifyConfiguration self._GetInitScriptFileName() File "/root/cx_Freeze-4.1/cx_Freeze/freezer.py", line 246, in _GetInitScriptFileName raise ConfigError("no initscript named %s", name) cx_Freeze.freezer.ConfigError: no initscript named Console From flebber.crue at gmail.com Thu Oct 1 07:42:10 2009 From: flebber.crue at gmail.com (flebber) Date: Thu, 1 Oct 2009 04:42:10 -0700 (PDT) Subject: ActivePython 3.1.1.2 vs Python 3.1.1 for OSX? References: <3371b8c9-5c45-4ee2-90cf-f5deb0f6419c@m18g2000vbs.googlegroups.com> Message-ID: <191cdd2e-e0fd-40ad-8926-c542a8ad35f5@q40g2000prh.googlegroups.com> On Oct 1, 11:28?am, srid wrote: > On Sep 30, 4:51?pm, Robert Hicks wrote: > > > I am just curious which I should use. I am going to start learning > > Python soon. Are they comparable and I just do a "eenie meenie minie > > moe"? > > ActivePython is essentially same as the installers from python.org - > but it also comes with additional documentation and tutorials, such > as: > > Python FAQs > A snapshot of the Python Enhancement Proposals (PEPs) (For the most > recent version, refer to the PEPs on python.org .) > Dive Into Python (A tutorial for programmers) > Non-Programmers Tutorial For Python > > http://docs.activestate.com/activepython/3.1/whatsincluded.html > > Also note that 2.6.x is probably the best bet if you are going to use > some 3rd party libraries (after you learn the basics of Python) .. > because 3.x does not have many of those libraries ported yet. > > ?http://www.activestate.com/activepython/ > > Further, early next week - a new release of ActivePython-2.6 will be > made available that will include, for the first time, a new Python > package manager (PyPM) from ActiveState that makes it easier to > install packages from pypi.python.org(without having to compile them > yourself). This is similar to PPM from ActivePerl. > > -srid Thats awesome news. From cjns1989 at gmail.com Thu Oct 1 07:42:46 2009 From: cjns1989 at gmail.com (Chris Jones) Date: Thu, 01 Oct 2009 07:42:46 -0400 Subject: A new Internet-search website written in Python In-Reply-To: <30240d070910010222k71dac717jea220134d05c241e@mail.gmail.com> References: <7xocor1pii.fsf@ruckus.brouhaha.com> <30240d070910010222k71dac717jea220134d05c241e@mail.gmail.com> Message-ID: <20091001114246.GA3003@turki.gavron.org> On Thu, Oct 01, 2009 at 05:22:50AM EDT, hrgzsl at gmail.com wrote: > On 10/1/09, Paul Rubin wrote: ... > By the way, I have noticed that the address in the "from" field in > your e-mail is set to "http://phr.cx at nospam.invalid". Is this supposed > to imply that my previous e-mail is considered to be spam? It just means that Paul Rubin is not giving out his email address. Why he's adding insult to injury by cooking up a syntaxically malformed address - notice the colon (":") - is anybody's guess, though. cf. "Section 3.3 - Lexical Tokens" of this document: http://www.ietf.org/rfc/rfc0822.txt CJ From ppriyajs at gmail.com Thu Oct 1 07:55:23 2009 From: ppriyajs at gmail.com (padmapriya sekaran) Date: Thu, 1 Oct 2009 13:55:23 +0200 Subject: hii Message-ID: <8512a6f90910010455u6eb3e86ag2173183aef924f4c@mail.gmail.com> I have a problem in using interp from numpy for which i need 3 array. my first array is x = scipy.linspace(0.009,0.53,100) and the other two array should be read from my file with 100x2 dimension (file1_lines) but it is read as string instead of two columns for line in file1_lines: line = line.split() xp = line[0] yp = line[1] y = scipy.interp(x,xp,yp) if i code like this it takes only one value for xp and yp but 100 values for x. i dont know what to do please help. -- Padmapriya Sundrashekaran Master Student Life Science Informatics University of Bonn. -------------- next part -------------- An HTML attachment was scrubbed... URL: From 7gaga7 at gmail.com Thu Oct 1 08:34:16 2009 From: 7gaga7 at gmail.com (Greg) Date: Thu, 1 Oct 2009 05:34:16 -0700 (PDT) Subject: Python book References: Message-ID: <18b5084f-b86d-4389-832f-e12600168948@c37g2000yqi.googlegroups.com> On Sep 30, 6:58?am, "lallous" wrote: > Hello > > Can anyone suggest a good book Python book for advancing from beginner > level? > > (I started with Learning Python 3rd ed) > > Regards, > Elias Elias, Try "Core Python Programming", 2nd Edition, by Wesley J. Chun. I love it! Cheers, Greg From banibrata.dutta at gmail.com Thu Oct 1 09:06:47 2009 From: banibrata.dutta at gmail.com (Banibrata Dutta) Date: Thu, 1 Oct 2009 18:36:47 +0530 Subject: Python book In-Reply-To: <18b5084f-b86d-4389-832f-e12600168948@c37g2000yqi.googlegroups.com> References: <18b5084f-b86d-4389-832f-e12600168948@c37g2000yqi.googlegroups.com> Message-ID: <3de8e1f70910010606o59f2d298mc081f0ff760d5929@mail.gmail.com> Asking about 'Python' book recommendation is something that always leads to a near religious recommendation set. I have my favourites too, but would recommend original poster to check the no. of reviews on Amazon, B&N etc., after collecting recommendations from this list, before finalizing on one. -------------- next part -------------- An HTML attachment was scrubbed... URL: From John at nospam.net Thu Oct 1 10:08:41 2009 From: John at nospam.net (John) Date: Thu, 01 Oct 2009 10:08:41 -0400 Subject: cx_freeze problem on Ubuntu Message-ID: Sorry if this might be a repost. I'm having problems with my newsreader. My system: cx_freeze 4.1 Python 2.6 Ubuntu Jaunty I downloaded the cx_freeze source code from http://cx-freeze.sourceforge.net/ into a directory. I wrote a one line python program 'print( "hello world" )' According to the instructions, all I have to do is this: ./cxfreeze hello.py But I get this error: ./cxfreeze hello.py Traceback (most recent call last): File "./cxfreeze", line 5, in main() File "/root/cx_Freeze-4.1/cx_Freeze/main.py", line 178, in main silent = options.silent) File "/root/cx_Freeze-4.1/cx_Freeze/freezer.py", line 85, in __init__ self._VerifyConfiguration() File "/root/cx_Freeze-4.1/cx_Freeze/freezer.py", line 325, in _VerifyConfiguration self._GetInitScriptFileName() File "/root/cx_Freeze-4.1/cx_Freeze/freezer.py", line 246, in _GetInitScriptFileName raise ConfigError("no initscript named %s", name) cx_Freeze.freezer.ConfigError: no initscript named Console From here: http://linux.softpedia.com/get/Programming/Assembler-Tools/cx-Freeze-8247.shtml the directions state: Installation: "Extract the archive into a directory. In order to build cx_Freeze you need to have gcc and Python development include files and libraries available. Run the following commands. python MakeFrozenBases.py python FreezePython.py --no-copy-deps FreezePython.py" I can't find either of these Python programs and searching the Internet for clues was fruitless. I thinking that I'm missing something that's painfully obvious. As far as I can tell, no one else has ever posted this problem so I don't know what corrective actions to make. I'd greatly appreciate any assistance. From hyugaricdeau at gmail.com Thu Oct 1 10:09:53 2009 From: hyugaricdeau at gmail.com (Hyuga) Date: Thu, 1 Oct 2009 07:09:53 -0700 (PDT) Subject: unicode issue References: <9a67ac5b-0bec-4a95-a55f-fe44b6ed8d62@a21g2000yqc.googlegroups.com> Message-ID: On Sep 30, 3:34?am, gentlestone wrote: > Why don't work this code on Python 2.6? Or how can I do this job? > > _MAP = { > ? ? # LATIN > ? ? u'?': 'A', u'?': 'A', u'?': 'A', u'?': 'A', u'?': 'A', u'?': 'A', > u'?': 'AE', u'?':'C', > ? ? u'?': 'E', u'?': 'E', u'?': 'E', u'?': 'E', u'?': 'I', u'?': 'I', > u'?': 'I', > ? ? u'?': 'I', u'?': 'D', u'?': 'N', u'?': 'O', u'?': 'O', u'?': 'O', > u'?': 'O', u'?':'O', > ? ? u'?': 'O', u'?': 'O', u'?': 'U', u'?': 'U', u'?': 'U', u'?': 'U', > u'?': 'U', > ? ? u'?': 'Y', u'?': 'TH', u'?': 'ss', u'?':'a', u'?':'a', u'?': 'a', > u'?': 'a', u'?':'a', > ? ? u'?': 'a', u'?': 'ae', u'?': 'c', u'?': 'e', u'?': 'e', u'?': 'e', > u'?': 'e', > ? ? u'?': 'i', u'?': 'i', u'?': 'i', u'?': 'i', u'?': 'd', u'?': 'n', > u'?': 'o', u'?':'o', > ? ? u'?': 'o', u'?': 'o', u'?': 'o', u'?': 'o', u'?': 'o', u'?': 'u', > u'?': 'u', > ? ? u'?': 'u', u'?': 'u', u'?': 'u', u'?': 'y', u'?': 'th', u'?': 'y', > ? ? # LATIN_SYMBOLS > ? ? u'?':'(c)', > ? ? # GREEK > ? ? u'?':'a', u'?':'b', u'?':'g', u'?':'d', u'?':'e', u'?':'z', > u'?':'h', u'?':'8', > ? ? u'?':'i', u'?':'k', u'?':'l', u'?':'m', u'?':'n', u'?':'3', > u'?':'o', u'?':'p', > ? ? u'?':'r', u'?':'s', u'?':'t', u'?':'y', u'?':'f', u'?':'x', > u'?':'ps', u'?':'w', > ? ? u'?':'a', u'?':'e', u'?':'i', u'?':'o', u'?':'y', u'?':'h', > u'?':'w', u'?':'s', > ? ? u'?':'i', u'?':'y', u'?':'y', u'?':'i', > ? ? u'?':'A', u'?':'B', u'?':'G', u'?':'D', u'?':'E', u'?':'Z', > u'?':'H', u'?':'8', > ? ? u'?':'I', u'?':'K', u'?':'L', u'?':'M', u'?':'N', u'?':'3', > u'?':'O', u'?':'P', > ? ? u'?':'R', u'?':'S', u'?':'T', u'?':'Y', u'?':'F', u'?':'X', > u'?':'PS', u'?':'W', > ? ? u'?':'A', u'?':'E', u'?':'I', u'?':'O', u'?':'Y', u'?':'H', > u'?':'W', u'?':'I', u'?':'Y', > ? ? # TURKISH > ? ? u'?':'s', u'?':'S', u'?':'i', u'?':'I', u'?':'c', u'?':'C', > u'?':'u', u'?':'U', > ? ? u'?':'o', u'?':'O', u'?':'g', u'?':'G', > ? ? # RUSSIAN > ? ? u'?':'a', u'?':'b', u'?':'v', u'?':'g', u'?':'d', u'?':'e', > u'?':'yo', u'?':'zh', > ? ? u'?':'z', u'?':'i', u'?':'j', u'?':'k', u'?':'l', u'?':'m', > u'?':'n', u'?':'o', > ? ? u'?':'p', u'?':'r', u'?':'s', u'?':'t', u'?':'u', u'?':'f', > u'?':'h', u'?':'c', > ? ? u'?':'ch', u'?':'sh', u'?':'sh', u'?':'', u'?':'y', u'?':'', > u'?':'e', u'?':'yu', u'?':'ya', > ? ? u'?':'A', u'?':'B', u'?':'V', u'?':'G', u'?':'D', u'?':'E', > u'?':'Yo', u'?':'Zh', > ? ? u'?':'Z', u'?':'I', u'?':'J', u'?':'K', u'?':'L', u'?':'M', > u'?':'N', u'?':'O', > ? ? u'?':'P', u'?':'R', u'?':'S', u'?':'T', u'?':'U', u'?':'F', > u'?':'H', u'?':'C', > ? ? u'?':'Ch', u'?':'Sh', u'?':'Sh', u'?':'', u'?':'Y', u'?':'', > u'?':'E', u'?':'Yu', u'?':'Ya', > ? ? # UKRAINIAN > ? ? u'?':'Ye', u'?':'I', u'?':'Yi', u'?':'G', u'?':'ye', u'?':'i', > u'?':'yi', u'?':'g', > ? ? # CZECH > ? ? u'?':'c', u'?':'d', u'?':'e', u'?':'n', u'?':'r', u'?':'s', > u'?':'t', u'?':'u', > ? ? u'?':'z', u'?':'C', u'?':'D', u'?':'E', u'?':'N', u'?':'R', > u'?':'S', u'?':'T', u'?':'U', u'?':'Z', > ? ? # POLISH > ? ? u'?':'a', u'?':'c', u'?':'e', u'?':'l', u'?':'n', u'?':'o', > u'?':'s', u'?':'z', > ? ? u'?':'z', u'?':'A', u'?':'C', u'?':'e', u'?':'L', u'?':'N', > u'?':'o', u'?':'S', > ? ? u'?':'Z', u'?':'Z', > ? ? # LATVIAN > ? ? u'?':'a', u'?':'c', u'?':'e', u'?':'g', u'?':'i', u'?':'k', > u'?':'l', u'?':'n', > ? ? u'?':'s', u'?':'u', u'?':'z', u'?':'A', u'?':'C', u'?':'E', > u'?':'G', u'?':'i', > ? ? u'?':'k', u'?':'L', u'?':'N', u'?':'S', u'?':'u', u'?':'Z' > > } > > def downcode(name): > ? ? """ > ? ? >>> downcode(u"?abovit? zmie?an? ka?a") > ? ? u'Zabovita zmiesana kasa' > ? ? """ > ? ? for key, value in _MAP.iteritems(): > ? ? ? ? name = name.replace(key, value) > ? ? return name Though C Python is pretty optimized under the hood for this sort of single-character replacement, this still seems pretty inefficient since you're calling replace for every character you want to map. I think that a better approach might be something like: def downcode(name): return ''.join(_MAP.get(c, c) for c in name) Or using string.translate: import string def downcode(name): table = string.maketrans( '??????...', 'AAAAAA...') return name.translate(table) From exarkun at twistedmatrix.com Thu Oct 1 10:17:38 2009 From: exarkun at twistedmatrix.com (exarkun at twistedmatrix.com) Date: Thu, 01 Oct 2009 14:17:38 -0000 Subject: Python RPG Codebase In-Reply-To: References: <54ae24ff-7ad2-4075-b28d-784a51344e5a@f18g2000prf.googlegroups.com> Message-ID: <20091001141738.1533.462754129.divmod.xquotient.498@localhost.localdomain> On 05:46 am, jackdied at gmail.com wrote: >On Thu, Oct 1, 2009 at 1:22 AM, Lanny >wrote: >>I've been thinking about putting together a text based RPG written >>fully in Python, possibly expanding to a MUD system. I'd like to know >>if anyone feels any kind of need for this thing or if I'd be wasting >>my time, and also if anyone would be interested in participating, >>because of the highly modular nature of any RPG it should make for >>easy cooperation. > >You might not be aware that twisted (the popular asynchronous TCP/IP >server) was started as a MUD project. It grew to do many other things >but Gylph still hacks on his MUD code in fits and starts (interesting >stuff too, not your typical LPC/Mush/MOO/Diku codebase). There are >also a couple pure-python MUD clients if you're into that kind of >thing. > >I still follow this stuff in passing because I learned more in >undergrad hacking LPC than I did in my classes proper (probably >because I spent hundreds of hours in class but thousands of hours >writing LPC). This project's current home is . Unfortunately, there's not much there. :) I'm hoping to give a talk on Imaginary at PyCon next year. Part of my preparation for that will be writing a lot more documentation, improving the web site, and generally making things friendlier towards newcomers. Jean-Paul From williwortel at hotmail.com Thu Oct 1 11:07:02 2009 From: williwortel at hotmail.com (pw_ ^) Date: Thu, 1 Oct 2009 08:07:02 -0700 (PDT) Subject: Isaac Cipher impl. in python Message-ID: <82336097-7534-435f-8713-25f76f31a6c6@e8g2000yqo.googlegroups.com> Hello, I'm looking for an Isaac Cipher impl. in Python. If anyone has it, please contact me. Thanks, pw_^ From walter at livinglogic.de Thu Oct 1 11:10:58 2009 From: walter at livinglogic.de (=?UTF-8?B?V2FsdGVyIETDtnJ3YWxk?=) Date: Thu, 01 Oct 2009 17:10:58 +0200 Subject: unicode issue In-Reply-To: References: <9a67ac5b-0bec-4a95-a55f-fe44b6ed8d62@a21g2000yqc.googlegroups.com> Message-ID: <4AC4C682.3010300@livinglogic.de> On 01.10.09 16:09, Hyuga wrote: > On Sep 30, 3:34 am, gentlestone wrote: >> Why don't work this code on Python 2.6? Or how can I do this job? >> >> _MAP = { >> # LATIN >> u'?': 'A', u'?': 'A', u'?': 'A', u'?': 'A', u'?': 'A', u'?': 'A', >> u'?': 'AE', u'?':'C', >> u'?': 'E', u'?': 'E', u'?': 'E', u'?': 'E', u'?': 'I', u'?': 'I', >> u'?': 'I', >> u'?': 'I', u'?': 'D', u'?': 'N', u'?': 'O', u'?': 'O', u'?': 'O', >> u'?': 'O', u'?':'O', >> u'?': 'O', u'?': 'O', u'?': 'U', u'?': 'U', u'?': 'U', u'?': 'U', >> u'?': 'U', >> u'?': 'Y', u'?': 'TH', u'?': 'ss', u'?':'a', u'?':'a', u'?': 'a', >> u'?': 'a', u'?':'a', >> u'?': 'a', u'?': 'ae', u'?': 'c', u'?': 'e', u'?': 'e', u'?': 'e', >> u'?': 'e', >> u'?': 'i', u'?': 'i', u'?': 'i', u'?': 'i', u'?': 'd', u'?': 'n', >> u'?': 'o', u'?':'o', >> u'?': 'o', u'?': 'o', u'?': 'o', u'?': 'o', u'?': 'o', u'?': 'u', >> u'?': 'u', >> u'?': 'u', u'?': 'u', u'?': 'u', u'?': 'y', u'?': 'th', u'?': 'y', >> # LATIN_SYMBOLS >> u'?':'(c)', >> # GREEK >> u'?':'a', u'?':'b', u'?':'g', u'?':'d', u'?':'e', u'?':'z', >> u'?':'h', u'?':'8', >> u'?':'i', u'?':'k', u'?':'l', u'?':'m', u'?':'n', u'?':'3', >> u'?':'o', u'?':'p', >> u'?':'r', u'?':'s', u'?':'t', u'?':'y', u'?':'f', u'?':'x', >> u'?':'ps', u'?':'w', >> u'?':'a', u'?':'e', u'?':'i', u'?':'o', u'?':'y', u'?':'h', >> u'?':'w', u'?':'s', >> u'?':'i', u'?':'y', u'?':'y', u'?':'i', >> u'?':'A', u'?':'B', u'?':'G', u'?':'D', u'?':'E', u'?':'Z', >> u'?':'H', u'?':'8', >> u'?':'I', u'?':'K', u'?':'L', u'?':'M', u'?':'N', u'?':'3', >> u'?':'O', u'?':'P', >> u'?':'R', u'?':'S', u'?':'T', u'?':'Y', u'?':'F', u'?':'X', >> u'?':'PS', u'?':'W', >> u'?':'A', u'?':'E', u'?':'I', u'?':'O', u'?':'Y', u'?':'H', >> u'?':'W', u'?':'I', u'?':'Y', >> # TURKISH >> u'?':'s', u'?':'S', u'?':'i', u'?':'I', u'?':'c', u'?':'C', >> u'?':'u', u'?':'U', >> u'?':'o', u'?':'O', u'?':'g', u'?':'G', >> # RUSSIAN >> u'?':'a', u'?':'b', u'?':'v', u'?':'g', u'?':'d', u'?':'e', >> u'?':'yo', u'?':'zh', >> u'?':'z', u'?':'i', u'?':'j', u'?':'k', u'?':'l', u'?':'m', >> u'?':'n', u'?':'o', >> u'?':'p', u'?':'r', u'?':'s', u'?':'t', u'?':'u', u'?':'f', >> u'?':'h', u'?':'c', >> u'?':'ch', u'?':'sh', u'?':'sh', u'?':'', u'?':'y', u'?':'', >> u'?':'e', u'?':'yu', u'?':'ya', >> u'?':'A', u'?':'B', u'?':'V', u'?':'G', u'?':'D', u'?':'E', >> u'?':'Yo', u'?':'Zh', >> u'?':'Z', u'?':'I', u'?':'J', u'?':'K', u'?':'L', u'?':'M', >> u'?':'N', u'?':'O', >> u'?':'P', u'?':'R', u'?':'S', u'?':'T', u'?':'U', u'?':'F', >> u'?':'H', u'?':'C', >> u'?':'Ch', u'?':'Sh', u'?':'Sh', u'?':'', u'?':'Y', u'?':'', >> u'?':'E', u'?':'Yu', u'?':'Ya', >> # UKRAINIAN >> u'?':'Ye', u'?':'I', u'?':'Yi', u'?':'G', u'?':'ye', u'?':'i', >> u'?':'yi', u'?':'g', >> # CZECH >> u'?':'c', u'?':'d', u'?':'e', u'?':'n', u'?':'r', u'?':'s', >> u'?':'t', u'?':'u', >> u'?':'z', u'?':'C', u'?':'D', u'?':'E', u'?':'N', u'?':'R', >> u'?':'S', u'?':'T', u'?':'U', u'?':'Z', >> # POLISH >> u'?':'a', u'?':'c', u'?':'e', u'?':'l', u'?':'n', u'?':'o', >> u'?':'s', u'?':'z', >> u'?':'z', u'?':'A', u'?':'C', u'?':'e', u'?':'L', u'?':'N', >> u'?':'o', u'?':'S', >> u'?':'Z', u'?':'Z', >> # LATVIAN >> u'?':'a', u'?':'c', u'?':'e', u'?':'g', u'?':'i', u'?':'k', >> u'?':'l', u'?':'n', >> u'?':'s', u'?':'u', u'?':'z', u'?':'A', u'?':'C', u'?':'E', >> u'?':'G', u'?':'i', >> u'?':'k', u'?':'L', u'?':'N', u'?':'S', u'?':'u', u'?':'Z' >> >> } >> >> def downcode(name): >> """ >> >>> downcode(u"?abovit? zmie?an? ka?a") >> u'Zabovita zmiesana kasa' >> """ >> for key, value in _MAP.iteritems(): >> name = name.replace(key, value) >> return name > > Though C Python is pretty optimized under the hood for this sort of > single-character replacement, this still seems pretty inefficient > since you're calling replace for every character you want to map. I > think that a better approach might be something like: > > def downcode(name): > return ''.join(_MAP.get(c, c) for c in name) > > Or using string.translate: > > import string > def downcode(name): > table = string.maketrans( > '??????...', > 'AAAAAA...') > return name.translate(table) Or even simpler: import unicodedata def downcode(name): return unicodedata.normalize("NFD", name)\ .encode("ascii", "ignore")\ .decode("ascii") Servus, Walter From oyinbo55 at gmail.com Thu Oct 1 11:21:43 2009 From: oyinbo55 at gmail.com (oyinbo55) Date: Thu, 1 Oct 2009 08:21:43 -0700 (PDT) Subject: Pyserial non-standard baud rate Message-ID: <2feb36fc-106c-4d7c-a697-db59971dc910@a7g2000yqo.googlegroups.com> Hello all: this is my first post. I hope I'm doing it right. I have a digital multimeter that sends data through an RS232 interface at 19230 baud. I would like to record and graph the output in Python. Pyserial does not want me to set the baudrate at a non-standard value: >>>ser=serial.Serial('/dev/ttyUSB0' ,baudrate = 19230, bytesize = 7, parity = 'N', stopbits = 1) ValueError : Invalid baud rate : 19230 Using the standard 19200 baud results in gobbledegook from the multimeter. How can I connect to this device? I am using Python 2.5.1 running on Ubuntu 4.2.3 using IDLE 1.2.2 Thanks From lallous at lgwm.org Thu Oct 1 11:30:09 2009 From: lallous at lgwm.org (lallous) Date: Thu, 1 Oct 2009 17:30:09 +0200 Subject: emptying a list Message-ID: Hello What is faster when clearing a list? del L[:] or L = [] -- Elias From R.Brodie at rl.ac.uk Thu Oct 1 11:36:00 2009 From: R.Brodie at rl.ac.uk (Richard Brodie) Date: Thu, 1 Oct 2009 16:36:00 +0100 Subject: Pyserial non-standard baud rate References: <2feb36fc-106c-4d7c-a697-db59971dc910@a7g2000yqo.googlegroups.com> Message-ID: "oyinbo55" wrote in message news:2feb36fc-106c-4d7c-a697-db59971dc910 at a7g2000yqo.googlegroups.com... > Using the standard 19200 baud results in gobbledegook from the > multimeter. You aren't going to notice a 0.1% clock skew within 1 byte. Forget about the difference between 19200 and 19230. If you have a scope handy, see what the output waveform looks like, and check the timings. If not play around with the rates, parity etc., until you find something that works. From rami.chowdhury at gmail.com Thu Oct 1 11:50:15 2009 From: rami.chowdhury at gmail.com (Rami Chowdhury) Date: Thu, 01 Oct 2009 08:50:15 -0700 Subject: unicode issue In-Reply-To: <4AC4C682.3010300@livinglogic.de> References: <9a67ac5b-0bec-4a95-a55f-fe44b6ed8d62@a21g2000yqc.googlegroups.com> <4AC4C682.3010300@livinglogic.de> Message-ID: On Thu, 01 Oct 2009 08:10:58 -0700, Walter D?rwald wrote: > On 01.10.09 16:09, Hyuga wrote: >> On Sep 30, 3:34 am, gentlestone wrote: >>> Why don't work this code on Python 2.6? Or how can I do this job? >>> >>> [snip _MAP] >>> >>> def downcode(name): >>> """ >>> >>> downcode(u"?abovit? zmie?an? ka?a") >>> u'Zabovita zmiesana kasa' >>> """ >>> for key, value in _MAP.iteritems(): >>> name = name.replace(key, value) >>> return name >> >> Though C Python is pretty optimized under the hood for this sort of >> single-character replacement, this still seems pretty inefficient >> since you're calling replace for every character you want to map. I >> think that a better approach might be something like: >> >> def downcode(name): >> return ''.join(_MAP.get(c, c) for c in name) >> >> Or using string.translate: >> >> import string >> def downcode(name): >> table = string.maketrans( >> '??????...', >> 'AAAAAA...') >> return name.translate(table) > > Or even simpler: > > import unicodedata > > def downcode(name): > return unicodedata.normalize("NFD", name)\ > .encode("ascii", "ignore")\ > .decode("ascii") > > Servus, > Walter As I understand it, the "ignore" argument to str.encode *removes* the undecodable characters, rather than replacing them with an ASCII approximation. Is that correct? If so, wouldn't that rather defeat the purpose? -- Rami Chowdhury "Never attribute to malice that which can be attributed to stupidity" -- Hanlon's Razor 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD) From __peter__ at web.de Thu Oct 1 12:01:11 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 01 Oct 2009 18:01:11 +0200 Subject: unicode issue References: <9a67ac5b-0bec-4a95-a55f-fe44b6ed8d62@a21g2000yqc.googlegroups.com> <4AC4C682.3010300@livinglogic.de> Message-ID: Rami Chowdhury wrote: > On Thu, 01 Oct 2009 08:10:58 -0700, Walter D?rwald > wrote: > >> On 01.10.09 16:09, Hyuga wrote: >>> On Sep 30, 3:34 am, gentlestone wrote: >>>> Why don't work this code on Python 2.6? Or how can I do this job? >>>> >>>> [snip _MAP] >>>> >>>> def downcode(name): >>>> """ >>>> >>> downcode(u"?abovit? zmie?an? ka?a") >>>> u'Zabovita zmiesana kasa' >>>> """ >>>> for key, value in _MAP.iteritems(): >>>> name = name.replace(key, value) >>>> return name >>> >>> Though C Python is pretty optimized under the hood for this sort of >>> single-character replacement, this still seems pretty inefficient >>> since you're calling replace for every character you want to map. I >>> think that a better approach might be something like: >>> >>> def downcode(name): >>> return ''.join(_MAP.get(c, c) for c in name) >>> >>> Or using string.translate: >>> >>> import string >>> def downcode(name): >>> table = string.maketrans( >>> '??????...', >>> 'AAAAAA...') >>> return name.translate(table) >> >> Or even simpler: >> >> import unicodedata >> >> def downcode(name): >> return unicodedata.normalize("NFD", name)\ >> .encode("ascii", "ignore")\ >> .decode("ascii") >> >> Servus, >> Walter > > As I understand it, the "ignore" argument to str.encode *removes* the > undecodable characters, rather than replacing them with an ASCII > approximation. Is that correct? If so, wouldn't that rather defeat the > purpose? You didn't take the normalization step into your consideration. Example: >>> import unicodedata >>> s = u"?" >>> unicodedata.normalize("NFD", s) u'A\u0308' >>> _.encode("ascii", "ignore") 'A' From walter at livinglogic.de Thu Oct 1 12:03:38 2009 From: walter at livinglogic.de (=?UTF-8?B?V2FsdGVyIETDtnJ3YWxk?=) Date: Thu, 01 Oct 2009 18:03:38 +0200 Subject: unicode issue In-Reply-To: References: <9a67ac5b-0bec-4a95-a55f-fe44b6ed8d62@a21g2000yqc.googlegroups.com> <4AC4C682.3010300@livinglogic.de> Message-ID: <4AC4D2DA.5080701@livinglogic.de> On 01.10.09 17:50, Rami Chowdhury wrote: > On Thu, 01 Oct 2009 08:10:58 -0700, Walter D?rwald > wrote: > >> On 01.10.09 16:09, Hyuga wrote: >>> On Sep 30, 3:34 am, gentlestone wrote: >>>> Why don't work this code on Python 2.6? Or how can I do this job? >>>> >>>> [snip _MAP] >>>> >>>> def downcode(name): >>>> """ >>>> >>> downcode(u"?abovit? zmie?an? ka?a") >>>> u'Zabovita zmiesana kasa' >>>> """ >>>> for key, value in _MAP.iteritems(): >>>> name = name.replace(key, value) >>>> return name >>> >>> Though C Python is pretty optimized under the hood for this sort of >>> single-character replacement, this still seems pretty inefficient >>> since you're calling replace for every character you want to map. I >>> think that a better approach might be something like: >>> >>> def downcode(name): >>> return ''.join(_MAP.get(c, c) for c in name) >>> >>> Or using string.translate: >>> >>> import string >>> def downcode(name): >>> table = string.maketrans( >>> '??????...', >>> 'AAAAAA...') >>> return name.translate(table) >> >> Or even simpler: >> >> import unicodedata >> >> def downcode(name): >> return unicodedata.normalize("NFD", name)\ >> .encode("ascii", "ignore")\ >> .decode("ascii") >> >> Servus, >> Walter > > As I understand it, the "ignore" argument to str.encode *removes* the > undecodable characters, rather than replacing them with an ASCII > approximation. Is that correct? If so, wouldn't that rather defeat the > purpose? Yes, but any accented characters have been split into the base character and the combining accent via normalize() before, so only the accent gets removed. Of course non-decomposable characters will be removed completely, but it would be possible to replace .encode("ascii", "ignore").decode("ascii") with something like this: u"".join(c for c in name if unicodedata.category(c) == "Mn") Servus, Walter From rami.chowdhury at gmail.com Thu Oct 1 12:06:15 2009 From: rami.chowdhury at gmail.com (Rami Chowdhury) Date: Thu, 01 Oct 2009 09:06:15 -0700 Subject: unicode issue In-Reply-To: <4AC4D2DA.5080701@livinglogic.de> References: <9a67ac5b-0bec-4a95-a55f-fe44b6ed8d62@a21g2000yqc.googlegroups.com> <4AC4C682.3010300@livinglogic.de> <4AC4D2DA.5080701@livinglogic.de> Message-ID: On Thu, 01 Oct 2009 09:03:38 -0700, Walter D?rwald wrote: > > Yes, but any accented characters have been split into the base character > and the combining accent via normalize() before, so only the accent gets > removed. Of course non-decomposable characters will be removed > completely, but it would be possible to replace > > .encode("ascii", "ignore").decode("ascii") > > with something like this: > > u"".join(c for c in name if unicodedata.category(c) == "Mn") > > Servus, > Walter Thank you for the clarification! -- Rami Chowdhury "Never attribute to malice that which can be attributed to stupidity" -- Hanlon's Razor 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD) From geoffrey.clementsNO at SPAMbaesystems.com Thu Oct 1 12:15:00 2009 From: geoffrey.clementsNO at SPAMbaesystems.com (Geoffrey Clements) Date: Thu, 1 Oct 2009 17:15:00 +0100 Subject: emptying a list References: Message-ID: <4ac4d56e$1_1@glkas0286.greenlnk.net> "lallous" wrote in message news:ha2htc$u9u$1 at aioe.org... > Hello > > What is faster when clearing a list? > > del L[:] > > or > > L = [] > Oh, "L = []" definitely, on the basis that there are fewer characters to type. http://docs.python.org/3.1/library/profile.html -- Geoff From joncle at googlemail.com Thu Oct 1 12:34:13 2009 From: joncle at googlemail.com (Jon Clements) Date: Thu, 1 Oct 2009 09:34:13 -0700 (PDT) Subject: cx_freeze problem on Ubuntu References: Message-ID: <1d71d6fc-315c-401a-a7a3-52bd8d8abf97@d10g2000yqh.googlegroups.com> On 1 Oct, 15:08, John wrote: > Sorry if this might be a repost. ?I'm having problems with my newsreader. > > My system: > > cx_freeze 4.1 > Python 2.6 > Ubuntu Jaunty > > I downloaded the cx_freeze source code fromhttp://cx-freeze.sourceforge.net/into a directory. > > I wrote a one line python program 'print( "hello world" )' > > According to the instructions, all I have to do is this: > > ./cxfreeze hello.py > > But I get this error: > > ./cxfreeze hello.py > Traceback (most recent call last): > File "./cxfreeze", line 5, in > main() > File "/root/cx_Freeze-4.1/cx_Freeze/main.py", line 178, in main > silent = options.silent) > File "/root/cx_Freeze-4.1/cx_Freeze/freezer.py", line 85, in __init__ > self._VerifyConfiguration() > File "/root/cx_Freeze-4.1/cx_Freeze/freezer.py", line 325, in > _VerifyConfiguration > self._GetInitScriptFileName() > File "/root/cx_Freeze-4.1/cx_Freeze/freezer.py", line 246, in > _GetInitScriptFileName > raise ConfigError("no initscript named %s", name) > cx_Freeze.freezer.ConfigError: no initscript named Console > > ?From here:http://linux.softpedia.com/get/Programming/Assembler-Tools/cx-Freeze-... > the directions state: > > Installation: > > "Extract the archive into a directory. In order to build cx_Freeze you > need to have gcc and Python development include files and libraries > available. Run the following commands. > > python MakeFrozenBases.py > python FreezePython.py --no-copy-deps FreezePython.py" > > I can't find either of these Python programs and searching the Internet > for clues was fruitless. > > I thinking that I'm missing something that's painfully obvious. > > As far as I can tell, no one else has ever posted this problem so I > don't know what corrective actions to make. ?I'd greatly appreciate any > assistance. Firstly -- never used the library, hadn't even heard of it -- disclaimer over :) Err, python 2.6 doesn't have print as a function by default. Have you done a from __future__ import ? def _GetInitScriptFileName(self, argsSource = None): if argsSource is None: argsSource = self name = argsSource.initScript if name is None: if argsSource.copyDependentFiles: name = "Console" (****) else: name = "ConsoleKeepPath" if sys.version_info[0] >= 3: name += "3" argsSource.initScript = self._GetFileName("initscripts", name) if argsSource.initScript is None: raise ConfigError("no initscript named %s", name) Should be sufficient clues(****), Cheers, Jon From joncle at googlemail.com Thu Oct 1 12:43:58 2009 From: joncle at googlemail.com (Jon Clements) Date: Thu, 1 Oct 2009 09:43:58 -0700 (PDT) Subject: emptying a list References: Message-ID: <96ea8f01-6cc6-4629-af49-ba249479ed38@l13g2000yqb.googlegroups.com> On 1 Oct, 16:30, "lallous" wrote: > Hello > > What is faster when clearing a list? > > del L[:] > > or > > L = [] > > -- > Elias Does it really matter that much? And you're really talking about two different things, which quite often come up on this group. Example follows: >>> x = range(5) >>> x = y >>> print x, y [1, 2, 3, 4] [1, 2, 3, 4] >>> x = [] >>> print x, y [] [1, 2, 3, 4] >>> x = y >>> print x, y [1, 2, 3, 4] [1, 2, 3, 4] >>> del x[:] >>> print x, y [] [] Cheers, Jon. From mcfletch at vrplumber.com Thu Oct 1 12:57:54 2009 From: mcfletch at vrplumber.com (Mike C. Fletcher) Date: Thu, 01 Oct 2009 12:57:54 -0400 Subject: PyOpenGL and graphics card support In-Reply-To: <8241f105-aa73-44fc-96fb-a09bf4f2d5e1@v15g2000prn.googlegroups.com> References: <8241f105-aa73-44fc-96fb-a09bf4f2d5e1@v15g2000prn.googlegroups.com> Message-ID: <4AC4DF92.60807@vrplumber.com> jefm wrote: > these are the imports I use: > > from OpenGL.GL import * > from OpenGL.GLUT import * > from OpenGL.GLU import * > PyOpenGL will use the default OpenGL renderer for your system, however, before you have an OpenGL context (rendering window) the system can report whatever the heck it wants to for the various values (in this case looks like it is reporting the generic microsoft renderer until you ask for a context). Try this instead: from OpenGL.GL import * from OpenGL.GLUT import * from OpenGL.GLU import * resX,resY = (400,300 ) if __name__ == "__main__": glutInit([]) glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(resX, resY) glutInitWindowPosition(0, 0) window = glutCreateWindow("hello") for name in (GL_VENDOR,GL_RENDERER,GL_SHADING_LANGUAGE_VERSION,GL_EXTENSIONS): print name,glGetString(name) HTH, Mike -- ________________________________________________ Mike C. Fletcher Designer, VR Plumber, Coder http://www.vrplumber.com http://blog.vrplumber.com From no.email at please.post Thu Oct 1 13:08:33 2009 From: no.email at please.post (kj) Date: Thu, 1 Oct 2009 17:08:33 +0000 (UTC) Subject: Q: sort's key and cmp parameters Message-ID: Challenge: to come up with a sorting task that cannot be achieved by passing to the sort method (or sorted function) suitable values for its key and reverse parameters, but instead *require* giving a value to its cmp parameter. For example, from random import random scrambled = some_list.sort(cmp=lambda x, y: cmp(random(), 0.5)) can be achieved (to a very good approximation at least) with scrambled = some_list.sort(key=lambda x: random()) Is there a real-life sorting task that requires (or is far more efficient with) cmp and can't be easily achieved with key and reverse? Many thanks in advance, G. P.S. I guess that, if sort is going to produce a non-trivial, *consistent* order, a function CMP passed as the value of its cmp parameter must satisfy the following properties: 0. CMP(x, y) must be non-zero for some elements x, y of the list; 1. anti-symmetry: sign(CMP(x, y)) must be equal to -sign(CMP(y, x)); 2. transitivity: if sign(CMP(x, y)) equals sign(CMP(y, z)), then sign(CMP(x, z)) must be equal to sign(CMP(x, y)). In (1) and (2), sign() stands for the function -1 if x < 0 sign(x) = 0 if x == 0 1 if x > 0 I suppose that all bets are off if these properties are not satisfied, though the documentation does not make this entirely clear, as far as I can tell. (If I'm wrong about this alleged omission, please point me to the part of the docs that I missed.) From nagle at animats.com Thu Oct 1 13:13:52 2009 From: nagle at animats.com (John Nagle) Date: Thu, 01 Oct 2009 10:13:52 -0700 Subject: M2Crypto 0.20.1 won't build on Red Hat Linux In-Reply-To: References: <4ac39492$0$1642$742ec2ed@news.sonic.net> Message-ID: <4ac4e173$0$1674$742ec2ed@news.sonic.net> Heikki Toivonen wrote: > John Nagle wrote: >> M2Crypto, from >> >> http://pypi.python.org/packages/source/M/M2Crypto/M2Crypto-0.20.1.tar.gz >> >> won't build on Red Hat Linux / 386. The error is >> >> It's some incompatibility between Red Hat include file packaging and >> M2Crypto. > > Yup, all Fedora Core-based systems actually. > > > I've also added this to the FAQ: > http://chandlerproject.org/Projects/MeTooCrypto#Code OK, did that. The build is now failing with -bash-3.1$ ./fedora-setup.sh build running build running build_py creating build ... building 'M2Crypto.__m2crypto' extension swigging SWIG/_m2crypto.i to SWIG/_m2crypto_wrap.c swig -python -I/usr/local/include/python2.5 -I/usr/include -includeall -o SWIG/_m2crypto_wrap.c SWIG/_m2crypto.i SWIG/_evp.i:9: Error: Unable to find 'openssl/opensslconf-i686.h' SWIG/_ec.i:7: Error: Unable to find 'openssl/opensslconf-i686.h' error: command 'swig' failed with exit status 1 -bash-3.1$ Installed OpenSSL is "OpenSSL 0.9.8a 11 Oct 2005", and it doesn't have an "openssl/opensslconf-i686.h". It has an "openssl/opensslconf-i386.h". M2Crypto documentation says: 0.20.1: * OpenSSL 0.9.7 or newer o Some optional new features will require OpenSSL 0.9.8 or newer so that's current. The problem, of course, is that using "uname -m" is asking the wrong question. It prints "i686" on CPUs that are 64-bit capable, even when they are running a 32-bit version of Linux. This is running on a 64-bit capable server. The right question is "uname --hardware-platform". That returns "i386" if running on something emulating a 386, even it it's 64-bit capable. With that change, the build runs to completion and and the regression tests mostly run. Test failure occurred at: ====================================================================== FAIL: test_cipher_ok (tests.test_ssl.MiscSSLClientTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/var/www/vhosts/sitetruth.com/private/downloads/M2Crypto/M2Crypto-0.20.1/tests/test_ssl.py", line 485, in test_cipher_ok assert s.get_cipher().name() == 'AES128-SHA', s.get_cipher().name() AssertionError: AES256-SHA ---------------------------------------------------------------------- Ran 226 tests in 91.672s Is 'AES128-SHA' a cypher that Fedora Core doesn't support, or what? John Nagle From __peter__ at web.de Thu Oct 1 13:51:02 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 01 Oct 2009 19:51:02 +0200 Subject: Q: sort's key and cmp parameters References: Message-ID: kj wrote: > Challenge: to come up with a sorting task that cannot be achieved > by passing to the sort method (or sorted function) suitable values > for its key and reverse parameters, but instead *require* giving > a value to its cmp parameter. > > For example, > > from random import random > scrambled = some_list.sort(cmp=lambda x, y: cmp(random(), 0.5)) The above violates the transitivity requirement as stated below. The result will have a bias. > can be achieved (to a very good approximation at least) with > > scrambled = some_list.sort(key=lambda x: random()) > > Is there a real-life sorting task that requires (or is far more > efficient with) cmp and can't be easily achieved with key and > reverse? The core developers don't think there is one. list.sort() no longer supports the cmp parameter in 3.x. > Many thanks in advance, > > G. > > P.S. I guess that, if sort is going to produce a non-trivial, > *consistent* order, a function CMP passed as the value of its cmp > parameter must satisfy the following properties: > > 0. CMP(x, y) must be non-zero for some elements x, y of the list; > 1. anti-symmetry: sign(CMP(x, y)) must be equal to -sign(CMP(y, x)); > 2. transitivity: if sign(CMP(x, y)) equals sign(CMP(y, z)), then > sign(CMP(x, z)) must be equal to sign(CMP(x, y)). > > In (1) and (2), sign() stands for the function > > -1 if x < 0 > sign(x) = 0 if x == 0 > 1 if x > 0 > > I suppose that all bets are off if these properties are not satisfied, > though the documentation does not make this entirely clear, as far > as I can tell. (If I'm wrong about this alleged omission, please > point me to the part of the docs that I missed.) From gandalf at shopzeus.com Thu Oct 1 13:51:14 2009 From: gandalf at shopzeus.com (Laszlo Nagy) Date: Thu, 01 Oct 2009 19:51:14 +0200 Subject: Q: sort's key and cmp parameters In-Reply-To: References: Message-ID: <4AC4EC12.5050608@shopzeus.com> Is this a homework? > Challenge: to come up with a sorting task that cannot be achieved > by passing to the sort method (or sorted function) suitable values > for its key and reverse parameters, but instead *require* giving > a value to its cmp parameter. > Let me put up this question: how do you define the difference between an object to be sorted in the list, and the passed "suitable value" that is used for finding the position for the object in the list. If the only difference is that they need to be different Python objects, then you can always use [x] instead of x. This value is perfectly suitable. ;-) If you could give us somewhat stronger requirements regarding keys and objects, we may give you a different answer. Best, Laszlo From gandalf at shopzeus.com Thu Oct 1 14:01:42 2009 From: gandalf at shopzeus.com (Laszlo Nagy) Date: Thu, 01 Oct 2009 20:01:42 +0200 Subject: AJAX Widget Framework Message-ID: <4AC4EE86.7020802@shopzeus.com> I'm looking for an open source, AJAX based widget/windowing framework. Here is what I need: - end user opens up a browser, points it to a URL, logs in - on the server site, sits my application, creating a new session for each user that is logged in - on the server site, I create windows(frames), put widgets on them, write event handlers etc. Just like with wx or pygtk. - However, windows are created in the user's browser, and events are triggered by Javascript, and sent back to server through AJAX. - the server side would be - of course - written in Python. I was looking these projects: http://www.uize.com/ http://pyjs.org/ There are many frameworks listed here which I did not check: http://internetmindmap.com/javascript_frameworks. I have no idea which has Python support, and probably there are only a few that worth looking at. I wonder if you could tell me which are the pros and contras for these frameworks. If there is a comparison available on the NET, pointing me to the right URL would help a lot. The bottom line... My main goal is to develop enterprise class OLTP database applications. I'll need grid widget to display data in tabular format, and I'll use events heavily for data manipulation, live search, tooltips etc. I'm familiar with wxWidgets, pygtk and other toolkits, but not with AJAX. I have never used a system like that. Thank you, Laszlo From gandalf at shopzeus.com Thu Oct 1 14:05:21 2009 From: gandalf at shopzeus.com (Laszlo Nagy) Date: Thu, 01 Oct 2009 20:05:21 +0200 Subject: Q: sort's key and cmp parameters In-Reply-To: References: Message-ID: <4AC4EF61.3020509@shopzeus.com> >> can be achieved (to a very good approximation at least) with >> >> scrambled = some_list.sort(key=lambda x: random()) >> >> Is there a real-life sorting task that requires (or is far more >> efficient with) cmp and can't be easily achieved with key and >> reverse? >> > > The core developers don't think there is one. list.sort() no longer supports > the cmp parameter in 3.x. > LOL :-D BTW if you really want to sort a list then you can. Using keys or values, it doesn't matter. The op's question has no practical usage. REALLY looks like a homework. L From sajmikins at gmail.com Thu Oct 1 14:10:48 2009 From: sajmikins at gmail.com (Simon Forman) Date: Thu, 1 Oct 2009 14:10:48 -0400 Subject: How different are a generator's send and next methods In-Reply-To: <7659cab30909301324v7438ca2atb060b5acff75c413@mail.gmail.com> References: <7659cab30909301324v7438ca2atb060b5acff75c413@mail.gmail.com> Message-ID: <50f98a4c0910011110x53b6d790sc45530b78252c78@mail.gmail.com> On Wed, Sep 30, 2009 at 4:24 PM, Andrey Fedorov wrote: > As far as I can tell, a generator's .next() is equivalent to .send(None). Is > this true? They are equivalent AFAIK. > If so, [why] aren't they unified in a method with a single argument which defaults > to None? > - Andrey next() predates send(). From ethan at stoneleaf.us Thu Oct 1 14:21:38 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 01 Oct 2009 11:21:38 -0700 Subject: Q: sort's key and cmp parameters In-Reply-To: <4AC4EF61.3020509@shopzeus.com> References: <4AC4EF61.3020509@shopzeus.com> Message-ID: <4AC4F332.10905@stoneleaf.us> Laszlo Nagy wrote: > >>> can be achieved (to a very good approximation at least) with >>> >>> scrambled = some_list.sort(key=lambda x: random()) >>> >>> Is there a real-life sorting task that requires (or is far more >>> efficient with) cmp and can't be easily achieved with key and >>> reverse? >>> >> >> >> The core developers don't think there is one. list.sort() no longer >> supports the cmp parameter in 3.x. >> > > LOL :-D > > BTW if you really want to sort a list then you can. Using keys or > values, it doesn't matter. The op's question has no practical usage. > REALLY looks like a homework. > > L > If it is, it's one he's contemplating giving his students. :-D ~Ethan~ From carsten.haese at gmail.com Thu Oct 1 14:26:41 2009 From: carsten.haese at gmail.com (Carsten Haese) Date: Thu, 01 Oct 2009 14:26:41 -0400 Subject: Q: sort's key and cmp parameters In-Reply-To: References: Message-ID: kj wrote: > > Challenge: to come up with a sorting task that cannot be achieved > by passing to the sort method (or sorted function) suitable values > for its key and reverse parameters, but instead *require* giving > a value to its cmp parameter. Such a task can't exist, because any arbitrary comparison function can be transformed into a key function. The idea behind the transformation is to construct wrapper objects that can compare themselves to other wrapper objects by invoking the given comparison function on their wrapped originals. Google for "Hettinger cmp2key" if you want to see the code that does this transformation. HTH, -- Carsten Haese http://informixdb.sourceforge.net From sajmikins at gmail.com Thu Oct 1 14:27:29 2009 From: sajmikins at gmail.com (Simon Forman) Date: Thu, 1 Oct 2009 14:27:29 -0400 Subject: iterate over list while changing it In-Reply-To: References: Message-ID: <50f98a4c0910011127v1ddd57efmf7a6c010cbf8ecfa@mail.gmail.com> On Wed, Sep 30, 2009 at 11:19 PM, Daniel Stutzbach wrote: > On Thu, Sep 24, 2009 at 3:32 PM, Torsten Mohr wrote: >> >> a = [1, 2, 3, 4, 5, 6] >> >> for i, x in enumerate(a): >> ? ?if x == 3: >> ? ? ? ?a.pop(i) >> ? ? ? ?continue >> >> ? ?if x == 4: >> ? ? ? ?a.push(88) >> >> ? ?print "i", i, "x", x >> >> I'd like to iterate over a list and change that list while iterating. >> I'd still like to work on all items in that list, which is not happening >> in the example above. > > I assume that by "a.push" you meant "a.append". > > I believe this will do what you want: > > a = [1, 2, 3, 4, 5, 6] > i = 0 > while i < len(a): > ?? x = a[i] > ?? if x == 3: > ? ? ? ?a.pop(i) > ?????? i += 1 > ? ? ? ?continue > > ? ?if x == 4: > ? ? ? ?a.append(88) > > ? ?print "i", i, "x", x > ?? i += 1 > > -- > Daniel Stutzbach, Ph.D. > President, Stutzbach Enterprises, LLC > In the case of x == 3 when you remove an item from the list you should not increment the index. From sajmikins at gmail.com Thu Oct 1 14:34:22 2009 From: sajmikins at gmail.com (Simon Forman) Date: Thu, 1 Oct 2009 14:34:22 -0400 Subject: hii In-Reply-To: <8512a6f90910010455u6eb3e86ag2173183aef924f4c@mail.gmail.com> References: <8512a6f90910010455u6eb3e86ag2173183aef924f4c@mail.gmail.com> Message-ID: <50f98a4c0910011134w1449546ei75ab58200af45619@mail.gmail.com> On Thu, Oct 1, 2009 at 7:55 AM, padmapriya sekaran wrote: > I have a problem in using interp from numpy for which i need 3 array. > my first array is > x = scipy.linspace(0.009,0.53,100) > > and the other two array should be read from my file with 100x2 dimension > (file1_lines) but it is read as string instead of two columns > > for line in file1_lines: > ??????? line = line.split() > ?? ? ?? xp = line[0] > ?? ? ?? yp = line[1] > ??????? y = scipy.interp(x,xp,yp) > if i code like this it takes only one value for xp and yp but 100 values for > x. i dont know what to do please help. It is difficult to say without a sample of the file data. If you would please provide some? Also, once you are sure the lines are being split correctly, you can just say: xp, yp = line.split() From http Thu Oct 1 14:41:22 2009 From: http (Paul Rubin) Date: 01 Oct 2009 11:41:22 -0700 Subject: Q: sort's key and cmp parameters References: Message-ID: <7x1vln2bzh.fsf@ruckus.brouhaha.com> kj writes: > Is there a real-life sorting task that requires (or is far more > efficient with) cmp and can't be easily achieved with key and > reverse? Yes, think of sorting tree structures where you have to recursively compare them til you find an unequal pair of nodes. To sort with key=... you have to wrap a class instance around each tree, with special comparison methods. Blecch. From bearophileHUGS at lycos.com Thu Oct 1 15:39:23 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Thu, 1 Oct 2009 12:39:23 -0700 (PDT) Subject: Q: sort's key and cmp parameters References: <7x1vln2bzh.fsf@ruckus.brouhaha.com> Message-ID: Paul Rubin: > Yes, think of sorting tree structures where you have to recursively > compare them til you find an unequal pair of nodes. That's cute. In what situations do you have to perform such kind of sort? Bye, bearophile From aahz at pythoncraft.com Thu Oct 1 15:41:15 2009 From: aahz at pythoncraft.com (Aahz) Date: 1 Oct 2009 12:41:15 -0700 Subject: What does the list_folders() method of mailbox.Maildir actually ?do (if anything)? References: Message-ID: In article , wrote: >Tim Roberts wrote: >> tinnews at isbd.co.uk wrote: >>> >>>My maildir hierarchy is created by mutt which is a *very* standards >>>compliant MUA, surely standard python libraries should work with >>>standard maildirs not some wierd extension thereof. >> >> The Maildir specification does not allow for subfolders. That was added in >> Maildir++, and the subfolder names start with a dot. It's not a "wierd >> extension thereof". > >No sub-folders within maildir folders but that's not what I was expecting. > >I was just expecting to be able to list my maildir mailboxes. Now you know why *my* mutt uses mboxes. ;-) -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "....Normal is what cuts off your sixth finger and your tail..." --Siobhan From aahz at pythoncraft.com Thu Oct 1 15:49:13 2009 From: aahz at pythoncraft.com (Aahz) Date: 1 Oct 2009 12:49:13 -0700 Subject: Unofficial Python GIS SIG References: <2edb4c08-5853-4d21-9bcc-5895c431250b@l13g2000yqb.googlegroups.com> Message-ID: In article <2edb4c08-5853-4d21-9bcc-5895c431250b at l13g2000yqb.googlegroups.com>, sean.gillies at gmail.com wrote: > >There's growing interest among GIS users of Python for a discussion >group, so I've started an unofficial Python GIS SIG at: >http://groups.google.com/group/python-gis-sig. Please join if you're >interested in improving Python's GIS story. At any rate, the group >should (hopefully) reduce the number of GIS questions that get >mistakenly posted to Web-SIG ("GIS" is commonly known as "SIG" outside >the US/UK). Any particular reason you didn't create a list @python.org? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "....Normal is what cuts off your sixth finger and your tail..." --Siobhan From duncan.booth at invalid.invalid Thu Oct 1 15:55:03 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 1 Oct 2009 19:55:03 GMT Subject: Q: sort's key and cmp parameters References: Message-ID: kj wrote: > Is there a real-life sorting task that requires (or is far more > efficient with) cmp and can't be easily achieved with key and > reverse? > There is no sorting task that *requires* cmp. If all else fails you can define a key class to wrap the original wrapper such that comparing the keys simply calls the comparison function that you *would* have used. This has been discussed before and if you google sufficiently you should find the code that has been posted to do exactly that. From baloand at gmail.com Thu Oct 1 16:19:18 2009 From: baloand at gmail.com (Andreas Balogh) Date: Thu, 01 Oct 2009 22:19:18 +0200 Subject: accessing dictionary keys Message-ID: Hello, when building a list of points like points = [ ] points.append((1, 2)) points.append((2, 3)) point = points[0] eventually I'd like to access the tuple contents in a more descriptive way, for example: print point.x, point.y but instead I have to write (not very legible) print point[0], point[1] Note: I am using Python 2.5 Well, I can use a dictionary: points.append({"x": 1, "y": 2}) When accessing values more typing is involved: print point["x"], point["y"] Or I can use a Bunch (see ActiveState cookbooks): class Bunch: def __init__(self, **kwds): self.__dict__.update(kwds) and do it like this: points.append(Bunch(x=4, y=5)) print points[-1].x, points[-1].y With the bunch at least all the quotes go away. Is there any shortcut which allows to use point.x with a dictionary, or defining keys with tuples and lists? Regards, Andreas -- Andreas Balogh baloand (at) gmail.com From carsten.haese at gmail.com Thu Oct 1 16:34:49 2009 From: carsten.haese at gmail.com (Carsten Haese) Date: Thu, 01 Oct 2009 16:34:49 -0400 Subject: accessing dictionary keys In-Reply-To: References: Message-ID: Andreas Balogh wrote: > Hello, > > when building a list of points like > > points = [ ] > points.append((1, 2)) > points.append((2, 3)) > > point = points[0] > > eventually I'd like to access the tuple contents in a more descriptive > way, for example: > > print point.x, point.y I'm not sure exactly what you're asking, but maybe the following code will serve as a zeroth iteration toward the solution you seek: py> class Point(object): ... def __init__(self, x, y): ... self.x = x ... self.y = y ... py> points = [] py> points.append(Point(1, 2)) py> points.append(Point(2, 3)) py> py> point = points[0] py> print point.x, point.y 1 2 HTH, -- Carsten Haese http://informixdb.sourceforge.net From rami.chowdhury at gmail.com Thu Oct 1 16:35:16 2009 From: rami.chowdhury at gmail.com (Rami Chowdhury) Date: Thu, 01 Oct 2009 13:35:16 -0700 Subject: accessing dictionary keys In-Reply-To: References: Message-ID: On Thu, 01 Oct 2009 13:19:18 -0700, Andreas Balogh wrote: > > Is there any shortcut which allows to use point.x with a dictionary, or > defining keys with tuples and lists? > > Regards, Andreas > It sounds like you want collections.namedtuple (Python 2.6 and up; recipe for Python 2.4 or 2.5 at http://code.activestate.com/recipes/500261/)? HTH, Rami -- Rami Chowdhury "Never attribute to malice that which can be attributed to stupidity" -- Hanlon's Razor 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD) From malaclypse2 at gmail.com Thu Oct 1 16:44:57 2009 From: malaclypse2 at gmail.com (Jerry Hill) Date: Thu, 1 Oct 2009 16:44:57 -0400 Subject: accessing dictionary keys In-Reply-To: References: Message-ID: <16651e80910011344q4f01b63bhcf544a2b37ecfd26@mail.gmail.com> On Thu, Oct 1, 2009 at 4:19 PM, Andreas Balogh wrote: > Is there any shortcut which allows to use point.x with a dictionary, or > defining keys with tuples and lists? A namedtuple (introduced in python 2.6), acts like a tuple with named fields. Here's an example: >>> from collections import namedtuple >>> Point = namedtuple('Point', 'x y') >>> points = [] >>> points.append(Point(1, 2)) >>> points [Point(x=1, y=2)] >>> points.append(Point(2, 3)) >>> points [Point(x=1, y=2), Point(x=2, y=3)] >>> points[0].x 1 >>> points[0].y 2 >>> points[0][0] 1 >>> points[0][1] 2 >>> See http://docs.python.org/library/collections.html#collections.namedtuple for more information. I believe there is a recipe in the online python cookbook that provides this same functionality for earlier versions of python. -- Jerry From python at rcn.com Thu Oct 1 16:54:35 2009 From: python at rcn.com (Raymond Hettinger) Date: Thu, 1 Oct 2009 13:54:35 -0700 (PDT) Subject: Q: sort's key and cmp parameters References: Message-ID: On Oct 1, 10:08?am, kj wrote: > Challenge: to come up with a sorting task that cannot be achieved > by passing to the sort method (or sorted function) suitable values > for its key and reverse parameters, but instead *require* giving > a value to its cmp parameter. If you're assuming a consistent sort-order (transitivity, not evolving over time, etc), then the cmp method and key method are mathematically equivalent (you could always do a compare sort first, record the order produced, and assign the position number as a key function): # constructive proof of the existence of a key function # corresponding to a given cmp function tmplist = sorted(s, cmp=mycmp) pos = dict(zip(map(id, tmplist), range(len(tmplist)))) result = sorted(s, key=lambda x: pos[id(x)]) assert tmplist == result Given equivalence, what is really at stake is convenience. If you're starting point is a cmp function (for instance, asking a dating service member whether they prefer mate x to mate y), then having to convert to a key function can be inconvenient. If you need to sort by an ascending primary key and a descending secondary key, you may find it easiest to sort in two passes (taking advantage of guaranteed sort stability): sorted(s, key=secondary, reversed=True) sorted(s, key=primary) With a cmp function, the above could be achieved in a single pass: def mycmp(x, y): p = cmp(primary(a), primary(b)) return p if p else cmp(secondary(b), secondary(a)) sorted(s, cmp=mycmp) Also, as Paul pointed-out, there are some structures such as tree comparisons that may be challenging to express directly as a key function. As Carsten pointed-out, the cmp2key function allows cmp functions to trivially (though indirectly) be recast as key functions. All that being said, for many everyday uses, a key function is simpler to write and faster to run than a cmp function approach. Raymond From sean.gillies at gmail.com Thu Oct 1 18:05:23 2009 From: sean.gillies at gmail.com (seang) Date: Thu, 1 Oct 2009 15:05:23 -0700 (PDT) Subject: Unofficial Python GIS SIG References: <2edb4c08-5853-4d21-9bcc-5895c431250b@l13g2000yqb.googlegroups.com> Message-ID: <07bd0443-5e60-4661-b03d-1db1b5de6729@k4g2000yqb.googlegroups.com> On Oct 1, 9:49?pm, a... at pythoncraft.com (Aahz) wrote: > In article <2edb4c08-5853-4d21-9bcc-5895c4312... at l13g2000yqb.googlegroups.com>, > > sean.gill... at gmail.com wrote: > > >There's growing interest among GIS users of Python for a discussion > >group, so I've started an unofficial Python GIS SIG at: > >http://groups.google.com/group/python-gis-sig. Please join if you're > >interested in improving Python's GIS story. At any rate, the group > >should (hopefully) reduce the number of GIS questions that get > >mistakenly posted to Web-SIG ("GIS" is commonly known as "SIG" outside > >the US/UK). > > Any particular reason you didn't create a list @python.org? > -- > Aahz (a... at pythoncraft.com) ? ? ? ? ? <*> ? ? ? ?http://www.pythoncraft.com/ > > "....Normal is what cuts off your sixth finger and your tail..." ?--Siobhan Only that I am not entirely sure this will take off and don't want to leave python.org with a defunct list, and I'm unfamiliar with what's involved with starting a list on python.org. A Google group seemed low risk. There's fairly good sign-up so far. Am I alienating anybody by not going with python.org? -- Sean From no.email at please.post Thu Oct 1 18:05:24 2009 From: no.email at please.post (kj) Date: Thu, 1 Oct 2009 22:05:24 +0000 (UTC) Subject: Q: sort's key and cmp parameters References: Message-ID: In Raymond Hettinger writes: Thanks for an extremely helpful reply! >If you need to sort by an ascending primary key and a descending >secondary key, you may find it easiest to sort in two passes >(taking advantage of guaranteed sort stability): > sorted(s, key=secondary, reversed=3DTrue) > sorted(s, key=primary) In the special case where the value returned by secondary is numeric, I suppose one could do this in one go with sorted(s, key=lambda x: (primary(x), -secondary(x))) ...but I can't think of a way to generalize this... kj From no.email at please.post Thu Oct 1 18:16:13 2009 From: no.email at please.post (kj) Date: Thu, 1 Oct 2009 22:16:13 +0000 (UTC) Subject: Q: sort's key and cmp parameters References: <7x1vln2bzh.fsf@ruckus.brouhaha.com> Message-ID: In <7x1vln2bzh.fsf at ruckus.brouhaha.com> Paul Rubin writes: >kj writes: >> Is there a real-life sorting task that requires (or is far more >> efficient with) cmp and can't be easily achieved with key and >> reverse? >Yes, think of sorting tree structures where you have to recursively >compare them til you find an unequal pair of nodes. To sort with >key=... you have to wrap a class instance around each tree, with >special comparison methods. Blecch. Good point. This example convinces me that it was a bad idea to get rid of cmp in Python 3, even if situations like this one are rare. With the cmp parameter as an option, coding this type of sort was accessible even to those with a rudementary knowledge of Python. Now one needs to be pretty clever and pretty skilled with Python to figure this one out... Granted, anyone who needs to perform such sorts is probably smart enough to handle the required solution, but not necessarily very proficient with Python. Besides, why make something that was relatively straightforward before become an exercise in cleverness? I wonder what was gained by eliminating the cmp option... kj From find.mig at paa.google Thu Oct 1 18:24:01 2009 From: find.mig at paa.google (Esben von Buchwald) Date: Fri, 02 Oct 2009 00:24:01 +0200 Subject: Looking for documentation tools Message-ID: <4ac52c0c$0$285$14726298@news.sunsite.dk> Hello. For documening my thesis project, i'm, looking for 2 things: A GUI tool that allows me to enter descriptions, arguments, return values etc, for each function, class, etc. in some forms and then generates and inserts the correct comment syntax, so pydoc can generate the documentation HTML. (preferrably for windows) And a diagram tool that reverse engineers the used python modules and draws a class diagram with relations between the classes etc.? Either as a standalone app or as something that generates graphviz scripts or similar. I hope you guys have some suggestions? -Esben From nyamatongwe+thunder at gmail.com Thu Oct 1 18:40:40 2009 From: nyamatongwe+thunder at gmail.com (Neil Hodgson) Date: Thu, 01 Oct 2009 22:40:40 GMT Subject: unicode issue In-Reply-To: References: <9a67ac5b-0bec-4a95-a55f-fe44b6ed8d62@a21g2000yqc.googlegroups.com> Message-ID: Dave Angel: > I know that the clipboard has type tags, but I haven't looked at them in > so long that I forget what they look like. For text, is it just ASCII > and Unicode? Or are there other possible encodings that the source and > sink negotiate? The normal thing seen is that the clipboard differentiates between Unicode text and locale-dependent 8 bit text. Depending on platform Unicode text may be in UTF-8 (Linux) or UTF-16 (Windows). The encoding of 8-bit text strings is not well defined and is normally assumed to be compatible with whatever is currently in the document or the current user interface encoding. Neil From pavlovevidence at gmail.com Thu Oct 1 18:52:37 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 1 Oct 2009 15:52:37 -0700 (PDT) Subject: Python and lost files References: Message-ID: <9251ee92-d693-4b59-9c70-c99b4f062c04@c37g2000yqi.googlegroups.com> On Sep 30, 11:35?pm, "Timothy W. Grove" wrote: > Recently I purchased some software to recover some files which I had > lost. (A python project, incidentally! Yes, I should have kept better > backups!) They were nowhere to found in the file system, nor in the > recycle bin, but this software was able to locate them and restore them. I could have used that yesterday, if it were able to work for a network Samba drive. (Yeah, not likely.) > I was just wondering if there was a way using python to view and recover > files from the hard drive which would otherwise remain lost forever? Obviously, if that program was able to do it, it's possible. On Unix-like OSes, and probably others, it's possible to read the raw data on a disk the same way as you would read any file. So Python can do it without any system-level programming. Recent versions (I think 2.6+) can use mmap, too, now that it supports an offset parameter. I don't think you can do that in Windows, though. I think you'd have to use special system calls (via ctypes, for example). Carl Banks From John at nospam.net Thu Oct 1 19:01:07 2009 From: John at nospam.net (John) Date: Thu, 01 Oct 2009 19:01:07 -0400 Subject: cx_freeze problem on Ubuntu References: <1d71d6fc-315c-401a-a7a3-52bd8d8abf97@d10g2000yqh.googlegroups.com> Message-ID: Jon Clements wrote: > > Firstly -- never used the library, hadn't even heard of it -- > disclaimer over :) > > Err, python 2.6 doesn't have print as a function by default. Have you > done a from __future__ import ? > > def _GetInitScriptFileName(self, argsSource = None): > if argsSource is None: > argsSource = self > name = argsSource.initScript > if name is None: > if argsSource.copyDependentFiles: > name = "Console" (****) > else: > name = "ConsoleKeepPath" > if sys.version_info[0] >= 3: > name += "3" > argsSource.initScript = self._GetFileName("initscripts", name) > if argsSource.initScript is None: > raise ConfigError("no initscript named %s", name) > > Should be sufficient clues(****), > > Cheers, > Jon You wrote 'Firstly' which implies a 'Secondly' which you failed to add. You lose -1 points. You gained +1 points for your perspicacity to minutia. You lose -2 points to help resolve the problem. Life is all about gaining points, even if no one cares. Cheers, John, From http Thu Oct 1 19:16:52 2009 From: http (Paul Rubin) Date: 01 Oct 2009 16:16:52 -0700 Subject: Q: sort's key and cmp parameters References: Message-ID: <7xzl8ahfh7.fsf@ruckus.brouhaha.com> Duncan Booth writes: > > Is there a real-life sorting task that requires (or is far more > > efficient with) cmp and can't be easily achieved with key and reverse? > > > There is no sorting task that *requires* cmp. If all else fails you can > define a key class to wrap the original wrapper such that comparing the > keys simply calls the comparison function that you *would* have used. I would count that as key being far less efficient, though still giving the same result. From http Thu Oct 1 19:19:05 2009 From: http (Paul Rubin) Date: 01 Oct 2009 16:19:05 -0700 Subject: Q: sort's key and cmp parameters References: Message-ID: <7xvdiyhfdi.fsf@ruckus.brouhaha.com> Raymond Hettinger writes: > If you're assuming a consistent sort-order (transitivity, not > evolving over time, etc), then the cmp method and key method > are mathematically equivalent (you could always do a compare > sort first, record the order produced, and assign the position > number as a key function) But that is an efficiency hit. > If you're starting point is a cmp function (for instance, > asking a dating service member whether they prefer > mate x to mate y), then having to convert to a key function > can be inconvenient. Well, the issue there is that the comparison function may not be transitive. Maybe you really want a topological sort for that. > All that being said, for many everyday uses, a key function is > simpler to write and faster to run than a cmp function approach. I still have never understood why cmp was removed. Sure, key is more convenient a lot (or maybe most) of the time, but it's not always. From wuwei23 at gmail.com Thu Oct 1 20:00:17 2009 From: wuwei23 at gmail.com (alex23) Date: Thu, 1 Oct 2009 17:00:17 -0700 (PDT) Subject: Q: sort's key and cmp parameters References: <7x1vln2bzh.fsf@ruckus.brouhaha.com> Message-ID: kj wrote: > This example convinces me that it was a bad idea to > get rid of cmp in Python 3, even if situations like this one are > rare. It sounds like the entire point of this exercise was to get other people to confirm your bias for you. From oyinbo55 at gmail.com Thu Oct 1 20:33:26 2009 From: oyinbo55 at gmail.com (oyinbo55) Date: Thu, 1 Oct 2009 17:33:26 -0700 (PDT) Subject: Pyserial non-standard baud rate References: <2feb36fc-106c-4d7c-a697-db59971dc910@a7g2000yqo.googlegroups.com> Message-ID: <917b9e31-19d8-442a-be3a-3f87c309c4d4@o36g2000vbl.googlegroups.com> On Oct 1, 11:36?am, "Richard Brodie" wrote: > "oyinbo55" wrote in message > > news:2feb36fc-106c-4d7c-a697-db59971dc910 at a7g2000yqo.googlegroups.com... > > > Using the standard 19200 baud results in gobbledegook from the > > multimeter. > > You aren't going to notice a 0.1% clock skew within 1 byte. > Forget about the difference between 19200 and 19230. > > If you have a scope handy, see what the output waveform > looks like, and check the timings. If not play around with > the rates, parity etc., until you find something that works. Thank you, Richard, you are absolutely right. Looking back at the documentation for the instrument, I found the following: "The data format complies with JIS 7Bits-transmission code with a baud rate of 19230. " I had overlooked the JIS transmission code because I didn't know what that meant. (It is the Japanese Industrial Standard). Changing my script to display the data one character at a time, I found it was sprinkled with escape characters that Japanese machines use to switch from ASCII to Kanji characters and back. Does Python have a module that will translate the data stream and display normal ASCII characters? The escape codes appear to alter the values of the characters in some systematic way. Thanks again for your help. From ahoover at eecs.berkeley.edu Thu Oct 1 20:51:44 2009 From: ahoover at eecs.berkeley.edu (Aaron Hoover) Date: Thu, 1 Oct 2009 17:51:44 -0700 Subject: Threaded GUI slowing method execution? Message-ID: I have a wx GUI application that connects to a serial port in a separate thread, reads from the port, and then is supposed to put the data it finds into a queue to be used by the main GUI thread. Generally speaking, it's working as expected. However, one method (that's part of a library I've written to parse the packet structure of the data that's coming over the serial port) executes approximately 1000 times slower (50ms vs. 50us) when called from the serial management thread in the GUI as compared to calling the same function from within a command line Python script. I checked it by wrapping the call as follows in both cases (GUI and command line script): tic = time.time() print time.time() - tic All the thread is doing most of the time is sitting around checking the serial port for waiting data, reading it, and appending it to a list when it finds it. Then, in the same thread, the method that seems to be remarkably slow works its way through that list looking for packets of data and appending the packet payloads it finds to a queue to be handled in some way by the GUI. My question is, what am I missing about either threading or the fact that this is running in a GUI that might explain such a huge slowdown. I'm sending data over the serial at a true rate of about 24k bytes per second, or approximately 2 packets per ms. Is it too much to ask to be able to process this data in realtime from within a GUI (I'm not talking about plotting or anything - just read it and find packets)? Like I said, the process pretty much runs in realtime from a command line script. This packet parsing needs to happen continuously, so it seems calling join() to ensure it's not interrupted by the GUI thread, won't work. Thanks in advance for your help. Aaron From fetchinson at googlemail.com Thu Oct 1 21:23:59 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Thu, 1 Oct 2009 18:23:59 -0700 Subject: AJAX Widget Framework In-Reply-To: <4AC4EE86.7020802@shopzeus.com> References: <4AC4EE86.7020802@shopzeus.com> Message-ID: > I'm looking for an open source, AJAX based widget/windowing framework. > Here is what I need: > > - end user opens up a browser, points it to a URL, logs in > - on the server site, sits my application, creating a new session for > each user that is logged in > - on the server site, I create windows(frames), put widgets on them, > write event handlers etc. Just like with wx or pygtk. > - However, windows are created in the user's browser, and events are > triggered by Javascript, and sent back to server through AJAX. > - the server side would be - of course - written in Python. > > I was looking these projects: > > http://www.uize.com/ > http://pyjs.org/ > > There are many frameworks listed here which I did not check: > http://internetmindmap.com/javascript_frameworks. I have no idea which > has Python support, and probably there are only a few that worth looking > at. I wonder if you could tell me which are the pros and contras for > these frameworks. If there is a comparison available on the NET, > pointing me to the right URL would help a lot. > > The bottom line... > > My main goal is to develop enterprise class OLTP database applications. > I'll need grid widget to display data in tabular format, and I'll use > events heavily for data manipulation, live search, tooltips etc. I'm > familiar with wxWidgets, pygtk and other toolkits, but not with AJAX. I > have never used a system like that. I do things like these via turbogears on the server side and extjs on the client: turbogears.org extjs.com HTH, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From exarkun at twistedmatrix.com Thu Oct 1 21:33:18 2009 From: exarkun at twistedmatrix.com (exarkun at twistedmatrix.com) Date: Fri, 02 Oct 2009 01:33:18 -0000 Subject: Concurrent threads to pull web pages? In-Reply-To: References: Message-ID: <20091002013318.1533.678152062.divmod.xquotient.527@localhost.localdomain> On 1 Oct, 09:28 am, nospam at nospam.com wrote: >Hello > > I recently asked how to pull companies' ID from an SQLite >database, >have multiple instances of a Python script download each company's web >page from a remote server, eg. www.acme.com/company.php?id=1, and use >regexes to extract some information from each page. > >I need to run multiple instances to save time, since each page takes >about 10 seconds to be returned to the script/browser. > >Since I've never written a multi-threaded Python script before, to >save time investigating, I was wondering if someone already had a >script that downloads web pages and save some information into a >database. There's no need to use threads for this. Have a look at Twisted: http://twistedmatrix.com/trac/ Here's an example of how to use the Twisted HTTP client: http://twistedmatrix.com/projects/web/documentation/examples/getpage.py Jean-Paul From kyle at kyleterry.com Thu Oct 1 21:36:49 2009 From: kyle at kyleterry.com (Kyle Terry) Date: Thu, 1 Oct 2009 18:36:49 -0700 Subject: Concurrent threads to pull web pages? In-Reply-To: <20091002013318.1533.678152062.divmod.xquotient.527@localhost.localdomain> References: <20091002013318.1533.678152062.divmod.xquotient.527@localhost.localdomain> Message-ID: <65a7ac1b0910011836q45f3730ake2d473db0cc0664c@mail.gmail.com> On Thu, Oct 1, 2009 at 6:33 PM, wrote: > On 1 Oct, 09:28 am, nospam at nospam.com wrote: > >> Hello >> >> I recently asked how to pull companies' ID from an SQLite database, >> have multiple instances of a Python script download each company's web >> page from a remote server, eg. www.acme.com/company.php?id=1, and use >> regexes to extract some information from each page. >> >> I need to run multiple instances to save time, since each page takes >> about 10 seconds to be returned to the script/browser. >> >> Since I've never written a multi-threaded Python script before, to >> save time investigating, I was wondering if someone already had a >> script that downloads web pages and save some information into a >> database. >> > > There's no need to use threads for this. Have a look at Twisted: > > http://twistedmatrix.com/trac/ > > Here's an example of how to use the Twisted HTTP client: > > http://twistedmatrix.com/projects/web/documentation/examples/getpage.py > I don't think he was looking for a framework... Specifically a framework that you work on. > > Jean-Paul > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From sajmikins at gmail.com Thu Oct 1 21:39:17 2009 From: sajmikins at gmail.com (Simon Forman) Date: Thu, 1 Oct 2009 21:39:17 -0400 Subject: emptying a list In-Reply-To: References: Message-ID: <50f98a4c0910011839v2dca3a37te4f3d778dfd2a95f@mail.gmail.com> On Thu, Oct 1, 2009 at 11:30 AM, lallous wrote: > Hello > > What is faster when clearing a list? > > del L[:] > > or > > L = [] > > -- > Elias > -- > http://mail.python.org/mailman/listinfo/python-list > The first form actually clears the list, the second for just re-binds the name 'L' to a new, empty list. From python at mrabarnett.plus.com Thu Oct 1 21:46:33 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 02 Oct 2009 02:46:33 +0100 Subject: Concurrent threads to pull web pages? In-Reply-To: References: Message-ID: <4AC55B79.50703@mrabarnett.plus.com> Gilles Ganault wrote: > Hello > > I recently asked how to pull companies' ID from an SQLite database, > have multiple instances of a Python script download each company's web > page from a remote server, eg. www.acme.com/company.php?id=1, and use > regexes to extract some information from each page. > > I need to run multiple instances to save time, since each page takes > about 10 seconds to be returned to the script/browser. > > Since I've never written a multi-threaded Python script before, to > save time investigating, I was wondering if someone already had a > script that downloads web pages and save some information into a > database. > > Thank you for any tip. You could put the URLs into a queue and have multiple worker threads repeatedly get a URL from the queue, download the page, and then put the page into another queue for processing by another extraction thread. This post might help: http://mail.python.org/pipermail/python-list/2009-September/195866.html From exarkun at twistedmatrix.com Thu Oct 1 21:48:06 2009 From: exarkun at twistedmatrix.com (exarkun at twistedmatrix.com) Date: Fri, 02 Oct 2009 01:48:06 -0000 Subject: Concurrent threads to pull web pages? In-Reply-To: <65a7ac1b0910011836q45f3730ake2d473db0cc0664c@mail.gmail.com> References: <20091002013318.1533.678152062.divmod.xquotient.527@localhost.localdomain> <65a7ac1b0910011836q45f3730ake2d473db0cc0664c@mail.gmail.com> Message-ID: <20091002014806.1533.1165493991.divmod.xquotient.529@localhost.localdomain> On 01:36 am, kyle at kyleterry.com wrote: >On Thu, Oct 1, 2009 at 6:33 PM, wrote: >>On 1 Oct, 09:28 am, nospam at nospam.com wrote: >>>Hello >>> >>> I recently asked how to pull companies' ID from an SQLite >>>database, >>>have multiple instances of a Python script download each company's >>>web >>>page from a remote server, eg. www.acme.com/company.php?id=1, and use >>>regexes to extract some information from each page. >>> >>>I need to run multiple instances to save time, since each page takes >>>about 10 seconds to be returned to the script/browser. >>> >>>Since I've never written a multi-threaded Python script before, to >>>save time investigating, I was wondering if someone already had a >>>script that downloads web pages and save some information into a >>>database. >> >>There's no need to use threads for this. Have a look at Twisted: >> >> http://twistedmatrix.com/trac/ >> >>Here's an example of how to use the Twisted HTTP client: >> >>http://twistedmatrix.com/projects/web/documentation/examples/getpage.py > >I don't think he was looking for a framework... Specifically a >framework >that you work on. He's free to use anything he likes. I'm offering an option he may not have been aware of before. It's okay. It's great to have options. Jean-Paul From no.email at please.post Thu Oct 1 22:13:29 2009 From: no.email at please.post (kj) Date: Fri, 2 Oct 2009 02:13:29 +0000 (UTC) Subject: Q: sort's key and cmp parameters References: <7x1vln2bzh.fsf@ruckus.brouhaha.com> Message-ID: In alex23 writes: >kj wrote: >> This example convinces me that it was a bad idea to >> get rid of cmp in Python 3, even if situations like this one are >> rare. >It sounds like the entire point of this exercise was to get other >people to confirm your bias for you. The only problem with this hypothesis is that my original bias was exactly the opposite to the one you quote: when I sent my original post I thought that cmp was in fact useless (after all I could not think of a situation that required it or even made it preferable), and was not even aware that it had been dropped in Python 3. Paul Rubin's post convinced me otherwise. BTW, what's with this business of ascribing underhanded motives to me? Earlier some other clown alleged that that my original post was homework??? WTF? kj From http Thu Oct 1 22:21:12 2009 From: http (Paul Rubin) Date: 01 Oct 2009 19:21:12 -0700 Subject: Q: sort's key and cmp parameters References: <7x1vln2bzh.fsf@ruckus.brouhaha.com> Message-ID: <7x4oqipmcn.fsf@ruckus.brouhaha.com> Bearophile writes: > > Yes, think of sorting tree structures where you have to recursively > > compare them til you find an unequal pair of nodes. > > That's cute. In what situations do you have to perform such kind of > sort? It came up in a search engine application I've been involved with. From invalid at invalid.invalid Thu Oct 1 22:49:36 2009 From: invalid at invalid.invalid (Grant Edwards) Date: Fri, 2 Oct 2009 02:49:36 +0000 (UTC) Subject: Python and lost files References: Message-ID: On 2009-10-01, Timothy W. Grove wrote: > Recently I purchased some software to recover some files which > I had lost. (A python project, incidentally! Yes, I should > have kept better backups!) They were nowhere to found in the > file system, nor in the recycle bin, but this software was > able to locate them and restore them. I was just wondering if > there was a way using python to view and recover files from > the hard drive which would otherwise remain lost forever? Yes, you could write a filesystem analysis and file recovery program in Python. [I guess that's what you were asking.] -- Grant From nagle at animats.com Fri Oct 2 00:38:16 2009 From: nagle at animats.com (John Nagle) Date: Thu, 01 Oct 2009 21:38:16 -0700 Subject: Pyserial non-standard baud rate In-Reply-To: <917b9e31-19d8-442a-be3a-3f87c309c4d4@o36g2000vbl.googlegroups.com> References: <2feb36fc-106c-4d7c-a697-db59971dc910@a7g2000yqo.googlegroups.com> <917b9e31-19d8-442a-be3a-3f87c309c4d4@o36g2000vbl.googlegroups.com> Message-ID: <4ac581db$0$1621$742ec2ed@news.sonic.net> oyinbo55 wrote: > On Oct 1, 11:36 am, "Richard Brodie" wrote: >> "oyinbo55" wrote in message >> >> news:2feb36fc-106c-4d7c-a697-db59971dc910 at a7g2000yqo.googlegroups.com... >> >>> Using the standard 19200 baud results in gobbledegook from the >>> multimeter. >> You aren't going to notice a 0.1% clock skew within 1 byte. >> Forget about the difference between 19200 and 19230. >> >> If you have a scope handy, see what the output waveform >> looks like, and check the timings. If not play around with >> the rates, parity etc., until you find something that works. > > Thank you, Richard, you are absolutely right. Looking back at the > documentation for the instrument, I found the following: > > "The data format complies with JIS 7Bits-transmission code with a baud > rate of 19230. " OK. First off, 19200 baud should work. 19230 is a number which comes from devices that use a 4MHz crystal for the clock and divide by 208. 4000000 / 208 = 19230.769, which is as close as you can get to 19200 baud given a 4MHz timing source. Serial ports can usually tolerate an error of about 1/4 a bit time, which is 2.5%. Since you're running a little fast, I'd suggest sending 2 stop bits, which guarantees the receiver can't gain on the transmitter during long blocks sent at full speed. > > I had overlooked the JIS transmission code because I didn't know what > that meant. (It is the Japanese Industrial Standard). Changing my > script to display the data one character at a time, I found it was > sprinkled with escape characters that Japanese machines use to switch > from ASCII to Kanji characters and back. > > Does Python have a module that will translate the data stream and > display normal ASCII characters? The escape codes appear to alter the > values of the characters in some systematic way. If you're really receiving content in Shift-JIS, you just need to translate it into Unicode. Python can do that. See http://www.velocityreviews.com/forums/t646242-251-shiftjis-to-unicode.html Now you'll have Unicode strings that contain kanjii, which Python can process just fine. Printing kanjii in a terminal window is troublesome on some operating systems. But it's possible to get everything working in Unicode, which will result in kanjii text appearing. Reading the Japanese may be a problem, although you can put the text through Google Translate. John Nagle From bigboss1964 at gmail.com Fri Oct 2 00:56:01 2009 From: bigboss1964 at gmail.com (TerryP) Date: Thu, 1 Oct 2009 21:56:01 -0700 (PDT) Subject: Looking for documentation tools References: <4ac52c0c$0$285$14726298@news.sunsite.dk> Message-ID: > > A GUI tool that allows me to enter descriptions, arguments, return > values etc, for each function, class, etc. in some forms and then > generates and inserts the correct comment syntax, ?so pydoc can generate > the documentation HTML. (preferrably for windows) > Maybe I am a bastard, but personally I would consider such a tool, to be a useless bit of dribble compared to writing decent doc strings and comments within the code itself. Beyond that, might I suggest using your favorite text editor and a bit of munged scripting? > And a diagram tool that reverse engineers the used python modules and > draws a class diagram with relations between the classes etc.? > > Either as a standalone app or as something that generates graphviz > scripts or similar. > You might take a look at tools such as Umbrello and Gaphor. You might also find this link helpful: http://en.wikipedia.org/wiki/List_of_UML_tools From 504crank at gmail.com Fri Oct 2 01:10:55 2009 From: 504crank at gmail.com (504crank at gmail.com) Date: Thu, 1 Oct 2009 22:10:55 -0700 (PDT) Subject: Regular expression to structure HTML Message-ID: <98ffa1be-c4da-4c38-9b68-275c14c1948b@e12g2000yqi.googlegroups.com> I'm kind of new to regular expressions, and I've spent hours trying to finesse a regular expression to build a substitution. What I'd like to do is extract data elements from HTML and structure them so that they can more readily be imported into a database. No -- sorry -- I don't want to use BeautifulSoup (though I have for other projects). Humor me, please -- I'd really like to see if this can be done with just regular expressions. Note that the output is referenced using named groups. My challenge is successfully matching the HTML tags in between the first table row, and the second table row. I'd appreciate any suggestions to improve the approach. rText = "8583New Horizon Technical Academy, Inc #4Jefferson701149371Career Learning CenterJefferson70113" rText = re.compile(r'()(?P\d+)()()()(?P[A- Za-z0-9#\s\S\W]+)().+$').sub(r'LICENSE:\g|NAME: \g\n', rText) print rText LICENSE:8583|NAME:New Horizon Technical Academy, Inc #4Jefferson701149371Career Learning Center|PARISH:Jefferson|ZIP:70113 From greg.ewing at canterbury.ac.nz Fri Oct 2 01:31:11 2009 From: greg.ewing at canterbury.ac.nz (Greg Ewing) Date: Fri, 02 Oct 2009 17:31:11 +1200 Subject: ANN: Humerus 2.1 Message-ID: Humerus 2.1 is now available: http://www.cosc.canterbury.ac.nz/greg.ewing/python/Albow/Humerus-2.1.0.zip Online documentation: http://www.cosc.canterbury.ac.nz/greg.ewing/python/Albow/Humerus-2.1.0/doc/ In this version, the code for handling levels has been separated out into a new pair of classes. This makes it easier to use Humerus for games that don't have levels. What is Humerus? ---------------- Humerus is a companion to the Albow widget library for PyGame. It provides a framework for games made up of a sequence of levels, including user interface and back-end logic for saving and restoring game state, loading levels, and sundry other details. There is also optional support for a built-in level editor, including code for loading and saving levels to be edited, and asking whether to save modified levels. Albow can be found here: http://www.cosc.canterbury.ac.nz/greg.ewing/python/Albow/ From jef.mangelschots at gmail.com Fri Oct 2 01:40:08 2009 From: jef.mangelschots at gmail.com (jefm) Date: Thu, 1 Oct 2009 22:40:08 -0700 (PDT) Subject: PyOpenGL and graphics card support References: <8241f105-aa73-44fc-96fb-a09bf4f2d5e1@v15g2000prn.googlegroups.com> Message-ID: that works. Thx below is the output for my system: gluGetString - GLU_VERSION: 1.2.2.0 Microsoft Corporation gluGetString - GLU_EXTENSIONS: GL_EXT_bgra glGetString - GL_VENDOR: NVIDIA Corporation glGetString - GL_RENDERER: GeForce 9500 GT/PCI/SSE2 glGetString - GL_SHADING_LANGUAGE_VERSION: 1.40 NVIDIA via Cg compiler glGetString - GL_EXTENSIONS: GL_ARB_color_buffer_float GL_ARB_compatibility GL_ARB_copy_buffer GL_ARB_depth_buffer_float GL_ARB_depth_texture GL_ARB_draw_buffers GL_ARB_draw_instanced GL_ARB_fragment_program GL_ARB_fragment_program_shadow GL_ARB_fragment_shader GL_ARB_half_float_pixel GL_ARB_half_float_vertex GL_ARB_framebuffer_object GL_ARB_geometry_shader4 GL_ARB_imaging GL_ARB_map_buffer_range GL_ARB_multisample GL_ARB_multitexture GL_ARB_occlusion_query GL_ARB_pixel_buffer_object GL_ARB_point_parameters GL_ARB_point_sprite GL_ARB_shadow GL_ARB_shader_objects GL_ARB_shading_language_100 GL_ARB_texture_border_clamp GL_ARB_texture_buffer_object GL_ARB_texture_compression GL_ARB_texture_cube_map GL_ARB_texture_env_add GL_ARB_texture_env_combine GL_ARB_texture_env_dot3 GL_ARB_texture_float GL_ARB_texture_mirrored_repeat GL_ARB_texture_non_power_of_two GL_ARB_texture_rectangle GL_ARB_texture_rg GL_ARB_transpose_matrix GL_ARB_vertex_array_object GL_ARB_vertex_buffer_object GL_ARB_vertex_program GL_ARB_vertex_shader GL_ARB_window_pos GL_ATI_draw_buffers GL_ATI_texture_float GL_ATI_texture_mirror_once GL_S3_s3tc GL_EXT_texture_env_add GL_EXT_abgr GL_EXT_bgra GL_EXT_blend_color GL_EXT_blend_equation_separate GL_EXT_blend_func_separate GL_EXT_blend_minmax GL_EXT_blend_subtract GL_EXT_compiled_vertex_array GL_EXT_Cg_shader GL_EXT_bindable_uniform GL_EXT_depth_bounds_test GL_EXT_direct_state_access GL_EXT_draw_buffers2 GL_EXT_draw_instanced GL_EXT_draw_range_elements GL_EXT_fog_coord GL_EXT_framebuffer_blit GL_EXT_framebuffer_multisample GL_EXT_framebuffer_object GL_EXTX_framebuffer_mixed_formats GL_EXT_framebuffer_sRGB GL_EXT_geometry_shader4 GL_EXT_gpu_program_parameters GL_EXT_gpu_shader4 GL_EXT_multi_draw_arrays GL_EXT_packed_depth_stencil GL_EXT_packed_float GL_EXT_packed_pixels GL_EXT_pixel_buffer_object GL_EXT_point_parameters GL_EXT_provoking_vertex GL_EXT_rescale_normal GL_EXT_secondary_color GL_EXT_separate_shader_objects GL_EXT_separate_specular_color GL_EXT_shadow_funcs GL_EXT_stencil_two_side GL_EXT_stencil_wrap GL_EXT_texture3D GL_EXT_texture_array GL_EXT_texture_buffer_object GL_EXT_texture_compression_latc GL_EXT_texture_compression_rgtc GL_EXT_texture_compression_s3tc GL_EXT_texture_cube_map GL_EXT_texture_edge_clamp GL_EXT_texture_env_combine GL_EXT_texture_env_dot3 GL_EXT_texture_filter_anisotropic GL_EXT_texture_integer GL_EXT_texture_lod GL_EXT_texture_lod_bias GL_EXT_texture_mirror_clamp GL_EXT_texture_object GL_EXT_texture_sRGB GL_EXT_texture_swizzle GL_EXT_texture_shared_exponent GL_EXT_timer_query GL_EXT_vertex_array GL_EXT_vertex_array_bgra GL_IBM_rasterpos_clip GL_IBM_texture_mirrored_repeat GL_KTX_buffer_region GL_NV_blend_square GL_NV_copy_depth_to_color GL_NV_copy_image GL_NV_depth_buffer_float GL_NV_conditional_render GL_NV_depth_clamp GL_NV_explicit_multisample GL_NV_fence GL_NV_float_buffer GL_NV_fog_distance GL_NV_fragment_program GL_NV_fragment_program_option GL_NV_fragment_program2 GL_NV_framebuffer_multisample_coverage GL_NV_geometry_shader4 GL_NV_gpu_program4 GL_NV_half_float GL_NV_light_max_exponent GL_NV_multisample_coverage GL_NV_multisample_filter_hint GL_NV_occlusion_query GL_NV_packed_depth_stencil GL_NV_parameter_buffer_object GL_NV_parameter_buffer_object2 GL_NV_pixel_data_range GL_NV_point_sprite GL_NV_primitive_restart GL_NV_register_combiners GL_NV_register_combiners2 GL_NV_texgen_reflection GL_NV_texture_compression_vtc GL_NV_texture_env_combine4 GL_NV_texture_expand_normal GL_NV_texture_rectangle GL_NV_texture_shader GL_NV_texture_shader2 GL_NV_texture_shader3 GL_NV_transform_feedback GL_NV_vertex_array_range GL_NV_vertex_array_range2 GL_NV_vertex_program GL_NV_vertex_program1_1 GL_NV_vertex_program2 GL_NV_vertex_program2_option GL_NV_vertex_program3 GL_NVX_conditional_render GL_NV_vertex_buffer_unified_memory GL_NV_shader_buffer_load GL_SGIS_generate_mipmap GL_SGIS_texture_lod GL_SGIX_depth_texture GL_SGIX_shadow GL_SUN_slice_accum GL_WIN_swap_hint WGL_EXT_swap_control From s.selvamsiva at gmail.com Fri Oct 2 01:56:16 2009 From: s.selvamsiva at gmail.com (S.Selvam) Date: Fri, 2 Oct 2009 11:26:16 +0530 Subject: emptying a list In-Reply-To: <96ea8f01-6cc6-4629-af49-ba249479ed38@l13g2000yqb.googlegroups.com> References: <96ea8f01-6cc6-4629-af49-ba249479ed38@l13g2000yqb.googlegroups.com> Message-ID: On Thu, Oct 1, 2009 at 10:13 PM, Jon Clements wrote: > On 1 Oct, 16:30, "lallous" wrote: > > Hello > > > > What is faster when clearing a list? > > > > del L[:] > > > > or > > > > L = [] > > > > -- > > Elias > > Does it really matter that much? > > And you're really talking about two different things, which quite > often come up on this group. > > Example follows: > > >>> x = range(5) > >>> x = y > Is n't it y=x ? >>> print x, y > [1, 2, 3, 4] [1, 2, 3, 4] > >>> x = [] > >>> print x, y > [] [1, 2, 3, 4] > >>> x = y > >>> print x, y > [1, 2, 3, 4] [1, 2, 3, 4] > >>> del x[:] > >>> print x, y > [] [] > > Cheers, > Jon. > -- > http://mail.python.org/mailman/listinfo/python-list > -- Yours, S.Selvam -------------- next part -------------- An HTML attachment was scrubbed... URL: From hjtoi-better-remove-before-reply at comcast.net Fri Oct 2 02:37:08 2009 From: hjtoi-better-remove-before-reply at comcast.net (Heikki Toivonen) Date: Thu, 01 Oct 2009 23:37:08 -0700 Subject: M2Crypto 0.20.1 won't build on Red Hat Linux References: <4ac39492$0$1642$742ec2ed@news.sonic.net> <4ac4e173$0$1674$742ec2ed@news.sonic.net> Message-ID: John Nagle wrote: > The right question is "uname --hardware-platform". That returns "i386" if > running on something emulating a 386, even it it's 64-bit capable. Thanks, I'll make a note that I'll need to clarify that part. > With that change, the build runs to completion and and the regression tests > mostly run. Test failure occurred at: > > ====================================================================== > FAIL: test_cipher_ok (tests.test_ssl.MiscSSLClientTestCase) > ---------------------------------------------------------------------- > Traceback (most recent call last): > File > "/var/www/vhosts/sitetruth.com/private/downloads/M2Crypto/M2Crypto-0.20.1/tests/test_ssl.py", > line 485, in test_cipher_ok > assert s.get_cipher().name() == 'AES128-SHA', s.get_cipher().name() > AssertionError: AES256-SHA > > ---------------------------------------------------------------------- > Ran 226 tests in 91.672s > > Is 'AES128-SHA' a cypher that Fedora Core doesn't support, or what? That seems strange. OpenSSL does allow fine tuning what ciphers to build and operate with, but this is the first I hear AES128-SHA might not be turned on anymore. And even stranger still is that the test is explicitly asking for 128 yet it is getting 256 back. Could you rerun the test again just to make sure you can repeat it, and if so, let me know the fedora core, openssl, swig and python versions? -- Heikki Toivonen - http://heikkitoivonen.net From martien.verbruggen at invalid.see.sig Fri Oct 2 03:00:04 2009 From: martien.verbruggen at invalid.see.sig (Martien Verbruggen) Date: Fri, 2 Oct 2009 17:00:04 +1000 Subject: Open file on remote linux server References: <7hvj0fF2tvoe1U1@mid.uni-berlin.de> Message-ID: On Wed, 23 Sep 2009 23:41:35 +0200, Diez B. Roggisch wrote: > The Bear schrieb: >> Hi I'm looking to do something like this >> >> f = f.openfileobj(remotefileloc, localfilelikeobj) >> >> my remote files are on a solaris box that i can access using ssh (could >> prehap request othe protocols if necessary) >> >> anyone got any ideas? > > try paramiko. Or just use subprocess to scp and open the file locally. import paramiko ssh = paramiko.SSHClient() ssh.load_system_host_keys(os.environ['HOME'] + '/.ssh/known_hosts') ssh.connect('localhost') try: ftp = ssh.open_sftp() # To write fh = ftp.file('/tmp/foo.test', 'w') fh.write('This is a test string\nAnd this is another one') fh.close() # To read fh = ftp.file('/tmp/foo.test', 'r') for l in fh: print l, fh.close() finally: ssh.close() You may need to add some password handling in there (for me my environment takes care of that). Martien -- | Martien Verbruggen | You can't have everything, where would first.last at heliotrope.com.au | you put it? | From thomas.j.johnson at gmail.com Fri Oct 2 03:03:30 2009 From: thomas.j.johnson at gmail.com (Thomas Johnson) Date: Fri, 2 Oct 2009 00:03:30 -0700 (PDT) Subject: Timestamps for TCP packets? Message-ID: <70f68f5d-a122-4df3-a404-43fc19f74e33@u16g2000pru.googlegroups.com> Is there any way to get kernel-level timestamps for TCP packets while still using the standard python sockets library for communication? I need to communicate over a TCP connection as easily as possible, but also record the timestamps of the incoming and outgoing timestamps at microsecond or nanosecond resolution. The sockets library is of course great for the communication, and I've seen some python libraries that do packet sniffing and record timestamps, but it's not clear that I can do both at the same time. From bruno.42.desthuilliers at websiteburo.invalid Fri Oct 2 03:56:24 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Fri, 02 Oct 2009 09:56:24 +0200 Subject: Regular expression to structure HTML In-Reply-To: <98ffa1be-c4da-4c38-9b68-275c14c1948b@e12g2000yqi.googlegroups.com> References: <98ffa1be-c4da-4c38-9b68-275c14c1948b@e12g2000yqi.googlegroups.com> Message-ID: <4ac5b228$0$407$426a34cc@news.free.fr> 504crank at gmail.com a ?crit : > I'm kind of new to regular expressions, and I've spent hours trying to > finesse a regular expression to build a substitution. > > What I'd like to do is extract data elements from HTML and structure > them so that they can more readily be imported into a database. > > No -- sorry -- I don't want to use BeautifulSoup (though I have for > other projects). Humor me, please -- I'd really like to see if this > can be done with just regular expressions. > I'm kind of new to hammers, and I've spent hours trying to find out how to drive a screw with a hammer. No -- sorry -- I don't want to use a screwdriver. From milesck at umich.edu Fri Oct 2 04:20:58 2009 From: milesck at umich.edu (Miles Kaufmann) Date: Fri, 2 Oct 2009 01:20:58 -0700 Subject: Timestamps for TCP packets? In-Reply-To: <70f68f5d-a122-4df3-a404-43fc19f74e33@u16g2000pru.googlegroups.com> References: <70f68f5d-a122-4df3-a404-43fc19f74e33@u16g2000pru.googlegroups.com> Message-ID: <249406FA-6346-42DE-87BD-EE7D9C2E7688@umich.edu> On Oct 2, 2009, at 12:03 AM, Thomas Johnson wrote: > Is there any way to get kernel-level timestamps for TCP packets while > still using the standard python sockets library for communication? I > need to communicate over a TCP connection as easily as possible, but > also record the timestamps of the incoming and outgoing timestamps at > microsecond or nanosecond resolution. > > The sockets library is of course great for the communication, and I've > seen some python libraries that do packet sniffing and record > timestamps, but it's not clear that I can do both at the same time. Have you tried it? I don't know of any reason that using sockets and doing a packet capture would interfere with each other. What are you trying to accomplish with the packet sniffing, though? -Miles From ptmcg at austin.rr.com Fri Oct 2 04:22:24 2009 From: ptmcg at austin.rr.com (Paul McGuire) Date: Fri, 2 Oct 2009 01:22:24 -0700 (PDT) Subject: Regular expression to structure HTML References: <98ffa1be-c4da-4c38-9b68-275c14c1948b@e12g2000yqi.googlegroups.com> Message-ID: <5c022b51-6c33-452f-8755-d711a0cbe59d@z24g2000yqb.googlegroups.com> On Oct 2, 12:10?am, "504cr... at gmail.com" <504cr... at gmail.com> wrote: > I'm kind of new to regular expressions, and I've spent hours trying to > finesse a regular expression to build a substitution. > > What I'd like to do is extract data elements from HTML and structure > them so that they can more readily be imported into a database. Oy! If I had a nickel for every misguided coder who tried to scrape HTML with regexes... Some reasons why RE's are no good at parsing HTML: - tags can be mixed case - tags can have whitespace in many unexpected places - tags with no body can combine opening and closing tag with a '/' before the closing '>', as in "
" - tags can have attributes that you did not expect (like "
") - attributes can occur in any order within the tag - attribute names can also be in unexpected upper/lower case - attribute values can be enclosed in double quotes, single quotes, or even (surprise!) NO quotes For HTML that is machine-generated, you *may* be able to make some page-specific assumptions. But if edited by human hands, or if you are trying to make a generic page scraper, RE's will never cut it. -- Paul From ole-usenet-spam at gmx.net Fri Oct 2 04:54:11 2009 From: ole-usenet-spam at gmx.net (Ole Streicher) Date: Fri, 02 Oct 2009 10:54:11 +0200 Subject: weak reference to bound method Message-ID: Hi group, I am trying to use a weak reference to a bound method: class MyClass(object): def myfunc(self): pass o = MyClass() print o.myfunc >>>> > import weakref r = weakref.ref(o.myfunc) print r() >>>> None This is what I do not understand. The object "o" is still alive, and therefore the bound method "o.myfunc" shall exists. Why does the weak reference claim that it is removed? And how can I hold the reference to the method until the object is removed? Is this a bug or a feature? (Python 2.6) Best regards Ole From t.lehmann at rtsgroup.net Fri Oct 2 05:27:43 2009 From: t.lehmann at rtsgroup.net (Thomas Lehmann) Date: Fri, 2 Oct 2009 02:27:43 -0700 (PDT) Subject: weak reference to bound method References: Message-ID: <3e35db93-81a7-4b80-b51a-155f3738506d@v36g2000yqv.googlegroups.com> > I am trying to use a weak reference to a bound method: > > class MyClass(object): > def myfunc(self): > pass > > o = MyClass() > print o.myfunc > >>>> > > > import weakref > r = weakref.ref(o.myfunc) > print r() > >>>> None > > This is what I do not understand. The object "o" is still alive, and > therefore the bound method "o.myfunc" shall exists. > > Why does the weak reference claim that it is removed? And how can I hold > the reference to the method until the object is removed? > k = o.myfunc r = weakref.ref(k) print r() >>>>> Don't ask me why! I have just been interested for what you are trying... From __peter__ at web.de Fri Oct 2 05:33:52 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 02 Oct 2009 11:33:52 +0200 Subject: weak reference to bound method References: Message-ID: Ole Streicher wrote: > I am trying to use a weak reference to a bound method: > > class MyClass(object): > def myfunc(self): > pass > > o = MyClass() > print o.myfunc >>>>> >>>> 0xc675d0>> > > import weakref > r = weakref.ref(o.myfunc) > print r() >>>>> None > > This is what I do not understand. The object "o" is still alive, and > therefore the bound method "o.myfunc" shall exists. No. o.myfunc is a different object, a bound method, and every time you access o's myfunc attribute a new bound method is created: >>> class MyClass(object): ... def myfunc(self): ... pass ... >>> o = MyClass() >>> a = o.myfunc >>> b = o.myfunc >>> a == b True >>> id(a) == id(b) False The bound method holds references to the instance and the function, not the other way around: >>> a.im_class, a.im_self, a.im_func (, <__main__.MyClass object at 0x7f1437146710>, ) > Why does the weak reference claim that it is removed? Because there are indeed no more strong references to the bound method. > And how can I hold the reference to the method until the object is removed? If you kept a strong reference to the bound method it would in turn keep the MyClass instance alive. Maybe you could store the bound method in the instance and rely on cyclic garbage collection. Is there an actual use case? Peter From ole-usenet-spam at gmx.net Fri Oct 2 05:40:27 2009 From: ole-usenet-spam at gmx.net (Ole Streicher) Date: Fri, 02 Oct 2009 11:40:27 +0200 Subject: weak reference to bound method References: <3e35db93-81a7-4b80-b51a-155f3738506d@v36g2000yqv.googlegroups.com> Message-ID: Hi Thomas, Thomas Lehmann writes: >> r = weakref.ref(o.myfunc) >> print r() >> >>>> None > k = o.myfunc > r = weakref.ref(k) > print r() >>>>>> > Don't ask me why! I have just been interested for what you are trying... This is clear: in your case, o.myfunc is explicitely referenced by k, this avoids the garbage collection. My problem is that I have a class that delegates a function call, like: --------------------8<------------------ import weakref class WeakDelegator(object): def __init__(self, func): self._func = weakref.ref(func) def __call__(self): func = self._func() return func() if func else None --------------------8<------------------ This does not work for bound methods because the weak reference to a bound method will always point to None, even if the object still exists. Why is that the case and how can I implement such a class properly? Best regards Ole From mal at egenix.com Fri Oct 2 05:55:52 2009 From: mal at egenix.com (M.-A. Lemburg) Date: Fri, 02 Oct 2009 11:55:52 +0200 Subject: custom data warehouse in python vs. out-of-the-box ETL tool In-Reply-To: <708a1e8b-3068-46df-a760-48ab2d86bdb2@v2g2000vbb.googlegroups.com> References: <5670bc91-177d-4cca-9708-8fd5fd899e17@f10g2000vbf.googlegroups.com> <34a53388-8390-4e98-88a3-d12d65a84e88@f10g2000vbf.googlegroups.com> <708a1e8b-3068-46df-a760-48ab2d86bdb2@v2g2000vbb.googlegroups.com> Message-ID: <4AC5CE28.8080606@egenix.com> Tony Schmidt wrote: > Hi, Marc-Andre - well, so far you seem to be the only one suggesting > that cross-database joins is the way to go - everyone else has been > telling me to build a warehouse. I initially was trying to avoid the > warehouse idea to "avoid going through the external temporary > resource", as you say. But then the situation came up where the data > warehouse would give me another benefit by enabling power users with > tools like OOBase to do their own reporting. So I started to re- > consider it, and since then, no one has advised the direct approach > with connections/joins on multiple databases. > > Have you had much luck circumventing the need for a data warehouse in > this way? Depending on what your data sources are, you will end up using a data warehouse sooner or later. We've worked with clients from the middle office banking world and there you typically have to aggregate data, polish it up, transform it and then send it off for others to process. Cross-database joins don't work in such a scenario because the data sources are too diverse. Logging and auditing requirements can also lead to having to use a warehouse approach. However, if you just to put together data from a few local sources without too much data to work on, I'd suggest to try the direct Python approach first. If you have enough RAM to work with, Python can easily handle millions of records and if you'd rather use a SQL based approach, it's also possible to temporarily store the data in a SQLite database as on-demand data warehouse. Regards, -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Oct 02 2009) >>> Python/Zope Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ ::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ > Thanks. > > On Sep 24, 4:10 am, "M.-A. Lemburg" wrote: >> snfctech wrote: >>> @Lemburg: Thanks for the suggestion. I'm sure you make a fine >>> product, but my development platform is Linux, and I don't want any >>> additional Windows servers to deal with (than the ones I'm already >>> stuck with.) >> >> Strange, EasySoft used to support their product on Linux as well... >> >> http://download.freshmeat.net/projects/easysoftsqlengine >> >> Looks like they stopped with supporting it on Linux and now only >> have Windows downloads available. >> >> mxODBC and mxODBC Connect work just fine on Linux, so you could use >> them to build a custom joining solution in Python. >> >> It really depends on whether you need to run those joins on >> an on-demand basis or not: >> >> AFAIK, Talend can only run jobs which >> then apply the JOINs and put the resulting data somewhere, e.g. >> into a file or another database table. >> >> With Python, you could do the join straight away and use the >> data directly without the Python script - without having >> to go through an external temporary resource. >> >> Anyway, just a suggestion. >> >> >> >>> On Sep 23, 2:02 am, "M.-A. Lemburg" wrote: >>>> snfctech wrote: >>>>> Does anyone have experience building a datawarehousein python? Any >>>>> thoughts on custom vs using an out-of-the-box product like Talend or >>>>> Informatica? >> >>>>> I have an integrated system Dashboard project that I was going to >>>>> build using cross-vendor joins on existing DBs, but I keep hearing >>>>> that a datawarehouseis the way to go. e.g. I want to create orders >>>>> and order_items with relations to members (MS Access DB), products >>>>> (flat file) and employees (MySQL). >> >>>>> Thanks in advance for any tips. >> >>>> You might want to look at this solution for doing cross-database JOINs: >> >>>> EasySoft ODBC Join-Engine: >> >>>> http://www.easysoft.com/products/data_access/odbc_odbc_join_engine/in... >> >>>> and then use our mxODBC to access EasySoft's Engine: >> >>>> http://www.egenix.com/products/python/mxODBC/ >> >>>> or mxODBC Connect, if you have a client-server setup: >> >>>> http://www.egenix.com/products/python/mxODBCConnect/ >> >>>> Some database engines also allow integrating external ODBC >>>> data sources - the external tables then look like normal >>>> database tables and can be used in JOINs as well. I know that DB2 >>>> and Oracle support this kind of setup. You can access those using >>>> mxODBC as well. >> >>>> -- >>>> Marc-Andre Lemburg >>>> eGenix.com >> >>>> Professional Python Services directly from the Source (#1, Sep 23 2009)>>> Python/Zope Consulting and Support ... http://www.egenix.com/ >>>>>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ >>>>>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ >> >>>> ________________________________________________________________________ >> >>>> ::: Try our new mxODBC.Connect Python Database Interface for free ! :::: >> >>>> eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 >>>> D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg >>>> Registered at Amtsgericht Duesseldorf: HRB 46611 >>>> http://www.egenix.com/company/contact/ >> >> -- >> Marc-Andre Lemburg >> eGenix.com >> >> Professional Python Services directly from the Source (#1, Sep 24 2009)>>> Python/Zope Consulting and Support ... http://www.egenix.com/ >>>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ >>>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ >> >> ________________________________________________________________________ >> >> ::: Try our new mxODBC.Connect Python Database Interface for free ! :::: >> >> eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 >> D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg >> Registered at Amtsgericht Duesseldorf: HRB 46611 >> http://www.egenix.com/company/contact/ > From ole-usenet-spam at gmx.net Fri Oct 2 05:55:56 2009 From: ole-usenet-spam at gmx.net (Ole Streicher) Date: Fri, 02 Oct 2009 11:55:56 +0200 Subject: weak reference to bound method References: Message-ID: Hello Peter, Peter Otten <__peter__ at web.de> writes: > Is there an actual use case? I discussed this in the german newsgroup. Here is the use in my class: -----------------------------8<----------------------- import threading import weakref class DoAsync(threading.Thread): def __init__(self, func): threading.Thread.__init__(self) self.setDaemon(True) self._cond = threading.Condition() self.scheduled = False self._func = weakref.ref(func, self._cleanup) self.start() def run(self): while self._func(): with self._cond: while not self.scheduled and self._func(): self._cond.wait() self.scheduled = False func = self._func() if func: func() def __call__(self): with self._cond: self.scheduled = True self._cond.notify() def _cleanup(self, ref): self() -----------------------------8<----------------------- The use for this callable class is to take a function call, and whenever the DoAsync object is called, trigger a call to the stored function. Other classes use it like: -----------------------------8<----------------------- class MyClass: def __init__(self): ... self.update = DoAsync(self._do_update) def _do_update(self): do_something_that_takes_long_and_shall_be_done_after_an_update() -----------------------------8<----------------------- Since DoAsync starts its own thread, I get a classical deadlock situation: DoAsync needs a reference to the method to be called, and as long as the thread is running, the MyClass object (which contains the method) cannot be cleaned up. This would be a classic case for a weak reference, if Python would not create it at calling time. > No. o.myfunc is a different object, a bound method, and every time you > access o's myfunc attribute a new bound method is created: What is the reason for that behaviour? It looks quite silly to me. And how can I get a reference to a bound method that lives as long as the method itself? Regards Ole From __peter__ at web.de Fri Oct 2 05:58:23 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 02 Oct 2009 11:58:23 +0200 Subject: weak reference to bound method References: <3e35db93-81a7-4b80-b51a-155f3738506d@v36g2000yqv.googlegroups.com> Message-ID: Ole Streicher wrote: > Hi Thomas, > > Thomas Lehmann writes: >>> r = weakref.ref(o.myfunc) >>> print r() >>> >>>> None >> k = o.myfunc >> r = weakref.ref(k) >> print r() >>>>>>> > >> Don't ask me why! I have just been interested for what you are trying... > > This is clear: in your case, o.myfunc is explicitely referenced by k, > this avoids the garbage collection. > > My problem is that I have a class that delegates a function call, like: > > --------------------8<------------------ > import weakref > > class WeakDelegator(object): > def __init__(self, func): > self._func = weakref.ref(func) > > def __call__(self): > func = self._func() > return func() if func else None > --------------------8<------------------ > > This does not work for bound methods because the weak reference to a > bound method will always point to None, even if the object still exists. > > Why is that the case and how can I implement such a class properly? Maybe: from weakref import ref class A(object): def f(self): return "f" class Method(object): def __init__(self, obj, func=None): if func is None: func = obj.im_func obj = obj.im_self self._im_self = ref(obj) self._im_func = ref(func) def __call__(self): obj = self._im_self() func = self._im_func() if obj is not None and func is not None: return func.__get__(obj)() a = A() m = Method(a.f) print m() del a print m() It's still not clear to me why you would want to do that... Peter From milesck at umich.edu Fri Oct 2 05:59:42 2009 From: milesck at umich.edu (Miles Kaufmann) Date: Fri, 2 Oct 2009 02:59:42 -0700 Subject: weak reference to bound method In-Reply-To: References: Message-ID: <17897C6D-5220-4673-8C67-90002947C915@umich.edu> On Oct 2, 2009, at 1:54 AM, Ole Streicher wrote: > I am trying to use a weak reference to a bound method: > > class MyClass(object): > def myfunc(self): > pass > > o = MyClass() > print o.myfunc >>>>> >>>> 0xc675d0>> > > import weakref > r = weakref.ref(o.myfunc) > print r() >>>>> None > > This is what I do not understand. The object "o" is still alive, and > therefore the bound method "o.myfunc" shall exists. Like Peter said, bound methods are created on demand when they are obtained from the instance, not when the instance is created. > Why does the weak reference claim that it is removed? And how can I > hold > the reference to the method until the object is removed? You could combine unbound methods with a weakref to the object: r = weakref.ref(o) MyClass.myfunc(r()) You could also create a wrapper object that holds a weak reference to the instance and creates a bound method on demand: class WeakMethod(object): def __init__(self, bound_method): self.im_func = bound_method.im_func self.im_self = weakref.ref(bound_method.im_self) self.im_class = bound_method.im_class def __call__(self): obj = self.im_self() if obj is None: return None return types.MethodType(self.im_func, obj, self.im_class) # could alternately act like a callableproxy -Miles From paul at boddie.org.uk Fri Oct 2 06:00:40 2009 From: paul at boddie.org.uk (Paul Boddie) Date: Fri, 2 Oct 2009 03:00:40 -0700 (PDT) Subject: cx_freeze problem on Ubuntu References: Message-ID: <575c00fa-7c8e-429c-b25d-7cb9fe62f8c6@k33g2000yqa.googlegroups.com> On 1 Okt, 16:08, John wrote: > > I downloaded the cx_freeze source code fromhttp://cx-freeze.sourceforge.net/into a directory. [...] > ?From here:http://linux.softpedia.com/get/Programming/Assembler-Tools/cx-Freeze-... > the directions state: What about the documentation available from the SourceForge project? I don't know how reliable or up-to-date the Softpedia stuff is, given that they're apparently aggregating content from elsewhere on the Internet. [...] > As far as I can tell, no one else has ever posted this problem so I > don't know what corrective actions to make. ?I'd greatly appreciate any > assistance. The author appears to use the mailing list for cx_Freeze: https://lists.sourceforge.net/lists/listinfo/cx-freeze-users Perhaps you can get some help in that forum. Paul From ole-usenet-spam at gmx.net Fri Oct 2 06:10:19 2009 From: ole-usenet-spam at gmx.net (Ole Streicher) Date: Fri, 02 Oct 2009 12:10:19 +0200 Subject: weak reference to bound method References: Message-ID: Hi Miles, Miles Kaufmann writes: > You could also create a wrapper object that holds a weak reference to the > instance and creates a bound method on demand: > class WeakMethod(object): > def __init__(self, bound_method): > self.im_func = bound_method.im_func > self.im_self = weakref.ref(bound_method.im_self) > self.im_class = bound_method.im_class In this case, I can use it only for bound methods, so I would need to handle the case of unbound methods separately. Is there a way to find out whether a function is bound? Or do I have to use hasattr(im_func) and hasattr(im_self) and hasattr(im_class)? Best regards Ole From davea at ieee.org Fri Oct 2 06:53:56 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 02 Oct 2009 06:53:56 -0400 Subject: Python and lost files In-Reply-To: <9251ee92-d693-4b59-9c70-c99b4f062c04@c37g2000yqi.googlegroups.com> References: <9251ee92-d693-4b59-9c70-c99b4f062c04@c37g2000yqi.googlegroups.com> Message-ID: <4AC5DBC4.9010900@ieee.org> Carl Banks wrote: > On Sep 30, 11:35 pm, "Timothy W. Grove" wrote: > >> Recently I purchased some software to recover some files which I had >> lost. (A python project, incidentally! Yes, I should have kept better >> backups!) They were nowhere to found in the file system, nor in the >> recycle bin, but this software was able to locate them and restore them. >> > > I could have used that yesterday, if it were able to work for a > network Samba drive. (Yeah, not likely.) > > > >> I was just wondering if there was a way using python to view and recover >> files from the hard drive which would otherwise remain lost forever? >> > > Obviously, if that program was able to do it, it's possible. > > On Unix-like OSes, and probably others, it's possible to read the raw > data on a disk the same way as you would read any file. So Python can > do it without any system-level programming. Recent versions (I think > 2.6+) can use mmap, too, now that it supports an offset parameter. > > I don't think you can do that in Windows, though. I think you'd have > to use special system calls (via ctypes, for example). > > > Carl Banks > > To write such a program, you have two challenges. First is to get read-access to the raw sectors of the partition, and second, to analyze them to discover which ones are interesting, and how they need to be combined to reconstruct the lost data. In Windows, the first challenge is pretty easy for drives other than the system drive (usually drive C:, but not necessarily.) You use one of the following: \\.\X: where X: is the logical drive letter or \\.\PhysicalDriveN where N is the hard drive # (0, 1, 2...) Normally you'd use this only if the data is on a "deleted" or "foreign" partition that Windows doesn't recognize. Naturally, make sure the scratch files and result files you create are going to a different partition/drive. The second challenge is the file system format. If you go with physical drive, you'll have to parse the partitioning information to find individual partitions within the drive, and once you get to a partition, you have to parse the particular file system. Most likely NTFS (which has had several versions). But could be FAT32, FAT16, or a couple of other less likely candidates. While you can also do this for a system partition, there are some challenges that I've no relevant experience with. DaveA From patrick.just4fun at gmail.com Fri Oct 2 07:04:31 2009 From: patrick.just4fun at gmail.com (Patrick Sabin) Date: Fri, 02 Oct 2009 13:04:31 +0200 Subject: setuptools, accessing ressource files Message-ID: <4AC5DE3F.3060304@gmail.com> I use setuptools to create a package. In this package I included some images and I checked that they are in the egg-file. The problem is how can I access the images in the package? I tried pkgutil.get_data, but only got an IOError, because the EGG-INFO directory doesn't exist. I tried pkg_resources.get_distribution('dist').get_metadata('images/image.png') with a similar error (IOError) What is the best way to access images distributed in an egg file? -Patrick From sanneh27 at hotmail.com Fri Oct 2 07:04:56 2009 From: sanneh27 at hotmail.com (baboucarr sanneh) Date: Fri, 2 Oct 2009 11:04:56 +0000 Subject: New Python Novice Message-ID: Hello Everyone, My name is Baboucarr ..am from the gambia (west africa)..I just read about python and i want to know how to program with it.. I would like you guys to help me in my road to becoming a python guru..Am a novice so i would welcome any suggestions etc.. Will be posting again soon.Thanks Regards, $LIM $H at DY _________________________________________________________________ Show them the way! Add maps and directions to your party invites. http://www.microsoft.com/windows/windowslive/products/events.aspx -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Fri Oct 2 07:29:37 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 02 Oct 2009 07:29:37 -0400 Subject: Threaded GUI slowing method execution? In-Reply-To: References: Message-ID: <4AC5E421.8040802@ieee.org> Aaron Hoover wrote: >
I have a > wx GUI application that connects to a serial port in a separate > thread, reads from the port, and then is supposed to put the data it > finds into a queue to be used by the main GUI thread. Generally > speaking, it's working as expected. > > However, one method (that's part of a library I've written to parse > the packet structure of the data that's coming over the serial port) > executes approximately 1000 times slower (50ms vs. 50us) when called > from the serial management thread in the GUI as compared to calling > the same function from within a command line Python script. I checked > it by wrapping the call as follows in both cases (GUI and command > line script): > > tic = time.time() > > print time.time() - tic > > All the thread is doing most of the time is sitting around checking > the serial port for waiting data, reading it, and appending it to a > list when it finds it. Then, in the same thread, the method that seems > to be remarkably slow works its way through that list looking for > packets of data and appending the packet payloads it finds to a queue > to be handled in some way by the GUI. > > My question is, what am I missing about either threading or the fact > that this is running in a GUI that might explain such a huge slowdown. > I'm sending data over the serial at a true rate of about 24k bytes per > second, or approximately 2 packets per ms. Is it too much to ask to be > able to process this data in realtime from within a GUI (I'm not > talking about plotting or anything - just read it and find packets)? > Like I said, the process pretty much runs in realtime from a command > line script. > > This packet parsing needs to happen continuously, so it seems calling > join() to ensure it's not interrupted by the GUI thread, won't work. > > Thanks in advance for your help. > > Aaron > >
> Threading in any language is tricky, so it may be just that. But in Python in particular, you have the GIL (Global Interpreter Lock, I believe), which usually makes writing threaded code easier, but can really hurt efficiency. If you do a search of this list for GIL, you'll see lots of discussion. Or you can google for "GIL python". Look also at http://www.dabeaz.com/python/GIL.pdf I'm no expert here, but let me try to summarize what I recall. The GIL makes writing background threads easier when all but one of the threads are frequently blocked for system calls. But as soon as you have two threads doing "busy work," instead of them getting 50% each, the threading overhead goes way up, and you spend much of your time just deciding what not to do next. And it gets enormously worse than that when there are more than one CPU available (eg. multicore). Many people have concluded that (in Python) much of what threads are used for should be done with processes. DaveA From simon at brunningonline.net Fri Oct 2 07:32:53 2009 From: simon at brunningonline.net (Simon Brunning) Date: Fri, 2 Oct 2009 12:32:53 +0100 Subject: New Python Novice In-Reply-To: References: Message-ID: <8c7f10c60910020432l2488fa86u4cbd3f6e6539d5eb@mail.gmail.com> 2009/10/2 baboucarr sanneh : > Hello Everyone, > > My name is Baboucarr ..am from the gambia (west africa).. I visited some years back. Friendly people. > I just read about > python and i want to know how to program with it.. > I would like you guys to help me in my road to becoming a python guru..Am a > novice so i would welcome any suggestions etc.. You might want to start with . -- Cheers, Simon B. From ole-usenet-spam at gmx.net Fri Oct 2 07:33:37 2009 From: ole-usenet-spam at gmx.net (Ole Streicher) Date: Fri, 02 Oct 2009 13:33:37 +0200 Subject: weak reference to bound method References: <3e35db93-81a7-4b80-b51a-155f3738506d@v36g2000yqv.googlegroups.com> Message-ID: Hi Peter, Peter Otten <__peter__ at web.de> writes: > class Method(object): > def __init__(self, obj, func=None): > if func is None: > func = obj.im_func > obj = obj.im_self This requires that func is a bound method. What I want is to have a universal class that "always" works: with unbound functions, with bound function, with lambda expressions, with locally defined functions, ... For a user of my class, there is no visible reason, why some of them shall work, while others dont. Viele Gr??e Ole From duncan.booth at invalid.invalid Fri Oct 2 07:35:02 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 2 Oct 2009 11:35:02 GMT Subject: Q: sort's key and cmp parameters References: <7xzl8ahfh7.fsf@ruckus.brouhaha.com> Message-ID: Paul Rubin wrote: > Duncan Booth writes: >> > Is there a real-life sorting task that requires (or is far more >> > efficient with) cmp and can't be easily achieved with key and reverse? >> > >> There is no sorting task that *requires* cmp. If all else fails you can >> define a key class to wrap the original wrapper such that comparing the >> keys simply calls the comparison function that you *would* have used. > > I would count that as key being far less efficient, though still > giving the same result. You'll notice I carefully didn't comment on the efficiency. However, without testing it I'm not convinced that it would be 'far less' efficient. Using cmp2key you have an overhead of one class per sort plus one instance for each element being sorted, and an additional one function call for each comparison. The only example given so far that would justify needing this (sorting trees which require recursive comparison) by its very nature would make both of these additional overheads insignificant as each element already contains multiple instances and each comparison contains multiple function calls. From __peter__ at web.de Fri Oct 2 07:47:22 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 02 Oct 2009 13:47:22 +0200 Subject: weak reference to bound method References: <3e35db93-81a7-4b80-b51a-155f3738506d@v36g2000yqv.googlegroups.com> Message-ID: Ole Streicher wrote: > Peter Otten <__peter__ at web.de> writes: >> class Method(object): >> def __init__(self, obj, func=None): >> if func is None: >> func = obj.im_func >> obj = obj.im_self > > This requires that func is a bound method. What I want is to have a > universal class that "always" works: with unbound functions, with > bound function, with lambda expressions, with locally defined functions, That's left as an exercise to the reader ;) From ole-usenet-spam at gmx.net Fri Oct 2 07:54:20 2009 From: ole-usenet-spam at gmx.net (Ole Streicher) Date: Fri, 02 Oct 2009 13:54:20 +0200 Subject: weak reference to bound method References: <3e35db93-81a7-4b80-b51a-155f3738506d@v36g2000yqv.googlegroups.com> Message-ID: Hello Peter, Peter Otten <__peter__ at web.de> writes: >> What I want is to have a universal class that "always" works: with >> unbound functions, with bound function, with lambda expressions, with >> locally defined functions, > That's left as an exercise to the reader ;) Do you have the feeling that there exists any reader that is able to solve this exercise? :-) I am a bit surprised that already such a simple problem is virtually unsolvable in python. Do you think that my concept of having a DoAsync class is wrong? Best regards Ole From sanneh27 at hotmail.com Fri Oct 2 08:01:29 2009 From: sanneh27 at hotmail.com (baboucarr sanneh) Date: Fri, 2 Oct 2009 12:01:29 +0000 Subject: New Python Novice In-Reply-To: <8c7f10c60910020432l2488fa86u4cbd3f6e6539d5eb@mail.gmail.com> References: Message-ID: Thanks simon, Can't wait to get my hands dirty on it.. $LIM $H at DY > From: simon at brunningonline.net > Date: Fri, 2 Oct 2009 12:32:53 +0100 > Subject: Re: New Python Novice > To: sanneh27 at hotmail.com > CC: python-list at python.org > > 2009/10/2 baboucarr sanneh : > > Hello Everyone, > > > > My name is Baboucarr ..am from the gambia (west africa).. > > I visited some years back. Friendly people. > > > I just read about > > python and i want to know how to program with it.. > > I would like you guys to help me in my road to becoming a python guru..Am a > > novice so i would welcome any suggestions etc.. > > You might want to start with . > > -- > Cheers, > Simon B. _________________________________________________________________ Invite your mail contacts to join your friends list with Windows Live Spaces. It's easy! http://spaces.live.com/spacesapi.aspx?wx_action=create&wx_url=/friends.aspx&mkt=en-us -------------- next part -------------- An HTML attachment was scrubbed... URL: From drt80d at gmail.com Fri Oct 2 08:07:28 2009 From: drt80d at gmail.com (Deven T) Date: Fri, 2 Oct 2009 17:37:28 +0530 Subject: New Python Novice In-Reply-To: References: Message-ID: On Fri, Oct 2, 2009 at 4:34 PM, baboucarr sanneh wrote: > Hello Everyone, > > My name is Baboucarr ..am from the gambia (west africa)..I just read about > python and i want to know how to program with it.. > I would like you guys to help me in my road to becoming a python guru..Am a > novice so i would welcome any suggestions etc.. > Will be posting again soon.Thanks > > This is one is very good for beginners: http://www.swaroopch.com/notes/Python -Deven -------------- next part -------------- An HTML attachment was scrubbed... URL: From albert at spenarnc.xs4all.nl Fri Oct 2 08:08:33 2009 From: albert at spenarnc.xs4all.nl (Albert van der Horst) Date: 02 Oct 2009 12:08:33 GMT Subject: easy question, how to double a variable References: <5fc637eb-0a22-4f19-b0e5-755a96b09d43@y20g2000vbk.googlegroups.com> Message-ID: In article , daggerdvm wrote: >you brain needs error checking! Whose brain? At least I know this: Your brain is beyond repair. Go for a brain transplant. Groetjes Albert -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst From __peter__ at web.de Fri Oct 2 08:11:42 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 02 Oct 2009 14:11:42 +0200 Subject: weak reference to bound method References: <3e35db93-81a7-4b80-b51a-155f3738506d@v36g2000yqv.googlegroups.com> Message-ID: Ole Streicher wrote: > Hello Peter, > > Peter Otten <__peter__ at web.de> writes: >>> What I want is to have a universal class that "always" works: with >>> unbound functions, with bound function, with lambda expressions, with >>> locally defined functions, > >> That's left as an exercise to the reader ;) > > Do you have the feeling that there exists any reader that is able to > solve this exercise? :-) I was thinking of you. > I am a bit surprised that already such a simple problem is virtually > unsolvable in python. Do you think that my concept of having a DoAsync > class is wrong? I don't understand the example you give in the other post. If you are trying to use reference counting as a means of inter-thread communication, then yes, I think that's a bad idea. From sanneh27 at hotmail.com Fri Oct 2 08:14:28 2009 From: sanneh27 at hotmail.com (baboucarr sanneh) Date: Fri, 2 Oct 2009 12:14:28 +0000 Subject: New Python Novice In-Reply-To: References: Message-ID: Thanks Deven... Appreciate it $LIM $H at DY Date: Fri, 2 Oct 2009 17:37:28 +0530 Subject: Re: New Python Novice From: drt80d at gmail.com To: sanneh27 at hotmail.com CC: python-list at python.org On Fri, Oct 2, 2009 at 4:34 PM, baboucarr sanneh wrote: Hello Everyone, My name is Baboucarr ..am from the gambia (west africa)..I just read about python and i want to know how to program with it.. I would like you guys to help me in my road to becoming a python guru..Am a novice so i would welcome any suggestions etc.. Will be posting again soon.Thanks This is one is very good for beginners: http://www.swaroopch.com/notes/Python -Deven _________________________________________________________________ More than messages?check out the rest of the Windows Live?. http://www.microsoft.com/windows/windowslive/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From sanneh27 at hotmail.com Fri Oct 2 08:26:58 2009 From: sanneh27 at hotmail.com (baboucarr sanneh) Date: Fri, 2 Oct 2009 12:26:58 +0000 Subject: PyCon 2010 Message-ID: Hi Guys...want to register to the PyCon 2010 conference but i cannot have access to the page because it says the page was not found on the server..I have seen also that they can give out financial aid for those who want to go to the conference.am interested in that too as it will be very expensive for me to pay for my self and also i would like to be using python in my projects..Thanks $LIM $H at DY _________________________________________________________________ More than messages?check out the rest of the Windows Live?. http://www.microsoft.com/windows/windowslive/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From sccolbert at gmail.com Fri Oct 2 08:29:52 2009 From: sccolbert at gmail.com (Chris Colbert) Date: Fri, 2 Oct 2009 14:29:52 +0200 Subject: easy question, how to double a variable In-Reply-To: References: <5fc637eb-0a22-4f19-b0e5-755a96b09d43@y20g2000vbk.googlegroups.com> Message-ID: <7f014ea60910020529l5254bd8auabb821f192ade5ba@mail.gmail.com> I come from a scientific background, so my approach to the solution of this problem is a little different. It makes use of some numerical approximations, but that's not necessarily a bad thing, because it helps avoid singularities. So it could be a little more robust than other solutions presented here. It also has a little more functionality: Say you wanted to return three times a variable, you wouldnt want to write another function to do that for you, so now you just pass in how many times you want the variable repeated as the first parameter. Hope this helps! Cheers! import math def repeat(how_many_times, x): def f(n): return 1./(2**n) def summation(func, howmany): if howmany == 1: return func(1) else: return func(howmany) + summation(func, howmany-1) def eulerify(num): return abs(math.cos(math.pi) + 1j*(math.sin(math.pi))) * num def get_coefficient(multiplier): return eulerify(multiplier * summation(f, 100)) return int(eulerify(get_coefficient(how_many_times) * x)) On Fri, Oct 2, 2009 at 2:08 PM, Albert van der Horst wrote: > In article , > daggerdvm ? wrote: >>you brain needs error checking! > > Whose brain? At least I know this: > > Your brain is beyond repair. Go for a brain transplant. > > Groetjes Albert > > -- > -- > Albert van der Horst, UTRECHT,THE NETHERLANDS > Economic growth -- being exponential -- ultimately falters. > albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst > > -- > http://mail.python.org/mailman/listinfo/python-list > From stefan_ml at behnel.de Fri Oct 2 08:32:56 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 02 Oct 2009 14:32:56 +0200 Subject: Regular expression to structure HTML In-Reply-To: <5c022b51-6c33-452f-8755-d711a0cbe59d@z24g2000yqb.googlegroups.com> References: <98ffa1be-c4da-4c38-9b68-275c14c1948b@e12g2000yqi.googlegroups.com> <5c022b51-6c33-452f-8755-d711a0cbe59d@z24g2000yqb.googlegroups.com> Message-ID: <4ac5f2f9$0$30222$9b4e6d93@newsspool1.arcor-online.net> Paul McGuire wrote: > On Oct 2, 12:10 am, "504cr... at gmail.com" <504cr... at gmail.com> wrote: >> I'm kind of new to regular expressions, and I've spent hours trying to >> finesse a regular expression to build a substitution. >> >> What I'd like to do is extract data elements from HTML and structure >> them so that they can more readily be imported into a database. > > Oy! If I had a nickel for every misguided coder who tried to scrape > HTML with regexes... > > Some reasons why RE's are no good at parsing HTML: > - tags can be mixed case > - tags can have whitespace in many unexpected places > - tags with no body can combine opening and closing tag with a '/' > before the closing '>', as in "
" > - tags can have attributes that you did not expect (like "
CLEAR=ALL>") > - attributes can occur in any order within the tag > - attribute names can also be in unexpected upper/lower case > - attribute values can be enclosed in double quotes, single quotes, or > even (surprise!) NO quotes BTW, BeautifulSoup's parser also uses regexes, so if the OP used it, he/she could claim to have solved the problem "with regular expressions" without even lying. Stefan From ole-usenet-spam at gmx.net Fri Oct 2 08:50:28 2009 From: ole-usenet-spam at gmx.net (Ole Streicher) Date: Fri, 02 Oct 2009 14:50:28 +0200 Subject: weak reference to bound method References: <3e35db93-81a7-4b80-b51a-155f3738506d@v36g2000yqv.googlegroups.com> Message-ID: Hi Peter, Peter Otten <__peter__ at web.de> writes: > Ole Streicher wrote: >> Peter Otten <__peter__ at web.de> writes: >>>> What I want is to have a universal class that "always" works: with >>>> unbound functions, with bound function, with lambda expressions, with >>>> locally defined functions, >>> That's left as an exercise to the reader ;) >> Do you have the feeling that there exists any reader that is able to >> solve this exercise? :-) > I was thinking of you. I could imagine that. However, I am just a beginner in Python and I dont know which types "callables" there exist in python and which "smart" ideas (like re-creating them at every call) additionally occur when I implement such a beast. For example, for locally defined functions, I have still no idea at all on how to keep them away from the garbage collector. >> I am a bit surprised that already such a simple problem is virtually >> unsolvable in python. Do you think that my concept of having a DoAsync >> class is wrong? > I don't understand the example you give in the other post. Hmm. I am programming a GUI client application. The client will receive some input data (via network, and via user input) and shall be updated after these data. Unfortunately, the input data (and ofcourse the user input) do not come regularly; there may times when the data come too fast to process all of them. Imagine, for example, that I want to provide a 2d-gaussian fit to some region of an image and display the result in a separate window, and updated this whenever the mouse is moved. The fit takes (let's say) some seconds, so I cannot just call the fit routine within the mouse move event (this would block other gui operations, and f.e. the display of the mouse coordinates). So I need just to trigger the fit routine on mouse movement, and to check afterwards whether the mouse position is still current. This is the reason for the DoAsync class: when it is called, it shall trigger the function that was given in the constructor, t.m. class MyClass: def __init__(self): self.update_fit = DoAsync(update_the_fit) def mouse_move(self, event): self.set_coordinates(event.x, event.y) self.update_fit() # trigger the update_the_fit() call ... Thus, the mouse_move() method is fast, even if the update_the_fit() method takes some time to process. I want to implement it now that DoAsync will be automatically garbage collected whenever the MyClass object is deleted. Since DoAsync starts its own thread (which only shall finish when the MyClass object is deleted), a reference to MyClass (or one of its functions) will keep the MyClass object from garbage collection. > If you are trying to use reference counting as a means of inter-thread > communication, then yes, I think that's a bad idea. No; my problem is: - a thread started in DoAsync will keep the DoAsync object from garbage collection - a reference to a MyClass realted object (the bound method) in DoAsync will thus also prevent the MyClass object from garbage collection - Even if I dont use the MyClass object anymore, and nobody else uses the DoAsync object, both stand in memory forever, and the thread also never finishes. Did you get the problem? Best regards Ole From __peter__ at web.de Fri Oct 2 09:12:09 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 02 Oct 2009 15:12:09 +0200 Subject: weak reference to bound method References: <3e35db93-81a7-4b80-b51a-155f3738506d@v36g2000yqv.googlegroups.com> Message-ID: Ole Streicher wrote: > Hi Peter, > > Peter Otten <__peter__ at web.de> writes: >> Ole Streicher wrote: >>> Peter Otten <__peter__ at web.de> writes: >>>>> What I want is to have a universal class that "always" works: with >>>>> unbound functions, with bound function, with lambda expressions, with >>>>> locally defined functions, >>>> That's left as an exercise to the reader ;) >>> Do you have the feeling that there exists any reader that is able to >>> solve this exercise? :-) >> I was thinking of you. > > I could imagine that. However, I am just a beginner in Python and I dont > know which types "callables" there exist in python and which "smart" > ideas (like re-creating them at every call) additionally occur when I > implement such a beast. For example, for locally defined functions, I > have still no idea at all on how to keep them away from the garbage > collector. > >>> I am a bit surprised that already such a simple problem is virtually >>> unsolvable in python. Btw, have you implemented such a design in another language? >>> Do you think that my concept of having a DoAsync >>> class is wrong? >> I don't understand the example you give in the other post. > > Hmm. I am programming a GUI client application. The client will receive > some input data (via network, and via user input) and shall be updated > after these data. > > Unfortunately, the input data (and ofcourse the user input) do not come > regularly; there may times when the data come too fast to process all > of them. > > Imagine, for example, that I want to provide a 2d-gaussian fit to some > region of an image and display the result in a separate window, and > updated this whenever the mouse is moved. > > The fit takes (let's say) some seconds, so I cannot just call the fit > routine within the mouse move event (this would block other gui > operations, and f.e. the display of the mouse coordinates). So I need > just to trigger the fit routine on mouse movement, and to check > afterwards whether the mouse position is still current. > > This is the reason for the DoAsync class: when it is called, it shall > trigger the function that was given in the constructor, t.m. > > class MyClass: > def __init__(self): > self.update_fit = DoAsync(update_the_fit) > > def mouse_move(self, event): > self.set_coordinates(event.x, event.y) > self.update_fit() # trigger the update_the_fit() call > ... > > Thus, the mouse_move() method is fast, even if the update_the_fit() > method takes some time to process. > > I want to implement it now that DoAsync will be automatically garbage > collected whenever the MyClass object is deleted. Since DoAsync starts > its own thread (which only shall finish when the MyClass object is > deleted), a reference to MyClass (or one of its functions) will keep the > MyClass object from garbage collection. > >> If you are trying to use reference counting as a means of inter-thread >> communication, then yes, I think that's a bad idea. > > No; my problem is: > > - a thread started in DoAsync will keep the DoAsync object from > garbage collection > - a reference to a MyClass realted object (the bound method) in DoAsync > will thus also prevent the MyClass object from garbage collection > - Even if I dont use the MyClass object anymore, and nobody else uses > the DoAsync object, both stand in memory forever, and the thread also > never finishes. I think I'd go for a simpler approach, manage the lifetime of MyClass instances manually and add a MyClass.close() method that sets a flag which in turn is periodically read by DoAsync() and will eventually make it stop. Peter From jmg3000 at gmail.com Fri Oct 2 09:38:16 2009 From: jmg3000 at gmail.com (John) Date: Fri, 2 Oct 2009 06:38:16 -0700 (PDT) Subject: Regular expression to structure HTML References: <98ffa1be-c4da-4c38-9b68-275c14c1948b@e12g2000yqi.googlegroups.com> Message-ID: On Oct 2, 1:10?am, "504cr... at gmail.com" <504cr... at gmail.com> wrote: > I'm kind of new to regular expressions, and I've spent hours trying to > finesse a regular expression to build a substitution. > > What I'd like to do is extract data elements from HTML and structure > them so that they can more readily be imported into a database. > > No -- sorry -- I don't want to use BeautifulSoup (though I have for > other projects). Humor me, please -- I'd really like to see if this > can be done with just regular expressions. > > Note that the output is referenced using named groups. > > My challenge is successfully matching the HTML tags in between the > first table row, and the second table row. > > I'd appreciate any suggestions to improve the approach. > > rText = "8583
href=lic_details.asp?lic_number=8583>New Horizon Technical Academy, > Inc #4Jefferson70114 tr>9371 lic_number=9371>Career Learning Center valign=top>Jefferson70113" > > rText = re.compile(r'()(?P\d+)()( valign=top>)()(?P[A- > Za-z0-9#\s\S\W]+)().+$').sub(r'LICENSE:\g|NAME: > \g\n', rText) > > print rText > > LICENSE:8583|NAME:New Horizon Technical Academy, Inc #4 valign=top>Jefferson70114 valign=top>9371 lic_number=9371>Career Learning Center|PARISH:Jefferson|ZIP:70113 Some suggestions to start off with: * triple-quote your multiline strings * consider using the re.X, re.M, and re.S options for re.compile() * save your re object after you compile it * note that re.sub() returns a new string Also, it sounds like you want to replace the first 2 elements for each element with their content separated by a pipe (throwing away the tags themselves), correct? ---John From invalid at invalid.invalid Fri Oct 2 09:52:12 2009 From: invalid at invalid.invalid (Grant Edwards) Date: Fri, 2 Oct 2009 13:52:12 +0000 (UTC) Subject: Timestamps for TCP packets? References: <70f68f5d-a122-4df3-a404-43fc19f74e33@u16g2000pru.googlegroups.com> Message-ID: On 2009-10-02, Thomas Johnson wrote: > Is there any way to get kernel-level timestamps for TCP packets while > still using the standard python sockets library for communication? libpcap http://sourceforge.net/projects/pylibpcap/ > I need to communicate over a TCP connection as easily as > possible, but also record the timestamps of the incoming and > outgoing timestamps at microsecond or nanosecond resolution. That shouldn't be a problem, except for the communication API and the capture-packets-with-timestamp API are separate. Remember that there's no guarantee that send() recv() calls correspond in any way to the actual packets sent/recevied. > The sockets library is of course great for the communication, > and I've seen some python libraries that do packet sniffing > and record timestamps, but it's not clear that I can do both > at the same time. Yes, you can do both at the same time. But you have to use separate APIs. -- Grant Edwards grante Yow! TONY RANDALL! Is YOUR at life a PATIO of FUN?? visi.com From briandenzer at gmail.com Fri Oct 2 10:35:04 2009 From: briandenzer at gmail.com (Brian D) Date: Fri, 2 Oct 2009 07:35:04 -0700 (PDT) Subject: Regular expression to structure HTML References: <98ffa1be-c4da-4c38-9b68-275c14c1948b@e12g2000yqi.googlegroups.com> Message-ID: Yes, John, that's correct. I'm trying to trap and discard the row elements, re-formatting with pipes so that I can more readily import the data into a database. The tags are, of course, initially useful for pattern discovery. But there are other approaches -- I could just replace the tags and capture the data as an array. I'm well aware of the problems using regular expressions for html parsing. This isn't merely a question of knowing when to use the right tool. It's a question about how to become a better developer using regular expressions. I'm trying to figure out where the regular expression fails. The structure of the page I'm scraping is uniform in the production of tags -- it's an old ASP page that pulls data from a database. What's different in the first row is the appearance of a comma, a # pound symbol, and a number (", Inc #4"). I'm making the assumption that's what's throwing off the remainder of the regular expression -- because (despite the snark by others above) the expression is working for every other data row. But I could be wrong. Of course, if I could identify the problem, I wouldn't be asking. That's why I posted the question for other eyes to review. I discovered that I may actually be properly parsing the data from the tags when I tried this test in a Python interpreter: >>> s = "New Horizon Technical Academy, Inc #4" >>> p = re.compile(r'([\s\S\WA-Za-z0-9]*)()') >>> m = p.match(s) >>> m = p.match(s) >>> m.group(0) "New Horizon Technical Academy, Inc #4" >>> m.group(1) "New Horizon Technical Academy, Inc #4" >>> m.group(2) '' I found it curious that I was capturing the groups as sequences, but I didn't understand how to use this knowledge in named groups -- or maybe I am merely mis-identifying the source of the regular expression problem. It's a puzzle. I'm hoping someone will want to share the wisdom of their experience, not criticize for the attempt to learn. Maybe one shouldn't learn how to use a hammer on a screw, but I wouldn't say that I have never hammered a screw into a piece of wood just because I only had a hammer. Thanks, Brian On Oct 2, 8:38?am, John wrote: > On Oct 2, 1:10?am, "504cr... at gmail.com" <504cr... at gmail.com> wrote: > > > > > I'm kind of new to regular expressions, and I've spent hours trying to > > finesse a regular expression to build a substitution. > > > What I'd like to do is extract data elements from HTML and structure > > them so that they can more readily be imported into a database. > > > No -- sorry -- I don't want to use BeautifulSoup (though I have for > > other projects). Humor me, please -- I'd really like to see if this > > can be done with just regular expressions. > > > Note that the output is referenced using named groups. > > > My challenge is successfully matching the HTML tags in between the > > first table row, and the second table row. > > > I'd appreciate any suggestions to improve the approach. > > > rText = "8583 > href=lic_details.asp?lic_number=8583>New Horizon Technical Academy, > > Inc #4Jefferson70114 > tr>9371 > lic_number=9371>Career Learning Center > valign=top>Jefferson70113" > > > rText = re.compile(r'()(?P\d+)()( > valign=top>)()(?P[A- > > Za-z0-9#\s\S\W]+)().+$').sub(r'LICENSE:\g|NAME: > > \g\n', rText) > > > print rText > > > LICENSE:8583|NAME:New Horizon Technical Academy, Inc #4 > valign=top>Jefferson70114 > valign=top>9371 > lic_number=9371>Career Learning Center|PARISH:Jefferson|ZIP:70113 > > Some suggestions to start off with: > > ? * triple-quote your multiline strings > ? * consider using the re.X, re.M, and re.S options for re.compile() > ? * save your re object after you compile it > ? * note that re.sub() returns a new string > > Also, it sounds like you want to replace the first 2 elements for > each element with their content separated by a pipe (throwing > away the tags themselves), correct? > > ---John From Scott.Daniels at Acm.Org Fri Oct 2 10:38:07 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 02 Oct 2009 07:38:07 -0700 Subject: Q: sort's key and cmp parameters In-Reply-To: <7xvdiyhfdi.fsf@ruckus.brouhaha.com> References: <7xvdiyhfdi.fsf@ruckus.brouhaha.com> Message-ID: Paul Rubin wrote: > I still have never understood why cmp was removed. Sure, key is more > convenient a lot (or maybe most) of the time, but it's not always. Not just more convenient. cmp will always be N log N, in that _every_ comparison runs your function, while key is linear, in that it is run once per element. Most cases are moreeasily done with key, and it is a good idea to make the most accessible way to a sort be the most efficient one. In the rare case that you really want each comparison, the cmp-injection function will do nicely (and can be written as a recipe. In short, make the easy path the fast path, and more will use it; provide two ways, and the first that springs to mind is the one used. --Scott David Daniels Scott.Daniels at Acm.Org From briandenzer at gmail.com Fri Oct 2 10:48:33 2009 From: briandenzer at gmail.com (Brian D) Date: Fri, 2 Oct 2009 07:48:33 -0700 (PDT) Subject: Regular expression to structure HTML References: <98ffa1be-c4da-4c38-9b68-275c14c1948b@e12g2000yqi.googlegroups.com> Message-ID: The other thought I had was that I may not be properly trapping the end of the first row, and the beginning of the next row. On Oct 2, 8:38?am, John wrote: > On Oct 2, 1:10?am, "504cr... at gmail.com" <504cr... at gmail.com> wrote: > > > > > I'm kind of new to regular expressions, and I've spent hours trying to > > finesse a regular expression to build a substitution. > > > What I'd like to do is extract data elements from HTML and structure > > them so that they can more readily be imported into a database. > > > No -- sorry -- I don't want to use BeautifulSoup (though I have for > > other projects). Humor me, please -- I'd really like to see if this > > can be done with just regular expressions. > > > Note that the output is referenced using named groups. > > > My challenge is successfully matching the HTML tags in between the > > first table row, and the second table row. > > > I'd appreciate any suggestions to improve the approach. > > > rText = "8583 > href=lic_details.asp?lic_number=8583>New Horizon Technical Academy, > > Inc #4Jefferson70114 > tr>9371 > lic_number=9371>Career Learning Center > valign=top>Jefferson70113" > > > rText = re.compile(r'()(?P\d+)()( > valign=top>)()(?P[A- > > Za-z0-9#\s\S\W]+)().+$').sub(r'LICENSE:\g|NAME: > > \g\n', rText) > > > print rText > > > LICENSE:8583|NAME:New Horizon Technical Academy, Inc #4 > valign=top>Jefferson70114 > valign=top>9371 > lic_number=9371>Career Learning Center|PARISH:Jefferson|ZIP:70113 > > Some suggestions to start off with: > > ? * triple-quote your multiline strings > ? * consider using the re.X, re.M, and re.S options for re.compile() > ? * save your re object after you compile it > ? * note that re.sub() returns a new string > > Also, it sounds like you want to replace the first 2 elements for > each element with their content separated by a pipe (throwing > away the tags themselves), correct? > > ---John From george.sakkis at gmail.com Fri Oct 2 10:52:08 2009 From: george.sakkis at gmail.com (George Sakkis) Date: Fri, 2 Oct 2009 07:52:08 -0700 (PDT) Subject: int rich comparisons Message-ID: <3553f463-e58c-4371-9a80-6bfc38a8d31d@b15g2000yqd.googlegroups.com> I stumbled upon the following strangeness (python 2.6.2): >>> getattr(int, '__gt__') >>> getattr(5, '__gt__') Traceback (most recent call last):n File "", line 1, in AttributeError: 'int' object has no attribute '__gt__' Is this a bug ? George From exarkun at twistedmatrix.com Fri Oct 2 11:09:23 2009 From: exarkun at twistedmatrix.com (exarkun at twistedmatrix.com) Date: Fri, 02 Oct 2009 15:09:23 -0000 Subject: Concurrent threads to pull web pages? In-Reply-To: References: <20091002013318.1533.678152062.divmod.xquotient.527@localhost.localdomain> Message-ID: <20091002150923.1533.492636545.divmod.xquotient.564@localhost.localdomain> On 05:48 am, wlfraed at ix.netcom.com wrote: >On Fri, 02 Oct 2009 01:33:18 -0000, exarkun at twistedmatrix.com declaimed >the following in gmane.comp.python.general: >>There's no need to use threads for this. Have a look at Twisted: >> >> http://twistedmatrix.com/trac/ > > Strange... While I can easily visualize how to convert the >problem >to a task pool -- especially given that code to do a single occurrence >is already in place... > > ... conversion to an event-dispatch based system is something >/I/ >can not imagine... The cool thing is that there's not much conversion to do from the single request version to the multiple request version, if you're using Twisted. The single request version looks like this: getPage(url).addCallback(pageReceived) And the multiple request version looks like this: getPage(firstURL).addCallback(pageReceived) getPage(secondURL).addCallback(pageReceived) Since the APIs don't block, doing things concurrently ends up being the easy thing. Not to say it isn't a bit of a challenge to get into this mindset, but I think anyone who wants to put a bit of effort into it can manage. :) Getting used to using Deferreds in the first place (necessary to write/use even the single request version) is probably where more people have trouble. Jean-Paul From francis.moro at gmail.com Fri Oct 2 11:20:00 2009 From: francis.moro at gmail.com (Francis Moreau) Date: Fri, 2 Oct 2009 08:20:00 -0700 (PDT) Subject: question about the GC implementation Message-ID: <5ae5d99c-ec50-4bfe-a25b-4c4db00904ef@f16g2000yqm.googlegroups.com> Hello, I'm looking at gcmodule.c and in move_unreachable() function, the code assumes that if an object has its gc.gc_refs stuff to 0 then it *may* be unreachable. How can an object tagged as unreachable could suddenly become reachable later ? Thanks From ole-usenet-spam at gmx.net Fri Oct 2 11:20:19 2009 From: ole-usenet-spam at gmx.net (Ole Streicher) Date: Fri, 02 Oct 2009 17:20:19 +0200 Subject: weak reference to bound method References: <3e35db93-81a7-4b80-b51a-155f3738506d@v36g2000yqv.googlegroups.com> Message-ID: <874oqhesb0.fsf@news.ole.ath.cx> Hi Peter, Peter Otten <__peter__ at web.de> writes: >>>> I am a bit surprised that already such a simple problem is virtually >>>> unsolvable in python. > Btw, have you implemented such a design in another language? No. > I think I'd go for a simpler approach, manage the lifetime of MyClass > instances manually and add a MyClass.close() method that sets a flag which > in turn is periodically read by DoAsync() and will eventually make it stop. Has the disadvantage that I rely on the user. We already have a garbage collector, so why not use it? It is exactly made for what I want: delete unused objects. The solution I have now is still untested, but maybe the separation of the thread will work: ------------------------------<8---------------------------- import threading import weakref class AsyncThread(threading.Thread): def __init__(self, func): threading.Thread.__init__(self) self.setDaemon(True) self._cond = threading.Condition() self._func = weakref.ref(func, self.run) self.start() def run(self): while self._func(): with self._cond: while not self.scheduled: self._cond.wait() self.scheduled = False self._func() func = self._func() if func: func() def __call__(self): with self._cond: self.scheduled = True self._cond.notify() class DoAsync(object): def __init__(self, func): self._func = func self._thread = AsyncThread(self._func) def __call__(self): self._thread() ------------------------------<8---------------------------- The AsyncThread has now no reference that could prevent the referenced from the GC. And the function is always stored in the DoAsync object and will be only collected if this is removed (what is the case if the parent object is being deleted) Regards Ole From dickinsm at gmail.com Fri Oct 2 11:59:31 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Fri, 2 Oct 2009 08:59:31 -0700 (PDT) Subject: int rich comparisons References: <3553f463-e58c-4371-9a80-6bfc38a8d31d@b15g2000yqd.googlegroups.com> Message-ID: <3c361eb2-6feb-4edf-af2d-eed701e04374@l9g2000yqi.googlegroups.com> On Oct 2, 3:52?pm, George Sakkis wrote: > I stumbled upon the following strangeness (python 2.6.2): > > >>> getattr(int, '__gt__') > > > > >>> getattr(5, '__gt__') > > Traceback (most recent call last):n > ? File "", line 1, in > AttributeError: 'int' object has no attribute '__gt__' > > Is this a bug ? I don't think so. Notice that in the first example you're picking up the __gt__ method of the *type* object, not the int object; i.e., the method that's executed when you do: >>> int > float True You get similar results with __call__: >>> int.__call__ >>> int.__call__(3) 3 >>> (5).__call__ Traceback (most recent call last): File "", line 1, in AttributeError: 'int' object has no attribute '__call__' Mark From vorticitywolfe at gmail.com Fri Oct 2 12:05:14 2009 From: vorticitywolfe at gmail.com (J Wolfe) Date: Fri, 2 Oct 2009 09:05:14 -0700 (PDT) Subject: re.sub do not replace portion of match Message-ID: <3c4b94a2-52a1-476f-839a-253f104f87df@b15g2000yqd.googlegroups.com> Hi, Is there a way to flag re.sub not to replace a portion of the string? I have a very long string that I want to add two new line's to rather than one, but keep the value X: string = "test........X.\n.today" <-- note X is a value string = re.sub("test........X.\n.","test........X.\n\n.", string) This just replaces X with the replacement string. Thanks, Jonathan From carlo.dicelico at gmail.com Fri Oct 2 12:07:47 2009 From: carlo.dicelico at gmail.com (Carlo DiCelico) Date: Fri, 2 Oct 2009 09:07:47 -0700 (PDT) Subject: Simple Python struct issue Message-ID: <3e8a944b-f677-4fa2-b682-8936c9c1c5bf@j39g2000yqh.googlegroups.com> I saw an article on O'Reilly about using NumPy and Dislin to analyze and visualize WAV files. It's a really fantastic article but was a little out of date. I updated the script to work with the newer modules &etc but am still having trouble getting it working. The line temp[i,:] = array(struct.unpack("%dB"%(fft_length), tempb),Float) - 128.0 always returns the same error: "Traceback (most recent call last): File "pysono.py", line 31, in temp[i,:] = array(struct.unpack("%dB"%(fft_length),tempb),float) - 128.0 struct.error: unpack requires a string argument of length 256" when I do python pysono.py test.wav 256 I'm sure it's probably something simple but I just can't see what it is! Here's the original code: http://onlamp.com/python/2001/01/31/graphics/pysono.py Thanks! From duncan.booth at invalid.invalid Fri Oct 2 12:10:28 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 2 Oct 2009 16:10:28 GMT Subject: re.sub do not replace portion of match References: <3c4b94a2-52a1-476f-839a-253f104f87df@b15g2000yqd.googlegroups.com> Message-ID: J Wolfe wrote: > Hi, > > Is there a way to flag re.sub not to replace a portion of the string? > > I have a very long string that I want to add two new line's to rather > than one, but keep the value X: > > string = "test........X.\n.today" <-- note X is a value > string = re.sub("test........X.\n.","test........X.\n\n.", string) > > This just replaces X with the replacement string. > > Thanks, > Jonathan > Have you tried reading the documentation? http://www.python.org/doc/current/library/re.html#re.sub > Backreferences, such as \6, are replaced with the substring matched by > group 6 in the pattern. and more such options in the docs. From editor at pythonrag.org Fri Oct 2 12:14:21 2009 From: editor at pythonrag.org (Bernie) Date: Fri, 02 Oct 2009 11:14:21 -0500 Subject: The Python: Rag October issue available Message-ID: The Python: Rag October issue available The October issue of The Python: Rag is available at: http://www.pythonrag.org A monthly, free, community run, Python magazine - issues are in pdf format, intended for anyone interested in Python, without being particularly serious. If you have anything you would like to say about Python, please contribute. From sajmikins at gmail.com Fri Oct 2 12:15:54 2009 From: sajmikins at gmail.com (Simon Forman) Date: Fri, 2 Oct 2009 12:15:54 -0400 Subject: Simple Python struct issue In-Reply-To: <3e8a944b-f677-4fa2-b682-8936c9c1c5bf@j39g2000yqh.googlegroups.com> References: <3e8a944b-f677-4fa2-b682-8936c9c1c5bf@j39g2000yqh.googlegroups.com> Message-ID: <50f98a4c0910020915n708531cfn81c4e2d8072c2921@mail.gmail.com> On Fri, Oct 2, 2009 at 12:07 PM, Carlo DiCelico wrote: > I saw an article on O'Reilly about using NumPy and Dislin to analyze > and visualize WAV files. It's a really fantastic article but was a > little out of date. I updated the script to work with the newer > modules &etc but am still having trouble getting it working. > > The line > > temp[i,:] = array(struct.unpack("%dB"%(fft_length), tempb),Float) - > 128.0 > > always returns the same error: "Traceback (most recent call last): > ?File "pysono.py", line 31, in > ? temp[i,:] = array(struct.unpack("%dB"%(fft_length),tempb),float) - > 128.0 > struct.error: unpack requires a string argument of length 256" when I > do python pysono.py test.wav 256 > > I'm sure it's probably something simple but I just can't see what it > is! > > Here's the original code: http://onlamp.com/python/2001/01/31/graphics/pysono.py > > Thanks! In: struct.unpack("%dB" % (fft_length), tempb) tempb is not length 256. Also, note that (foo) is the same as just foo. To create a tuple of length 1 you must say (foo,) HTH, ~Simon From vorticitywolfe at gmail.com Fri Oct 2 12:20:20 2009 From: vorticitywolfe at gmail.com (J Wolfe) Date: Fri, 2 Oct 2009 09:20:20 -0700 (PDT) Subject: re.sub do not replace portion of match References: <3c4b94a2-52a1-476f-839a-253f104f87df@b15g2000yqd.googlegroups.com> Message-ID: Thanks Duncan, I did look at that, but it was kinda greek to me. Thanks for pulling out the part I was looking for that should do the trick. Jonathan > http://www.python.org/doc/current/library/re.html#re.sub > > > Backreferences, such as \6, are replaced with the substring matched by > > group 6 in the pattern. From carlo.dicelico at gmail.com Fri Oct 2 12:35:11 2009 From: carlo.dicelico at gmail.com (Carlo DiCelico) Date: Fri, 2 Oct 2009 09:35:11 -0700 (PDT) Subject: Simple Python struct issue References: <3e8a944b-f677-4fa2-b682-8936c9c1c5bf@j39g2000yqh.googlegroups.com> Message-ID: <37b040a9-5e12-4675-a8fa-2852456f8f4f@a21g2000yqc.googlegroups.com> On Oct 2, 12:15?pm, Simon Forman wrote: > On Fri, Oct 2, 2009 at 12:07 PM, Carlo DiCelico > > > > > > wrote: > > I saw an article on O'Reilly about using NumPy and Dislin to analyze > > and visualize WAV files. It's a really fantastic article but was a > > little out of date. I updated the script to work with the newer > > modules &etc but am still having trouble getting it working. > > > The line > > > temp[i,:] = array(struct.unpack("%dB"%(fft_length), tempb),Float) - > > 128.0 > > > always returns the same error: "Traceback (most recent call last): > > ?File "pysono.py", line 31, in > > ? temp[i,:] = array(struct.unpack("%dB"%(fft_length),tempb),float) - > > 128.0 > > struct.error: unpack requires a string argument of length 256" when I > > do python pysono.py test.wav 256 > > > I'm sure it's probably something simple but I just can't see what it > > is! > > > Here's the original code:http://onlamp.com/python/2001/01/31/graphics/pysono.py > > > Thanks! > > In: > > struct.unpack("%dB" % (fft_length), tempb) > > tempb is not length 256. > > Also, note that (foo) is the same as just foo. ?To create a tuple of > length 1 you must say (foo,) > > HTH, > ~Simon I'm sorry, I'm not sure what you're referring to when you say "Also, note that (foo) is the same as just foo. To create a tuple of length 1 you must say (foo,)". The 256 is passed into the script as an argument and then assigned to the variable fft_length, which you can see in that line. So, whatever value I pass in comes out in the error. What you're saying is that the error happens because tempb isn't the length of any of those values (i.e., 128, 256, 512, etc)? (1) How could I determine the length of tempb? and (2) It's doing this -> tempb = fp.readframes(fft_length); right before doing the struct.unpack (); could the splitting of the frames into frames of length fft_length be causing this error? Thanks for the help! Carlo From python at mrabarnett.plus.com Fri Oct 2 12:49:56 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 02 Oct 2009 17:49:56 +0100 Subject: Simple Python struct issue In-Reply-To: <3e8a944b-f677-4fa2-b682-8936c9c1c5bf@j39g2000yqh.googlegroups.com> References: <3e8a944b-f677-4fa2-b682-8936c9c1c5bf@j39g2000yqh.googlegroups.com> Message-ID: <4AC62F34.6090407@mrabarnett.plus.com> Carlo DiCelico wrote: > I saw an article on O'Reilly about using NumPy and Dislin to analyze > and visualize WAV files. It's a really fantastic article but was a > little out of date. I updated the script to work with the newer > modules &etc but am still having trouble getting it working. > > The line > > temp[i,:] = array(struct.unpack("%dB"%(fft_length), tempb),Float) - > 128.0 > > always returns the same error: "Traceback (most recent call last): > File "pysono.py", line 31, in > temp[i,:] = array(struct.unpack("%dB"%(fft_length),tempb),float) - > 128.0 > struct.error: unpack requires a string argument of length 256" when I > do python pysono.py test.wav 256 > > I'm sure it's probably something simple but I just can't see what it > is! > > Here's the original code: http://onlamp.com/python/2001/01/31/graphics/pysono.py > .readframes(n) returns (at most) n _frames_ as a bytestring (str), not n bytes. I tried reading 256 frames from a .wav file containing stereo at 16 bits per channel and got 1024 bytes (4 bytes per frame, not surprisingly!). From nagle at animats.com Fri Oct 2 14:06:10 2009 From: nagle at animats.com (John Nagle) Date: Fri, 02 Oct 2009 11:06:10 -0700 Subject: M2Crypto 0.20.1 won't build on Red Hat Linux In-Reply-To: References: <4ac39492$0$1642$742ec2ed@news.sonic.net> <4ac4e173$0$1674$742ec2ed@news.sonic.net> Message-ID: <4ac63f35$0$1658$742ec2ed@news.sonic.net> Heikki Toivonen wrote: > John Nagle wrote: >> The right question is "uname --hardware-platform". That returns "i386" if >> running on something emulating a 386, even it it's 64-bit capable. > > Thanks, I'll make a note that I'll need to clarify that part. > >> With that change, the build runs to completion and and the regression tests >> mostly run. Test failure occurred at: >> >> ====================================================================== >> FAIL: test_cipher_ok (tests.test_ssl.MiscSSLClientTestCase) >> ---------------------------------------------------------------------- >> Traceback (most recent call last): >> File >> "/var/www/vhosts/sitetruth.com/private/downloads/M2Crypto/M2Crypto-0.20.1/tests/test_ssl.py", >> line 485, in test_cipher_ok >> assert s.get_cipher().name() == 'AES128-SHA', s.get_cipher().name() >> AssertionError: AES256-SHA >> >> ---------------------------------------------------------------------- >> Ran 226 tests in 91.672s >> >> Is 'AES128-SHA' a cypher that Fedora Core doesn't support, or what? > > That seems strange. OpenSSL does allow fine tuning what ciphers to build > and operate with, but this is the first I hear AES128-SHA might not be > turned on anymore. And even stranger still is that the test is > explicitly asking for 128 yet it is getting 256 back. Could you rerun > the test again just to make sure you can repeat it, and if so, let me > know the fedora core, openssl, swig and python versions? Ran it again. Same error. python setup.py test ... test_blocking0 (tests.test_ssl.MiscSSLClientTestCase) ... ok test_blocking1 (tests.test_ssl.MiscSSLClientTestCase) ... ok test_cipher_mismatch (tests.test_ssl.MiscSSLClientTestCase) ... ok test_cipher_ok (tests.test_ssl.MiscSSLClientTestCase) ... FAIL test_info_callback (tests.test_ssl.MiscSSLClientTestCase) ... LOOP: SSL connect: before/connect initialization LOOP: SSL connect: SSLv2/v3 write client hello A ... test_26_compat (tests.test_ssl.FtpslibTestCase) ... ok ====================================================================== FAIL: test_cipher_ok (tests.test_ssl.MiscSSLClientTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/var/www/vhosts/sitetruth.com/private/downloads/M2Crypto/M2Crypto-0.20.1/tests/test_ssl.py", line 485, in test_cipher_ok assert s.get_cipher().name() == 'AES128-SHA', s.get_cipher().name() AssertionError: AES256-SHA ---------------------------------------------------------------------- Ran 226 tests in 166.875s FAILED (failures=1) System info: -bash-3.1$ uname -r -v 2.6.18-1.2239.fc5smp #1 SMP Fri Nov 10 13:22:44 EST 2006 -bash-3.1$ python --version Python 2.5 -bash-3.1$ swig -version SWIG Version 1.3.31 Compiled with i386-redhat-linux-g++ [i386-redhat-linux-gnu] Please see http://www.swig.org for reporting bugs and further information -bash-3.1$ openssl OpenSSL> version OpenSSL 0.9.8a 11 Oct 2005 OpenSSL> So, per "http://chandlerproject.org/bin/view/Projects/MeTooCrypto": Required Python version: 2.4 or newer. Using Python 2.5. REQUIREMENT MET. Required SWIG version: 1.3.30 or newer. Using SWIG 1.3.31 REQUIREMENT MET. Required OpenSSL version: 0.9.8 or newer: Using OpenSSL 0.9.8a. REQUIREMENT MET. John Nagle From sturlamolden at yahoo.no Fri Oct 2 14:09:47 2009 From: sturlamolden at yahoo.no (sturlamolden) Date: Fri, 2 Oct 2009 11:09:47 -0700 (PDT) Subject: Threaded GUI slowing method execution? References: Message-ID: On 2 Okt, 13:29, Dave Angel wrote: > Many people have concluded that (in Python) much of what threads are > used for should be done with processes. Remember that threads were invented long before multi-core CPUs were common. Java had threads before the VM could support more than one CPU. The early Linux kernel had threads despite of the BKL, etc. Threads have more usage than concurrent CPU bound busy work. If you need to program multi-cores for speed, consider that you incur a 200x speed penalty from Python alone. If you are worried about speed, chances are you are not using Python anyway. If you still have "need for speed" on a multicore, you can use Cython and release the GIL when appropriate. Then launch multiple Python threads and be happy. In C, C++ and Fortran, one would not use threads for multicore programming at all, but OpenMP. Using more than one process is always an option, i.e. os.fork if you have it or multiprocessing if you don't. Processes don't share GIL. S.M. From ole-usenet-spam at gmx.net Fri Oct 2 14:19:35 2009 From: ole-usenet-spam at gmx.net (Ole Streicher) Date: Fri, 02 Oct 2009 20:19:35 +0200 Subject: Threaded GUI slowing method execution? References: Message-ID: <87r5tlr748.fsf@news.ole.ath.cx> sturlamolden writes: > On 2 Okt, 13:29, Dave Angel wrote: > If you are worried about speed, chances are you are not using Python > anyway. I *do* worry about speed. And I use Python. Why not? There are powerful libraries available. > If you still have "need for speed" on a multicore, you can use Cython > and release the GIL when appropriate. Then launch multiple Python > threads and be happy. Usually this is not an option: numpy is AFAIK not available for Cython, neither is scipy (ofcourse). Especially for numeric calculations, speed *matters*. > Using more than one process is always an option, i.e. os.fork if you > have it or multiprocessing if you don't. Processes don't share GIL. Not if the threads/processes need to share lots of data. Interprocess communication can be very expensive -- even more if one needs to share Python objects. Also, the support of sharing python objects between processes seems to me not well supported at least by the standard python libs. Ole From sturlamolden at yahoo.no Fri Oct 2 14:22:19 2009 From: sturlamolden at yahoo.no (sturlamolden) Date: Fri, 2 Oct 2009 11:22:19 -0700 (PDT) Subject: Threaded GUI slowing method execution? References: Message-ID: <30bb25a0-ddf8-41dc-aee9-780fc9b57548@v36g2000yqv.googlegroups.com> On 2 Okt, 02:51, Aaron Hoover wrote: > All the thread is doing most of the time is sitting around checking ? > the serial port for waiting data, reading it, and appending it to a ? > list when it finds it. Do your threads ever block waiting for I/O? If they do, is the GIL released while they are blocked? If they don't, you could add a time.sleep(0) to the loop that polls the port. It will return the rest of the current time slice back to the system. Then, in the same thread, the method that seems ? > to be remarkably slow works its way through that list looking for ? > packets of data and appending the packet payloads it finds to a queue ? > to be handled in some way by the GUI. > > My question is, what am I missing about either threading or the fact ? > that this is running in a GUI that might explain such a huge slowdown. ? > I'm sending data over the serial at a true rate of about 24k bytes per ? > second, or approximately 2 packets per ms. Is it too much to ask to be ? > able to process this data in realtime from within a GUI (I'm not ? > talking about plotting or anything - just read it and find packets)? ? > Like I said, the process pretty much runs in realtime from a command ? > line script. > > This packet parsing needs to happen continuously, so it seems calling ? > join() to ensure it's not interrupted by the GUI thread, won't work. > > Thanks in advance for your help. > > Aaron From steven.oldner at gmail.com Fri Oct 2 14:32:41 2009 From: steven.oldner at gmail.com (steven.oldner) Date: Fri, 2 Oct 2009 11:32:41 -0700 (PDT) Subject: The Python: Rag October issue available References: Message-ID: <9f17fba3-3a9e-40c3-9975-e79a42998ca9@y36g2000yqh.googlegroups.com> On Oct 2, 11:14?am, Bernie wrote: > The Python: Rag October issue available > > The October issue of The Python: Rag is available at: > > http://www.pythonrag.org > > A monthly, free, community run, Python magazine - issues are in pdf > format, intended for anyone interested in Python, without being > particularly serious. ?If you have anything you would like to say about > Python, please contribute. Thanks! Any way to subscribe to it? From montaseri at gmail.com Fri Oct 2 14:38:38 2009 From: montaseri at gmail.com (Medi) Date: Fri, 2 Oct 2009 11:38:38 -0700 (PDT) Subject: epydoc - can I provide multiple dirs to parse Message-ID: Can I present multiple directories to epydoc to process. For example epydoc -option -option -option dir_1 dir_2 dir_3 where dir_i is a directory containing some python src codes ? I am currently running it on two directories with about 10M + 20 Meg which takes a very long time (more than 40 minutes) and strace(1) is showing mmap(2) ops back to back....might not be related but the process gets wedged somewhere Thanks Medi From sturlamolden at yahoo.no Fri Oct 2 14:42:53 2009 From: sturlamolden at yahoo.no (sturlamolden) Date: Fri, 2 Oct 2009 11:42:53 -0700 (PDT) Subject: Threaded GUI slowing method execution? References: <87r5tlr748.fsf@news.ole.ath.cx> Message-ID: <20d2e97f-56d3-45de-a795-fb5556d490c7@b2g2000yqi.googlegroups.com> On 2 Okt, 20:19, Ole Streicher wrote: > I *do* worry about speed. And I use Python. Why not? There are powerful > libraries available. I do as well. But "powerful libraries" should release the GIL. Let me rephrase that: I am not worried about speed in the part of my code that uses Python. > Usually this is not an option: numpy is AFAIK not available for Cython, > neither is scipy (ofcourse). Anything available to Python is available to Cython. Cython even has a special syntax for working with NumPy arrays. > > Using more than one process is always an option, i.e. os.fork if you > > have it or multiprocessing if you don't. Processes don't share GIL. > > Not if the threads/processes need to share lots of data. Interprocess > communication can be very expensive -- even more if one needs to share > Python objects. I have written NumPy arrays that uses named shared memory as buffer. They are pickled by name (i.e. the buffer is not copied) and therefore very efficient when used with multiprocessing. http://folk.uio.no/sturlamo/python/sharedmem-feb13-2009.zip IPC without shared memory is generally cheap. The overhead of a pipe or a unix domain socket is little more than that of a memcpy. The expensive part is serializing and deserializing the Python object. Also consider that the majority of the world's supercomputers are programmed with MPI, which uses processes and IPC instead of threads. On clusters, threads are not even an option. On shared memory machines, MPI tends to be more efficient than threads/OpenMP (there are often issues with cache use and false sharing when using threads). S.M. From stef.mientki at gmail.com Fri Oct 2 14:53:02 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Fri, 02 Oct 2009 20:53:02 +0200 Subject: store encrypted data in sqlite ? Message-ID: <4AC64C0E.6040406@gmail.com> hello, I want to store some fields in an sqlite database. I use ezPyCrypto to encrypt and decrypt: User = ['z684684', 'Mientki, Stef', 1,1,0,1,1 ] encryption_key_1 = ezPyCrypto.key ( 512 ) SQL_Base = 'insert or replace into __USERS__ values (' for field in User : SQL += ",'" + encryption_key_1.encString ( str ( item ))+ "'" SQL += ')' Now this fails, probably, because the second character of the encrypted string is a binary zero. By trial and error, I found a work around, but I'm not sure this will garantee that it will work always: by converting the encrypted buffer with base64.encode: SQL += ",'" + base64.encodestring(EnCrypt_1 ( str ( item )))+ "'" Will this method work always ? Are there better methods ? thanks, Stef Mientki From 504crank at gmail.com Fri Oct 2 15:05:10 2009 From: 504crank at gmail.com (504crank at gmail.com) Date: Fri, 2 Oct 2009 12:05:10 -0700 (PDT) Subject: Regular expression to structure HTML References: <98ffa1be-c4da-4c38-9b68-275c14c1948b@e12g2000yqi.googlegroups.com> Message-ID: <175f65aa-3959-44fa-a089-a1d7c321cb3f@o10g2000yqa.googlegroups.com> Screw: >>> html = """ 14313 Python Hammer Institute #2 Jefferson 70114 8583 New Screwdriver Technical Academy, Inc #4 Jefferson 70114 9371 Career RegEx Center Jefferson 70113 """ Hammer: First remove line returns. Then remove extra spaces. Then insert a line return to restore logical rows on each combination. For more information, see: http://www.qc4blog.com/?p=55 >>> s = re.sub(r'\n','', html) >>> s = re.sub(r'\s{2,}', '', s) >>> s = re.sub('()()', r'\1\n\2', s) >>> print s 14313Python Hammer Institute #2Jefferson70114 8583New Screwdriver Technical Academy, Inc #4Jefferson70114 9371Career RegEx CenterJefferson70113 >>> p = re.compile(r"()(?P\d+)()()()(?P[\s\S\WA-Za-z0-9]*?)()()(?:)(?P[\s\WA-Za-z]+)()()(?P\d+)()()$", re.M) >>> n = p.sub(r'LICENSE:\g|NAME:\g|PARISH:\g|ZIP:\g', s) >>> print n LICENSE:14313|NAME:Python Hammer Institute #2|PARISH:Jefferson|ZIP: 70114 LICENSE:8583|NAME:New Screwdriver Technical Academy, Inc #4| PARISH:Jefferson|ZIP:70114 LICENSE:9371|NAME:Career RegEx Center|PARISH:Jefferson|ZIP:70113 >>> The solution was to escape the period in the ".asp" string, e.g., "\.asp". I also had to limit the pattern in the grouping by using a "?" qualifier to limit the "greediness" of the "*" pattern metacharacter. Now, who would like to turn that re.compile pattern into a MULTILINE expression, combining the re.M and re.X flags? Documentation says that one should be able to use the bitwise OR operator (e.g., re.M | re.X), but I sure couldn't get it to work. Sometimes a hammer actually is the right tool if you hit the screw long and hard enough. I think I'll try to hit some more screws with my new hammer. Good day. On Oct 2, 12:10?am, "504cr... at gmail.com" <504cr... at gmail.com> wrote: > I'm kind of new to regular expressions, and I've spent hours trying to > finesse a regular expression to build a substitution. > > What I'd like to do is extract data elements from HTML and structure > them so that they can more readily be imported into a database. > > No -- sorry -- I don't want to use BeautifulSoup (though I have for > other projects). Humor me, please -- I'd really like to see if this > can be done with just regular expressions. > > Note that the output is referenced using named groups. > > My challenge is successfully matching the HTML tags in between the > first table row, and the second table row. > > I'd appreciate any suggestions to improve the approach. > > rText = "8583 href=lic_details.asp?lic_number=8583>New Horizon Technical Academy, > Inc #4Jefferson70114 tr>9371 lic_number=9371>Career Learning Center valign=top>Jefferson70113" > > rText = re.compile(r'()(?P\d+)()( valign=top>)()(?P[A- > Za-z0-9#\s\S\W]+)().+$').sub(r'LICENSE:\g|NAME: > \g\n', rText) > > print rText > > LICENSE:8583|NAME:New Horizon Technical Academy, Inc #4 valign=top>Jefferson70114 valign=top>9371 lic_number=9371>Career Learning Center|PARISH:Jefferson|ZIP:70113 From dickinsm at gmail.com Fri Oct 2 15:07:48 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Fri, 2 Oct 2009 12:07:48 -0700 (PDT) Subject: Fast decimal arithmetic module released References: Message-ID: On Oct 2, 8:53?pm, Stefan Krah wrote: > Hi, > > today I have released the following packages for fast arbitrary precision > decimal arithmetic: > [...] Nice! I'd been wondering how you'd been finding all those decimal.py bugs. Now I know. :) -- Mark From sajmikins at gmail.com Fri Oct 2 15:17:07 2009 From: sajmikins at gmail.com (Simon Forman) Date: Fri, 2 Oct 2009 15:17:07 -0400 Subject: Simple Python struct issue In-Reply-To: <37b040a9-5e12-4675-a8fa-2852456f8f4f@a21g2000yqc.googlegroups.com> References: <3e8a944b-f677-4fa2-b682-8936c9c1c5bf@j39g2000yqh.googlegroups.com> <37b040a9-5e12-4675-a8fa-2852456f8f4f@a21g2000yqc.googlegroups.com> Message-ID: <50f98a4c0910021217i37b262ebvc34da5cb2b4fee13@mail.gmail.com> On Fri, Oct 2, 2009 at 12:35 PM, Carlo DiCelico wrote: > On Oct 2, 12:15?pm, Simon Forman wrote: >> On Fri, Oct 2, 2009 at 12:07 PM, Carlo DiCelico >> wrote: >> > I saw an article on O'Reilly about using NumPy and Dislin to analyze >> > and visualize WAV files. It's a really fantastic article but was a >> > little out of date. I updated the script to work with the newer >> > modules &etc but am still having trouble getting it working. >> >> > The line >> >> > temp[i,:] = array(struct.unpack("%dB"%(fft_length), tempb),Float) - >> > 128.0 >> >> > always returns the same error: "Traceback (most recent call last): >> > ?File "pysono.py", line 31, in >> > ? temp[i,:] = array(struct.unpack("%dB"%(fft_length),tempb),float) - >> > 128.0 >> > struct.error: unpack requires a string argument of length 256" when I >> > do python pysono.py test.wav 256 >> >> > I'm sure it's probably something simple but I just can't see what it >> > is! >> >> > Here's the original code:http://onlamp.com/python/2001/01/31/graphics/pysono.py >> >> > Thanks! >> >> In: >> >> struct.unpack("%dB" % (fft_length), tempb) >> >> tempb is not length 256. >> >> Also, note that (foo) is the same as just foo. ?To create a tuple of >> length 1 you must say (foo,) >> >> HTH, >> ~Simon > > I'm sorry, I'm not sure what you're referring to when you say "Also, > note that (foo) is the same as just foo. ?To create a tuple of length > 1 you must say (foo,)". Sorry about that. All I mean is that in python putting parentheses around an expression "(some_object)" does not create a tuple unless you also use a comma "," I hope the following examples show what I mean: In [1]: (2) Out[1]: 2 In [2]: (2,) Out[2]: (2,) In [3]: 2, Out[3]: (2,) In [4]: n = 2, In [5]: "%s" % (n,) Out[5]: '(2,)' In [6]: "%s" % (n) Out[6]: '2' In [7]: "%s" % n Out[7]: '2' Your code: "%dB" % (fft_length) is the same as "%dB" % fft_length But what you probably meant was "%dB" % (fft_length,) Note the comma. People do this to prevent string formatting errors if fft_length should ever happen to be a tuple rather than an int (or whatever.) > The 256 is passed into the script as an > argument and then assigned to the variable fft_length, which you can > see in that line. So, whatever value I pass in comes out in the error. > What you're saying is that the error happens because tempb isn't the > length of any of those values (i.e., 128, 256, 512, etc)? Yes, whatever tempb is, it isn't "a string argument of length 256" as the traceback says. > (1) How > could I determine the length of tempb? If it's a string, len(tempb) will return it's length. > and (2) It's doing this -> > tempb = fp.readframes(fft_length); right before doing the struct.unpack > (); could the splitting of the frames into frames of length fft_length > be causing this error? See MRAB's reply. :] > Thanks for the help! You're very welcome. One other thing, in the code you posted it has "Float", but in the traceback it has "float". Generally speaking when you post code to a newsgroup for help you should paste in the exact code, rather than retyping it manually. Warm regards, and happy hacking! From aaron.watters at gmail.com Fri Oct 2 15:24:18 2009 From: aaron.watters at gmail.com (Aaron Watters) Date: Fri, 2 Oct 2009 12:24:18 -0700 (PDT) Subject: AJAX Widget Framework References: Message-ID: <2a0a4b75-0b22-4ffd-9cbe-ccd55b7b3b32@a21g2000yqc.googlegroups.com> On Oct 1, 2:01?pm, Laszlo Nagy wrote: > I'm looking for an open source, AJAX based widget/windowing framework. > Here is what I need: > > - end user opens up a browser, points it to a URL, logs in > - on the server site, sits my application, creating a new session for > each user that is logged in > - on the server site, I create windows(frames), put widgets on them, > write event handlers etc. Just like with wx or pygtk. > - However, windows are created in the user's browser, and events are > triggered by Javascript, and sent back to server through AJAX. > - the server side would be - of course - written in Python.... WHIFF provides support for these type of AJAX capabilities. Please have a look at the AJAX calculator tutorial at http://aaron.oirt.rutgers.edu/myapp/docs/W1100_1400.calc for an example with discussion of some of the built in support for AJAX. You could probably use this as a starting point for building your "grid" widget. I've used WHIFF with jquery and mootools javascript libraries with much success. Please let me know what you think. -- Aaron Watters === Ban DHMO! http://www.dhmo.org/ From davea at ieee.org Fri Oct 2 15:30:42 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 02 Oct 2009 15:30:42 -0400 Subject: Threaded GUI slowing method execution? In-Reply-To: <97B7C9E0-AC7D-4DDE-91C5-FDDD25ADAB8D@EECS.Berkeley.EDU> References: <4AC5E421.8040802@ieee.org> <97B7C9E0-AC7D-4DDE-91C5-FDDD25ADAB8D@EECS.Berkeley.EDU> Message-ID: <4AC654E2.4010503@ieee.org> (you responded off-list, which isn't the way these mailing lists work. So I'm pasting your message back to the list, with my response at the end) Aaron Hoover wrote: >>> > >> But as soon as you have two threads doing "busy work," instead of >> them getting 50% each, the threading overhead goes way up, and you >> spend much of your time just deciding what not to do next. > > Yeah, I had briefly read some grumblings regarding the GIL, but > naively assumed it wouldn't bite me too badly. > > >> And it gets enormously worse than that when there are more than one >> CPU available (eg. multicore). > > Well, I'm running on a MacBook Pro, so count me in for that multicore > performance degradation! > > >> >> Many people have concluded that (in Python) much of what threads are >> used for should be done with processes. > > Presumably you're referring to the processing module (pre 2.6 - > multiprocessing in 2.6)? Do you have any canonical references or > example code for using processing in the producer/consumer type > paradigm I'm trying to use in my application. Specially, do you know > if it's possible for processes to post wx events that can be handled > by the main GUI thread? > > > Thanks for the help. > > Cheers, > Aaron Actually, I was thinking of the subprocess module (introduced in 2.4). But the multiprocessing module would be useful if you were porting threading code to a process model. The real point is the distinction between threads and separate processes. If the processes are separate, then the only time synchronization is needed is when they communicate with each other, or with other shared services. So on a multicore system they each run at full tilt till they communicate. There are tons of ways to communicate between processes, though you can't do the simple variable sharing that threads can (sometimes) get away with. I would normally point you to queues, but there a number of possibilities. And since the one process is running a GUI event loop, you might want to piggyback on the OS capability to post events between processes. The code might end up OS-dependent, but I'd bet the overhead will be minimal. What is your target operating system? Your numbers in the original message make me nervous; sending an event between processes (or even threads) every 0.5 millisecond is impossible. But I think you might have been confusing bytes and packets. There could very well be multiprocess support in wxPython. I'd check there first, before re-inventing the wheel. Presumably you know of the wxPython news group, hosted on Google groups? DaveA From philip at semanchuk.com Fri Oct 2 15:50:16 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Fri, 2 Oct 2009 15:50:16 -0400 Subject: Opinions, please, on PEP 8 and local, 3rd party imports Message-ID: <3D0E0B2C-2615-4AE2-8B5A-81B317BBF7E7@semanchuk.com> Hi all, Our project uses some libraries that were written by 3rd parties (i.e. not us). These libraries fit into a single Python file and live in our source tree alongside other modules we've written. When our app is distributed, they'll be included in the installation. In other words, they're not prerequisites that we'll make the user install, and they won't live in site-packages. For example, we use configobj from , and I think Beautiful Soup can be installed as a single file too, although we're not using that particular module. PEP 8 says the following: Imports should be grouped in the following order: 1. standard library imports 2. related third party imports 3. local application/library specific imports I'm not sure in which category to place local, 3rd-party modules like configobj. Clearly, they belong in category 3 since they're local. Clearly, they also belong in category 2 since they're 3rd party modules, and explicitly labeling an imported module as "this is code we didn't write" is useful. But I've always reserved category 2 for external libraries (e.g. numpy, wxPython, Twisted, PIL, etc.). Clearly, the best choice is category 2.5? In your Pythonic opinion, should 3rd-party modules that live alongside homegrown code be listed in import category 2 or 3? Thanks Philip From stefan-usenet at bytereef.org Fri Oct 2 15:53:27 2009 From: stefan-usenet at bytereef.org (Stefan Krah) Date: Fri, 2 Oct 2009 21:53:27 +0200 Subject: Fast decimal arithmetic module released Message-ID: <20091002195327.GA7925@mail.bytereef.org> Hi, today I have released the following packages for fast arbitrary precision decimal arithmetic: 1. libmpdec ============ Libmpdec is a C (C++ ready) library for arbitrary precision decimal arithmetic. It is a complete implementation of Mike Cowlishaw's General Decimal Arithmetic specification. 2. fastdec.so ============== Fastdec.so is a Python C module with the same functionality as decimal.py. With some restrictions, code written for for decimal.py should work identically. 3. deccheck.py =============== deccheck.py performs redundant calculations using both decimal.py and fastdec.so. For each calculation the results of both modules are compared and an exception is raised if they differ. This module was mainly developed for testing, but could in principle be used for redundant calculations. Correctness ============ Libmpdec passes IBM's official test suite and a multitude of additional tests. Fastdec.so passes (with minor modifications) all Python unit tests. When run directly, deccheck.py performs very exhaustive tests that compare fastdec.so with decimal.py. All tests complete successfully under Valgrind. Speed ====== In a couple of initial benchmarks, libmpdec compares very well against decNumber and the Intel decimal library. For very large numbers, the speed is roughly the same as the speed of the apfloat library. Fastdec.so compares quite well against gmpy and even native Python floats. In the benchmarks, it is significantly faster than Java's BigDecimal class. Portability ============ All tests have been completed on Linux 64/32-bit, Windows 64/32-bit, OpenSolaris 32-bit, OpenBSD 32-bit and Debian Mips 32-bit. For 32-bit platforms there is a pure ANSI C version, 64 bit platforms require a couple of asm lines. Further information and downloads at: http://www.bytereef.org/libmpdec.html Stefan Krah From duncan.booth at invalid.invalid Fri Oct 2 16:05:04 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 2 Oct 2009 20:05:04 GMT Subject: Opinions, please, on PEP 8 and local, 3rd party imports References: Message-ID: Philip Semanchuk wrote: > In your Pythonic opinion, should 3rd-party modules that live alongside > homegrown code be listed in import category 2 or 3? > PEP 8 also starts by saying "This document gives coding conventions for the Python code comprising the standard library in the main Python distribution." So while you are free to try to apply it to other code there's no obligation on you to take all or even any of it as gospel. You suggest option 2.5 sounds best. Sounds good to me. If it's good for you then go for it. From sturlamolden at yahoo.no Fri Oct 2 16:18:08 2009 From: sturlamolden at yahoo.no (sturlamolden) Date: Fri, 2 Oct 2009 13:18:08 -0700 (PDT) Subject: Threaded GUI slowing method execution? References: <4AC5E421.8040802@ieee.org> <97B7C9E0-AC7D-4DDE-91C5-FDDD25ADAB8D@EECS.Berkeley.EDU> Message-ID: <28b9a2f1-880e-4cc9-8623-dbecf720f6e0@j19g2000yqk.googlegroups.com> On 2 Okt, 21:30, Dave Angel wrote: > There could very well be multiprocess support in wxPython. ?I'd check > there first, before re-inventing the wheel. I don't think there is. But one can easily make a thread in the subprocess that polls a pipe and calls wx.PostEvent or wx.CallLater when something is received. I particularly like the Queue object in multiprocessing for this. One could also use win32api.SendMessage from pywin32 to send the event from one process to another. The parent must know the hwnd of the subprocess main wx.Frame. The subprocess gets that from calling the GetHandle() method of its wx.Frame, and must post it back to the parent, presumably via a pipe. From barry at python.org Fri Oct 2 16:21:43 2009 From: barry at python.org (Barry Warsaw) Date: Fri, 2 Oct 2009 16:21:43 -0400 Subject: RELEASED Python 2.6.3 Message-ID: <5B2596C4-1128-4B5B-8471-FE0409369D2D@python.org> On behalf of the Python community, I'm happy to announce the availability of Python 2.6.3. This is the latest production-ready version in the Python 2.6 series. Somewhere near 100 bugs have been fixed since Python 2.6.2 was released in April 2009. Please see the NEWS file for all the gory details. http://www.python.org/download/releases/2.6.3/NEWS.txt Source tarballs, Windows installers and the Mac OS X disk image can be downloaded from the Python 2.6.3 page. http://www.python.org/download/releases/2.6.3/ For more information on Python 2.6 in general, please see http://docs.python.org/whatsnew/2.6.html Please report bugs for any Python version in the Python tracker. http://bugs.python.org Enjoy, -Barry Barry Warsaw barry at python.org Python 2.6 Release Manager (on behalf of the entire python-dev team) -------------- next part -------------- A non-text attachment was scrubbed... Name: PGP.sig Type: application/pgp-signature Size: 849 bytes Desc: This is a digitally signed message part URL: From ahoover at EECS.Berkeley.EDU Fri Oct 2 16:29:01 2009 From: ahoover at EECS.Berkeley.EDU (Aaron Hoover) Date: Fri, 2 Oct 2009 13:29:01 -0700 Subject: Threaded GUI slowing method execution? In-Reply-To: <4AC654E2.4010503@ieee.org> References: <4AC5E421.8040802@ieee.org> <97B7C9E0-AC7D-4DDE-91C5-FDDD25ADAB8D@EECS.Berkeley.EDU> <4AC654E2.4010503@ieee.org> Message-ID: On Oct 2, 2009, at 12:30 PM, Dave Angel wrote: > (you responded off-list, which isn't the way these mailing lists > work. So I'm pasting your message back to the list, with my > response at the end) > Sorry about that - a slip of the "reply" button. >> > Actually, I was thinking of the subprocess module (introduced in > 2.4). But the multiprocessing module would be useful if you were > porting threading code to a process model. Previously, I had been using threads, so I just tried moving to multiprocessing because it required few changes. > There are tons of ways to communicate between processes, though you > can't do the simple variable sharing that threads can (sometimes) > get away with. I would normally point you to queues, but there a > number of possibilities. And since the one process is running a GUI > event loop, you might want to piggyback on the OS capability to post > events between processes. The code might end up OS-dependent, but > I'd bet the overhead will be minimal. What is your target operating > system? My target OS is Mac OS X Leopard. I've decided just to try and trigger WX events in the GUI based on data put in one of the queues by the processing thread. > > Your numbers in the original message make me nervous; sending an > event between processes (or even threads) every 0.5 millisecond is > impossible. But I think you might have been confusing bytes and > packets. My external hardware is actually sending 2000 packets per second right now (but that can also be changed). Each packet currently contains 6 bytes of data and 6 bytes of overhead. So, 12 bytes per packet * 2000 packets per second is 24k bytes per second. However, the serial processing process should be finding the packet payload data and adding to a queue for the GUI to deal with and plot (obviously at a much lower framerate) (at an ideal rate of 2KHz). > > There could very well be multiprocess support in wxPython. I'd > check there first, before re-inventing the wheel. Presumably you > know of the wxPython news group, hosted on Google groups? I actually tried using multiprocess to create a separate process for reading the serial data. The process gets passed a TX queue and an RX queue in its constructor, then it's supposed to enter an infinite loop where it waits for commands on the TX queue and reads data and puts in in the RX queue. However, I've discovered for some reason that the infinite loop terminates after the first call I make to either of the queues. Perhaps this behavior should be expected, but this is my first go-round with processes, so I was a little surprised by this result. Maybe someone with more multiprocessing experience can give me some pointers. Maybe I am barking up the wrong tree with Python from a speed perspective, but it's just some much faster and more fun to write than anything else... Aaron From jgardner at jonathangardner.net Fri Oct 2 16:37:45 2009 From: jgardner at jonathangardner.net (Jonathan Gardner) Date: Fri, 2 Oct 2009 13:37:45 -0700 (PDT) Subject: store encrypted data in sqlite ? References: Message-ID: <058b0d7b-5fc5-4b38-8696-7dd35c32433c@f18g2000prf.googlegroups.com> On Oct 2, 11:53?am, Stef Mientki wrote: > > Will this method work always ? > Are there better methods ? > I SQLite doesn't like raw data (with all its \0 glory), you're out of luck, unfortunately. Base64 encoding is a really good solution for places like this. You are aware, of course, of the dangers of storing sensitive data in memory? That is, if you are storing the sensitive data anywhere in a Python variable, it is possible for someone with access to the memory of the machine to discover it. If it is only the storage of the sensitive data you are concerned about, or the sensitivity of the data if it is transferred over the network, there are other, easier ways to protect the data than in an encrypted field. From jgardner at jonathangardner.net Fri Oct 2 16:40:13 2009 From: jgardner at jonathangardner.net (Jonathan Gardner) Date: Fri, 2 Oct 2009 13:40:13 -0700 (PDT) Subject: epydoc - can I provide multiple dirs to parse References: Message-ID: On Oct 2, 11:38?am, Medi wrote: > Can I present multiple directories to epydoc to process. For example > > epydoc -option -option -option dir_1 dir_2 dir_3 > I know nothing of epydoc. However, it looks like it should be something like: epydoc -option dir_1 -option dir_2 -option dir_3 ...if anything would work at all. You may want to contact the epydoc mailing list for better guidance, though. From carsten.haese at gmail.com Fri Oct 2 16:41:01 2009 From: carsten.haese at gmail.com (Carsten Haese) Date: Fri, 02 Oct 2009 16:41:01 -0400 Subject: store encrypted data in sqlite ? In-Reply-To: <4AC64C0E.6040406@gmail.com> References: <4AC64C0E.6040406@gmail.com> Message-ID: Stef Mientki wrote: > hello, > > I want to store some fields in an sqlite database. > > I use ezPyCrypto to encrypt and decrypt: > > User = ['z684684', 'Mientki, Stef', 1,1,0,1,1 ] > > encryption_key_1 = ezPyCrypto.key ( 512 ) > > SQL_Base = 'insert or replace into __USERS__ values (' > for field in User : > SQL += ",'" + encryption_key_1.encString ( str ( item ))+ "'" > SQL += ')' > > > Now this fails, probably, because the second character of the encrypted > string is a binary zero. > > By trial and error, I found a work around, > but I'm not sure this will garantee that it will work always: > by converting the encrypted buffer with base64.encode: > > SQL += ",'" + base64.encodestring(EnCrypt_1 ( str ( item )))+ "'" > > Will this method work always ? > Are there better methods ? There is definitely a better method! You should use parameter binding instead of rolling the query by hand: SQL = "insert or replace into __USERS__ values (?,?,?,?,?,?,?)" params = [ encryption_key_1.encString(str(x)) for x in User ] cur.execute(SQL, params) That way, the parameters are passed separately and safely, and the query syntax is protected from all the dangerous characters that are floating around in the parameters. HTH, -- Carsten Haese http://informixdb.sourceforge.net From george.trojan at noaa.gov Fri Oct 2 16:41:15 2009 From: george.trojan at noaa.gov (George Trojan) Date: Fri, 02 Oct 2009 20:41:15 +0000 Subject: numpy f2py question Message-ID: I have a problem with numpy's vectorize class and f2py wrapped old FORTRAN code. I found that the function _get_nargs() in site-packages/numpy/lib/function_base.py tries to find the number of arguments for a function from an error message generated when the function is invoked with no arguments. However, with numpy 1.3.0 the error message is different that the code expects. Is there any known solution? I don';t know where the message is coming from, a cursory check of numpy files did not yield any hits. A ctypes issue? I did find an unanswered seven weeks old related posting http://article.gmane.org/gmane.comp.python.numeric.general/32168/match=f2py+number+arguments though I don't think this is gfortran related. Mine is 4.1.2. George From robert.kern at gmail.com Fri Oct 2 16:47:04 2009 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 02 Oct 2009 15:47:04 -0500 Subject: int rich comparisons In-Reply-To: <3553f463-e58c-4371-9a80-6bfc38a8d31d@b15g2000yqd.googlegroups.com> References: <3553f463-e58c-4371-9a80-6bfc38a8d31d@b15g2000yqd.googlegroups.com> Message-ID: George Sakkis wrote: > I stumbled upon the following strangeness (python 2.6.2): > >>>> getattr(int, '__gt__') > > >>>> getattr(5, '__gt__') > Traceback (most recent call last):n > File "", line 1, in > AttributeError: 'int' object has no attribute '__gt__' > > Is this a bug ? I believe that ints in the 2.x series still use __cmp__. In Python 3.x, __gt__ and the rest are defined. -- 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 http Fri Oct 2 16:49:34 2009 From: http (Paul Rubin) Date: 02 Oct 2009 13:49:34 -0700 Subject: Q: sort's key and cmp parameters References: <7xvdiyhfdi.fsf@ruckus.brouhaha.com> Message-ID: <7xtyyhikrl.fsf@ruckus.brouhaha.com> Scott David Daniels writes: > Most cases are moreeasily done with key, and it is > a good idea to make the most accessible way to a sort be the most > efficient one. In the rare case that you really want each comparison, > the cmp-injection function will do nicely (and can be written as a > recipe. I don't think wrapping the sorted objects in an otherwise useless special purpose class is "nicely", either from a performance or from a code verbosity point of view. I avoid Java and its useless extra classes for a reason ;-). > In short, make the easy path the fast path, and more will use it; > provide two ways, and the first that springs to mind is the one > used. I think we are saying the same thing. Python 2.x provides two ways and you can use whichever one fits the application better. I have never understood why Python 3.x finds it necessary to break one of them. Maybe I can migrate to Haskell by the time Python 2.x becomes deprecated. From carlo.dicelico at gmail.com Fri Oct 2 16:56:17 2009 From: carlo.dicelico at gmail.com (Carlo DiCelico) Date: Fri, 2 Oct 2009 13:56:17 -0700 (PDT) Subject: Simple Python struct issue References: <3e8a944b-f677-4fa2-b682-8936c9c1c5bf@j39g2000yqh.googlegroups.com> <37b040a9-5e12-4675-a8fa-2852456f8f4f@a21g2000yqc.googlegroups.com> Message-ID: On Oct 2, 3:17?pm, Simon Forman wrote: > On Fri, Oct 2, 2009 at 12:35 PM, Carlo DiCelico > > > > > > wrote: > > On Oct 2, 12:15?pm, Simon Forman wrote: > >> On Fri, Oct 2, 2009 at 12:07 PM, Carlo DiCelico > >> wrote: > >> > I saw an article on O'Reilly about using NumPy and Dislin to analyze > >> > and visualize WAV files. It's a really fantastic article but was a > >> > little out of date. I updated the script to work with the newer > >> > modules &etc but am still having trouble getting it working. > > >> > The line > > >> > temp[i,:] = array(struct.unpack("%dB"%(fft_length), tempb),Float) - > >> > 128.0 > > >> > always returns the same error: "Traceback (most recent call last): > >> > ?File "pysono.py", line 31, in > >> > ? temp[i,:] = array(struct.unpack("%dB"%(fft_length),tempb),float) - > >> > 128.0 > >> > struct.error: unpack requires a string argument of length 256" when I > >> > do python pysono.py test.wav 256 > > >> > I'm sure it's probably something simple but I just can't see what it > >> > is! > > >> > Here's the original code:http://onlamp.com/python/2001/01/31/graphics/pysono.py > > >> > Thanks! > > >> In: > > >> struct.unpack("%dB" % (fft_length), tempb) > > >> tempb is not length 256. > > >> Also, note that (foo) is the same as just foo. ?To create a tuple of > >> length 1 you must say (foo,) > > >> HTH, > >> ~Simon > > > I'm sorry, I'm not sure what you're referring to when you say "Also, > > note that (foo) is the same as just foo. ?To create a tuple of length > > 1 you must say (foo,)". > > Sorry about that. ?All I mean is that in python putting parentheses > around an expression "(some_object)" does not create a tuple unless > you also use a comma "," > > I hope the following examples show what I mean: > > In [1]: (2) > Out[1]: 2 > > In [2]: (2,) > Out[2]: (2,) > > In [3]: 2, > Out[3]: (2,) > > In [4]: n = 2, > > In [5]: "%s" % (n,) > Out[5]: '(2,)' > > In [6]: "%s" % (n) > Out[6]: '2' > > In [7]: "%s" % n > Out[7]: '2' > > Your code: > > "%dB" % (fft_length) > > is the same as > > "%dB" % fft_length > > But what you probably meant was > > "%dB" % (fft_length,) > > Note the comma. ?People do this to prevent string formatting errors if > fft_length should ever happen to be a tuple rather than an int (or > whatever.) > > > The 256 is passed into the script as an > > argument and then assigned to the variable fft_length, which you can > > see in that line. So, whatever value I pass in comes out in the error. > > What you're saying is that the error happens because tempb isn't the > > length of any of those values (i.e., 128, 256, 512, etc)? > > Yes, whatever tempb is, it isn't "a string argument of length 256" as > the traceback says. > > > (1) How > > could I determine the length of tempb? > > If it's a string, len(tempb) will return it's length. > > > and (2) It's doing this -> > > tempb = fp.readframes(fft_length); right before doing the struct.unpack > > (); could the splitting of the frames into frames of length fft_length > > be causing this error? > > See MRAB's reply. :] > > > Thanks for the help! > > You're very welcome. > > One other thing, in the code you posted it has "Float", but in the > traceback it has "float". ?Generally speaking when you post code to a > newsgroup for help you should paste in the exact code, rather than > retyping it manually. > > Warm regards, and happy hacking! Ah, yes, makes so much more sense now. Thanks very much for the help! The discrepancy between Float and float has to do with the fact that in the code I linked to, he's using an old version of numpy. In my code, I'm using the new version of numpy, which uses float instead of Float. Sorry about that, thanks again. From carlo.dicelico at gmail.com Fri Oct 2 17:01:15 2009 From: carlo.dicelico at gmail.com (Carlo DiCelico) Date: Fri, 2 Oct 2009 14:01:15 -0700 (PDT) Subject: Simple Python struct issue References: <3e8a944b-f677-4fa2-b682-8936c9c1c5bf@j39g2000yqh.googlegroups.com> Message-ID: On Oct 2, 12:49?pm, MRAB wrote: > Carlo DiCelico wrote: > > I saw an article on O'Reilly about using NumPy and Dislin to analyze > > and visualize WAV files. It's a really fantastic article but was a > > little out of date. I updated the script to work with the newer > > modules &etc but am still having trouble getting it working. > > > The line > > > temp[i,:] = array(struct.unpack("%dB"%(fft_length), tempb),Float) - > > 128.0 > > > always returns the same error: "Traceback (most recent call last): > > ? File "pysono.py", line 31, in > > ? ?temp[i,:] = array(struct.unpack("%dB"%(fft_length),tempb),float) - > > 128.0 > > struct.error: unpack requires a string argument of length 256" when I > > do python pysono.py test.wav 256 > > > I'm sure it's probably something simple but I just can't see what it > > is! > > > Here's the original code:http://onlamp.com/python/2001/01/31/graphics/pysono.py > > .readframes(n) returns (at most) n _frames_ as a bytestring (str), not n > bytes. I tried reading 256 frames from a .wav file containing stereo at > 16 bits per channel and got 1024 bytes (4 bytes per frame, not > surprisingly!). Yes! Thank you very much for pointing this out, don't know how I missed it! From victorsubervi at gmail.com Fri Oct 2 17:25:55 2009 From: victorsubervi at gmail.com (Victor Subervi) Date: Fri, 2 Oct 2009 16:25:55 -0500 Subject: Multiple Select Message-ID: <4dc0cfea0910021425s1bf200a6w384dfdafb3568455@mail.gmail.com> Hi; I'm trying to create an HTML . What do? TIA Victor -------------- next part -------------- An HTML attachment was scrubbed... URL: From rami.chowdhury at gmail.com Fri Oct 2 17:46:06 2009 From: rami.chowdhury at gmail.com (Rami Chowdhury) Date: Fri, 02 Oct 2009 14:46:06 -0700 Subject: Multiple Select In-Reply-To: <4dc0cfea0910021425s1bf200a6w384dfdafb3568455@mail.gmail.com> References: <4dc0cfea0910021425s1bf200a6w384dfdafb3568455@mail.gmail.com> Message-ID: On Fri, 02 Oct 2009 14:25:55 -0700, Victor Subervi wrote: > Hi; > I'm trying to create an HTML . What do? > TIA > Victor How are you retrieving the values in the CGI script? -- Rami Chowdhury "Never attribute to malice that which can be attributed to stupidity" -- Hanlon's Razor 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD) From victorsubervi at gmail.com Fri Oct 2 17:58:24 2009 From: victorsubervi at gmail.com (Victor Subervi) Date: Fri, 2 Oct 2009 16:58:24 -0500 Subject: Multiple Select In-Reply-To: References: <4dc0cfea0910021425s1bf200a6w384dfdafb3568455@mail.gmail.com> Message-ID: <4dc0cfea0910021458v4122e71bk69d607a3bd559216@mail.gmail.com> I have the select called variable "these". Whichever option is selected is passed through the cgi: import cgitb; cgitb.enable() import cgi form = cgi.FieldStorage() these = form.getfirst('these', '') As I mentioned before, I get the variable all right. That is not the issue. What is the issue is that I would like to enable the user to select *several* options; in essence, to turn "these" into a tuple. That way, I could capture *several* options and enter them all into my database at once. TIA, V On Fri, Oct 2, 2009 at 4:46 PM, Rami Chowdhury wrote: > On Fri, 02 Oct 2009 14:25:55 -0700, Victor Subervi < > victorsubervi at gmail.com> wrote: > > Hi; >> I'm trying to create an HTML . What do? >> TIA >> Victor >> > > How are you retrieving the values in the CGI script? > > -- > Rami Chowdhury > "Never attribute to malice that which can be attributed to stupidity" -- > Hanlon's Razor > 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD) > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ethan at stoneleaf.us Fri Oct 2 18:22:57 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 02 Oct 2009 15:22:57 -0700 Subject: Multiple Select In-Reply-To: <4dc0cfea0910021458v4122e71bk69d607a3bd559216@mail.gmail.com> References: <4dc0cfea0910021425s1bf200a6w384dfdafb3568455@mail.gmail.com> <4dc0cfea0910021458v4122e71bk69d607a3bd559216@mail.gmail.com> Message-ID: <4AC67D41.2020007@stoneleaf.us> Victor Subervi wrote: > I have the select called variable "these". Whichever option is selected > is passed through the cgi: > > import cgitb; cgitb.enable() > import cgi > > form = cgi.FieldStorage() > these = form.getfirst('these', '') > > As I mentioned before, I get the variable all right. That is not the > issue. What is the issue is that I would like to enable the user to > select *several* options; in essence, to turn "these" into a tuple. That > way, I could capture *several* options and enter them all into my > database at once. > TIA, > V > Please don't top post. From the docs: getfirst( name[, default]) This method always *returns* *only* *one* value associated with form field name. The method returns only the first value in case that more values were posted under such name. Please note that the order in which the values are received may vary from browser to browser and should not be counted on.18.1 If no such form field or value exists then the method returns the value specified by the optional parameter default. This parameter defaults to None if not specified. getlist( name) This method always *returns* *a* *list* of values associated with form field name. The method returns an empty list if no such form field or value exists for name. It returns a list consisting of one item if only one such value exists. Hope this helps. ~Ethan~ From sturlamolden at yahoo.no Fri Oct 2 18:33:08 2009 From: sturlamolden at yahoo.no (sturlamolden) Date: Fri, 2 Oct 2009 15:33:08 -0700 (PDT) Subject: Threaded GUI slowing method execution? References: <4AC5E421.8040802@ieee.org> <97B7C9E0-AC7D-4DDE-91C5-FDDD25ADAB8D@EECS.Berkeley.EDU> <4AC654E2.4010503@ieee.org> Message-ID: <6dc12dd5-4b40-4889-b4f4-b92b3a0cd78d@w36g2000yqm.googlegroups.com> On 2 Okt, 22:29, Aaron Hoover wrote: > My external hardware is actually sending 2000 packets per second right ? > now (but that can also be changed). Each packet currently contains 6 ? > bytes of data and 6 bytes of overhead. So, 12 bytes per packet * 2000 ? > packets per second is 24k bytes per second. > Maybe I am barking up the wrong tree with Python from a speed ? > perspective, but it's just some much faster and more fun to write than ? > anything else... I have processed data in "real time" with Python from a system that delivered data from 64 channels at 48 kHz, 2 bytes per sample, video tracker, I/O ports, etc. Each package was 432 bytes and arrived at 16 kHz, that is 6.59 MiB per second. Python is much faster than you think. You just have to do some buffering. :-) If your hardware does not provide sufficient buffering, I'd write a small C function that takes care of this. This would be a function that collects data as it comes in, writes to a queue, and waits for more. Run this C function from a background Python thread using ctypes.CDLL. From matt.ernst at loomia.com Fri Oct 2 18:41:02 2009 From: matt.ernst at loomia.com (Matt Ernst) Date: Fri, 2 Oct 2009 15:41:02 -0700 (PDT) Subject: Python resident memory retention & Evan Jones' improvements Message-ID: My apologies in advance if this has been addressed before. Google does not presently seem to return search results for this group from more than a couple of months ago. I have some long-running Python processes that slowly increase in resident memory size, and whose resident size goes down only when they are restarted. I spent hours with gc and heapy but was unable to identify obvious culprits. I eventually tracked the problem down to buffering data to a queue for later processing. Putting to the queue increases resident size, but getting from it never decreases resident size. In fact I see the same behavior when I use plain lists instead of queue objects. I thought Evan Jones altered Python to deal with this very problem, and the change went into the release of 2.5. Here is Tim Peters announcing the change: http://mail.python.org/pipermail/python-dev/2006-March/061991.html He included this simple test program to show the improvement: """ x = [] for i in xrange(1000000): x.append([]) raw_input("full ") del x[:] raw_input("empty ") """ If you look at resident size in the "full" stage, the interpreter has grown to tens of megabytes. If you look at it in the "empty" stage, it goes back down to less than 10 megabytes. But if you run this trivial variation on the same program, memory use goes up and stays up: """ x = [] for i in xrange(1000000): x.append([]) raw_input("full ") del x[:] for i in xrange(1000000): x.append([]) del x[:] raw_input("empty ") """ At the "empty" prompt resident memory size has not decreased. I see this pattern of behavior in CPython 3.1.1, 2.6.3, 2.5.2, and Jython 2.5.1. I have tested under 32 and 64 bit Intel Linux. At this point I suspect that I am not going to be able to force my long-running processes to shrink their resident size, since I can't force it in much simpler tests. I am curious about why it happens though. That the second program should retain a larger resident memory footprint than the first is (to me) quite surprising. From lolmcbride at googlemail.com Fri Oct 2 19:06:39 2009 From: lolmcbride at googlemail.com (xera121) Date: Fri, 2 Oct 2009 16:06:39 -0700 (PDT) Subject: multiprocessing newbie - semaphore question Message-ID: <12836462-1b42-4edf-82f4-4fa57b87cfa1@g31g2000yqc.googlegroups.com> Hi, I'm interested in this type of programming but need some fundamental questions answered if someone could oblige: In the docs for the mutiprocessing module it mentions right at the beginning about an underlying semaphore system - would MPI2 fit the bill running on a Beowulf cluster? Is the mutiprocessing module usable for writing apps to run on a Beowulf cluster - i.e. can I use it for an app that will share the work amongst the nodes? I am looking for a way to share the processing on a task which requires intensive and repetitive work done over a large dataset in a sqlite table and I wondered if this would be a way to share the load over a number of pcs I have available for this task. My other idea is a simple(ish) client/server model which I am pretty confident I can execute whereas the setup of the cluster and the writing of a multiprocessing app would require a lot of prep and study/ testing. Therefore the bottom line is is it worth it? I know I haven't been too specific of what I'm doing but I just wanted a quick feel for the task from those who have more knowledge of the subject - Oh and btw I am NOT an expert programmer by any means and would expect almost all my code to be deemed as amateurish - but it keeps me happy and only breaks every other iteration ;-) xera121 From sturlamolden at yahoo.no Fri Oct 2 19:14:41 2009 From: sturlamolden at yahoo.no (sturlamolden) Date: Fri, 2 Oct 2009 16:14:41 -0700 (PDT) Subject: numpy f2py question References: Message-ID: <553f8b29-70e6-4526-a272-cf2bfe6b19be@t32g2000yqj.googlegroups.com> On 2 Okt, 22:41, George Trojan wrote: > I have a problem with numpy's vectorize class and f2py wrapped old > FORTRAN code. I found that the function _get_nargs() in > site-packages/numpy/lib/function_base.py tries to find the number of > arguments for a function from an error message generated when the > function is invoked with no arguments. However, with numpy 1.3.0 the > error message is different that the code expects. Is there any known > solution? I don';t know where the message is coming from, a cursory > check of numpy files did not yield any hits. A ctypes issue? > I did find an ?unanswered seven weeks old related postinghttp://article.gmane.org/gmane.comp.python.numeric.general/32168/matc... > though I don't think this is gfortran related. Mine is 4.1.2. The wrappers generated by f2py just calls PyArg_ParseTupleAndKeywords and return NULL if it fails. The error message from PyArg_ParseTupleAndKeywords does not fit the regex used by _get_nargs: re.compile(r'.*? takes exactly (?P\d+) argument(s|) \((? P\d+) given\)') It should have said re.compile(r'.*? takes (exactly|at least) (?P\d+) argument(s|) \((?P\d+) given\)') It is not just an f2py issue. It affects any function that uses keyword arguments. I'll file this as a bug on NumPy trac. S.M. From python at rcn.com Fri Oct 2 19:19:18 2009 From: python at rcn.com (Raymond Hettinger) Date: Fri, 2 Oct 2009 16:19:18 -0700 (PDT) Subject: Fast decimal arithmetic module released References: Message-ID: [Stefan Krah] > today I have released the following packages for fast arbitrary precision > decimal arithmetic: Nice. Raymond From sturlamolden at yahoo.no Fri Oct 2 19:31:23 2009 From: sturlamolden at yahoo.no (sturlamolden) Date: Fri, 2 Oct 2009 16:31:23 -0700 (PDT) Subject: numpy f2py question References: Message-ID: <37cea382-22fc-4dc9-909d-486cd7d2f3e4@c37g2000yqi.googlegroups.com> http://projects.scipy.org/numpy/ticket/1247 From xahlee at gmail.com Fri Oct 2 19:33:22 2009 From: xahlee at gmail.com (Xah Lee) Date: Fri, 2 Oct 2009 16:33:22 -0700 (PDT) Subject: Haskell's new logo, and the idiocy of tech geekers Message-ID: Haskell has a new logo. A fantastic one. Beautiful. For creator, context, detail, see bottom of: ? A Lambda Logo Tour http://xahlee.org/UnixResource_dir/lambda_logo.html this is posted here because it relates to various computer software/ language's logo, a subject discussed by me several times in the past years in comp.lang.lisp, comp.lang.scheme, comp.lang.python, as well discussed now and then in these communities often without my involvement. for my criticism or comment on logos, typical response by these people are showcases of complete ignorance of social function of logos, and a complete iliteracy in visual design (that any art student learns in first year), usually coupled with a teen-age militant attitude. (usually, these tech geeker's mentality is like: ?you are a fucking idiot... we don't need a logo. Logos are for kids and blood-sucking marketers.?) Some of these tech geekers, are motherfucking aggresive, so that they would ban me, or call me as a troll, or call upon whatever rules to oust me (such as ?off topicality?) , for any criticism on them. (if requested, i'll take some time to cite the exact url for numerous of these threads that happened in the past 5 years. My frustration in discussion logos happened in python, lojban, lisp communities. Note: not all people in these communities are idiotic about logos. Typically, it's just few of priest fuckheads (e.g. a newsgroup regular, or somewhat significant contributor of the particular community). This particularly happened in several threads in the lojban community where i criticized its logo during mid 2000s. (have a look at lojban's motherfucking idiotic logo here: http://en.wikipedia.org/wiki/Lojban )) ? The Purpose of Logos and Principles of Logo Design http://xahlee.org/UnixResource_dir/logo_design.html ? Lisp Needs A Logo http://xahlee.org/UnixResource_dir/writ/logo_lisp.html ---------------- computer language and software logo gallery: ? The Unix Pestilence http://xahlee.org/UnixResource_dir/freebooks.html ? A Lambda Logo Tour http://xahlee.org/UnixResource_dir/lambda_logo.html ? LISP Logo http://xahlee.org/emacs/lisp_logo.html ? Qi Language Logo http://xahlee.org/emacs/qi_logo.html ? Haskell Logo http://xahlee.org/haskell/haskell-logo.html ? The Emacs Logo http://xahlee.org/emacs/emacs_logo.html ? Xah's Java Logo http://xahlee.org/java-a-day/java_logo.html Xah ? http://xahlee.org/ ? From andymac at bullseye.apana.org.au Fri Oct 2 19:47:56 2009 From: andymac at bullseye.apana.org.au (Andrew MacIntyre) Date: Sat, 03 Oct 2009 09:47:56 +1000 Subject: Python resident memory retention & Evan Jones' improvements In-Reply-To: References: Message-ID: <4AC6912C.6030509@bullseye.andymac.org> Matt Ernst wrote: {...} > I thought Evan Jones altered Python to deal with this very problem, > and the change went into the release of 2.5. > > Here is Tim Peters announcing the change: > http://mail.python.org/pipermail/python-dev/2006-March/061991.html > > He included this simple test program to show the improvement: > """ > x = [] > for i in xrange(1000000): > x.append([]) > raw_input("full ") > del x[:] > raw_input("empty ") > """ > > If you look at resident size in the "full" stage, the interpreter has > grown to tens of megabytes. If you look at it in the "empty" stage, it > goes back down to less than 10 megabytes. But if you run this trivial > variation on the same program, memory use goes up and stays up: > > """ > x = [] > for i in xrange(1000000): > x.append([]) > raw_input("full ") > del x[:] > for i in xrange(1000000): > x.append([]) > del x[:] > raw_input("empty ") > """ > > At the "empty" prompt resident memory size has not decreased. I see > this pattern of behavior in CPython 3.1.1, 2.6.3, 2.5.2, and Jython > 2.5.1. I have tested under 32 and 64 bit Intel Linux. > > At this point I suspect that I am not going to be able to force my > long-running processes to shrink their resident size, since I can't > force it in much simpler tests. I am curious about why it happens > though. That the second program should retain a larger resident memory > footprint than the first is (to me) quite surprising. There are two things you need to be aware of in this situation: - not all Python's memory is allocated through Python's specialised malloc() - int and float objects in particular (in 2.x at least) are allocated directly via a privately managed free list, and any allocation requiring more than 256 bytes is directed to the platform malloc(). Any memory not allocated via Python's malloc() is not subject to the memory release facility referred to above. Python 2.6 does improve the management of memory consumed by int and float objects via the garbage collector. - while Python attempts to maximally utilise memory arenas to improve the chances of being able to free them, Python's malloc() does not do any compaction of memory (ie moving allocations between arenas) within existing arenas. Nor does garbage collection do this. So fragmented allocations can cause the retention of nearly empty arenas. I suspect that what you see with the second test script above is caused by some sort of fragmentation. My recollection of Evan's objective with his work was to deal with the case where long running processes created lots of objects on startup, but having initialised no longer need most of the created objects. Without his modification, the memory would have been retained unused for the remaining life of the process. It also helps with cyclic bursts of object creation/deletion. But there are circumstances where it doesn't kick in. To get a deeper understanding of your issue will require deeper debugging. I have done this at times by building Python with a wrapper around malloc() (& friends) to log memory allocation activity. -- ------------------------------------------------------------------------- Andrew I MacIntyre "These thoughts are mine alone..." E-mail: andymac at bullseye.apana.org.au (pref) | Snail: PO Box 370 andymac at pcug.org.au (alt) | Belconnen ACT 2616 Web: http://www.andymac.org/ | Australia From kentilton at gmail.com Fri Oct 2 19:51:36 2009 From: kentilton at gmail.com (Kenneth Tilton) Date: Fri, 02 Oct 2009 19:51:36 -0400 Subject: Haskell's new logo, and the idiocy of tech geekers In-Reply-To: References: Message-ID: <4ac691ea$0$5003$607ed4bc@cv.net> Xah Lee wrote: > Haskell has a new logo. A fantastic one. Beautiful. For creator, > context, detail, see bottom of: > > ? A Lambda Logo Tour > http://xahlee.org/UnixResource_dir/lambda_logo.html Cool survey, and yes, that is a nice new one for Haskell. I saw beauty the other day changing an application to talk to RDF for what it used to get from CLOS and seeing it Just Work after a couple of hours of poking around. Connecting those two paragraphs left as an exercise. kt From pjb at informatimago.com Fri Oct 2 19:58:22 2009 From: pjb at informatimago.com (Pascal J. Bourguignon) Date: Sat, 03 Oct 2009 01:58:22 +0200 Subject: Haskell's new logo, and the idiocy of tech geekers References: <4ac691ea$0$5003$607ed4bc@cv.net> Message-ID: <87ske1wdpd.fsf@galatea.local> Kenneth Tilton writes: > Xah Lee wrote: >> Haskell has a new logo. A fantastic one. Beautiful. For creator, >> context, detail, see bottom of: >> ? A Lambda Logo Tour >> http://xahlee.org/UnixResource_dir/lambda_logo.html Don't do that! If you want to watch the logo, just google for haskell logo. There's no need to go thru xahlee.org. -- __Pascal Bourguignon__ From sajmikins at gmail.com Fri Oct 2 20:22:38 2009 From: sajmikins at gmail.com (Simon Forman) Date: Fri, 2 Oct 2009 20:22:38 -0400 Subject: Opinions, please, on PEP 8 and local, 3rd party imports In-Reply-To: <3D0E0B2C-2615-4AE2-8B5A-81B317BBF7E7@semanchuk.com> References: <3D0E0B2C-2615-4AE2-8B5A-81B317BBF7E7@semanchuk.com> Message-ID: <50f98a4c0910021722w47f432aya893aaa352497dbf@mail.gmail.com> On Fri, Oct 2, 2009 at 3:50 PM, Philip Semanchuk wrote: > Hi all, > Our project uses some libraries that were written by 3rd parties (i.e. not > us). These libraries fit into a single Python file and live in our source > tree alongside other modules we've written. When our app is distributed, > they'll be included in the installation. In other words, they're not > prerequisites that we'll make the user install, and they won't live in > site-packages. > > For example, we use configobj from > , and I think Beautiful > Soup can be installed as a single file too, although we're not using that > particular module. > > PEP 8 says the following: > > ? Imports should be grouped in the following order: > ? 1. standard library imports > ? 2. related third party imports > ? 3. local application/library specific imports > > > I'm not sure in which category to place local, 3rd-party modules like > configobj. > > Clearly, they belong in category 3 since they're local. > > Clearly, they also belong in category 2 since they're 3rd party modules, and > explicitly labeling an imported module as "this is code we didn't write" is > useful. But I've always reserved category 2 ?for external libraries (e.g. > numpy, wxPython, Twisted, PIL, etc.). > > Clearly, the best choice is category 2.5? > > In your Pythonic opinion, should 3rd-party modules that live alongside > homegrown code be listed in import category 2 or 3? > > Thanks > Philip > -- > http://mail.python.org/mailman/listinfo/python-list > 2.5 +1 ;] From python at rcn.com Fri Oct 2 20:38:14 2009 From: python at rcn.com (Raymond Hettinger) Date: Fri, 2 Oct 2009 17:38:14 -0700 (PDT) Subject: Q: sort's key and cmp parameters References: <7x1vln2bzh.fsf@ruckus.brouhaha.com> Message-ID: <85f89926-6a1c-40ec-8f29-3acb2f4bd00f@2g2000prl.googlegroups.com> [Paul Rubin] > Yes, think of sorting tree structures where you have to recursively > compare them til you find an unequal pair of nodes. ? I'm not sure what you mean by this. What are the semantics of sorting a tree? Can you outline an example of tree that could be sorted easily with a cmp function but not a key function? t = [[9,6],[7,1]],[[2,5],[4,3]] # inputs t.sort(mycmp) # what would the cmp function be? print t # what would the output be Raymond From kentilton at gmail.com Fri Oct 2 20:51:32 2009 From: kentilton at gmail.com (Kenneth Tilton) Date: Fri, 02 Oct 2009 20:51:32 -0400 Subject: Haskell's new logo, and the idiocy of tech geekers In-Reply-To: <87ske1wdpd.fsf@galatea.local> References: <4ac691ea$0$5003$607ed4bc@cv.net> <87ske1wdpd.fsf@galatea.local> Message-ID: <4ac69ff6$0$5000$607ed4bc@cv.net> Pascal J. Bourguignon wrote: > Kenneth Tilton writes: > >> Xah Lee wrote: >>> Haskell has a new logo. A fantastic one. Beautiful. For creator, >>> context, detail, see bottom of: >>> ? A Lambda Logo Tour >>> http://xahlee.org/UnixResource_dir/lambda_logo.html > > Don't do that! > If you want to watch the logo, just google for haskell logo. > There's no need to go thru xahlee.org. Hunh? He has done a nice job of collecting different logos and putting them all in one place where one can see them all just by scrolling. ie, it's a cool web page with added value available nowhere else. kt -- http://thelaughingstockatpngs.com/ http://www.facebook.com/pages/The-Laughingstock/115923141782?ref=nf From workitharder at gmail.com Fri Oct 2 21:14:44 2009 From: workitharder at gmail.com (bukzor) Date: Fri, 2 Oct 2009 18:14:44 -0700 (PDT) Subject: organizing your scripts, with plenty of re-use Message-ID: <17f2bf83-c2bf-451b-b629-6ce64aefa2b4@x37g2000yqj.googlegroups.com> I would assume that putting scripts into a folder with the aim of re- using pieces of them would be called a package, but since this is an "anti-pattern" according to Guido, apparently I'm wrong-headed here. (Reference: http://mail.python.org/pipermail/python-3000/2007-April/006793.html ) Say you have ~50 scripts or so with lots of re-use (importing from each other a lot) and you want to organize them into folders. How do you do this simply? The intent is to have people be able to check out the directly from CVS and have the scripts "just work", even if they're not directly on the PYTHONPATH. This seems to be the best discussion on the topic, but the conclusion seemed to be that there's no good way. That seems unthinkable considering python's dedication to simplicity and elegance. http://groups.google.com/group/comp.lang.python/browse_thread/thread/c44c769a72ca69fa/ It looks like I'm basically restating this post, which sadly got dropped without further comment: http://mail.python.org/pipermail/python-3000/2007-April/006814.html From sigzero at gmail.com Fri Oct 2 21:32:19 2009 From: sigzero at gmail.com (Robert H) Date: Fri, 2 Oct 2009 18:32:19 -0700 (PDT) Subject: Haskell's new logo, and the idiocy of tech geekers References: Message-ID: http://blog.plover.com/prog/haskell/logo.html Oops... From tjreedy at udel.edu Fri Oct 2 21:51:02 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 02 Oct 2009 21:51:02 -0400 Subject: int rich comparisons In-Reply-To: References: <3553f463-e58c-4371-9a80-6bfc38a8d31d@b15g2000yqd.googlegroups.com> Message-ID: Robert Kern wrote: > George Sakkis wrote: >> I stumbled upon the following strangeness (python 2.6.2): >> >>>>> getattr(int, '__gt__') >> >> >>>>> getattr(5, '__gt__') >> Traceback (most recent call last):n >> File "", line 1, in >> AttributeError: 'int' object has no attribute '__gt__' >> >> Is this a bug ? > > I believe that ints in the 2.x series still use __cmp__. In Python 3.x, > __gt__ and the rest are defined. Correct: Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> 5 .__gt__ # notice space after 5 From http Sat Oct 3 00:11:22 2009 From: http (Paul Rubin) Date: 02 Oct 2009 21:11:22 -0700 Subject: Q: sort's key and cmp parameters References: <7x1vln2bzh.fsf@ruckus.brouhaha.com> <85f89926-6a1c-40ec-8f29-3acb2f4bd00f@2g2000prl.googlegroups.com> Message-ID: <7x4oqh5d79.fsf@ruckus.brouhaha.com> Raymond Hettinger writes: > I'm not sure what you mean by this. What are the semantics of > sorting a tree? Can you outline an example of tree that > could be sorted easily with a cmp function but not a key function? The idea was that you have a list of trees that you want to sort, and an ordering relation between trees: def gt(tree1, tree2): ... where you recursively descend both trees until you find an unequal pair of nodes. You're not trying to sort the nodes within a single tree. From greg at cosc.canterbury.ac.nz Sat Oct 3 00:14:47 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Sat, 03 Oct 2009 16:14:47 +1200 Subject: Regular expression to structure HTML In-Reply-To: References: <98ffa1be-c4da-4c38-9b68-275c14c1948b@e12g2000yqi.googlegroups.com> Message-ID: <7io1dcF30vo2aU1@mid.individual.net> Brian D wrote: > This isn't merely a question of knowing when to use the right > tool. It's a question about how to become a better developer using > regular expressions. It could be said that if you want to learn how to use a hammer, it's better to practise on nails rather than screws. -- Greg From aahz at pythoncraft.com Sat Oct 3 00:23:39 2009 From: aahz at pythoncraft.com (Aahz) Date: 2 Oct 2009 21:23:39 -0700 Subject: removing a post References: Message-ID: In article , Sjoerd Mullender wrote: >On 2009-09-26 05:32, Mike L wrote: >> >> could you remove this old post, off topic and spam >> >> http://www.mail-archive.com/python-list at python.org/msg175722.html > >You need to put this request to postmaster at python.org. As mailing list >administrator I have no access to the archives. They won't do it, either. We can't afford to get into the sinkhole of editing the archives. (The awkward they/we construction comes from the fact that I'm one of the python.org admins but not actually one of the postmaster team.) -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "....Normal is what cuts off your sixth finger and your tail..." --Siobhan From searles at hoombah.nurt.bt.uk Sat Oct 3 00:41:22 2009 From: searles at hoombah.nurt.bt.uk (Dave Searles) Date: Sat, 03 Oct 2009 00:41:22 -0400 Subject: Haskell's new logo, and the idiocy of tech geekers In-Reply-To: References: Message-ID: Xah Lee wrote: > Haskell has a new logo. [Blam! It fucking blows because lambda means > pure functional] A truly pure functional language would be useless for anything but programming space heaters. Lambda seems not inappropriate on any language that strongly supports a functional style. Schemes and Clojure certainly do, and Haskell certainly does. Common Lisp is a bit less strongly functionally-oriented, but it still supports it better than, say, C or Java, by having first-class functions and closures. In fact the general pattern seems to be that lambda crops up in languages that have first-class functions, which not coincidentally tend to also have an anonymous function operator, often called lambda. From skorpio11 at gmail.com Sat Oct 3 02:14:22 2009 From: skorpio11 at gmail.com (skorpio11 at gmail.com) Date: Fri, 2 Oct 2009 23:14:22 -0700 (PDT) Subject: Calendar yr-mnth-day data to day since data Message-ID: <2d8702ac-b14a-4ce0-a100-c6d4de2e5def@s6g2000vbp.googlegroups.com> Hi all, I have some calendar data in three arrays corresponding to yr, month, day that I would like to convert to day since data and be consistent with changes in leap year. I've included a sample of the data structures below. Any suggestions??? Thanks in advance yr mnth day daySince 1970 1 1 1 1970 1 15 15 1970 1 28 28 1970 2 1 32 1970 2 27 59 1970 3 1 1970 3 4 1970 3 29 ... ... ... 2008 1 1 2008 1 8 2008 1 25 2008 2 1 From python at rcn.com Sat Oct 3 02:16:25 2009 From: python at rcn.com (Raymond Hettinger) Date: Fri, 2 Oct 2009 23:16:25 -0700 (PDT) Subject: Q: sort's key and cmp parameters References: <7x1vln2bzh.fsf@ruckus.brouhaha.com> <85f89926-6a1c-40ec-8f29-3acb2f4bd00f@2g2000prl.googlegroups.com> <7x4oqh5d79.fsf@ruckus.brouhaha.com> Message-ID: <2c85695c-ef24-4592-9b64-2dc71a3278eb@d9g2000prh.googlegroups.com> [Paul Rubin] > The idea was that you have a list of trees that you want to sort, and > an ordering relation between trees: > > ? ?def gt(tree1, tree2): ... > > where you recursively descend both trees until you find an unequal > pair of nodes. ?You're not trying to sort the nodes within a single > tree. Can you give an example of a list of trees and a cmp function that recursively compares them? Raymond From python at rcn.com Sat Oct 3 02:25:59 2009 From: python at rcn.com (Raymond Hettinger) Date: Fri, 2 Oct 2009 23:25:59 -0700 (PDT) Subject: Q: sort's key and cmp parameters References: <7x1vln2bzh.fsf@ruckus.brouhaha.com> <85f89926-6a1c-40ec-8f29-3acb2f4bd00f@2g2000prl.googlegroups.com> <7x4oqh5d79.fsf@ruckus.brouhaha.com> Message-ID: <9494594b-1001-4f8b-97b6-9390a902172a@f18g2000prf.googlegroups.com> [Paul Rubin] > The idea was that you have a list of trees that you want to sort, and > an ordering relation between trees: > > ? ?def gt(tree1, tree2): ... Are the trees user defined classes? Can the gt() function be added incorporated as __lt__ method so that you can just run a plain sort: sort(list_of_trees) Is the recursive search order something you can turn into a straight sequence: sort(list_of_trees, key=flattener) IOW, if there is an ordering relation between the trees, why can't it be either part of the tree API or collapsable into a list of successive nodes to be compared. >From the sound of it, the trees are static during the sort and would get a nice O(n log n) --> O(n) speed-up if a key function were allowed to flatten them in a single pass. Raymond From ben+python at benfinney.id.au Sat Oct 3 02:35:58 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 03 Oct 2009 16:35:58 +1000 Subject: Opinions, please, on PEP 8 and local, 3rd party imports References: Message-ID: <87my49x9v5.fsf@benfinney.id.au> Philip Semanchuk writes: > Our project uses some libraries that were written by 3rd parties (i.e. > not us). These libraries fit into a single Python file and live in our > source tree alongside other modules we've written. Why in the same source tree? They are maintained separately, so you shouldn't be duplicating them in your source. > When our app is distributed, they'll be included in the installation. That can be done simply by having the third-party's standard distribution included with yours, and use it *if it's not already installed* on the user's system. > In other words, they're not prerequisites that we'll make the user > install, and they won't live in site-packages. What if they already *do* exist in site-packages? Why not install them there yourself? > PEP 8 says the following: > > Imports should be grouped in the following order: > 1. standard library imports > 2. related third party imports > 3. local application/library specific imports > > > I'm not sure in which category to place local, 3rd-party modules like > configobj. They are third-party imports; category 2. > Clearly, they belong in category 3 since they're local. They shouldn't be local, since they're not part of your application's code base; they're maintained as separate distributions (even if you bundle them together when distributing your work). Moreover, if there is an update to configobj by its current maintainers, and I have installed three (or seven, or seventy-three) applications using it, I want *one* update into site-packages to fix it for *all* of those applications. Not have to fuss about with all the duplicated copies, and worry about which ones have been missed (or, worse, *not* worry and the be confused over why the bug is still occurring in some applications). > Clearly, they also belong in category 2 since they're 3rd party > modules, and explicitly labeling an imported module as "this is code > we didn't write" is useful. But I've always reserved category 2 for > external libraries (e.g. numpy, wxPython, Twisted, PIL, etc.). Third-party libraries *are* external libraries. Please, for the sake of your users, keep them so. You can *install* them at the same time, but please don't *duplicate* them, forking them as though they're part of your code base. -- \ ?We are not gonna be great; we are not gonna be amazing; we are | `\ gonna be *amazingly* amazing!? ?Zaphod Beeblebrox, _The | _o__) Hitch-Hiker's Guide To The Galaxy_, Douglas Adams | Ben Finney From marduk at letterboxes.org Sat Oct 3 02:43:51 2009 From: marduk at letterboxes.org (Albert Hopkins) Date: Sat, 03 Oct 2009 02:43:51 -0400 Subject: Opinions, please, on PEP 8 and local, 3rd party imports In-Reply-To: <50f98a4c0910021722w47f432aya893aaa352497dbf@mail.gmail.com> References: <3D0E0B2C-2615-4AE2-8B5A-81B317BBF7E7@semanchuk.com> <50f98a4c0910021722w47f432aya893aaa352497dbf@mail.gmail.com> Message-ID: <1254552231.15302.0.camel@centar> On Fri, 2009-10-02 at 20:22 -0400, Simon Forman wrote: > 2.5 +1 I'd like to suggest 2.46 instead. From chris at simplistix.co.uk Sat Oct 3 02:53:51 2009 From: chris at simplistix.co.uk (Chris Withers) Date: Sat, 03 Oct 2009 07:53:51 +0100 Subject: Haskell's new logo, and the idiocy of tech geekers In-Reply-To: References: Message-ID: <4AC6F4FF.2000908@simplistix.co.uk> Xah Lee wrote: > Haskell has a new logo. A fantastic one. Beautiful. For creator, > context, detail, see bottom of: What does this have to do with Python? Nothing. So why are you posting it to comp.lang.python? Chris -- Simplistix - Content Management, Batch Processing & Python Consulting - http://www.simplistix.co.uk From bieffe62 at gmail.com Sat Oct 3 03:02:16 2009 From: bieffe62 at gmail.com (Francesco Bochicchio) Date: Sat, 3 Oct 2009 00:02:16 -0700 (PDT) Subject: Opinions, please, on PEP 8 and local, 3rd party imports References: Message-ID: <19573229-687d-4297-8f3f-56bed9673e61@33g2000vbe.googlegroups.com> On Oct 2, 9:50?pm, Philip Semanchuk wrote: > Hi all, > > PEP 8 says the following: > > ? ? Imports should be grouped in the following order: > ? ? 1. standard library imports > ? ? 2. related third party imports > ? ? 3. local application/library specific imports > > I'm not sure in which category to place local, 3rd-party modules like ? > configobj. > > ... > Clearly, the best choice is category 2.5? > Actually 2.5 is doable :-) I translate it as "just after any of 2 and before any of 3" Ciao ---- FB From jackdied at gmail.com Sat Oct 3 03:14:04 2009 From: jackdied at gmail.com (Jack Diederich) Date: Sat, 3 Oct 2009 03:14:04 -0400 Subject: Haskell's new logo, and the idiocy of tech geekers In-Reply-To: <4AC6F4FF.2000908@simplistix.co.uk> References: <4AC6F4FF.2000908@simplistix.co.uk> Message-ID: It's Xah Lee, he's been trolling this and every other programing language group for over 10 years (preferably all at once). Let it go. On Sat, Oct 3, 2009 at 2:53 AM, Chris Withers wrote: > Xah Lee wrote: >> >> Haskell has a new logo. A fantastic one. Beautiful. For creator, >> context, detail, see bottom of: > > What does this have to do with Python? Nothing. > So why are you posting it to comp.lang.python? > > Chris > > -- > Simplistix - Content Management, Batch Processing & Python Consulting > ? ? ? ? ? - http://www.simplistix.co.uk > -- > http://mail.python.org/mailman/listinfo/python-list > From http Sat Oct 3 03:22:26 2009 From: http (Paul Rubin) Date: 03 Oct 2009 00:22:26 -0700 Subject: Q: sort's key and cmp parameters References: <7x1vln2bzh.fsf@ruckus.brouhaha.com> <85f89926-6a1c-40ec-8f29-3acb2f4bd00f@2g2000prl.googlegroups.com> <7x4oqh5d79.fsf@ruckus.brouhaha.com> <9494594b-1001-4f8b-97b6-9390a902172a@f18g2000prf.googlegroups.com> Message-ID: <7xhbuht00d.fsf@ruckus.brouhaha.com> Raymond Hettinger writes: > Can you give an example of a list of trees and a cmp function > that recursively compares them? Example of list of trees (nested dicts). In practice you could get such a list from the simplejson module: list_of_trees = [{'value':1, 'left':{'value':3,'left':None,'right':None}, 'right':{'value':7,'left':{'value':5, ...}}}, {'value':19, 'left':{'value':23', ...}}, ... ] Example comparison function: def compare(tree1, tree2): c = cmp(tree1['value'], tree2['value']) if c != 0: return c c = cmp(tree1['left'], tree2['left']) if c != 0: return c return cmp(tree1['right'], tree2['right]) > Are the trees user defined classes? Not in general. They might be nested tuples, lists, or dictionaries. Or they could come from a non-extensible library-defined class, like from cElementTree. > From the sound of it, the trees are static during the sort and > would get a nice O(n log n) --> O(n) speed-up if a key function > were allowed to flatten them in a single pass. But the key function has to do all those comparisons on the internal nodes. From http Sat Oct 3 03:28:36 2009 From: http (Paul Rubin) Date: 03 Oct 2009 00:28:36 -0700 Subject: Q: sort's key and cmp parameters References: <7x1vln2bzh.fsf@ruckus.brouhaha.com> <85f89926-6a1c-40ec-8f29-3acb2f4bd00f@2g2000prl.googlegroups.com> <7x4oqh5d79.fsf@ruckus.brouhaha.com> <9494594b-1001-4f8b-97b6-9390a902172a@f18g2000prf.googlegroups.com> <7xhbuht00d.fsf@ruckus.brouhaha.com> Message-ID: <7x8wft7x7f.fsf@ruckus.brouhaha.com> Paul Rubin writes: > Example comparison function: > > def compare(tree1, tree2): > c = cmp(tree1['value'], tree2['value']) > if c != 0: return c > c = cmp(tree1['left'], tree2['left']) > if c != 0: return c > return cmp(tree1['right'], tree2['right]) Sorry, meant recursive comparison. def compare(tree1, tree2): c = cmp(tree1['value'], tree2['value']) if c != 0: return c c = compare(tree1['left'], tree2['left']) if c != 0: return c return compare(tree1['right'], tree2['right]) From tamim.shahriar at gmail.com Sat Oct 3 03:45:41 2009 From: tamim.shahriar at gmail.com (subeen) Date: Sat, 3 Oct 2009 00:45:41 -0700 (PDT) Subject: Trouble sending / receiving compressed data (using zlib) as HTTP POST to server (in django) Message-ID: Hi, I am trying to send compressed data to a server written in django. But it shows error while decompressing the data in the server. After some experiment I found that the server is not receiving the exact data I am sending. data = 'hello, this is a test message this is another message' data = zlib.compress(data) # now it looks like: x??H????Q(??,V?D???????????T?p^~IFjL ?e # length is 45 in django (view) I receive it: data = request.POST['data'] # now it looks like: x????Q(?,V?D???????^~IFjL ?e # length is 34 Can anybody help me understand the issue and how to get over? thanks, Subeen. http://love-python.blogspot.com From stef.mientki at gmail.com Sat Oct 3 04:24:13 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Sat, 03 Oct 2009 10:24:13 +0200 Subject: organizing your scripts, with plenty of re-use In-Reply-To: <17f2bf83-c2bf-451b-b629-6ce64aefa2b4@x37g2000yqj.googlegroups.com> References: <17f2bf83-c2bf-451b-b629-6ce64aefa2b4@x37g2000yqj.googlegroups.com> Message-ID: <4AC70A2D.9020109@gmail.com> bukzor wrote: > I would assume that putting scripts into a folder with the aim of re- > using pieces of them would be called a package, but since this is an > "anti-pattern" according to Guido, apparently I'm wrong-headed here. > (Reference: http://mail.python.org/pipermail/python-3000/2007-April/006793.html > ) > > Say you have ~50 scripts or so with lots of re-use (importing from > each other a lot) and you want to organize them into folders. How do > you do this simply? > > Interesting question, ... ... and although I've a working situation, I would like to see other answers. In my situation I've an estimate of about 2000 scripts (in fact every script I ever wrote), with about zero redundancy. I still don't use (because I don't fully understand them) packages, but by trial and error I found a reasonable good working solution, with the following specifications - (Almost) any script (what ever it uses from one of the other scripts can run standalone - libraries that need another main program ( e.g. a grid component needs a GUI) can launch another main program to test itself - All __init__ files are generated automatically Although not containing the last ideas, here's an idea of what I do: http://mientki.ruhosting.nl/data_www/pylab_works/pw_importing.html cheers, Stef > The intent is to have people be able to check out the directly from > CVS and have the scripts "just work", even if they're not directly on > the PYTHONPATH. > > This seems to be the best discussion on the topic, but the conclusion > seemed to be that there's no good way. That seems unthinkable > considering python's dedication to simplicity and elegance. > http://groups.google.com/group/comp.lang.python/browse_thread/thread/c44c769a72ca69fa/ > > > It looks like I'm basically restating this post, which sadly got > dropped without further comment: > http://mail.python.org/pipermail/python-3000/2007-April/006814.html > From steve at REMOVE-THIS-cybersource.com.au Sat Oct 3 04:28:59 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Oct 2009 08:28:59 GMT Subject: organizing your scripts, with plenty of re-use References: <17f2bf83-c2bf-451b-b629-6ce64aefa2b4@x37g2000yqj.googlegroups.com> Message-ID: <00508f27$0$26920$c3e8da3@news.astraweb.com> On Fri, 02 Oct 2009 18:14:44 -0700, bukzor wrote: > I would assume that putting scripts into a folder with the aim of re- > using pieces of them would be called a package, A package is a special arrangement of folder + modules. To be a package, there must be a file called __init__.py in the folder, e.g.: parrot/ +-- __init__.py +-- feeding/ +-- __init__.py +-- eating.py +-- drinking.py +-- fighting.py +-- flying.py +-- sleeping.py +-- talking.py This defines a package called "parrot" which includes a sub-package feeding and modules fighting, flying, sleeping and talking. You can use it by any variant of the following: import parrot # loads parrot/__init__.py import parrot.talking # loads parrot/talking.py from parrot import sleeping import parrot.feeding from parrot.feeding.eating import eat_cracker and similar. Common (but not compulsory) behaviour is for parrot/__init__.py to import all the modules within the package, so that the caller can do this: import parrot parrot.feeding.give_cracker() without requiring to manually import sub-packages. The os module behaves similarly: having imported os, you can immediately use functions in os.path without an additional import. Just dumping a bunch of modules in a folder doesn't make it a package, it just makes it a bunch of modules in a folder. Unless that folder is in the PYTHONPATH, you won't be able to import the modules because Python doesn't look inside folders. The one exception is that it will look inside a folder for a __init__.py file, and if it finds one, it will treat that folder and its contents as a package. > but since this is an > "anti-pattern" according to Guido, apparently I'm wrong-headed here. > (Reference: > http://mail.python.org/pipermail/python-3000/2007-April/006793.html ) Guido's exact words were: "The only use case seems to be running scripts that happen to be living inside a module's directory, which I've always seen as an antipattern." I'm not sure precisely what he means by that, because modules don't have directories, they are in directories. Perhaps he meant package. In that case, the anti-pattern according to Guido is not to put modules in a folder, but to have modules inside a package be executable scripts. To use the above example, if the user can make the following call from the shell: $ python ./parrot/talking.py "polly want a cracker" and have the module talking do something sensible, that's an anti- pattern. Modules inside a package aren't intended to be executable scripts called by the user. There should be one (or more) front-end scripts which are called by the user. Since they aren't intended to be imported, they can be anywhere, not just on the PYTHONPATH. But they import the modules in the package, and that package *is* in the PYTHONPATH. Using the above example, you would install the parrot folder and its contents somewhere on the PYTHONPATH, and then have a front-end script (say) "talk-to-parrot" somewhere else. Notice that the script doesn't even need to be a legal name for a module, since you're never importing it. > Say you have ~50 scripts or so with lots of re-use (importing from each > other a lot) and you want to organize them into folders. How do you do > this simply? Of course you can have a flat hierarchy: one big folder, like the standard library, with a mixed back of very loosely connected modules: eating.py drinking.py feeding.py fighting.py flying.py parrot.py sleeping.py talking.py You can do that, of course, but it's a bit messy -- what if somebody installs parrot.py and eating.py, but not drinking.py, and as a consequence parrot.py fails to work correctly? Or what if the user already has a completely unrelated module talking.py? Chaos. The std library can get away with dumping (nearly) everything in the one directory, because it's managed chaos. Users aren't supposed to pick and choose which bits of the standard library get installed, or install other modules in the same location. Three alternatives are: * put your modules in a sub-folder, and tell the user to change the Python path to include your sub-folder, and hope they know what you're talking about; * put your modules in a package, tell the user to just place the entire package directory where they normally install Python code, and importing will just work; or * have each and every script manually manipulate the PYTHONPATH so that when the user calls that script, it adds its parent folder to the PYTHONPATH before importing what it needs. Messy and ugly. -- Steven From steve at REMOVE-THIS-cybersource.com.au Sat Oct 3 05:08:59 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Oct 2009 09:08:59 GMT Subject: organizing your scripts, with plenty of re-use References: <17f2bf83-c2bf-451b-b629-6ce64aefa2b4@x37g2000yqj.googlegroups.com> Message-ID: <00509888$0$26920$c3e8da3@news.astraweb.com> On Sat, 03 Oct 2009 10:24:13 +0200, Stef Mientki wrote: > I still don't use (because I don't fully understand them) packages, but > by trial and error I found a reasonable good working solution, with the > following specifications I find that fascinating. You haven't used packages because you don't understand them, but you've used another technique that you *also* don't understand well enough to generate a solution, and had to rely on trial and error. Packages are quite well documented. Since the alternative was trial-and- error on something you also don't fully understand, why did you avoid packages? -- Steven From donn.ingle at gmail.com Sat Oct 3 05:37:44 2009 From: donn.ingle at gmail.com (Donn) Date: Sat, 3 Oct 2009 11:37:44 +0200 Subject: organizing your scripts, with plenty of re-use In-Reply-To: <00508f27$0$26920$c3e8da3@news.astraweb.com> References: <17f2bf83-c2bf-451b-b629-6ce64aefa2b4@x37g2000yqj.googlegroups.com> <00508f27$0$26920$c3e8da3@news.astraweb.com> Message-ID: <200910031137.44853.donn.ingle@gmail.com> Great description - wish the Python docs could be as clear. Thanks. \d From http Sat Oct 3 05:39:36 2009 From: http (Paul Rubin) Date: 03 Oct 2009 02:39:36 -0700 Subject: Q: sort's key and cmp parameters References: <7x1vln2bzh.fsf@ruckus.brouhaha.com> <85f89926-6a1c-40ec-8f29-3acb2f4bd00f@2g2000prl.googlegroups.com> <7x4oqh5d79.fsf@ruckus.brouhaha.com> <9494594b-1001-4f8b-97b6-9390a902172a@f18g2000prf.googlegroups.com> <7xhbuht00d.fsf@ruckus.brouhaha.com> <7x8wft7x7f.fsf@ruckus.brouhaha.com> Message-ID: <7xeipkerzb.fsf@ruckus.brouhaha.com> Paul Rubin writes: > c = compare(tree1['left'], tree2['left']) Of course this recursive call crashes if either branch is None. Oh well, I'll stop trying to correct it since I'm sure you get the idea. From junu.pv at gmail.com Sat Oct 3 05:40:54 2009 From: junu.pv at gmail.com (Junaid) Date: Sat, 3 Oct 2009 02:40:54 -0700 (PDT) Subject: unicode text file References: <0267bef9-9548-4c43-bcdf-b624350c8f15@p23g2000vbl.googlegroups.com> Message-ID: <7e902e42-9452-477f-a66e-410e0e7a4eac@x6g2000prc.googlegroups.com> On Sep 27, 6:39?pm, "Mark Tolonen" wrote: > "Junaid" wrote in message > > news:0267bef9-9548-4c43-bcdf-b624350c8f15 at p23g2000vbl.googlegroups.com... > > >I want to do replacements in a utf-8 text file. example > > > f=open("test.txt","r") #this file is uft-8 encoded > > raw = f.read() > > txt = raw.decode("utf-8") > > You can use the codecs module to open and decode the file in one step > > > > > txt.replace{'English', ur'????????') #replacing raw unicode string, > > but not working > > The replace method returns the altered string. ?It does not modify it in > place. ?You also should use Unicode strings for both the arguments (although > it doesn't matter in this case). ?Using a raw Unicode string is also > unnecessary in this case. > > ? ? txt = txt.replace(u'English', u'????????') > > > f.write(txt) > > You opened the file for writing. ?You'll need to close the file and reopen > it for writing. > > > f.close() > > f.flush() > > Flush isn't required. ?close() will flush. > > Also to have text like ???????? in a file you'll need to declare the > encoding of the file at the top and be sure to actually save the file in the > encoding. > > In summary: > > ? ? # coding: utf-8 > ? ? import codecs > ? ? f = codecs.open('test.txt','r','utf-8') > ? ? txt = f.read() > ? ? txt = txt.replace(u'English', u'????????') > ? ? f.close() > ? ? f = codecs.open('test.txt','w','utf-8') > ? ? f.write(txt) > ? ? f.close() > > -Mark thanx everyone for replying, I did as Mark suggested, and it worked :) thanx once more From piet at cs.uu.nl Sat Oct 3 06:35:55 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 03 Oct 2009 12:35:55 +0200 Subject: Calendar yr-mnth-day data to day since data References: <2d8702ac-b14a-4ce0-a100-c6d4de2e5def@s6g2000vbp.googlegroups.com> Message-ID: >>>>> "skorpio11 at gmail.com" (sc) wrote: >sc> Hi all, >sc> I have some calendar data in three arrays corresponding to yr, month, >sc> day that I would like to convert to day since data and be consistent >sc> with changes in leap year. I've included a sample of the data >sc> structures below. Any suggestions??? Thanks in advance >sc> yr mnth day daySince >sc> 1970 1 1 1 >sc> 1970 1 15 15 >sc> 1970 1 28 28 >sc> 1970 2 1 >sc> 32 >sc> 1970 2 27 59 >sc> 1970 3 1 >sc> 1970 3 4 >sc> 1970 3 29 >sc> ... ... ... >sc> 2008 1 1 >sc> 2008 1 8 >sc> 2008 1 25 >sc> 2008 2 1 Days since what? It appears here to be since 1969-12-31, or since 1970-1-1 but then starting with 1 instead of 0. And your 59 is wrong if the others are deemed to be correct. Depending on what you want you have to add 1 to the following solution import datetime startdate = datetime.date(1970, 1, 1) enddate = datetime.date(1970,3,1) timediff = enddate - startdate print timediff.days result: 59 -- Piet van Oostrum WWW: http://pietvanoostrum.com/ PGP key: [8DAE142BE17999C4] From digitig at gmail.com Sat Oct 3 06:59:49 2009 From: digitig at gmail.com (Tim Rowe) Date: Sat, 3 Oct 2009 11:59:49 +0100 Subject: Haskell's new logo, and the idiocy of tech geekers In-Reply-To: References: Message-ID: 2009/10/3 Xah Lee : > for my criticism or comment on logos, typical response by these people > are showcases of complete ignorance of social function of logos [snip and rearrange] > discussed now and then in these communities often without my > involvement. > ?you are a fucking idiot... > motherfucking aggresive > it's just few of priest fuckheads > look at lojban's motherfucking idiotic logo If you really knew anything about social function you would be able to work out why people think you are a troll. -- Tim Rowe From piet at cs.uu.nl Sat Oct 3 07:03:15 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 03 Oct 2009 13:03:15 +0200 Subject: Trouble sending / receiving compressed data (using zlib) as HTTP POST to server (in django) References: Message-ID: >>>>> subeen (s) wrote: >s> Hi, >s> I am trying to send compressed data to a server written in django. But >s> it shows error while decompressing the data in the server. After some >s> experiment I found that the server is not receiving the exact data I >s> am sending. >s> data = 'hello, this is a test message this is another message' >s> data = zlib.compress(data) >s> # now it looks like: x??H????Q(??,V?D????.???????T?p^~IFj.L.?.e >s> # length is 45 Note: you can't just paste binary data in the message and expect something sensible. Better use the result of 'print data'. >s> in django (view) I receive it: >s> data = request.POST['data'] >s> # now it looks like: x????Q(?,V?D???.????^~IFj.L.?.e >s> # length is 34 >s> Can anybody help me understand the issue and how to get over? How did you post the data? If you post binary data you should indicate this with a proper mime type, like application/octet-stream. Otherwise it might be interpreted as text which it isn't. -- Piet van Oostrum WWW: http://pietvanoostrum.com/ PGP key: [8DAE142BE17999C4] From stefan-usenet at bytereef.org Sat Oct 3 08:04:41 2009 From: stefan-usenet at bytereef.org (Stefan Krah) Date: Sat, 3 Oct 2009 14:04:41 +0200 Subject: Fast decimal arithmetic module released In-Reply-To: References: Message-ID: <20091003120441.GA3935@mail.bytereef.org> Mark Dickinson wrote: > On Oct 2, 8:53?pm, Stefan Krah wrote: > > Hi, > > > > today I have released the following packages for fast arbitrary precision > > decimal arithmetic: > > > [...] > > Nice! I'd been wondering how you'd been finding all those decimal.py > bugs. Now I know. :) Thanks! Yes, actually deccheck.py deserves the credit. ;) Stefan Krah From gb345 at invalid.com Sat Oct 3 09:21:00 2009 From: gb345 at invalid.com (gb345) Date: Sat, 3 Oct 2009 13:21:00 +0000 (UTC) Subject: Need feedback on subprocess-using function Message-ID: I'm relatively new to Python, and I'm trying to get the hang of using Python's subprocess module. As an exercise, I wrote the Tac class below, which can prints output to a file "in reverse order", by piping it through the Unix tac utility. (The idea is to delegate the problem of managing the memory for an arbitrarily large task to tac.) class Tac(object): def __init__(self, path): out = open(path, 'w') self.pipe = subprocess.Popen(['tac'], stdout=out, stdin=subprocess.PIPE, stderr=subprocess.PIPE) def prn(self, string): try: self.pipe.stdin.write('%s\n' % string) except: self.close() raise def close(self): p = self.pipe p.stdin.close() err = p.stderr.read() if err: raise OSError(err) This works OK, as far as I can tell, but I'm not sure that I've dotted all the i's and crossed all the t's... E.g., I had to add the line "p.stdin.close()" to the close method when I when I ran into sporadic deadlock at the p.stderr.read() statement. Are there other similar problems lurking in this code? Also, there's no robust mechanism for invoking this close method in case of an exception (unless the exception happens during the execution of prn). Any comments and suggestions would be greatly appreciated. G From nick_keighley_nospam at hotmail.com Sat Oct 3 09:46:31 2009 From: nick_keighley_nospam at hotmail.com (Nick Keighley) Date: Sat, 3 Oct 2009 06:46:31 -0700 (PDT) Subject: Haskell's new logo, and the idiocy of tech geekers References: Message-ID: <09680e18-8709-4c5c-b329-46297f5c9906@g23g2000vbr.googlegroups.com> On 3 Oct, 00:33, Xah Lee wrote: > Haskell has a new logo. A fantastic one. Beautiful. For creator, > context, detail, see bottom of: > > ? A Lambda Logo Tour > ?http://xahlee.org/UnixResource_dir/lambda_logo.html I'm amazed he thinks anyone would donate 3 USD to that site From n00m at narod.ru Sat Oct 3 09:54:21 2009 From: n00m at narod.ru (n00m) Date: Sat, 3 Oct 2009 06:54:21 -0700 (PDT) Subject: Enormous Input and Output Test Message-ID: Hi, py.folk! I need your help to understand how http://www.spoj.pl/problems/INOUTEST/ can be passed in Python. I see two guys who managed to get accepted: http://www.spoj.pl/ranks/INOUTEST/lang=PYTH My code for this is: =========================================== import psyco psyco.full() import sys def noo(b): b = b.split() return str(int(b[0]) * int(b[1])) + '\n' def foo(): ##sys.stdin = open('D:/1583.txt', 'rt') a = sys.stdin.readlines() a = a[1:int(a[0]) + 1] a = map(noo, a) sys.stdout.writelines(a) foo() =========================================== But it gets "Time Limit Exceeded" verdict. Any ideas? From pje at telecommunity.com Sat Oct 3 10:32:28 2009 From: pje at telecommunity.com (PJ Eby) Date: Sat, 3 Oct 2009 07:32:28 -0700 (PDT) Subject: setuptools, accessing ressource files References: Message-ID: <35abb572-0c6e-45af-8eb9-aed7fb16037e@p9g2000vbl.googlegroups.com> On Oct 2, 7:04?am, Patrick Sabin wrote: > I use setuptools to create a package. In this package I included some > images and I checked that they are in the egg-file. The problem is how > can I access the images in the package? > > I tried pkgutil.get_data, but only got an IOError, because the EGG-INFO > directory doesn't exist. > > I tried > pkg_resources.get_distribution('dist').get_metadata('images/image.png') > with a similar error (IOError) > > What is the best way to access images distributed in an egg file? The resource_stream(), resource_string(), or resource_filename() functions: http://peak.telecommunity.com/DevCenter/PkgResources#basic-resource-access > > -Patrick From 504crank at gmail.com Sat Oct 3 10:37:17 2009 From: 504crank at gmail.com (504crank at gmail.com) Date: Sat, 3 Oct 2009 07:37:17 -0700 (PDT) Subject: Regular expression to structure HTML References: <98ffa1be-c4da-4c38-9b68-275c14c1948b@e12g2000yqi.googlegroups.com> <7io1dcF30vo2aU1@mid.individual.net> Message-ID: <7aeb1ee3-3774-486f-9ca6-138f662290c5@p23g2000vbl.googlegroups.com> On Oct 2, 11:14?pm, greg wrote: > Brian D wrote: > > This isn't merely a question of knowing when to use the right > > tool. It's a question about how to become a better developer using > > regular expressions. > > It could be said that if you want to learn how to use a > hammer, it's better to practise on nails rather than > screws. > > -- > Greg It could be said that the bandwidth in technical forums should be reserved for on-topic exchanges, not flaming intelligent people who might have something to contribute to the forum. The truth is, I found a solution where others were ostensibly either too lazy to attempt, or too eager grandstanding their superiority to assist. Who knows -- maybe I'll provide an alternative to BeautifulSoup one day. From victorsubervi at gmail.com Sat Oct 3 11:05:25 2009 From: victorsubervi at gmail.com (Victor Subervi) Date: Sat, 3 Oct 2009 10:05:25 -0500 Subject: Customizing Option Elements Message-ID: <4dc0cfea0910030805gcf42c50s2f5499c0054320c9@mail.gmail.com> Hi; I want to create here to go back.' else: message = 'No file was uploaded' print """\ Content-Type: text/html\n

%s

From aahz at pythoncraft.com Fri Oct 16 15:37:00 2009 From: aahz at pythoncraft.com (Aahz) Date: 16 Oct 2009 12:37:00 -0700 Subject: An assessment of Tkinter and IDLE References: <55a0833d-3f1f-402a-8eb7-cb4b7861cce5@q5g2000yqh.googlegroups.com> <290a6f9f-1d9b-4d0a-b704-6aa9d0b2e8f7@11g2000yqp.googlegroups.com> <87ljjhq7y7.fsf@benfinney.id.au> Message-ID: In article <87ljjhq7y7.fsf at benfinney.id.au>, Ben Finney wrote: >TerryP writes: >> >> One thing you should also learn about me, is I do not deal in >> absolutes. > >What, *never*? Well, hardly ever. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "To me vi is Zen. To use vi is to practice zen. Every command is a koan. Profound to the user, unintelligible to the uninitiated. You discover truth everytime you use it." --reddy at lion.austin.ibm.com From python.list at tim.thechases.com Fri Oct 16 15:40:23 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 16 Oct 2009 14:40:23 -0500 Subject: restriction on sum: intentional bug? In-Reply-To: <7a9c25c20910161031q67342767pd744d455714f00aa@mail.gmail.com> References: <7a9c25c20910160900p35600587v164b1c55c64718f1@mail.gmail.com> <4AD8A67C.2000508@tim.thechases.com> <7a9c25c20910161031q67342767pd744d455714f00aa@mail.gmail.com> Message-ID: <4AD8CC27.60304@tim.thechases.com> Stephen Hansen wrote: > There really is just a right way verses a wrong way to join strings > together; using + is always the wrong way. Sometimes that level of 'wrong' > is so tiny that no one cares, like if you are using it to join together two > small strings. But when joining together a sequence of strings, the wrong > amplifies to become /clearly/ wrong. Then I'm fine with sum() being smart enough to recognize this horrid case and do the "right" thing by returning ''.join() instead. If sum() were limited to int/floats like some array/numpy functions explicitly claim, that would be an "oh, we only handle these specific things and nothing else". But sum() is defined over "things that have an __add__ method", and strings have an __add__ method, making this breakage purely for breakage's sake. >>> class W: ... def __init__(self, s): ... self.s = s ... def __add__(self, other): ... return W(self.s + other.s) ... def __repr__(self): return "" % self.s ... def __str__(self): return self.s ... >>> lst = [W('hello'), W('world'), W('foo')] >>> print sum(lst, W('')) helloworldfoo It's not an error (that it *can't* be done)...it's just plain ornery :) >> count = 0 >> for i in range(1000000): >> if i % 1000: count += 1 >> >> instead of specifying the step-size? Or even forcing me to precompute this >> constant value for `count` because looping is inefficient in this case? > > That comparison is apples to... rocket launchers. > > The case with sum has nothing at all to do with the the above example or it > maybe one day trying to "force" you into doing one thing or the other in the > name of Efficiency-- or start going down some data-hiding road. For sum() to error out because strings are a special-case of inefficiency, the above loop should error out too because it's much more efficient to just say count = 999000 To look at the "for" loop version and tell me that's dumb is exactly why I feel the sum() case is dumb. If I have performance problems because I'm sum()ing strings when I should be ''.join()ing them, it's my responsibility to read the docs on sum() and see that's a foolish thing for me to be doing. But don't tell me I *can't* do dumb things. > Yes, sum() is doing some "hand holding" here, but only in one specific case: > because its -always-wrong- to use it in that case. What's always wrong is giving me an *error* when the semantics are perfectly valid. I don't care if the implementation is def sum(iterable, default=0): if is_instance(default, base_string): return ''.join(iterable) else: result = default for item in iterable: result += item return result to do the "right" thing of performing __add__ on all the elements of the iterable unless it's a string. If you want to special-case strings to perform a ''.join() the go right ahead. > The "consenting adults" argument sort of applies, sure. But these general > principles aren't absolutes. None of them are. In this case, someone decided > that it was way too easy for someone to NOT know that this is wrong and make > a mistake. Among consenting adults, it's not "wrong". You'll just discover there are better ways when your sum() becomes a hot-spot for CPU cycles. Just a burr in my boots. -tkc From bearophileHUGS at lycos.com Fri Oct 16 16:02:31 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Fri, 16 Oct 2009 13:02:31 -0700 (PDT) Subject: The rap against "while True:" loops References: <01ccc46d-5ea9-4dfe-ba22-699c6b859f00@v36g2000yqv.googlegroups.com> <2f203a56-f41b-44bd-87fe-cc0169c66273@b2g2000yqi.googlegroups.com> <7jg84fF3536pjU1@mid.individual.net> <4cdf9c03-d8e1-406c-b960-1a5b535f9430@m38g2000yqd.googlegroups.com> <7xhbu1ptue.fsf@ruckus.brouhaha.com> Message-ID: <4d2cff68-968f-405a-a7cc-1cccfe838e7f@a31g2000yqn.googlegroups.com> Paul Rubin: > ?http://scholar.google.com/scholar?cluster=17368311454828547380 > > Keep in mind that the article is 35 years old though, and is purely > imperative. ?Lots of stuff done with cockamamie looping constructs is > more cleanly done with Python generators, itertools, higher-order > functions, etc. That's a famous and very good paper, a good read for people that like to program. The Example1 and Example2 can be rewritten in several different ways, but several compilers today are not able to perform such optimizations yet, so what Knuth has written there are still among the faster ways to implement that algorithm. Bye, bearophile From dmcclouds at gmail.com Fri Oct 16 16:15:42 2009 From: dmcclouds at gmail.com (D) Date: Fri, 16 Oct 2009 13:15:42 -0700 (PDT) Subject: Spawning Cmd Window via Subprocess Message-ID: Hello, I would like to be able to spawn a new CMD window (specifing size, color and placement of the window), and write to it separately. Specifically, I have a backup program that displays each file backed up in the main window, and I would like to spawn and continually update a second CMD window that will display the current status (i.e. number of files backed up, amount of data backed up). Also, I only want to display the update messages, don't want to display any command prompts. I'm thinking I should be able to do this using subprocess, but I haven't been able to find out how. Any help would be greatly appreciated! From alan.isaac at gmail.com Fri Oct 16 16:18:06 2009 From: alan.isaac at gmail.com (Alan G Isaac) Date: Fri, 16 Oct 2009 16:18:06 -0400 Subject: restriction on sum: intentional bug? In-Reply-To: References: <7a9c25c20910160900p35600587v164b1c55c64718f1@mail.gmail.com> <4AD8A67C.2000508@tim.thechases.com> <7a9c25c20910161031q67342767pd744d455714f00aa@mail.gmail.com> Message-ID: On 10/16/2009 3:40 PM, Tim Chase wrote: > What's always wrong is giving me an *error* when the semantics are > perfectly valid. Exactly. Which is why I expected this to be fixed in Python 3. Alan Isaac From max at alcyone.com Fri Oct 16 16:19:10 2009 From: max at alcyone.com (Erik Max Francis) Date: Fri, 16 Oct 2009 13:19:10 -0700 Subject: id( ) function question In-Reply-To: References: <46d12d880910140349p5165ffa6q83622c992b69f57c@mail.gmail.com> <1ZSdnQfj1p6IHkrXnZ2dnUVZ_i1i4p2d@giganews.com> Message-ID: Mel wrote: > True, I don't see that exact expression going wrong. The actual poster, > trimmed for that post, used to go: > > def broadcast (self, message): > for p in players: > if p is not self: > p.send (message) This use of `is` is fine. > For my fears to come true, the for/in interface might be changed to do some > important piece of bookkeeping that needed the yielded objects to be > wrapped. I don't know what that would be. So far, Python is only changing > such things when it's well worth it. > > You could imagine a really intrusive debugging tool trying such things, at > the cost of un-debugging programs that relied on `id` or `is`. That would involve such a significant change to the language that it would break very many scripts. No such change is in the planning, nor would it make much sense to do in the case of Python where the point of `for` is precisely to iterate over the elements themselves. So while it's theoretically possible some future backwards-incompatible Python might do this (though it's hard to see why), it would be a "Python 4000" that you would be warned about well in advance of and would have no obligation to switch to, just as with 3.0. -- Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/ San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis Nothing spoils a confession like repentence. -- Anatole France From deets at nospam.web.de Fri Oct 16 16:24:24 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 16 Oct 2009 22:24:24 +0200 Subject: Rename file if it exists. In-Reply-To: References: Message-ID: <7js33pF37i47eU1@mid.uni-berlin.de> Stephen Reese schrieb: > The script below uploads files to a web server. Currently it > overwrites a file if it already exists. I'm instead trying to rename > the old file with an appended date/timestamp before the new file is > uploaded. I *think* I have the idea down but it's not be implemented > in the script correctly. Any hints would be great, thanks. > > #!/usr/bin/env python > import cgi, os > import cgitb; cgitb.enable() > #import os.path > import hashlib > import datetime > > try: # Windows needs stdio set for binary mode. > import msvcrt > msvcrt.setmode (0, os.O_BINARY) # stdin = 0 > msvcrt.setmode (1, os.O_BINARY) # stdout = 1 > except ImportError: > pass > > form = cgi.FieldStorage() > > # Generator to buffer file chunks > def fbuffer(f, chunk_size=10000): > while True: > chunk = f.read(chunk_size) > if not chunk: break > yield chunk > > # A nested FieldStorage instance holds the file > fileitem = form['file'] > > # Test if the file was uploaded > if fileitem.filename: > > # Test to determine if file name already exists in destination and > rename if it does exist to include date. > if os.path.isfile(file): > os.rename(file,file + "date") > else: > This isn't working because the else: is dangling. And I think your logic is flawed (I might be wrong of course) because you rename the *existing* file instead of giving the new one a new data. Thus e.g. a link to the file (if it's a webserver) will suddenly deliver a different file. I doubt that's what you wanted. And you also can only upload one file per *day*. Which doesn't sound good to me. You should disambiguate further. And if it's ok to append a date, I suggest you do that always. So instead, the logic should be something like this: import datetime basename = ... # however you get to that basename = basename + "_" + datetime.datetime.now().strftime("%Y-%m-%d") count = 0 filename = basename while os.path.isfile(filename): filename = basename + "." + count count += 1 Diez From somebody at somewhere.com Fri Oct 16 16:32:40 2009 From: somebody at somewhere.com (Stephen Fairchild) Date: Fri, 16 Oct 2009 21:32:40 +0100 Subject: how to write a unicode string to a file ? References: Message-ID: Stef Mientki wrote: > hello, > > By writing the following unicode string (I hope it can be send on this > mailing list) > > B?cken > > to a file > > fh.write ( line ) > > I get the following error: > > UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in > position 9: ordinal not in range(128) > > How should I write such a string to a file ? Your code fails because the unicode string denoted by the name, line, cannot be converted to ASCII, which python silently tries to do. Instead, use a compatible character encoding. Note the explicit conversion. fh.write(line.encode('utf-8')) Alternatively, you can write sixteen bit unicode directly to a file: import codecs f = codecs.open('unicodetest.txt', mode='w', encoding='utf-16') f.write(u'Hello world\n') f.close() -- Stephen Fairchild From carsten.haese at gmail.com Fri Oct 16 16:42:37 2009 From: carsten.haese at gmail.com (Carsten Haese) Date: Fri, 16 Oct 2009 16:42:37 -0400 Subject: More & More Fun w/ Pics & MySQL In-Reply-To: <4dc0cfea0910161223q326f1306jb24b7d62ab124d30@mail.gmail.com> References: <4dc0cfea0910161223q326f1306jb24b7d62ab124d30@mail.gmail.com> Message-ID: Victor Subervi wrote: > [snip...] > > print 'Content-type: image/jpeg' > print 'Content-Encoding: base64' > print > print pic().encode('base64') > print '' > > [snip...] Why are you printing "" at the end of a page that is supposed to be a base64-encoded JPEG file? -- Carsten Haese http://informixdb.sourceforge.net From xnews2 at fredp.lautre.net Fri Oct 16 16:58:38 2009 From: xnews2 at fredp.lautre.net (Fred Pacquier) Date: 16 Oct 2009 20:58:38 GMT Subject: Tracking down DLL load errors in Windows ? References: Message-ID: Christian Heimes said : > Fred P wrote: >> Is there any tool and/or methodology I could use to at least pinpoint >> the exact DLL that libpyexiv2 is failing to load, and ideally also >> the reason why ?... > > The depencency walker http://www.dependencywalker.com/ works fine for > me. I'd never heard of that tool before, but it looks powerful indeed ! Thanks a lot for the pointer, fp From lists at cheimes.de Fri Oct 16 17:03:33 2009 From: lists at cheimes.de (Christian Heimes) Date: Fri, 16 Oct 2009 23:03:33 +0200 Subject: restriction on sum: intentional bug? In-Reply-To: References: <7a9c25c20910160900p35600587v164b1c55c64718f1@mail.gmail.com> <4AD8A67C.2000508@tim.thechases.com> <7a9c25c20910161031q67342767pd744d455714f00aa@mail.gmail.com> Message-ID: Alan G Isaac schrieb: > On 10/16/2009 3:40 PM, Tim Chase wrote: >> What's always wrong is giving me an *error* when the semantics are >> perfectly valid. > > > Exactly. > Which is why I expected this to be fixed in Python 3. It's not going to happen. Christian From gervaz at gmail.com Fri Oct 16 17:04:08 2009 From: gervaz at gmail.com (mattia) Date: 16 Oct 2009 21:04:08 GMT Subject: print() Message-ID: <4ad8dfc8$0$1112$4fafbaef@reader2.news.tin.it> Is there a way to print to an unbuffered output (like stdout)? I've seen that something like sys.stdout.write("hello") works but it also prints the number of characters! From rsreese at gmail.com Fri Oct 16 17:08:51 2009 From: rsreese at gmail.com (Stephen Reese) Date: Fri, 16 Oct 2009 17:08:51 -0400 Subject: Rename file if it exists. In-Reply-To: <7js33pF37i47eU1@mid.uni-berlin.de> References: <7js33pF37i47eU1@mid.uni-berlin.de> Message-ID: <665172f40910161408j52a9a207u3f8dbcc57a9e5685@mail.gmail.com> > This isn't working because the else: is dangling. And I think your logic is > flawed (I might be wrong of course) because you rename the *existing* file > instead of giving the new one a new data. > > Thus e.g. a link to the file (if it's a webserver) will suddenly deliver a > different file. I doubt that's what you wanted. And you also can only upload > one file per *day*. Which doesn't sound good to me. You should disambiguate > further. And if it's ok to append a date, I suggest you do that always. > > So instead, the logic should be something like this: > > import datetime > > basename = ... # however you get to that > > basename = basename + "_" + datetime.datetime.now().strftime("%Y-%m-%d") > > count = 0 > filename = basename > while os.path.isfile(filename): > ? ?filename = basename + "." + count > ? ?count += 1 > > > Diez Diez, Ahh that would be much easier to just date the file upon upload. My initial concern was that renaming the file would alter the hash value, obviously I didn't think this one through too well. I adjusted the code and tried to incorporate your recommendation but still no go. Anything else obviously wrong with the following snippet? Thanks # Test if the file was uploaded if fileitem.filename: # strip leading path from file name to avoid directory traversal attacks fn = os.path.basename(fileitem.filename) # Include date in filename. basename = fn basename = basename + "_" + datetime.datetime.now().strftime("%Y-%m-%d") count = 0 fn = basename while os.path.isfile(fn): fn = basename + "." + count count += 1 # Open file for writing f = open('/var/www/dropbox/' + fn, 'wb', 10000) h = hashlib.md5() datalength = 0 # Read the file in chunks for chunk in fbuffer(fileitem.file): f.write(chunk) h.update(chunk) datalength += len(chunk) hexdigest = h.hexdigest() f.close() message = 'The file "' + fn + '" was uploaded successfully with a MD5 hash value of ' + hexdigest + ', click here to go back.' From clp2 at rebertia.com Fri Oct 16 17:19:13 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 16 Oct 2009 14:19:13 -0700 Subject: print() In-Reply-To: <4ad8dfc8$0$1112$4fafbaef@reader2.news.tin.it> References: <4ad8dfc8$0$1112$4fafbaef@reader2.news.tin.it> Message-ID: <50697b2c0910161419p6262088ew10ea3c3b1b4cb05b@mail.gmail.com> On Fri, Oct 16, 2009 at 2:04 PM, mattia wrote: > Is there a way to print to an unbuffered output (like stdout)? Just follow your prints or stdout.write()s with calls to stdout.flush() to flush the buffer. You could alternatively use the -u option to the python interpreter to make the standard streams operate unbuffered. Cheers, Chris -- http://blog.rebertia.com From russ.paielli at gmail.com Fri Oct 16 17:23:17 2009 From: russ.paielli at gmail.com (Russ P.) Date: Fri, 16 Oct 2009 14:23:17 -0700 (PDT) Subject: The rap against "while True:" loops References: Message-ID: <091b8eb5-1cf5-4d47-a0be-1c389928f4b8@g31g2000yqc.googlegroups.com> On Oct 10, 1:15?pm, kj wrote: > I'm coaching a group of biologists on basic Python scripting. ?One > of my charges mentioned that he had come across the advice never > to use loops beginning with "while True". ?Of course, that's one > way to start an infinite loop, but this seems hardly a sufficient > reason to avoid the construct altogether, as long as one includes > an exit that is always reached. ?(Actually, come to think of it, > there are many situations in which a bona fide infinite loops > (typically within a try: block) is the required construct, e.g. > when implementing an event loop.) > > I use "while True"-loops often, and intend to continue doing this > "while True", but I'm curious to know: how widespread is the > injunction against such loops? ?Has it reached the status of "best > practice"? Never, ever use "while True". It's an abomination. The correct form is "while 1". But seriously, folks ... "while condition" is nice when the desired break is at the beginning or end of the block, but otherwise it forces unnecessary contortions that can hamper readability. From bigboss1964 at gmail.com Fri Oct 16 17:26:28 2009 From: bigboss1964 at gmail.com (TerryP) Date: Fri, 16 Oct 2009 14:26:28 -0700 (PDT) Subject: Spawning Cmd Window via Subprocess References: Message-ID: <766f9620-7e18-4b1d-b541-a0c27a054c94@x37g2000yqj.googlegroups.com> On Oct 16, 8:15?pm, D wrote: > Hello, > > I would like to be able to spawn a new CMD window (specifing size, > color and placement of the window), ?and write to it separately. > Specifically, I have a backup program that displays each file backed > up in the main window, and I would like to spawn and continually > update a second CMD window that will display the current status (i.e. > number of files backed up, amount of data backed up). ?Also, I only > want to display the update messages, don't want to display any command > prompts. ?I'm thinking I should be able to do this using subprocess, > but I haven't been able to find out how. ?Any help would be greatly > appreciated! you'll likely want to fiddle with subprocess.Popen with the arguments set to suitable values to invoke a cmd window and establish pipes for communication; see the documentation. If that doesn't work, it would probably be time to muck with the Windows API. From dan.eloff at gmail.com Fri Oct 16 17:28:50 2009 From: dan.eloff at gmail.com (Eloff) Date: Fri, 16 Oct 2009 14:28:50 -0700 (PDT) Subject: How about adding slice notation to iterators/generators? References: Message-ID: On Oct 16, 3:54?am, Terry Reedy wrote: > There is already an obvious standard way to do this. > > it = > next(it) #toss first item > for item in it: > ? .... > That fails if there is no first item. You're taking one corner case and saying there's an easy way to do it, which is more or less true, but you miss my point that I'm suggesting we make the general case easier. By giving iterators a default, overridable, __getitem__ that is just syntactic sugar for islice, they would share a slightly larger interface subset with the builtin container types. In a duck-typed language like python, that's almost always a good thing. You could use iterators in more situations which expect something more like a list. As long as it breaks no rationally existing code, I can think of no good reason why not to do this in a future python. -Dan From bigboss1964 at gmail.com Fri Oct 16 17:30:40 2009 From: bigboss1964 at gmail.com (TerryP) Date: Fri, 16 Oct 2009 14:30:40 -0700 (PDT) Subject: print() References: <4ad8dfc8$0$1112$4fafbaef@reader2.news.tin.it> Message-ID: <7cbecdac-01d8-441c-bc29-3e73960e1db8@l2g2000yqd.googlegroups.com> On Oct 16, 9:04?pm, mattia wrote: > Is there a way to print to an unbuffered output (like stdout)? I've seen > that something like sys.stdout.write("hello") works but it also prints > the number of characters! http://docs.python.org/3.1/library/functions.html#print a suitable object passed that makes I/O behave as if unbuffered would be handy, if you don't want to stdout.flush() after a print() From victorsubervi at gmail.com Fri Oct 16 17:39:19 2009 From: victorsubervi at gmail.com (Victor Subervi) Date: Fri, 16 Oct 2009 17:39:19 -0400 Subject: More & More Fun w/ Pics & MySQL In-Reply-To: References: <4dc0cfea0910161223q326f1306jb24b7d62ab124d30@mail.gmail.com> Message-ID: <4dc0cfea0910161439v4401d1b8h1f0c325bf809251d@mail.gmail.com> I was trying all sorts of crap. I tried it without, too. Still didn't work :( V On Fri, Oct 16, 2009 at 4:42 PM, Carsten Haese wrote: > Victor Subervi wrote: > > [snip...] > > > > print 'Content-type: image/jpeg' > > print 'Content-Encoding: base64' > > print > > print pic().encode('base64') > > print '' > > > > [snip...] > > Why are you printing "" at the end of a page that is > supposed to be a base64-encoded JPEG file? > > -- > Carsten Haese > http://informixdb.sourceforge.net > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.isaac at gmail.com Fri Oct 16 17:45:01 2009 From: alan.isaac at gmail.com (Alan G Isaac) Date: Fri, 16 Oct 2009 17:45:01 -0400 Subject: restriction on sum: intentional bug? In-Reply-To: References: <7a9c25c20910160900p35600587v164b1c55c64718f1@mail.gmail.com> <4AD8A67C.2000508@tim.thechases.com> <7a9c25c20910161031q67342767pd744d455714f00aa@mail.gmail.com> Message-ID: On 10/16/2009 5:03 PM, Christian Heimes wrote: > It's not going to happen. That's a prediction, not a justification. As Tim explained in detail, and as Peter explained with brevity, whether it will happen or not, it should happen. This conversation has confirmed that current behavior is a wart: an error is raised despite correct semantics. Ugly! Alan Isaac From rylesny at gmail.com Fri Oct 16 18:53:01 2009 From: rylesny at gmail.com (ryles) Date: Fri, 16 Oct 2009 15:53:01 -0700 (PDT) Subject: restriction on sum: intentional bug? References: Message-ID: <623b93ea-0976-4004-9be4-01fbec5d8b55@37g2000yqm.googlegroups.com> On Oct 16, 11:39?am, Alan G Isaac wrote: > I expected this to be fixed in Python 3: > > >>> sum(['ab','cd'],'') > > Traceback (most recent call last): > ? ?File "", line 1, in > TypeError: sum() can't sum strings [use ''.join(seq) instead] Then you probably haven't read through these discussions: sum and strings: http://mail.python.org/pipermail/python-list/2006-August/subject.html summing a bunch of numbers: http://mail.python.org/pipermail/python-dev/2003-April/subj From rylesny at gmail.com Fri Oct 16 18:55:33 2009 From: rylesny at gmail.com (ryles) Date: Fri, 16 Oct 2009 15:55:33 -0700 (PDT) Subject: restriction on sum: intentional bug? References: <623b93ea-0976-4004-9be4-01fbec5d8b55@37g2000yqm.googlegroups.com> Message-ID: <57c1eb60-7dee-4fc2-9ae8-71a4a8e5c791@p4g2000yqm.googlegroups.com> On Oct 16, 6:53?pm, ryles wrote: > Then you probably haven't read through these discussions: > > sum and strings:http://mail.python.org/pipermail/python-list/2006-August/subject.html > summing a bunch of numbers:http://mail.python.org/pipermail/python-dev/2003-April/subj You meant: sum and strings: http://mail.python.org/pipermail/python-list/2006-August/subject.html summing a bunch of numbers: http://mail.python.org/pipermail/python-dev/2003-April/subject.html From pavlovevidence at gmail.com Fri Oct 16 19:38:27 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 16 Oct 2009 16:38:27 -0700 (PDT) Subject: restriction on sum: intentional bug? References: <7a9c25c20910160900p35600587v164b1c55c64718f1@mail.gmail.com> Message-ID: <7eeba04a-b31b-4228-8770-9239d1c3d28c@h40g2000prf.googlegroups.com> On Oct 16, 9:59?am, Tim Chase wrote: > So I agree with Alan & Peter that this creates an unfortunate > language wart among (as Peter aptly puts it) "consenting adults". Perhaps, BUT... If, say, you were to accept that Python is going to guard against a small number of especially bad cases, this has got to be one of the top candidates. Potentially grave inefficiency (order of magnitude loss of performance), and very likely to be misused. Carl Banks (Course I think the right solution would have been a separate concatenation operator, beyond hope now, though.) From half.italian at gmail.com Fri Oct 16 19:51:14 2009 From: half.italian at gmail.com (Sean DiZazzo) Date: Fri, 16 Oct 2009 16:51:14 -0700 (PDT) Subject: ftplib connection fails with multiple nics Message-ID: <9adf4434-321d-46f5-89e2-1341d5f7d7d6@w37g2000prg.googlegroups.com> Hi all, I'm trying to connect to an ftp site from a windows machine with two nics going to two different networks, but I keep getting the below exception: Traceback (most recent call last): File "ftp.pyo", line 70, in connect File "ftp.pyo", line 17, in __init__ File "ftplib.pyo", line 131, in connect File "socket.pyo", line 498, in create_connection gaierror: [Errno 10093] getaddrinfo failed I think it is because of the two nics, because the code runs fine on other machines. Any ideas on how to fix this? TIA. ~Sean From fordhaivat at gmail.com Fri Oct 16 19:53:37 2009 From: fordhaivat at gmail.com (Someone Something) Date: Fri, 16 Oct 2009 19:53:37 -0400 Subject: One class one file? Message-ID: I'm trying write a program that's going to be more than 100 lines or so but I need it all in one class. Is there a painless way to have one class in two files? -------------- next part -------------- An HTML attachment was scrubbed... URL: From pavlovevidence at gmail.com Fri Oct 16 19:53:40 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 16 Oct 2009 16:53:40 -0700 (PDT) Subject: restriction on sum: intentional bug? References: <7a9c25c20910160900p35600587v164b1c55c64718f1@mail.gmail.com> <4AD8A67C.2000508@tim.thechases.com> <7a9c25c20910161031q67342767pd744d455714f00aa@mail.gmail.com> Message-ID: <18a95057-b42e-4e4a-9f8e-b82d231de424@v37g2000prg.googlegroups.com> On Oct 16, 12:40?pm, Tim Chase wrote: > Then I'm fine with sum() being smart enough to recognize this > horrid case and do the "right" thing by returning ''.join() > instead. You don't want Python to get into this business. Trust me. Just don't go there. If you want sum to call ''.join transparently, then "".join would have to produce identical results to what sum() would have produced in all cases. That does not happen. If an object within the list defines both __str__ and __add__ methods, then "".join will call __str__, whereas sum would call __add__, leading to potentially different results. Therefore, transparently substituting a call to "".join is not an option. It'd be better to just remove the special case. Carl Banks From carsten.haese at gmail.com Fri Oct 16 19:54:55 2009 From: carsten.haese at gmail.com (Carsten Haese) Date: Fri, 16 Oct 2009 19:54:55 -0400 Subject: More & More Fun w/ Pics & MySQL In-Reply-To: <4dc0cfea0910161439v4401d1b8h1f0c325bf809251d@mail.gmail.com> References: <4dc0cfea0910161223q326f1306jb24b7d62ab124d30@mail.gmail.com> <4dc0cfea0910161439v4401d1b8h1f0c325bf809251d@mail.gmail.com> Message-ID: Victor Subervi wrote: > I was trying all sorts of crap. I tried it without, too. Still didn't > work :( Please help us help you. "Still didn't work" tells us nothing. See http://catb.org/~esr/faqs/smart-questions.html for hints on how you can ask a questions in a way that makes them more likely to get helpful answers. -- Carsten Haese http://informixdb.sourceforge.net From pavlovevidence at gmail.com Fri Oct 16 20:03:07 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 16 Oct 2009 17:03:07 -0700 (PDT) Subject: restriction on sum: intentional bug? References: Message-ID: On Oct 16, 8:39?am, Alan G Isaac wrote: > I expected this to be fixed in Python 3: A word of advice: the Python maintainers don't regard anything that behaves as it is documented to as a "bug", no matter how wrong it seems. This is taken a lot more seriously than you would think. In future complaints about the language, you'll cut down on techicalities if you respect the usage of the word "bug" and use a term such as "intentional misfeature" instead. Carl Banks From stef.mientki at gmail.com Fri Oct 16 20:07:57 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Sat, 17 Oct 2009 02:07:57 +0200 Subject: how to write a unicode string to a file ? In-Reply-To: <7a9c25c20910151659k5fa15ff8s111cb8bf8b3affb2@mail.gmail.com> References: <4AD7B3B2.2040208@gmail.com> <7a9c25c20910151659k5fa15ff8s111cb8bf8b3affb2@mail.gmail.com> Message-ID: <4AD90ADD.4060407@gmail.com> Stephen Hansen wrote: > On Thu, Oct 15, 2009 at 4:43 PM, Stef Mientki > wrote: > > hello, > > By writing the following unicode string (I hope it can be send on > this mailing list) > > B?cken > > to a file > > fh.write ( line ) > > I get the following error: > > UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' > in position 9: ordinal not in range(128) > > How should I write such a string to a file ? > > > First, you have to understand that a file never really contains > unicode-- not in the way that it exists in memory / in python when you > type line = u'B?cken'. It contains a series of bytes that are an > encoded form of that abstract unicode data. > > There's various encodings you can use-- UTF-8 and UTF-16 are in my > experience the most common. UTF-8 is an ASCII-superset, and its the > one I see most often. > > So, you can do: > > import codecs > f = codecs.open('filepath', 'w', 'utf-8') > f.write(line) > > To read such a file, you'd do codecs.open as well, just with a 'r' > mode and not a 'w' mode. Thanks guys, I didn't know the codecs module, and the codecs seems to be a good solution, at least it can safely write a file. But now I have to open that file in Excel 2000 ... 2007, and I get something completely wrong. After changing codecs to latin-1 or windows-1252, everything works fine. Which of the 2 should I use latin-1 or windows-1252 ? And a more general question, how should I organize my Python programs ? In general I've data coming from Excel, Delphi, SQLite. In Python I always use wxPython, so I'm forced to use unicode. My output often needs to be exported to Excel, SPSS, SQLite. So would this be a good design ? Excel | convert wxPython convert Excel Delphi |===> to ===> in ===> to ===> SQLite SQLite | unicode unicode latin-1 SPSS thanks, Stef Mientki > > Now, that uses a file object created with the "codecs" module which > operates with theoretical unicode streams. It will automatically take > any passed in unicode strings, encode them in the specified encoding > (utf8), and write the resulting bytes out. > > You can also do that manually with a regular file object, via: > > f.write(line.encode("utf8")) > > If you are reading such a file later with a normal file object (e.g., > not one created with codecs.open), you would do: > > f = open('filepath', 'rb') > byte_data = f.read() > uni_data = byte_data.decode("utf8") > > That will convert the byte-encoded data back to real unicode strings. > Be sure to do this even if it doesn't seem you need to if the file > contains encoded unicode data (a thing you can only know based on > documentation of whatever produced that file)... for example, a UTF8 > encoded file might look and work like a completely normal ASCII file, > but if its really UTF8... eventually your code will break that one > time someone puts in a non-ascii character. Since UTF8 is an ASCII > superset, its indistinguishable from ASCII until it contains a > non-ASCII character. > > HTH, > > --S From python at mrabarnett.plus.com Fri Oct 16 20:08:02 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 17 Oct 2009 01:08:02 +0100 Subject: One class one file? In-Reply-To: References: Message-ID: <4AD90AE2.6000106@mrabarnett.plus.com> Someone Something wrote: > I'm trying write a program that's going to be more than 100 lines or so > but I need it all in one class. Is there a painless way to have one > class in two files? > Python won't complain if you have more than 100 lines in one file, or even in one class in one file. There are plenty of examples in the standard library! From tjreedy at udel.edu Fri Oct 16 20:16:55 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 16 Oct 2009 20:16:55 -0400 Subject: restriction on sum: intentional bug? In-Reply-To: References: <7a9c25c20910160900p35600587v164b1c55c64718f1@mail.gmail.com> <4AD8A67C.2000508@tim.thechases.com> <7a9c25c20910161031q67342767pd744d455714f00aa@mail.gmail.com> Message-ID: Alan G Isaac wrote: > > As Tim explained in detail, and as Peter > explained with brevity, whether it will > happen or not, it should happen. This > conversation has confirmed that current > behavior is a wart: an error is raised > despite correct semantics. Ugly! The fact that two or three people who agree on something agree on the thing that they agree on confirms nothing. One could just as well argue that summing anything but numbers is semantically incoherent, not correct. Certainly, my dictionary points in that direction. tjr From victorsubervi at gmail.com Fri Oct 16 20:21:14 2009 From: victorsubervi at gmail.com (Victor Subervi) Date: Fri, 16 Oct 2009 20:21:14 -0400 Subject: More & More Fun w/ Pics & MySQL In-Reply-To: References: <4dc0cfea0910161223q326f1306jb24b7d62ab124d30@mail.gmail.com> <4dc0cfea0910161439v4401d1b8h1f0c325bf809251d@mail.gmail.com> Message-ID: <4dc0cfea0910161721v4812822arfcb42743c1359dd7@mail.gmail.com> I'm sorry. These scripts worked fine before and should have been plug-and-play. I have wasted 2 frustrating weeks trying to figure out why they don't work only to discover things that make no sense at all that do the trick. I thought programming was straight-forward and "logical"...boy, am I disappointed. Sorry for the jade. What I mean is that the code supplied merely posts a broken image, with the correct dimensions, interestingly enough. It makes no difference at all if one adds the line "print ''" or not. I also believe (and hope) my initial post is pretty clear. TIA, V On Fri, Oct 16, 2009 at 7:54 PM, Carsten Haese wrote: > Victor Subervi wrote: > > I was trying all sorts of crap. I tried it without, too. Still didn't > > work :( > > Please help us help you. "Still didn't work" tells us nothing. See > http://catb.org/~esr/faqs/smart-questions.html for hints on how you can > ask a questions in a way that makes them more likely to get helpful > answers. > > -- > Carsten Haese > http://informixdb.sourceforge.net > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From koranthala at gmail.com Fri Oct 16 20:25:17 2009 From: koranthala at gmail.com (koranthala) Date: Fri, 16 Oct 2009 17:25:17 -0700 (PDT) Subject: sqlite question Message-ID: <2bc8095b-c851-472a-8c6f-25a18dbf7ab5@f16g2000yqm.googlegroups.com> Hi, I use sqlalchemy to use a sqlite db in my program. The program is working perfectly as of now. But now, a new requirement has come that the db has to be encrypted. I found two options while searching internet - SQLite Encryption Extension and Sqlite-Crypt. Now, buying the license is not an issue. But I cannot understand how to use it along with sqlalchemy. There are two issues here - (1) If I am buying the encrypted db, then I get only the C-code. I am not sure how sqlalchemy links to the sqlite code. (2) In the sqlalchemy documention, I couldnt find anything associated with encryption. Has anybody done this? Is it possible to use encrypted sqlite along with sqlalchemy? TIA K From pavlovevidence at gmail.com Fri Oct 16 20:28:25 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 16 Oct 2009 17:28:25 -0700 (PDT) Subject: How about adding slice notation to iterators/generators? References: <0a0c8659-4165-4ac2-b267-f6cc81985f3c@e8g2000yqo.googlegroups.com> Message-ID: <29e3e336-5bd9-47fe-bbea-e4c7ad3c39ec@y32g2000prd.googlegroups.com> On Oct 16, 2:02?am, Bearophile wrote: > Terry Reedy: > > > 1. islice works with any iterator; generator method would only work with > > generators > > A slice syntax that's syntactic sugar for islice(some_iter,1,None) may > be added to all iterators. All custom iterators would then be burdened to support it. (That is more than enough reason for me to oppose it.) > >2. iterator protocol is intentionally simple.< > > Slice syntax is already available for lists, tuples, strings, arrays, > numpy, etc, so adding it to iterators too doesn't look like adding > that large amount of information to the mind of the programmer. Yes it would be, it'd be an unacceptable burden. The difference between iterators and containers is that it's a lot more common to write custom iterators, and a lot more useful to. The iterator protocol must be kept simple so that customizing is kept simple, the more burdens you place on iterator implementors, the less people will chose to use them, and the less useful iterators will be. For objects that are not commonly used directly by the programmer, adding a slice syntax to the protocol is absolutely ludicrous. There's no justification for it. Use islice(), or slice the iterable beforehand. Leave it out of iterators. Carl Banks From rylesny at gmail.com Fri Oct 16 20:29:47 2009 From: rylesny at gmail.com (ryles) Date: Fri, 16 Oct 2009 17:29:47 -0700 (PDT) Subject: How about adding slice notation to iterators/generators? References: Message-ID: <165f2240-0c52-4ee2-94cf-41c9d50474b1@p4g2000yqm.googlegroups.com> On Oct 16, 5:28?pm, Eloff wrote: > By giving iterators a default, overridable, __getitem__ that is just > syntactic sugar for islice, they would share a slightly larger > interface subset with the builtin container types. In a duck-typed > language like python, that's almost always a good thing. You could use > iterators in more situations which expect something more like a list. > > As long as it breaks no rationally existing code, I can think of no > good reason why not to do this in a future python. I think Python programmers have learned to expect certain things from objects that support __getitem__. For example, indexing and slicing is repeatable on the same object: a[1] == a[1] a[1:4] == a[1:4] If you saw the above code would you want to think twice above whether or not these expressions were true? Iterators don't have a general concept of "get item" like types such as strings, lists, etc. They have a concept of "get next item". So, with your proposal, i[1] != i[1] and i[1:4] != i[1:4]. Not only that, it's also common for types with __getitem__ to have __len__, which we obviously can't provide. So, ultimately, although it could afford some small conveniences, I think trying to mix iterators with __getitem__ would cause too much confusion. The use of islice() is both readable and explicit. It's very clear to the reader that you're working with iterators and that items will be consumed (something that's not reversible). From pavlovevidence at gmail.com Fri Oct 16 20:38:44 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 16 Oct 2009 17:38:44 -0700 (PDT) Subject: How about adding slice notation to iterators/generators? References: Message-ID: <11e5ab4b-6d65-4b21-a838-19457a2feead@i12g2000prg.googlegroups.com> On Oct 16, 2:28?pm, Eloff wrote: > As long as it breaks no rationally existing code, I can think of no > good reason why not to do this in a future python. You would burden everyone who writes a custom iterator to provide a __getitem__ method just because you're too lazy to type out the word islice? Carl Banks From apt.shansen at gmail.com Fri Oct 16 20:49:41 2009 From: apt.shansen at gmail.com (Stephen Hansen) Date: Fri, 16 Oct 2009 17:49:41 -0700 Subject: how to write a unicode string to a file ? In-Reply-To: <4AD90ADD.4060407@gmail.com> References: <4AD7B3B2.2040208@gmail.com> <7a9c25c20910151659k5fa15ff8s111cb8bf8b3affb2@mail.gmail.com> <4AD90ADD.4060407@gmail.com> Message-ID: <7a9c25c20910161749j38d7a0ebx15e28d3dfab74892@mail.gmail.com> On Fri, Oct 16, 2009 at 5:07 PM, Stef Mientki wrote: Unfortunately, there is no simple answer to these questions. > Thanks guys, > I didn't know the codecs module, > and the codecs seems to be a good solution, > at least it can safely write a file. > But now I have to open that file in Excel 2000 ... 2007, > and I get something completely wrong. > After changing codecs to latin-1 or windows-1252, > everything works fine. > > Which of the 2 should I use latin-1 or windows-1252 ? > > You should use the encoding that the file is expected to be in in; it was saved in a certain, explicit encoding. You may be able to find what it is on the web, but you'll have to find it and use that. Every file may be different. There's no universal right answer; and there's not really any way to tell what the answer SHOULD be, short of -- trying various encodings until one works. Doing research to find out what this other-program saves or expects to open is all you can do. It wouldn't surprise me if Excel used cp1252 by default; that's vaguely sorta like ISO-8859-1 (also known as latin1), except in the high byte range. The two are similar enough that they are confused in a lot of software with odd results. The thing is, I'd be VERY surprised (neigh, shocked!) if Excel can't open a file that is in UTF8-- it just might need to be TOLD that its utf8 when you go and open the file, as UTF8 looks just like ASCII -- until it contains characters that can't be expressed in ASCII. But I don't know what type of file it is you're saving. > And a more general question, how should I organize my Python programs ? > In general I've data coming from Excel, Delphi, SQLite. > In Python I always use wxPython, so I'm forced to use unicode. > My output often needs to be exported to Excel, SPSS, SQLite. > So would this be a good design ? > I have no idea what SPSS is, but in general the way I handle these issues are by following these rules: - Convert to Unicode from the earliest possible point; the moment data gets into my code, I convert it to unicode. - There has to be some heuristics / intelligent tests to determine HOW you convert it to unicode: you really have to /know/ before-hand what the data was encoded in before, in order to do so. You can often assume its ASCII, but unfortunately, that only works until that moment when its not. And it will eventually be not, guaranteed; you will have to base this decision on the type of source you're getting the data from. Is it from a file, if so, what kind of file? Does the program which produced it always write out a certain encoding? Or is it variable? Is it something user-specified (e.g., in an environment variable or preference), etc? Regardless, the moment you get data-- convert it into unicode, with unicode(data, "") ... the original-encoding is whatever you determine the encoding the data was in before you got it. - All private storage should be stored as unicode, encoded to UTF8. Private meaning, other programs you don't control don't mess with it. This should include data files AND databases-- you should be storing unicode as UTF in SQLIte. See the 'pragma encoding' instruction at http://www.sqlite.org/pragma.html - Do all processing in your program as unicode. - Encode the data at the last possible moment during the output process, according to whatever it needs to be; if at all possible encode at output as UTF8 if other programs can handle it, as life will one day be better when all programs can be on the same page here. But that's not always possible: when not, be sure the decision of what encoding to use when writing the data out is something your program remembers or can determine at a later point-- so that when/if you need to read it in, you know what encoding it was written out to. - Be prepared when writing data out to experience an error if the internal unicode data contains a character which can't be expressed in the limited output encoding if you're forced to use something non-UTF8, like latin1 or cp1252. If you're constrained to having to support these limited character sets, then you're going to have to make sure you handle that situation gracefully-- either by using an error handler when you encode it (e.g., line.encode("latin1", "ignore") which will exclude any characters that can't be handled in latin1, or I usually prefer line.encode("latin1", "xmlcharrefreplace") which will replace the non-working characters with &x1234; type notation) or by including validators in your UI which reject characters that can't fit into a desired encoding. Even with that validator, use unicode-strings internally. - Mourn for the good ol' days when you actually could imagine the pleasant fiction as something such as 'plain text' existing-- it never really has existed. :) So: I'd use unicode entirely -- in wxPython, stored in your database, and you should be able to use it in Delphi too I believe? Its been years since I used delphi, but. At the point where there's a barrier between 'your' stuff and 'other stuff', you convert from unicode into an encoding-- if you must. Anyways. That's just how I handle it, and rarely run into problems-- Only really when dealing with some new file format from strange old systems when its not obvious what encoding its in due to poor-documentation and na?ve implementations. -- Stephen Hansen Development Advanced Prepress Technology shansen at advpubtech.com (818) 748-9282 -------------- next part -------------- An HTML attachment was scrubbed... URL: From carsten.haese at gmail.com Fri Oct 16 20:54:07 2009 From: carsten.haese at gmail.com (Carsten Haese) Date: Fri, 16 Oct 2009 20:54:07 -0400 Subject: More & More Fun w/ Pics & MySQL In-Reply-To: <4dc0cfea0910161721v4812822arfcb42743c1359dd7@mail.gmail.com> References: <4dc0cfea0910161223q326f1306jb24b7d62ab124d30@mail.gmail.com> <4dc0cfea0910161439v4401d1b8h1f0c325bf809251d@mail.gmail.com> <4dc0cfea0910161721v4812822arfcb42743c1359dd7@mail.gmail.com> Message-ID: Victor Subervi wrote: > I'm sorry. These scripts worked fine before and should have been > plug-and-play. I have wasted 2 frustrating weeks trying to figure out > why they don't work only to discover things that make no sense at all > that do the trick. I thought programming was straight-forward and > "logical"...boy, am I disappointed. Sorry for the jade. Clearly, you're not jaded enough. When my code does things that I don't understand, I try to understand what's going on. Your approach seems to be to randomly mutate your code until it works. There is nothing inherently wrong with that as long as you then try to understand *why* it works once you get it work. You seem to have omitted this step. Since you never understood why your code worked, consequently now you don't understand why your code has stopped working. > What I mean is that the code supplied merely posts a broken image, with > the correct dimensions, interestingly enough. It makes no difference at > all if one adds the line "print ''" or not. I also believe > (and hope) my initial post is pretty clear. Actually, your original post was not clear at all. Your original post said "This will print all sorts of crap to the screen." This is not useful at all. The only reason why I responded at all was because I noticed the line in your code that clearly didn't have any business being there. The fact that you even considered the possibility that that line could help shows just how little you know about what's going on in your code. By the way, you now have changed your story from "random crap" to "a broken image". Which one is it? The only way we have a fighting chance to help you in figuring out what's going in is if you post the *exact* code you're running (and by that I mean the actual code that you know your server is executing, and not just some code that somewhat resembles the code that the server might be executing), a detailed description of the result you're expecting, and a detailed description of the result you're getting instead. -- Carsten Haese http://informixdb.sourceforge.net From pavlovevidence at gmail.com Fri Oct 16 20:55:32 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 16 Oct 2009 17:55:32 -0700 (PDT) Subject: set using alternative hash function? References: <31ed3cd6-5746-42f8-8833-7137b4924b18@k19g2000yqc.googlegroups.com> <0d53a96d-389e-435a-8442-47ae2ee7f8df@a7g2000yqo.googlegroups.com> Message-ID: On Oct 15, 9:31?pm, Austin Bingham wrote: > Yes, what you've got there provides the interface of what I want. And > no doubt we could concoct countless ways to implement some version of > this. However, if set itself accepted an alternate hash function, then > I could do it with no extra overhead. Consider your implementation: it > requires an extra set (extra space) and an extra lookup on many > operations (extra time.) My original hope was to not have to do that. Yes, Python let you down. You've said that repeatedly. Most people are happy to accept the extra overhead of homemade solution. You're not. We get it. If you want to do something about it, write a patch with some test cases and submit it. If you want to do something half-assed about it, submit hash= key as a wishlist item. If you want to do absolutely nothing about it, continue to whine about it here. Carl Banks From thom1948 at gmail.com Fri Oct 16 21:00:04 2009 From: thom1948 at gmail.com (Thomas) Date: Fri, 16 Oct 2009 18:00:04 -0700 (PDT) Subject: a splitting headache References: <221faf45-9e42-4e12-904c-61d09728e376@c3g2000yqd.googlegroups.com> Message-ID: <3da05ae3-fec0-4f4f-a988-b61ecf83cff6@j19g2000yqk.googlegroups.com> On Oct 15, 9:18?pm, Mensanator wrote: > All I wanted to do is split a binary number into two lists, > a list of blocks of consecutive ones and another list of > blocks of consecutive zeroes. > > But no, you can't do that. > > >>> c = '0010000110' > >>> c.split('0') > > ['', '', '1', '', '', '', '11', ''] > > Ok, the consecutive delimiters appear as empty strings for > reasons unknown (except for the first one). Except when they > start or end the string in which case the first one is included. > > Maybe there's a reason for this inconsistent behaviour but you > won't find it in the documentation. > > And the re module doesn't help. > > >>> f = ' ?1 2 ?3 ? 4 ? ?' > >>> re.split(' ',f) > > ['', '', '1', '2', '', '3', '', '', '4', '', '', '', ''] > > OTOH, if my digits were seperated by whitespace, I could use > str.split(), which behaves differently (but not re.split() > because it requires a string argument). > > >>> ' 1 ?11 ? 111 11 ? ?'.split() > > ['1', '11', '111', '11'] > > That means I can use re to solve my problem after all. > > >>> c = '0010000110' > >>> re.sub('0',' ',c).split() > ['1', '11'] > >>> re.sub('1',' ',c).split() > > ['00', '0000', '0'] > > Would it have been that difficult to show in the documentation > how to do this? PythonWin 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32. Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin' for further copyright information. >>> list('001010111100101') ['0', '0', '1', '0', '1', '0', '1', '1', '1', '1', '0', '0', '1', '0', '1'] >>> TC From joncle at googlemail.com Fri Oct 16 21:01:41 2009 From: joncle at googlemail.com (Jon Clements) Date: Fri, 16 Oct 2009 18:01:41 -0700 (PDT) Subject: restriction on sum: intentional bug? References: <7a9c25c20910160900p35600587v164b1c55c64718f1@mail.gmail.com> <4AD8A67C.2000508@tim.thechases.com> <7a9c25c20910161031q67342767pd744d455714f00aa@mail.gmail.com> Message-ID: On Oct 17, 1:16?am, Terry Reedy wrote: > Alan G Isaac wrote: > > > As Tim explained in detail, and as Peter > > explained with brevity, whether it will > > happen or not, it should happen. ?This > > conversation has confirmed that current > > behavior is a wart: an error is raised > > despite correct semantics. Ugly! > > The fact that two or three people who agree on something agree on the > thing that they agree on confirms nothing. One could just as well argue > that summing anything but numbers is semantically incoherent, not > correct. Certainly, my dictionary points in that direction. > > tjr I agree here. I don't think it's a case of "warning about inefficiency" that Python doesn't sum strings, but rather that 'summing' strings doesn't make sense. An OTT example could be sum(['010111010', '372']) # Binary and decimal Sum should return a *numeric* result, it has no way to do anything sensible with strings -- that's up to the coder and I think it'd be an error in Python to not raise an error. Jon. From kee at kagi.com Fri Oct 16 21:02:33 2009 From: kee at kagi.com (Kee Nethery) Date: Fri, 16 Oct 2009 18:02:33 -0700 Subject: how to write a unicode string to a file ? In-Reply-To: <7a9c25c20910161749j38d7a0ebx15e28d3dfab74892@mail.gmail.com> References: <4AD7B3B2.2040208@gmail.com> <7a9c25c20910151659k5fa15ff8s111cb8bf8b3affb2@mail.gmail.com> <4AD90ADD.4060407@gmail.com> <7a9c25c20910161749j38d7a0ebx15e28d3dfab74892@mail.gmail.com> Message-ID: On Oct 16, 2009, at 5:49 PM, Stephen Hansen wrote: > On Fri, Oct 16, 2009 at 5:07 PM, Stef Mientki > wrote: snip > The thing is, I'd be VERY surprised (neigh, shocked!) if Excel can't > open a file that is in UTF8-- it just might need to be TOLD that its > utf8 when you go and open the file, as UTF8 looks just like ASCII -- > until it contains characters that can't be expressed in ASCII. But I > don't know what type of file it is you're saving. We found that UTF-16 was required for Excel. It would not "do the right thing" when presented with UTF-8. Kee Nethery From apt.shansen at gmail.com Fri Oct 16 21:11:20 2009 From: apt.shansen at gmail.com (Stephen Hansen) Date: Fri, 16 Oct 2009 18:11:20 -0700 Subject: how to write a unicode string to a file ? In-Reply-To: References: <4AD7B3B2.2040208@gmail.com> <7a9c25c20910151659k5fa15ff8s111cb8bf8b3affb2@mail.gmail.com> <4AD90ADD.4060407@gmail.com> <7a9c25c20910161749j38d7a0ebx15e28d3dfab74892@mail.gmail.com> Message-ID: <7a9c25c20910161811r2cc91f12i246ba3d8f511c47@mail.gmail.com> On Fri, Oct 16, 2009 at 6:02 PM, Kee Nethery wrote: > > On Oct 16, 2009, at 5:49 PM, Stephen Hansen wrote: > >> On Fri, Oct 16, 2009 at 5:07 PM, Stef Mientki >> wrote: >> > > snip > >> The thing is, I'd be VERY surprised (neigh, shocked!) if Excel can't open >> a file that is in UTF8-- it just might need to be TOLD that its utf8 when >> you go and open the file, as UTF8 looks just like ASCII -- until it contains >> characters that can't be expressed in ASCII. But I don't know what type of >> file it is you're saving. >> > > We found that UTF-16 was required for Excel. It would not "do the right > thing" when presented with UTF-8. > > Oh, on reflection that doesn't surprise me. Lots of windows/microsoft stuff is UTF16. So, Stef-- for excel, I'd just write out as utf16. And use unicode internally everywhere else. If you wanted to be consistent you could just use utf16 for everything instead of UTF8. I like utf8 because of its compactness, personally. But up to you! -------------- next part -------------- An HTML attachment was scrubbed... URL: From rsreese at gmail.com Fri Oct 16 21:12:14 2009 From: rsreese at gmail.com (Stephen Reese) Date: Fri, 16 Oct 2009 21:12:14 -0400 Subject: Rename file if it exists. In-Reply-To: <7js33pF37i47eU1@mid.uni-berlin.de> References: <7js33pF37i47eU1@mid.uni-berlin.de> Message-ID: <665172f40910161812n3057effbk6fdeeb26d4205279@mail.gmail.com> > This isn't working because the else: is dangling. And I think your logic is > flawed (I might be wrong of course) because you rename the *existing* file > instead of giving the new one a new data. > > Thus e.g. a link to the file (if it's a webserver) will suddenly deliver a > different file. I doubt that's what you wanted. And you also can only upload > one file per *day*. Which doesn't sound good to me. You should disambiguate > further. And if it's ok to append a date, I suggest you do that always. > > So instead, the logic should be something like this: > > import datetime > > basename = ... # however you get to that > > basename = basename + "_" + datetime.datetime.now().strftime("%Y-%m-%d") > > count = 0 > filename = basename > while os.path.isfile(filename): > ? ?filename = basename + "." + count > ? ?count += 1 > > > Diez The script is working and appending the date to newly uploaded files but it is not adding the count? Any recommendations? Is the problem due to os.path.isfile(fn): being just the file name and not the full path? Thanks. # strip leading path from file name to avoid directory traversal attacks fn = os.path.basename(fileitem.filename) # Include date in filename. basename = fn basename = basename + "_" + datetime.datetime.now().strftime("%Y-%m-%d") count = 0 fn = basename while os.path.isfile(fn): fn = basename + "." + count count += 1 # Open the file for writing f = open('/var/www/apache2-default/' + fn, 'wb', 10000) From pavlovevidence at gmail.com Fri Oct 16 21:30:30 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 16 Oct 2009 18:30:30 -0700 (PDT) Subject: a splitting headache References: <221faf45-9e42-4e12-904c-61d09728e376@c3g2000yqd.googlegroups.com> <7xpr8oyuon.fsf@ruckus.brouhaha.com> Message-ID: <960252a9-fd95-4f75-bf84-2593109b830a@s21g2000prm.googlegroups.com> On Oct 15, 6:57?pm, Ishwor Gurung wrote: > Too bad groupby is only available in Python2.6+ > Since you're here, any chance of getting your NDK team to look into > getting some small subset of STL, Boost into Android? Aren't Java collections bad enough? :) Carl Banks From alan.isaac at gmail.com Fri Oct 16 21:41:26 2009 From: alan.isaac at gmail.com (Alan G Isaac) Date: Fri, 16 Oct 2009 21:41:26 -0400 Subject: restriction on sum: intentional bug? In-Reply-To: References: <7a9c25c20910160900p35600587v164b1c55c64718f1@mail.gmail.com> <4AD8A67C.2000508@tim.thechases.com> <7a9c25c20910161031q67342767pd744d455714f00aa@mail.gmail.com> Message-ID: On 10/16/2009 8:16 PM, Terry Reedy wrote: > The fact that two or three people who agree on something agree on the > thing that they agree on confirms nothing. One could just as well argue > that summing anything but numbers is semantically incoherent, not > correct. Certainly, my dictionary points in that direction. Come on now, that is just a silly argument. And dictionaries are obviously irrelevant; that is a sophomoric (literally) argument. Of course the numbers do not matter. The *reasons* matter. And by citing Tim and Peter, I was pointing to their quite specific *reasons*. The only serious reason that has been offered for the current behavior is that people who do not know better will sum strings instead of joining them, which is more efficient. That is a pretty weak argument for breaking expectations and so refusing to do duck typing that an error is raise. Especially in a language like Python. (As Tim and Peter make clear.) Alan From darcy at druid.net Fri Oct 16 21:42:14 2009 From: darcy at druid.net (D'Arcy J.M. Cain) Date: Fri, 16 Oct 2009 21:42:14 -0400 Subject: One class one file? In-Reply-To: References: Message-ID: <20091016214214.2cc843c2.darcy@druid.net> On Fri, 16 Oct 2009 19:53:37 -0400 Someone Something wrote: > I'm trying write a program that's going to be more than 100 lines or so but > I need it all in one class. Is there a painless way to have one class in two > files? I don't understand what the problem is with a 100 line class but if it is an absolute necessity to split it (e.g. homework assignment) think about inheritance. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner. From sajmikins at gmail.com Fri Oct 16 22:00:52 2009 From: sajmikins at gmail.com (Simon Forman) Date: Fri, 16 Oct 2009 22:00:52 -0400 Subject: Iterators In-Reply-To: References: <318F9A7177894D4F9061D20E1D2318D04E5C23@gisexch.gis.a-star.edu.sg> Message-ID: <50f98a4c0910161900j371b582eke594511a2d5e4fac@mail.gmail.com> On Fri, Oct 16, 2009 at 8:22 AM, Duncan Booth wrote: > Chris Rebert wrote: > >> Essentially, file iterators are dumb and don't keep track of where in >> the file the next line starts, instead relying on their associated >> file object to keep track of the current position in the file; the >> iterator's state is little more than a reference to its associated >> file object. When asked for the "next" line, a file iterator just >> reads forward to the next newline from the file object's current >> position, changing the current position as tracked by the file object >> as a side-effect. Thus, using multiple iterators to the same file >> object can have the results you're seeing when these side-effects >> interact. > > Nothing 'dumb' or 'smart' about it: it is simply that a file object is > already an iterator. Trying to create an iterator from an existing iterator > in Python never duplicates the iterator. > >>>> f = open('somefile') >>>> iter(f) is f > True > The OP's question has been answered but since no one mentioned the itertools.tee() function yet I figured I would.. http://docs.python.org/library/itertools.html#itertools.tee In [1]: open('afile', 'w').write('''line one ...: line two ...: line three''') In [2]: from itertools import tee In [3]: i0, i1 = tee(open('afile')) In [4]: i0.next() Out[4]: 'line one\n' In [5]: i0.next() Out[5]: 'line two\n' In [6]: i1.next() Out[6]: 'line one\n' In [7]: i0.next() Out[7]: 'line three' From yves at zioup.com Fri Oct 16 22:02:15 2009 From: yves at zioup.com (Yves) Date: Fri, 16 Oct 2009 20:02:15 -0600 Subject: executing a function/method from a variable Message-ID: What is the best way to execute a function which name is stored in a variable ? Right now I use an eval, but I'm wondering if there isn't a better way: Here is a simplified example, but what I use this for is to parse a formated text file, and execute a different method depending on the pattern: import sys class dummy(object): def __init__(self, arg): self.todo = 'self.print' + arg; def printa(self): print 'a' def printb(self): print 'b' def doit(self): #k = eval(self.todo) #k() eval(self.todo)() o = dummy(sys.argv[1]) o.doit() Thanks. -- Yves. http://www.sollers.ca/ From rridge at csclub.uwaterloo.ca Fri Oct 16 22:13:56 2009 From: rridge at csclub.uwaterloo.ca (Ross Ridge) Date: Fri, 16 Oct 2009 22:13:56 -0400 Subject: Are there any modules for IRC, that work with Python 3.1? References: <204e41fe-a00c-46d2-bb78-4f826ae750c1@o41g2000yqb.googlegroups.com> Message-ID: TerryP wrote: >Having recently been put into search for a new IRC client, and >everything I've thrown in the cauldron having become a >disappointment... Have you tried the IRC client script for Vim? Ross Ridge -- l/ // Ross Ridge -- The Great HTMU [oo][oo] rridge at csclub.uwaterloo.ca -()-/()/ http://www.csclub.uwaterloo.ca/~rridge/ db // From carsten.haese at gmail.com Fri Oct 16 22:35:42 2009 From: carsten.haese at gmail.com (Carsten Haese) Date: Fri, 16 Oct 2009 22:35:42 -0400 Subject: executing a function/method from a variable In-Reply-To: References: Message-ID: Yves wrote: > Right now I use an eval, but I'm wondering if there isn't a better way: There's (almost) always a better way than using eval. In this case, you should use getattr(). Here's your simplified example modified to use getattr: import sys class dummy(object): def __init__(self, arg): self.todo = "print"+arg def printa(self): print 'a' def printb(self): print 'b' def doit(self): func = getattr(self, self.todo) func() o = dummy(sys.argv[1]) o.doit() HTH, -- Carsten Haese http://informixdb.sourceforge.net From pjcoup at gmail.com Fri Oct 16 22:40:55 2009 From: pjcoup at gmail.com (pjcoup) Date: Fri, 16 Oct 2009 19:40:55 -0700 (PDT) Subject: SQL user function returning list for IN clause References: Message-ID: <759183bf-bea1-4cdf-b481-cc66522099ae@12g2000pri.googlegroups.com> On Oct 16, 9:50?am, Felix wrote: [snip] > I could create a new table matching each row in b to all values of > b.bar and use that to join but that would be inefficient and very > redundant. > [snip] Is foobar(b.bar) essentially static? (I'm guessing so if you considered this as an option). If so, then this actually sounds like the best option to me. Indexing on the foobar return values, this new table and joining as described has got to be faster than the other alternatives (even the foobar(b.bar,k) options). Without this new table, it seems you have to calculate foobar(b.bar) for every row of b (even if it is SQLite doing it, and not you directly), unless I'm missing something. I'm assuming that the overhead to store these function values won't kill you. Good luck! Pete From alan.isaac at gmail.com Fri Oct 16 22:41:20 2009 From: alan.isaac at gmail.com (Alan G Isaac) Date: Fri, 16 Oct 2009 22:41:20 -0400 Subject: restriction on sum: intentional bug? In-Reply-To: References: Message-ID: > Alan G Isaac gmail.com> writes: >> So of course join is better, as originally noted, >> but that does not constitute a reason to intentionally >> violate duck typing. On 10/16/2009 1:03 PM, Benjamin Peterson wrote: > As Stephen pointed out, duck typing is not an absolute. I do not recall anyone suggesting it was. Do you? If so, cite them. Thanks, Alan Isaac From joncle at googlemail.com Fri Oct 16 22:43:29 2009 From: joncle at googlemail.com (Jon Clements) Date: Fri, 16 Oct 2009 19:43:29 -0700 (PDT) Subject: executing a function/method from a variable References: Message-ID: <24e1559a-1e96-44db-ba28-4b7c179b1f12@l13g2000yqb.googlegroups.com> On Oct 17, 3:02?am, Yves wrote: > What is the best way to execute a function which name is stored in a variable ? > > Right now I use an eval, but I'm wondering if there isn't a better way: > > Here is a simplified example, but what I use this for is to parse a formated > text file, and execute a different method depending on the pattern: > > import sys > > class dummy(object): > ? ?def __init__(self, arg): > ? ? ?self.todo = 'self.print' + arg; > > ? ?def printa(self): > ? ? ?print 'a' > > ? ?def printb(self): > ? ? ?print 'b' > > ? ?def doit(self): > ? ? ?#k = eval(self.todo) > ? ? ?#k() > > ? ? ?eval(self.todo)() > > o = dummy(sys.argv[1]) > o.doit() > > Thanks. > > -- > Yves.http://www.sollers.ca/ Depending on your exact requirements, take a look at pyparsing. Has (from my experience) a small learning curve and is a useful library addition! It could well be overkill, but not knowing your exact requirements, it'd be worth looking at anyway. Jon. From davea at ieee.org Fri Oct 16 23:21:45 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 16 Oct 2009 23:21:45 -0400 Subject: putchar(8) In-Reply-To: <1255720658.9153.1385.camel@willow> References: <1255720658.9153.1385.camel@willow> Message-ID: <4AD93849.3020704@ieee.org> Jason Tackaberry wrote: > On Fri, 2009-10-16 at 12:01 -0700, gervaz wrote: > >> Hi all, is there in python the equivalent of the C function int putchar >> (int c)? I need to print putchar(8). >> > > >>>> print '\x08' >>>> > > or: > > >>>> print chr(8) >>>> > > > > If I recall correctly, putchar() takes an int value 0-255 and outputs a single character to stdout. So the equivalent would be: sys.stdout.write(char(c)) print does other stuff, which you presumably do not want. DaveA From davea at ieee.org Fri Oct 16 23:39:38 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 16 Oct 2009 23:39:38 -0400 Subject: print() In-Reply-To: <4ad8dfc8$0$1112$4fafbaef@reader2.news.tin.it> References: <4ad8dfc8$0$1112$4fafbaef@reader2.news.tin.it> Message-ID: <4AD93C7A.5030507@ieee.org> mattia wrote: > Is there a way to print to an unbuffered output (like stdout)? I've seen > that something like sys.stdout.write("hello") works but it also prints > the number of characters! > > What the other responses (so far) didn't address is your comment about "prints the number of characters." You're presumably testing this in the interpreter, which prints extra stuff. In particular, it prints the result value of any expressions entered at the interpreter prompt. So if you type sys.stdout.write("hello") then after the write() method is done, the return value of the method (5) will get printed by the interpreter. Either put the statement in a real script, or do the following trick to convince yourself: dummy = sys.stdout.write("hello") DaveA From aahz at pythoncraft.com Fri Oct 16 23:44:15 2009 From: aahz at pythoncraft.com (Aahz) Date: 16 Oct 2009 20:44:15 -0700 Subject: which "dictionary with attribute-style access"? References: Message-ID: In article , Andreas Balogh wrote: > >My question to the Python specialists: which one is the most correct? >Are there restrictions with regards to pickling or copy()? >Which one should I choose? What's your goal? I'd probably do the dirt simple myself: class AttrDict(dict): def __getattr__(self, attr): if attr in self: return self[attr] else: raise AttributeError def __setattr__(self, attr, value): self[attr] = value d = AttrDict() d.foo = 'bar' print d.foo -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "To me vi is Zen. To use vi is to practice zen. Every command is a koan. Profound to the user, unintelligible to the uninitiated. You discover truth everytime you use it." --reddy at lion.austin.ibm.com From mensanator at aol.com Sat Oct 17 00:05:59 2009 From: mensanator at aol.com (Mensanator) Date: Fri, 16 Oct 2009 21:05:59 -0700 (PDT) Subject: a splitting headache References: <221faf45-9e42-4e12-904c-61d09728e376@c3g2000yqd.googlegroups.com> <3da05ae3-fec0-4f4f-a988-b61ecf83cff6@j19g2000yqk.googlegroups.com> Message-ID: <6c1b35a8-0c14-479f-9c39-b748adb949dc@j19g2000yqk.googlegroups.com> On Oct 16, 8:00?pm, Thomas wrote: > On Oct 15, 9:18?pm, Mensanator wrote: > > > > > > > All I wanted to do is split a binary number into two lists, > > a list of blocks of consecutive ones and another list of > > blocks of consecutive zeroes. > > > But no, you can't do that. > > > >>> c = '0010000110' > > >>> c.split('0') > > > ['', '', '1', '', '', '', '11', ''] > > > Ok, the consecutive delimiters appear as empty strings for > > reasons unknown (except for the first one). Except when they > > start or end the string in which case the first one is included. > > > Maybe there's a reason for this inconsistent behaviour but you > > won't find it in the documentation. > > > And the re module doesn't help. > > > >>> f = ' ?1 2 ?3 ? 4 ? ?' > > >>> re.split(' ',f) > > > ['', '', '1', '2', '', '3', '', '', '4', '', '', '', ''] > > > OTOH, if my digits were seperated by whitespace, I could use > > str.split(), which behaves differently (but not re.split() > > because it requires a string argument). > > > >>> ' 1 ?11 ? 111 11 ? ?'.split() > > > ['1', '11', '111', '11'] > > > That means I can use re to solve my problem after all. > > > >>> c = '0010000110' > > >>> re.sub('0',' ',c).split() > > ['1', '11'] > > >>> re.sub('1',' ',c).split() > > > ['00', '0000', '0'] > > > Would it have been that difficult to show in the documentation > > how to do this? > > PythonWin 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit > (Intel)] on win32. > Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin' > for further copyright information.>>> list('001010111100101') > > ['0', '0', '1', '0', '1', '0', '1', '1', '1', '1', '0', '0', '1', '0', > '1'] Thanks, but what I wanted was ['00','1','0','1','0','1111','00','1','0' '1']. > > > > TC From davea at ieee.org Sat Oct 17 00:23:32 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 17 Oct 2009 00:23:32 -0400 Subject: Rename file if it exists. In-Reply-To: <665172f40910161812n3057effbk6fdeeb26d4205279@mail.gmail.com> References: <7js33pF37i47eU1@mid.uni-berlin.de> <665172f40910161812n3057effbk6fdeeb26d4205279@mail.gmail.com> Message-ID: <4AD946C4.5040407@ieee.org> Stephen Reese wrote: >> >> >> > The script is working and appending the date to newly uploaded files > but it is not adding the count? Any recommendations? Is the problem > due to os.path.isfile(fn): being just the file name and not the full > path? Thanks. > > # strip leading path from file name to avoid directory traversal attacks > fn =s.path.basename(fileitem.filename) > > # Include date in filename. > basename =n > basename =asename + "_" + datetime.datetime.now().strftime("%Y-%m-%d") > count = > fn =asename > while os.path.isfile(fn): > fn = basename + "." + count > count += > > # Open the file for writing > f =pen('/var/www/apache2-default/' + fn, 'wb', 10000) > > Clearly, the os.path.isfile() is never returning true, because once it does, you'll get a string error on the next line. You need fn = basename + "." + str(count) or something similar. Anyway, isfile() needs a complete path, there's no way it can guess what directory you plan to use. To build a complete path, you need something like: fullname = os.path.join('/var/www/apache2-default/', fn) You ought to use that on the actual open as well. As long as you're hard-coding the path prefix, plain string concatenation can work, but eventually you'll get the string from somewhere else, and what happens if someone forgets the trailing slash? From http Sat Oct 17 00:41:11 2009 From: http (Paul Rubin) Date: 16 Oct 2009 21:41:11 -0700 Subject: a splitting headache References: <221faf45-9e42-4e12-904c-61d09728e376@c3g2000yqd.googlegroups.com> <3da05ae3-fec0-4f4f-a988-b61ecf83cff6@j19g2000yqk.googlegroups.com> <6c1b35a8-0c14-479f-9c39-b748adb949dc@j19g2000yqk.googlegroups.com> Message-ID: <7xd44miqe0.fsf@ruckus.brouhaha.com> Mensanator writes: > Thanks, but what I wanted was > ['00','1','0','1','0','1111','00','1','0' '1']. >>> c = '001010111100101' >>> list(''.join(g) for k,g in groupby(c)) ['00', '1', '0', '1', '0', '1111', '00', '1', '0', '1'] is really not that unnatural. From dieter at handshake.de Sat Oct 17 00:54:29 2009 From: dieter at handshake.de (Dieter Maurer) Date: 17 Oct 2009 06:54:29 +0200 Subject: python performance on Solaris In-Reply-To: References: <1a4707f5-85be-4f5f-ac3e-cf8f5bd21bcb@b15g2000yqd.googlegroups.com> <4ad6b375$0$1631$742ec2ed@news.sonic.net> Message-ID: Antoine Pitrou writes on Thu, 15 Oct 2009 16:25:43 +0000 (UTC): > Le Wed, 14 Oct 2009 22:39:14 -0700, John Nagle a ?crit?: > > > > Note that multithreaded compute-bound Python programs really suck > > on multiprocessors. Adding a second CPU makes the program go slower, > > due to a lame mechanism for resolving conflicts over the global > > interpreter lock. > > I'm not sure what you're talking about. Python has no "mechanism for > resolving conflicts over the global interpreter lock" (let alone a lame > one :-)), it just trusts the OS to schedule a thread only when it is not > waiting on an unavailable resource (a lock). The GIL is just an OS-level > synchronization primitive and its behaviour (fairness, performance) will > depend on the behaviour of the underlying OS. But, independent from the OS and the fairness/performance of the GIL management itself: the GIL is there to prevent concurrent execution of Python code. Thus, at any time, at most one thread (in a process) is executing Python code -- other threads may run as well, as long as they are inside non Python code but cannot be executing Python bytecode, independent of available CPU resources. This implies that Python cannot fully exploit the power of multiprocessors. It is also true that adding CPUs may in fact reduce performance for compute bound multithreaded Python programs. While the additional computational resources cannot be use by Python, the additional overhead (switching between CPUs) may reduce overall performance. I agree with you that it is difficult to understand when this overhead were really significant. Dieter From invalid at invalid.invalid Sat Oct 17 01:15:13 2009 From: invalid at invalid.invalid (Grant Edwards) Date: Sat, 17 Oct 2009 05:15:13 +0000 (UTC) Subject: print() References: <4ad8dfc8$0$1112$4fafbaef@reader2.news.tin.it> Message-ID: On 2009-10-17, Dave Angel wrote: > mattia wrote: >> Is there a way to print to an unbuffered output (like stdout)? I've seen >> that something like sys.stdout.write("hello") works but it also prints >> the number of characters! >> >> > What the other responses (so far) didn't address is your comment about > "prints the number of characters." > > You're presumably testing this in the interpreter, which prints extra > stuff. In particular, it prints the result value of any expressions > entered at the interpreter prompt. So if you type > > sys.stdout.write("hello") > > then after the write() method is done, the return value of the method > (5) will get printed by the interpreter. Except sys.stdout.write("hello") doesn't return 5. It returns None. I don't know what the OP is talking about when he says "prints the number of characters": $ python Python 2.6.2 (r262:71600, Aug 25 2009, 22:35:31) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.stdout.write("hello\n") hello >>> >>> > Either put the statement in a real script, or do the following trick to > convince yourself: > > dummy = sys.stdout.write("hello") I don't see why the assignment is needed. -- Grant From marks542004 at yahoo.com Sat Oct 17 01:22:03 2009 From: marks542004 at yahoo.com (marks542004 at yahoo.com) Date: Fri, 16 Oct 2009 22:22:03 -0700 (PDT) Subject: zipfile extracting png files corrupt Message-ID: <4639c9e8-13dc-4b23-95bd-57abb3ea2b20@u36g2000prn.googlegroups.com> Hi, I am using the zipfile in Python 2.4 to extract files from existing zips. It appears to work but the files extracted are corrupt. Here is my code : import zipfile import os, sys , shutil epath = "c:/ziptest/" fil = "J:/archives/xzips/duplicates/gothicfence_1470.zip" ferr = file((epath + "/errlog.txt"),"w") print "Extracting to ",epath try: if zipfile.is_zipfile(fil): z = zipfile.ZipFile(fil,"r") nmes = z.namelist() for i in nmes: fn = os.path.split(i)[1] print "...",fn dta = z.read(i) if len(dta) >0 : enam = epath + fn fo = file(enam,"wb") fo.write(z.read(i)) fo.flush() fo.close() except: myerr = fil +"," + str(sys.exc_info()[1]) +"\n" ferr.write(myerr) ferr.close() any ideas ? thanks From gagsl-py2 at yahoo.com.ar Sat Oct 17 01:52:54 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 17 Oct 2009 02:52:54 -0300 Subject: print() References: <4ad8dfc8$0$1112$4fafbaef@reader2.news.tin.it> Message-ID: En Sat, 17 Oct 2009 02:15:13 -0300, Grant Edwards escribi?: > On 2009-10-17, Dave Angel wrote: >> mattia wrote: >>> Is there a way to print to an unbuffered output (like stdout)? I've >>> seen >>> that something like sys.stdout.write("hello") works but it also prints >>> the number of characters! >>> >>> >> What the other responses (so far) didn't address is your comment about >> "prints the number of characters." >> >> You're presumably testing this in the interpreter, which prints extra >> stuff. In particular, it prints the result value of any expressions >> entered at the interpreter prompt. So if you type >> >> sys.stdout.write("hello") >> >> then after the write() method is done, the return value of the method >> (5) will get printed by the interpreter. > > Except sys.stdout.write("hello") doesn't return 5. It returns > None. > > I don't know what the OP is talking about when he says "prints > the number of characters": > > $ python > Python 2.6.2 (r262:71600, Aug 25 2009, 22:35:31) > [GCC 4.3.2] on linux2 > Type "help", "copyright", "credits" or "license" for more > information. >>>> import sys >>>> sys.stdout.write("hello\n") > hello Presumably he's using Python 3: Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. p3> import sys p3> sys.stdout.write("hello") hello5 See http://bugs.python.org/issue6345 -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Sat Oct 17 01:57:49 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 17 Oct 2009 02:57:49 -0300 Subject: zipfile extracting png files corrupt References: <4639c9e8-13dc-4b23-95bd-57abb3ea2b20@u36g2000prn.googlegroups.com> Message-ID: En Sat, 17 Oct 2009 02:22:03 -0300, escribi?: > Hi, I am using the zipfile in Python 2.4 to extract files from > existing zips. > It appears to work but the files extracted are corrupt. Have you tested the zip files with other tools? I can't spot anything obviously wrong in your code (except a tendency to shrten vrble nmes :) ) -- Gabriel Genellina From kd1yv at arrl.net Sat Oct 17 02:22:43 2009 From: kd1yv at arrl.net (JimR) Date: Sat, 17 Oct 2009 02:22:43 -0400 Subject: Installation question 2.5.4 Message-ID: <4AD962B3.9060008@arrl.net> I just activated a new Mailman mailing list system on a Redhat virtual server. All was going well except the digests were not being delivered. The logged error was to the effect of decoding Unicode not being supported. In searching out this error, I found a reference that suggested upgrading from Python 2.5 to 2.5.2 or higher would resolve the error. I completed the configure, the make and the make install. However, the last instruction in README is to perform pkgmanager -a /usr/python As you may have guessed, Redhat does not have pkgmanager. What can I do to work-around this problem and get this list running? Thanks in advance, -- 73 de Jim, KD1YV From metolone+gmane at gmail.com Sat Oct 17 02:28:29 2009 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Fri, 16 Oct 2009 23:28:29 -0700 Subject: how to write a unicode string to a file ? References: <4AD7B3B2.2040208@gmail.com><7a9c25c20910151659k5fa15ff8s111cb8bf8b3affb2@mail.gmail.com><4AD90ADD.4060407@gmail.com><7a9c25c20910161749j38d7a0ebx15e28d3dfab74892@mail.gmail.com> Message-ID: "Kee Nethery" wrote in message news:AAAB63C6-6E44-4C07-B119-972D4F49E511 at kagi.com... > > On Oct 16, 2009, at 5:49 PM, Stephen Hansen wrote: > >> On Fri, Oct 16, 2009 at 5:07 PM, Stef Mientki >> wrote: > > snip > >> The thing is, I'd be VERY surprised (neigh, shocked!) if Excel can't >> open a file that is in UTF8-- it just might need to be TOLD that its >> utf8 when you go and open the file, as UTF8 looks just like ASCII -- >> until it contains characters that can't be expressed in ASCII. But I >> don't know what type of file it is you're saving. > > We found that UTF-16 was required for Excel. It would not "do the right > thing" when presented with UTF-8. Excel seems to expect a UTF-8-encoded BOM (byte order mark) to correctly decide a file is written in UTF-8. This worked for me: f=codecs.open('test.csv','wb','utf-8') f.write(u'\ufeff') # write a BOM f.write(u'??,testing,123\r\n') f.close() When opened in Excel without the BOM (\ufeff), I got gibberish, but with the BOM the Chinese characters were displayed correctly. -Mark From metolone+gmane at gmail.com Sat Oct 17 02:33:12 2009 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Fri, 16 Oct 2009 23:33:12 -0700 Subject: Tracking down DLL load errors in Windows ? References: Message-ID: "Fred P" wrote in message news:Xns9CA5E52BEC783PaCmAnRDLM at 212.27.60.39... > Hi, a bit of platform-specific advice sought here... I'm trying to > diagnose > one of those mysteries Windows is so fond of... > > Say that I have code that imports some binary Python module from site- > packages (in this case, libpyexiv2.pyd through pyexiv2.py, could be > anythng > else). > > On three Windows boxes I've tried it (XP Pro or Home, Python 2.5) > everything works fine. On a fourth, importing the module fails with a > traceback like this : > > Traceback (most recent call last): > File "", line 1, in > File "c:\python\lib\site-packages\pyexiv2.py", line 60, in > import libpyexiv2 > ImportError: DLL load failed: This application could not start > because its configuration is incorrect. Reinstalling it might solve > the problem. > > I have tried long and hard to spot a meaningful difference between the > first three systems and the last, without success. > > Is there any tool and/or methodology I could use to at least pinpoint the > exact DLL that libpyexiv2 is failing to load, and ideally also the reason > why ?... That error message is returned from executables built with later versions of Visual Studio (at least 2005 and 2008...not sure about 2003). The Windows application event log will indicate what DLL is missing. In addition to tools others have noted, depends.exe (comes with VS2005...was removed in VS2008 for unknown reasons) will list the DLLs required by an executable. Another Visual Studio tool, "dumpbin /imports " works, too. -Mark From rogerb at rogerbinns.com Sat Oct 17 03:18:05 2009 From: rogerb at rogerbinns.com (Roger Binns) Date: Sat, 17 Oct 2009 00:18:05 -0700 Subject: sqlite question In-Reply-To: References: <2bc8095b-c851-472a-8c6f-25a18dbf7ab5@f16g2000yqm.googlegroups.com> Message-ID: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Dennis Lee Bieber wrote: > I suspect, besides building an sqlite3.dll (if Windows), you might > have to modify the pysqlite DB-API adapter to support whatever new > arguments have been added to various calls (most like the db.connect() > parameters have changed to require an encryption password) The extension requires an extra C api call after the database is opened to set the encryption key. (There is also another API to change the key.) This means that it is not possible for the same pysqlite to work against SQLite built with and without the encryption extension. The changes needed in pysqlite are a simple matter of programming although you'll want them incorporated back into the core so you don't have to maintain them (wrapped in some sort of ifdef). Roger -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAkrZb6sACgkQmOOfHg372QRSYwCg2J/YSvkqLs8EJ1iJkE2wnbaY nl0AoKYxgMEnBQNjDyYHv1xWC0Tia74U =eA2B -----END PGP SIGNATURE----- From steve at REMOVE-THIS-cybersource.com.au Sat Oct 17 03:25:25 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 17 Oct 2009 07:25:25 GMT Subject: restriction on sum: intentional bug? References: <7a9c25c20910160900p35600587v164b1c55c64718f1@mail.gmail.com> <4AD8A67C.2000508@tim.thechases.com> <7a9c25c20910161031q67342767pd744d455714f00aa@mail.gmail.com> Message-ID: <0062f568$0$26941$c3e8da3@news.astraweb.com> On Fri, 16 Oct 2009 18:01:41 -0700, Jon Clements wrote: > Sum should return a *numeric* result, it has no way to do anything > sensible > with strings -- that's up to the coder and I think it'd be an error in > Python > to not raise an error. That's obviously wrong in Python. Mathematically, sum() is defined as the repeated application of the + operator. In Python, the + operator is well-defined for strings and lists as well as numbers. Since you can say "ab" + "cd" + "ef" and get a sensible result, then sum() should be able to do the same thing. And indeed, if you pass a list-of-lists to sum(), it does: >>> sum([[1,2], ['a',None], [1,'b']], []) [1, 2, 'a', None, 1, 'b'] (For the record, summing lists is O(N**2), and unlike strings, there's no optimization in CPython to avoid the slow behaviour.) Likewise if you defeat sum()'s feeble attempt to stop you from running with scissors, it also gives a sensible result for strings: >>> class S: ... def __add__(self, other): ... return other ... >>> sum(['a', 'b', 'c', 'd'], S()) 'abcd' In languages where + is *not* used for string or list concatenation, then it makes sense to argue that sum(strings or lists) is meaningless. But Python is not one of those languages. -- Steven From samslists at gmail.com Sat Oct 17 03:25:30 2009 From: samslists at gmail.com (Sam) Date: Sat, 17 Oct 2009 00:25:30 -0700 (PDT) Subject: Pretty Print Python Data Structures to HTML Message-ID: <7d032aee-4faf-4dcf-9934-f5a02a01ced5@a32g2000yqm.googlegroups.com> I'm looking for something like the pprint module but that can take a Python data structure and turn it into HTML (or better xHTML). It would read through the data structure but instead of formatting it using spaces it would format it using either dd/dt or ordered/ unordered lists or even as a html table. Have I explained this well enough that you can understand what I mean? Is such a library available anywhere? Thanks From solipsis at pitrou.net Sat Oct 17 03:29:48 2009 From: solipsis at pitrou.net (Antoine Pitrou) Date: Sat, 17 Oct 2009 07:29:48 +0000 (UTC) Subject: multi-threaded performance References: <1a4707f5-85be-4f5f-ac3e-cf8f5bd21bcb@b15g2000yqd.googlegroups.com> <4ad6b375$0$1631$742ec2ed@news.sonic.net> Message-ID: Le Sat, 17 Oct 2009 06:54:29 +0200, Dieter Maurer a ?crit?: > > It is also true that adding CPUs may in fact reduce performance for > compute bound multithreaded Python programs. While the additional > computational resources cannot be use by Python, the additional overhead > (switching between CPUs) may reduce overall performance. I agree with > you that it is difficult to understand when this overhead were really > significant. For what it's worth, I just wrote a little benchmark script to measure this kind of things: http://svn.python.org/view/sandbox/trunk/ccbench/ Regards Antoine. From steve at REMOVE-THIS-cybersource.com.au Sat Oct 17 03:34:31 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 17 Oct 2009 07:34:31 GMT Subject: restriction on sum: intentional bug? References: <7a9c25c20910160900p35600587v164b1c55c64718f1@mail.gmail.com> <15515ef0-0736-4cb4-bb67-543a08855b7d@k19g2000yqc.googlegroups.com> Message-ID: <0062f78a$0$26941$c3e8da3@news.astraweb.com> On Fri, 16 Oct 2009 11:06:30 -0700, Jon Clements wrote: > For instance, sum(['1', '2', '3']); it's not completely unreasonable for > someone to expect > a result of 6. This is Python, not Perl -- Python never tries to perform numeric arithmetic on strings. '1'+'2' will return '12' and not 3. It is completely unreasonable to expect sum() to coerce strings to ints when the + operator does not. -- Steven From southpolepen at hotmail.com Sat Oct 17 03:48:27 2009 From: southpolepen at hotmail.com (james27) Date: Sat, 17 Oct 2009 00:48:27 -0700 (PDT) Subject: using cx_freeze on linux Message-ID: <25936152.post@talk.nabble.com> hi.. im new to cx_freeze . i was searched much of time.but i couldn't found solution. i want to make exe file which can run on windows. is it possible to make windows exe file by use cx_freeze on linux? if anyone can help ,much appreciate. thanks -- View this message in context: http://www.nabble.com/using-cx_freeze-on-linux-tp25936152p25936152.html Sent from the Python - python-list mailing list archive at Nabble.com. From steve at REMOVE-THIS-cybersource.com.au Sat Oct 17 03:55:27 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 17 Oct 2009 07:55:27 GMT Subject: The rap against "while True:" loops References: <01ccc46d-5ea9-4dfe-ba22-699c6b859f00@v36g2000yqv.googlegroups.com> <7jg84fF3536pjU1@mid.individual.net> <4cdf9c03-d8e1-406c-b960-1a5b535f9430@m38g2000yqd.googlegroups.com> <005fde84$0$26920$c3e8da3@news.astraweb.com> Message-ID: <0062fc72$0$26941$c3e8da3@news.astraweb.com> On Fri, 16 Oct 2009 18:30:50 +0100, Tim Rowe wrote: >> Also, using exceptions this way is a structured form of GOTO -- it's >> easy to abuse and turn it into spaghetti code. Actually, not that easy >> to abuse, because you can't jump back into the try block. It's more >> like a multi-level break outside of a loop than a general GOTO. > > I don't think it's *only* the performance thing, it's also clarity. The > understood meaning of throwing an exception is to say "something > happened that shouldn't have". If one uses it when something has > happened that *should* have, because it happens to have the right > behaviour (even if the overhead doesn't matter), then one is > misrepresenting the program logic. No, you have a fundamental misunderstanding. They're called exceptions, not errors, because they represent exceptional cases. Often errors are exceptional cases, but they're not the only sort of exceptional case. Python uses exceptions for flow control: e.g. for-loops swallow StopIteration or IndexError to indicate the end of the loop. In the context of a for-loop, StopIteration or IndexError doesn't represent an error. It doesn't represent an unexpected case. It represents an expected, but exceptional (special) case: we expect that most sequences are finite, and it is normal to eventually reach the end of the sequence, after which the loop must change behaviour. Similarly, it's hardly an *error* for [1, 2, 3].index(5) to fail -- who is to say that the list is supposed to have 5 in it? ValueError (a slightly misleading name in this situation) is used to indicate an exceptional, but not unexpected, occurrence. Likewise, KeyboardInterrupt is used to allow the user to halt processing; SystemExit is used to shut down the Python virtual machine; and warnings are implemented using exceptions. There may be others among the built-ins and standard library, but even if there aren't, there is plenty of precedence for us to do the same. -- Steven From mensanator at aol.com Sat Oct 17 04:08:34 2009 From: mensanator at aol.com (Mensanator) Date: Sat, 17 Oct 2009 01:08:34 -0700 (PDT) Subject: a splitting headache References: <221faf45-9e42-4e12-904c-61d09728e376@c3g2000yqd.googlegroups.com> <3da05ae3-fec0-4f4f-a988-b61ecf83cff6@j19g2000yqk.googlegroups.com> <6c1b35a8-0c14-479f-9c39-b748adb949dc@j19g2000yqk.googlegroups.com> <7xd44miqe0.fsf@ruckus.brouhaha.com> Message-ID: <6bc6d8fa-778c-4f75-a795-8d96200da7ee@a32g2000yqm.googlegroups.com> On Oct 16, 11:41?pm, Paul Rubin wrote: > Mensanator writes: > > Thanks, but what I wanted was > > ['00','1','0','1','0','1111','00','1','0' '1']. > > ? ? >>> c = '001010111100101' > ? ? >>> list(''.join(g) for k,g in groupby(c)) > ? ? ['00', '1', '0', '1', '0', '1111', '00', '1', '0', '1'] > > is really not that unnatural. I thought someone else had suggested this solution earlier. Oh yeah, some guy named Paul Rubin. At first, I thought I needed to keep the 1's and 0's in seperate lists, but now that I see this example, I'm rethinking that. Thanks, and thanks to Paul Rubin. From steve at REMOVE-THIS-cybersource.com.au Sat Oct 17 04:12:08 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 17 Oct 2009 08:12:08 GMT Subject: How about adding slice notation to iterators/generators? References: Message-ID: <0063005a$0$26941$c3e8da3@news.astraweb.com> On Thu, 15 Oct 2009 18:29:51 -0700, Eloff wrote: > I was just working with a generator for a tree that I wanted to skip the > first result (root node.) > > And it occurs to me, why do we need to do: > > import sys > from itertools import islice > > my_iter = islice(my_iter, 1, sys.maxint) > > When we could simply add slice operations to generators? > > for x in my_iter[1:]: > pass Feel free to extend the iterator protocol to your own iterators, but it is a deliberately simple protocol. Many general iterators *can't* provide random access to slices, at least not without a horrible amount of work. Slicing support was dropped from xrange() years ago because it was surprisingly difficult to get all the odd corner cases correct. Now imagine trying to efficiently support this: def digits_of_pi(): """Yield the decimal digits of pi.""" # left as an exercise for the reader digits_of_pi()[99000:12309988234:7901] Or this: def spider(url): """Follow hyperlinks from url, yielding the contents of each page.""" # also left as an exercise for the reader spider()[999:154:-7] If you want to support slicing, go right ahead, but please, I beg you, don't force me to support it in my iterators! -- Steven From mensanator at aol.com Sat Oct 17 04:12:10 2009 From: mensanator at aol.com (Mensanator) Date: Sat, 17 Oct 2009 01:12:10 -0700 (PDT) Subject: Pretty Print Python Data Structures to HTML References: <7d032aee-4faf-4dcf-9934-f5a02a01ced5@a32g2000yqm.googlegroups.com> Message-ID: <62e1790c-8dd1-4140-858e-81da5dbb5463@k33g2000yqa.googlegroups.com> On Oct 17, 2:25?am, Sam wrote: > I'm looking for something like the pprint module but that can take a > Python data structure and turn it into HTML (or better xHTML). > > It would read through the data structure but instead of formatting it > using spaces it would format it using either dd/dt or ordered/ > unordered lists or even as a html table. > > Have I explained this well enough that you can understand what I > mean? ? Not sure. Have you tried using tags with regular Python string formatting? > Is such a library available anywhere? > > Thanks From catalinfest at gmail.com Sat Oct 17 04:21:23 2009 From: catalinfest at gmail.com (catalinfest at gmail.com) Date: Sat, 17 Oct 2009 01:21:23 -0700 (PDT) Subject: How to schedule system calls with Python References: <6c9196db-5a63-4cf5-96a6-fb83c412f2ff@g3g2000vbr.googlegroups.com> Message-ID: <0cd7cb80-887e-49cd-af91-321cd122c97f@b15g2000yqd.googlegroups.com> And if you have this commands :alias ,ll ? This commands not working , see "sh: ll: command not found" and for "watch" and "top" need to use something to out from loops . On Oct 15, 11:15?pm, TerryP wrote: > On Oct 15, 7:42?pm, Jeremy wrote: > > > I need to write a Python script that will call some command line > > programs (using os.system). ?I will have many such calls, but I want > > to control when the calls are made. ?I won't know in advance how long > > each program will run and I don't want to have 10 programs running > > when I only have one or two processors. ?I want to run one at a time > > (or two if I have two processors), wait until it's finished, and then > > call the next one. > > > How can I use Python to schedule these commands? > > > Thanks, > > Jeremy > > External programs are not system calls; external programs are invoked > through system calls; for example system() is a function call which > when implemented under UNIX systems invokes some form of fork() and > exec(), and likely spawn() under Windows NT. > > If you want simple sequenceal execution of external programs, use a > suitable blocking function to execute them (like system) combined with > a simple loop over the sequence of commands to run. > > for prog in ['cmd1', 'cmd2', 'cmd3']: > ? ? os.system(prog) > > blah. > > For anything more detailed (or complex) in response, try being more > detailed yourself ;). From deets at nospam.web.de Sat Oct 17 04:28:20 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sat, 17 Oct 2009 10:28:20 +0200 Subject: Pretty Print Python Data Structures to HTML In-Reply-To: <7d032aee-4faf-4dcf-9934-f5a02a01ced5@a32g2000yqm.googlegroups.com> References: <7d032aee-4faf-4dcf-9934-f5a02a01ced5@a32g2000yqm.googlegroups.com> Message-ID: <7jtdh4F375rijU1@mid.uni-berlin.de> Sam schrieb: > I'm looking for something like the pprint module but that can take a > Python data structure and turn it into HTML (or better xHTML). > > It would read through the data structure but instead of formatting it > using spaces it would format it using either dd/dt or ordered/ > unordered lists or even as a html table. > > Have I explained this well enough that you can understand what I > mean? Is such a library available anywhere? pprint + pygments should do the job. Diez From marek.rocki at wp.pl Sat Oct 17 04:43:45 2009 From: marek.rocki at wp.pl (marek.rocki at wp.pl) Date: Sat, 17 Oct 2009 01:43:45 -0700 (PDT) Subject: zipfile extracting png files corrupt References: <4639c9e8-13dc-4b23-95bd-57abb3ea2b20@u36g2000prn.googlegroups.com> Message-ID: <9ffb307f-1214-4913-885e-4f37cdd1780c@d10g2000yqh.googlegroups.com> Have you tried opening the zip file in binary mode? Regards, Marek From stef.mientki at gmail.com Sat Oct 17 04:48:07 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Sat, 17 Oct 2009 10:48:07 +0200 Subject: how to write a unicode string to a file ? In-Reply-To: <7a9c25c20910161749j38d7a0ebx15e28d3dfab74892@mail.gmail.com> References: <4AD7B3B2.2040208@gmail.com> <7a9c25c20910151659k5fa15ff8s111cb8bf8b3affb2@mail.gmail.com> <4AD90ADD.4060407@gmail.com> <7a9c25c20910161749j38d7a0ebx15e28d3dfab74892@mail.gmail.com> Message-ID: <4AD984C7.3040209@gmail.com> Stephen Hansen wrote: although this is a very good explanation, and showing character encoding isn't that easy ;-) thanks very much ! Wasn't aware of the SQLite pragma. also thanks to the others who replied. cheers, Stef From a.dexterlab at gmail.com Sat Oct 17 04:58:14 2009 From: a.dexterlab at gmail.com (Samir Alukovic@work) Date: Sat, 17 Oct 2009 01:58:14 -0700 (PDT) Subject: py2exe and croatian letters Message-ID: I am making a simple program in Croatian. In the beginning I set "# - *- coding: cp1250 -*-" code and when i run it in Python shell it comes out fine, but when i compile it with py2exe he doesn't print out croatian letters but he prints out tottaly other letters. I checked the CMD and it supports croatian letters. If you want an source code and setup file send me an e-mail at a.dexterlab at gmail.com From lists at cheimes.de Sat Oct 17 05:14:48 2009 From: lists at cheimes.de (Christian Heimes) Date: Sat, 17 Oct 2009 11:14:48 +0200 Subject: restriction on sum: intentional bug? In-Reply-To: References: <7a9c25c20910160900p35600587v164b1c55c64718f1@mail.gmail.com> <4AD8A67C.2000508@tim.thechases.com> <7a9c25c20910161031q67342767pd744d455714f00aa@mail.gmail.com> Message-ID: Alan G Isaac wrote: > On 10/16/2009 5:03 PM, Christian Heimes wrote: >> It's not going to happen. > > That's a prediction, not a justification. It's not a prediction, it's a statement. It's not going to happend because it violates Guido's gut feeling as well as the Zen of Python. From python.list at tim.thechases.com Sat Oct 17 05:15:55 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Sat, 17 Oct 2009 04:15:55 -0500 Subject: restriction on sum: intentional misfeature? In-Reply-To: <18a95057-b42e-4e4a-9f8e-b82d231de424@v37g2000prg.googlegroups.com> References: <7a9c25c20910160900p35600587v164b1c55c64718f1@mail.gmail.com> <4AD8A67C.2000508@tim.thechases.com> <7a9c25c20910161031q67342767pd744d455714f00aa@mail.gmail.com> <18a95057-b42e-4e4a-9f8e-b82d231de424@v37g2000prg.googlegroups.com> Message-ID: <4AD98B4B.1020608@tim.thechases.com> Carl Banks wrote: > On Oct 16, 12:40 pm, Tim Chase wrote: >> Then I'm fine with sum() being smart enough to recognize this >> horrid case and do the "right" thing by returning ''.join() >> instead. > > You don't want Python to get into this business. Trust me. Just > don't go there. Well python is already in this business of special cases -- it trys to be smart about a dumb operation by raising an error. Just call __add__ ... if it's slow, that's my problem as a programmer. Python doesn't complain about lists, which Steven points out Steven D'Aprano wrote: >And indeed, if you pass a list-of-lists to sum(), it >does: > >>>> >>> sum([[1,2], ['a',None], [1,'b']], []) >[1, 2, 'a', None, 1, 'b'] > >(For the record, summing lists is O(N**2), and unlike >strings, there's no optimization in CPython to avoid the >slow behaviour.) which is also slow. By your own words (from a subsequent email) > If, say, you were to accept that Python is going to guard against a > small number of especially bad cases, this has got to be one of the > top candidates. In guarding, you can do the intended thing (for strings, that's concatenation as the "+" operator does which can be optimized with ''.join()), or you can raise an error. I don't see how using ''.join() is much different from being smart enough to raise an error, except it doesn't break user expectations. > If you want sum to call ''.join transparently, then "".join would have > to produce identical results to what sum() would have produced in all > cases. That does not happen. If an object within the list defines > both __str__ and __add__ methods, then "".join will call __str__, > whereas sum would call __add__, leading to potentially different > results. Therefore, transparently substituting a call to "".join is > not an option. AFAICT, "".join() does not call __str__ on its elements: >>> ''.join(['hello', 42, 'world']) Traceback (most recent call last): File "", line 1, in TypeError: sequence item 1: expected string, int found >>> '__str__' in dir(42) True which is exactly what I'd expect from >>> 'hello' + 42 + "world" Traceback (most recent call last): File "", line 1, in TypeError: cannot concatenate 'str' and 'int' objects This is under 2.x (I don't have 3.x on hand to see if that changed unexpectedly) > It'd be better to just remove the special case. I'd be happy with either solution for summing strings. Be slow or be fast, but don't be erroneous. -tkc From python.list at tim.thechases.com Sat Oct 17 05:28:36 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Sat, 17 Oct 2009 04:28:36 -0500 Subject: restriction on sum: intentional bug? In-Reply-To: References: <7a9c25c20910160900p35600587v164b1c55c64718f1@mail.gmail.com> <4AD8A67C.2000508@tim.thechases.com> <7a9c25c20910161031q67342767pd744d455714f00aa@mail.gmail.com> Message-ID: <4AD98E44.9090001@tim.thechases.com> Christian Heimes wrote: > Alan G Isaac wrote: >> On 10/16/2009 5:03 PM, Christian Heimes wrote: >>> It's not going to happen. >> That's a prediction, not a justification. > > It's not a prediction, it's a statement. It's not going to happend > because it violates Guido's gut feeling as well as the Zen of Python. I'm not sure how it violates ZoP...particularly Simple is better than complex. [complex is special casing or explaining why there's an error] Special cases aren't special enough to break the rules. [raising an error is a special case] Although practicality beats purity. In the face of ambiguity, refuse the temptation to guess. [there's nothing ambiguous about sum() meaning "+"] If the implementation is easy to explain, it may be a good idea. [sum(lst_of_strs) should do exactly what it says..."+" 'em] -tkc From bettini at dsi.unifi.it Sat Oct 17 05:53:18 2009 From: bettini at dsi.unifi.it (Lorenzo Bettini) Date: Sat, 17 Oct 2009 11:53:18 +0200 Subject: CALL FOR PAPERS: TOOLS EUROPE 2010 Message-ID: <4ad9940b$1@news.x-privat.org> ========================================================================== CALL FOR PAPERS (Deadline: January 22, 2010) TOOLS EUROPE 2010 48th International Conference Objects, Models, Components, Patterns Co-located with *** International Conference on Model Transformation (ICMT 2010) *** *** International Conference on Software Composition (SC 2010) *** *** International Conference on Tests and Proofs (TAP 2010) *** M?laga - Spain, 28 June - 02 July 2010 http://malaga2010.lcc.uma.es/ ========================================================================== TOOLS EUROPE is devoted to the combination of technologies that have emerged as a result of object technology becoming "mainstream". Like its predecessors, TOOLS EUROPE combines an emphasis on quality with a strong practical focus. Started in 1989, TOOLS conferences, held in Europe, the USA, Australia, China and Eastern Europe, have played a major role in the development of object technology; many of seminal concepts were first presented at TOOLS. After an interruption of four years, the conference was revived in 2007 to reflect the maturing of the field and the new challenges ahead and has become a yearly event. Contributions are solicited on all aspects of object technology and related fields, in particular model-based development, component-based development, and patterns (design, analysis and other applications); more generally, any contribution addressing topics in advanced software technology fall within the scope of TOOLS. Reflecting the practical emphasis of TOOLS, contributions showcasing applications along with a sound conceptual contribution are particularly welcome. Topics include: * Object technology, including programming techniques, languages, tools * Testing of object-oriented systems * Patterns, pattern languages, tool support for patterns * Distributed and concurrent object systems * Real-time object-oriented programming and design * Experience reports, including efforts at standardisation * Applications to safety- and security-related software * Component-based programming, modelling, tools * Aspects and aspect-oriented programming and modelling * Frameworks for component-based development * Trusted and reliable components * Model-driven development and Model-Driven Architecture * Domain specific languages and language design * Tools and frameworks for supporting model-driven development * Language implementation techniques, compilers, run-time systems * Practical applications of program verification and analysis * Open source solutions & Reproduction studies All contributions will be subject to a rigorous selection process by the international Program Committee, with a stress on originality, practicality and overall quality. The proceedings will be published in Springer LNBIP. For detailed submission information see the conference page. Important Dates: Papers submission deadline: January 22, 2010 Acceptance notification: March 24, 2010 Camera-ready final copy: April 5, 2010 Conference: June 28 -- July 02, 2010 Conference Chair: Bertrand Meyer, ETH Z?rich and Eiffel Software Program Chair: Jan Vitek, Purdue University Publicity Chair: Osmar Santos, University of York Program Committee: Uwe Assman, University of Dresden, Germany Elisa Baniassad, Chinese University of Hong Kong, Hong Kong Alexandre Bergel, University of Chile, Chile Lorenzo Bettini, University of Torino, Italy Judith Bishop, University of Pretoria, South Africa William Cook, University of Texas Austin, USA Sophia Drossopolou, Imperial College London, UK Catherine Dubois, ENSIIE, France St?phane Ducasse, INRIA Lille, France Manuel Fahndrich, Microsoft Research, USA Harald Gall, University of Zurich, Switzerland Benoit Garbinato, University of Lausanne, Switzerland Angelo Gargantini, University of Bergamo, Italy Jeff Gray, University of Alabama Birmingham, USA Kathryn Gray, University of Cambridge, UK Thomas Gschwind, IBM Research, Switzerland Matthias Hauswith, University of Lugano, Switzerland Nigel Horspool, University of Victoria, Canada Tomas Kalibera, Charles University, Czech Republic Gerti Kappel, Vienna University of Technology, Austria Doug Lea, State University of New York Oswego, USA Shane Markstrum, Brucknell University, USA Peter M?ller, ETH Zurich, Switzerland Oscar Nierstrasz, University of Bern, Switzerland James Noble, Victoria University of Wellington, New Zealand Nate Nystrom, University of Texas Arlington, USA Manuel Oriol, University of York, UK Jonathan Ostroff, York University, Canada Richard Paige, University of York, UK Shaz Qadeer, Microsoft Research, USA Awais Rashid, Lancaster University, UK Vivek Sarkar, Rice University, USA Doug Schmidt, Vanderbilt University, USA Manuel Serrano, INRIA Sophia Antipolis, France Peter Thiemann, University of Freiburg, Germany Dave Thomas, Bedarra Research Labs, Canada Laurence Tratt, Bournemouth University, UK Mandana Vaziri, IBM Research, USA Tian Zhao, University of Wisconsin-Milwaukee, USA From gervaz at gmail.com Sat Oct 17 06:30:32 2009 From: gervaz at gmail.com (mattia) Date: 17 Oct 2009 10:30:32 GMT Subject: print() References: <4ad8dfc8$0$1112$4fafbaef@reader2.news.tin.it> <4AD93C7A.5030507@ieee.org> Message-ID: <4ad99cc8$0$1093$4fafbaef@reader3.news.tin.it> Il Fri, 16 Oct 2009 22:40:34 -0700, Dennis Lee Bieber ha scritto: > On Fri, 16 Oct 2009 23:39:38 -0400, Dave Angel > declaimed the following in gmane.comp.python.general: > > >> You're presumably testing this in the interpreter, which prints extra >> stuff. In particular, it prints the result value of any expressions >> entered at the interpreter prompt. So if you type >> >> sys.stdout.write("hello") >> >> then after the write() method is done, the return value of the method >> (5) will get printed by the interpreter. >> > I was about to respond that way myself, but before doing so I wanted > to produce an example in the interpreter window... But no effect? > > C:\Documents and Settings\Dennis Lee Bieber>python ActivePython 2.5.2.2 > (ActiveState Software Inc.) based on Python 2.5.2 (r252:60911, Mar 27 > 2008, 17:57:18) [MSC v.1310 32 bit (Intel)] on win32 > Type "help", "copyright", "credits" or "license" for more information. >>>> import sys >>>> sys.stdout.write("hello") > hello>>> > > > PythonWin 2.5.2 (r252:60911, Mar 27 2008, 17:57:18) [MSC v.1310 32 bit > (Intel)] on win32. > Portions Copyright 1994-2006 Mark Hammond - see 'Help/About PythonWin' > for further copyright information. >>>> import sys >>>> sys.stdout.write("This is a test") > This is a test >>>> >>>> print sys.stdout.write("Hello") > HelloNone >>>> >>>> > > No count shows up... neither PythonWin or Windows command line/ shell Indeed I'm using py3. But now everythong is fine. Everything I just wanted to know was just to run this simple script (I've also sent the msg 'putchar(8)' to the newsgroup): import time import sys val = ("|", "/", "-", "\\", "|", "/", "-", "\\") for i in range(100+1): print("=", end="") # print("| ", end="") print(val[i%len(val)], " ", sep="", end="") print(i, "%", sep="", end="") sys.stdout.flush() time.sleep(0.1) if i > 9: print("\x08"*5, " "*5, "\x08"*5, sep="", end="") else: print("\x08"*4, " "*4, "\x08"*4, sep="", end="") print(" 100%\nDownload complete!") From koranthala at gmail.com Sat Oct 17 06:46:30 2009 From: koranthala at gmail.com (koranthala) Date: Sat, 17 Oct 2009 03:46:30 -0700 (PDT) Subject: sqlite question References: <2bc8095b-c851-472a-8c6f-25a18dbf7ab5@f16g2000yqm.googlegroups.com> Message-ID: <77781d3f-a017-4fb5-a8e6-8f3f1e0d5158@g19g2000yqo.googlegroups.com> On Oct 17, 12:18?pm, Roger Binns wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Dennis Lee Bieber wrote: > > ? ?I suspect, besides building an sqlite3.dll (if Windows), you might > > have to modify the pysqlite DB-API adapter to support whatever new > > arguments have been added to various calls (most like the db.connect() > > parameters have changed to require an encryption password) > > The extension requires an extra C api call after the database is opened to > set the encryption key. ?(There is also another API to change the key.) > > This means that it is not possible for the same pysqlite to work against > SQLite built with and without the encryption extension. ?The changes needed > in pysqlite are a simple matter of programming although you'll want them > incorporated back into the core so you don't have to maintain them (wrapped > in some sort of ifdef). > > Roger > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.9 (GNU/Linux) > Comment: Using GnuPG with Mozilla -http://enigmail.mozdev.org > > iEYEARECAAYFAkrZb6sACgkQmOOfHg372QRSYwCg2J/YSvkqLs8EJ1iJkE2wnbaY > nl0AoKYxgMEnBQNjDyYHv1xWC0Tia74U > =eA2B > -----END PGP SIGNATURE----- One problem is that pysqlite is written in C, and I have no knowledge of C whatsoever. I will try to incorporate the changes mentioned, but I am not sure now at all :-( K From pavlovevidence at gmail.com Sat Oct 17 06:46:32 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Sat, 17 Oct 2009 03:46:32 -0700 (PDT) Subject: restriction on sum: intentional misfeature? References: <7a9c25c20910160900p35600587v164b1c55c64718f1@mail.gmail.com> <4AD8A67C.2000508@tim.thechases.com> <7a9c25c20910161031q67342767pd744d455714f00aa@mail.gmail.com> <18a95057-b42e-4e4a-9f8e-b82d231de424@v37g2000prg.googlegroups.com> Message-ID: <4687a7f3-f1a9-4f31-80f3-eb9f39dbe932@h40g2000prf.googlegroups.com> On Oct 17, 2:15?am, Tim Chase wrote: > Carl Banks wrote: > > On Oct 16, 12:40 pm, Tim Chase wrote: > >> Then I'm fine with sum() being smart enough to recognize this > >> horrid case and do the "right" thing by returning ''.join() > >> instead. > > > You don't want Python to get into this business. ?Trust me. ?Just > > don't go there. > > Well python is already in this business of special cases -- it > trys to be smart about a dumb operation by raising an error. Which is irrelevant to what I was saying. I followed up to the suggestion to transparently replace sum() with "".join, but it's not the special-casing, per se, that's the issue. It's micro=optimizing special cases at run time that I have a problem with and where Python should never, ever go. If Python starts doing stuff like that, everyone and their mother is going to want Python to do the same thing for their own pet performance bottlenecks. People will post here whining that sum() optimizing strings, why doesn't it optimize lists. Let Python optimize the general case, not the specific case. I am fine with other kinds of special-case behaviors, like throwing an exception for an particularly bad argument. That's is merely something you want to avoid in general. Let's sum up: Special-casing to raise an error, avoid in general. Special-casing to mirco-optimize stuff, don't go there. Now, if you want to argue that lots of special-case optimization do occur in Python, or that they dsn't but should, be my guest. At least you'll be answering my actual objection. > AFAICT, "".join() does not call __str__ on its elements: You are correct. Withdrawn. Perhaps "".join does do the same thing in all non-pathological cases as sum. In which case I'd upgrade this proposal from "not an option" to "a very, very bad option". > > It'd be better to just remove the special case. > > I'd be happy with either solution for summing strings. ?Be slow > or be fast, but don't be erroneous. It's not erroneous. It does exactly what it's documented to do. The options should be "slow", "fast", and (in your opinion) "poor design choice". Carl Banks From davea at ieee.org Sat Oct 17 06:48:10 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 17 Oct 2009 06:48:10 -0400 Subject: putchar(8) In-Reply-To: <4AD93849.3020704@ieee.org> References: <1255720658.9153.1385.camel@willow> <4AD93849.3020704@ieee.org> Message-ID: <4AD9A0EA.4090806@ieee.org> Dave Angel wrote: > > Jason Tackaberry wrote: >> On Fri, 2009-10-16 at 12:01 -0700, gervaz wrote: >> >>> Hi all, is there in python the equivalent of the C function int putchar >>> (int c)? I need to print putchar(8). >>> >> >> >>>>> print '\x08' >>>>> >> >> or: >> >> >>>>> print chr(8) >>>>> >> >> >> >> > If I recall correctly, putchar() takes an int value 0-255 and outputs > a single character to stdout. So the equivalent would be: > > sys.stdout.write(char(c)) > > print does other stuff, which you presumably do not want. > > DaveA > > > Oops. Instead of char(), I meant to type chr(). sys.stdout.write(chr(c)) chr() is a built-in that converts an integer to a single character. DaveA From gervaz at gmail.com Sat Oct 17 06:53:11 2009 From: gervaz at gmail.com (mattia) Date: 17 Oct 2009 10:53:11 GMT Subject: putchar(8) References: <1255720658.9153.1385.camel@willow> <4AD93849.3020704@ieee.org> Message-ID: <4ad9a216$0$1093$4fafbaef@reader3.news.tin.it> Il Sat, 17 Oct 2009 06:48:10 -0400, Dave Angel ha scritto: > Dave Angel wrote: >> >> Jason Tackaberry wrote: >>> On Fri, 2009-10-16 at 12:01 -0700, gervaz wrote: >>> >>>> Hi all, is there in python the equivalent of the C function int >>>> putchar (int c)? I need to print putchar(8). >>>> >>>> >>> >>>>>> print '\x08' >>>>>> >>>>>> >>> or: >>> >>> >>>>>> print chr(8) >>>>>> >>>>>> >>> >>> >>> >> If I recall correctly, putchar() takes an int value 0-255 and outputs a >> single character to stdout. So the equivalent would be: >> >> sys.stdout.write(char(c)) >> >> print does other stuff, which you presumably do not want. >> >> DaveA >> >> >> > Oops. Instead of char(), I meant to type chr(). > > > sys.stdout.write(chr(c)) > > chr() is a built-in that converts an integer to a single character. > > DaveA Yes, noticed ;-) From gervaz at gmail.com Sat Oct 17 07:01:35 2009 From: gervaz at gmail.com (mattia) Date: 17 Oct 2009 11:01:35 GMT Subject: print() References: <4ad8dfc8$0$1112$4fafbaef@reader2.news.tin.it> Message-ID: <4ad9a40e$0$1094$4fafbaef@reader3.news.tin.it> Il Fri, 16 Oct 2009 21:04:08 +0000, mattia ha scritto: > Is there a way to print to an unbuffered output (like stdout)? I've seen > that something like sys.stdout.write("hello") works but it also prints > the number of characters! Another question (always py3). How can I print only the first number after the comma of a division? e.g. print(8/3) --> 2.66666666667 I just want 2.6 (or 2.66) Thanks, Mattia From davea at ieee.org Sat Oct 17 07:03:04 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 17 Oct 2009 07:03:04 -0400 Subject: print() In-Reply-To: References: <4ad8dfc8$0$1112$4fafbaef@reader2.news.tin.it> Message-ID: <4AD9A468.80507@ieee.org> Grant Edwards wrote: > On 2009-10-17, Dave Angel wrote: > > > Except sys.stdout.write("hello") doesn't return 5. It returns > None. > > I don't know what the OP is talking about when he says "prints > the number of characters": > > You're right of course, I should have just tried it. I must have been thinking of read(), or maybe write() in some other language. But I know I've seen some problem like this, where the value the interpreter printed was mixed with what the user wanted. > > >> Either put the statement in a real script, or do the following trick to >> convince yourself: >> >> dummy = sys.stdout.write("hello") >> > > I don't see why the assignment is needed. > > Only if write() had returned a value other than None, which is of course not how it works, nor how it's documented. From pavlovevidence at gmail.com Sat Oct 17 07:06:02 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Sat, 17 Oct 2009 04:06:02 -0700 (PDT) Subject: restriction on sum: intentional bug? References: Message-ID: On Oct 16, 7:41?pm, Alan G Isaac wrote: > > Alan G Isaac ?gmail.com> ?writes: > >> So of course join is better, as originally noted, > >> but that does not constitute a reason to intentionally > >> violate duck typing. > > On 10/16/2009 1:03 PM, Benjamin Peterson wrote: > > > As Stephen pointed out, duck typing is not an absolute. > > I do not recall anyone suggesting it was. > Do you? ?If so, cite them. I don't recall anyone saying it either, however I'll go on record as saying it. Duck typing only helps when you don't know what kind of object you're getting. In practice, there isn't much useful code you can write that contains the call sum(x), but where you don't know whether x is a list of strings or a list of numbers. So you usually do know if your list contains strings, and if you know you have a list of strings you can just call "".join. (You might argue that you could write useful code that doesn't know whether x is a list of strings or a list of lists. True, but you shouldn't be calling sum() on lists or lists, either.) What I'm basically saying here is, by shutting out strings from sum, you don't really lose much in terms of duck typing, because duck typing wouldn't have been that helpful anyway. Carl Banks From davea at ieee.org Sat Oct 17 07:24:46 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 17 Oct 2009 07:24:46 -0400 Subject: zipfile extracting png files corrupt In-Reply-To: <4639c9e8-13dc-4b23-95bd-57abb3ea2b20@u36g2000prn.googlegroups.com> References: <4639c9e8-13dc-4b23-95bd-57abb3ea2b20@u36g2000prn.googlegroups.com> Message-ID: <4AD9A97E.1070608@ieee.org> marks542004 at yahoo.com wrote: > Hi, I am using the zipfile in Python 2.4 to extract files from > existing zips. > It appears to work but the files extracted are corrupt. > Have you done any analysis to see in what sense they are corrupt? For example, do text files work, but not binary ones? If the file fragment is small, are you getting a zero-length file out? Is the filesize always wrong? > Here is my code : > > import zipfile > import os, sys , shutil > > epath = "c:/ziptest/" > fil = "J:/archives/xzips/duplicates/gothicfence_1470.zip" > ferr = file((epath + "/errlog.txt"),"w") > print "Extracting to ",epath > try: > if zipfile.is_zipfile(fil): > z = zipfile.ZipFile(fil,"r") > nmes = z.namelist() > for i in nmes: > fn = os.path.split(i)[1] > print "...",fn > dta = z.read(i) > You read the data here, but don't use it. So the subsequent read below presumably misses this first part of the data. > if len(dta) >0 : > enam = epath + fn > fo = file(enam,"wb") > fo.write(z.read(i)) > fo.flush() > fo.close() > except: > myerr = fil +"," + str(sys.exc_info()[1]) +"\n" > ferr.write(myerr) > ferr.close() > > any ideas ? thanks > > In addition, I don't know if Zipfile.read() might return only a portion of the file. If so, you'll need to make a loop instead of a simple if in that final fragment. From rylesny at gmail.com Sat Oct 17 08:00:24 2009 From: rylesny at gmail.com (ryles) Date: Sat, 17 Oct 2009 05:00:24 -0700 (PDT) Subject: Installation question 2.5.4 References: Message-ID: <27d8850b-df32-49cd-a52c-6df870cdc80d@37g2000yqm.googlegroups.com> On Oct 17, 2:22?am, JimR wrote: > I completed the configure, the make and the make install. ?However, the > last instruction in README is to perform > pkgmanager -a /usr/python > > As you may have guessed, Redhat does not have pkgmanager. ?What can I do > to work-around this problem and get this list running? Don't worry, Jim, you don't need this. It looks like you're reading from an irrelevant part of the README. Here's the only part you typically need: ------------------------------ Congratulations on getting this far. :-) To start building right away (on UNIX): type "./configure" in the current directory and when it finishes, type "make". This creates an executable "./python"; to install in /usr/local, first do "su root" and then "make install". The section `Build instructions' below is still recommended reading. ------------------------------ I suspect you may have actually set your 'prefix' to /usr/python the first time around. If that's the case, just remember to remove that directory, since I doubt you'll want anything housed there. The default (/usr/local) is usually fine. That's freely changeable with: ./configure --prefix=/my/prefix Building from source can be a pain, because you may find that several Python modules are not successfully built (it will tell you all about this after 'make' completes). This is usually the result of various 'devel' packages (RPMs) not being installed. If this is the case, you might have better luck not building python yourself, and just installing a newer RPM for it. Hope this helps. Good luck. From digitig at gmail.com Sat Oct 17 08:12:52 2009 From: digitig at gmail.com (Tim Rowe) Date: Sat, 17 Oct 2009 13:12:52 +0100 Subject: The rap against "while True:" loops In-Reply-To: <0062fc72$0$26941$c3e8da3@news.astraweb.com> References: <01ccc46d-5ea9-4dfe-ba22-699c6b859f00@v36g2000yqv.googlegroups.com> <4cdf9c03-d8e1-406c-b960-1a5b535f9430@m38g2000yqd.googlegroups.com> <005fde84$0$26920$c3e8da3@news.astraweb.com> <0062fc72$0$26941$c3e8da3@news.astraweb.com> Message-ID: 2009/10/17 Steven D'Aprano : > No, you have a fundamental misunderstanding. They're called exceptions, > not errors, because they represent exceptional cases. Often errors are > exceptional cases, but they're not the only sort of exceptional case. The whole reason for the mechanism, across all languages that have it, is to deal with situations that you don't know how to deal with locally. That's why they have the overhead that they do. > Python uses exceptions for flow control: Yes, and in some cases I think that's a serious language wart. Not enough to put me off the language, but a serious wart nevertheless. > Similarly, it's hardly an *error* for [1, 2, 3].index(5) to fail -- who > is to say that the list is supposed to have 5 in it? ValueError (a > slightly misleading name in this situation) is used to indicate an > exceptional, but not unexpected, occurrence. That one is, I think, a legitimate use of an exception. The result returned by index is defined if the index is in bounds. If not, index doesn't know whether it was supposed to be in bounds or not, and so can't handle the case locally. It could suggest an error or merely (IMHO) poor programming. Because index cannot know what the proper action is, an exception is the appropriate response. > Likewise, KeyboardInterrupt is used to allow the user to halt processing; > SystemExit is used to shut down the Python virtual machine; and warnings > are implemented using exceptions. Again, I think it's fair to treat a program being killed from outside as an exception as far as the program is concerned. They're not things the program has brought about, and the original handling code has no way of knowinng what response is appropriate. Same with warnings; they *probably* shouldn't happen but only the application programmer can know whether they should or not. The point is that an exception causes a change in program flow, so of course they're used for flow control. It's what they do. The question is in what cases it's appropriate to use them. -- Tim Rowe From deets at nospam.web.de Sat Oct 17 08:49:53 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sat, 17 Oct 2009 14:49:53 +0200 Subject: using cx_freeze on linux In-Reply-To: References: Message-ID: <7jtsrhF36i9bqU1@mid.uni-berlin.de> james27 schrieb: > hi.. > im new to cx_freeze . > i was searched much of time.but i couldn't found solution. > i want to make exe file which can run on windows. > is it possible to make windows exe file by use cx_freeze on linux? > if anyone can help ,much appreciate. No, that's what py2exe *on windows* is for. Diez From xnews2 at fredp.lautre.net Sat Oct 17 08:56:36 2009 From: xnews2 at fredp.lautre.net (Fred Pacquier) Date: 17 Oct 2009 12:56:36 GMT Subject: Tracking down DLL load errors in Windows ? References: Message-ID: "Mark Tolonen" said : > That error message is returned from executables built with later > versions of Visual Studio (at least 2005 and 2008...not sure about > 2003). The Windows application event log will indicate what DLL is > missing. > > In addition to tools others have noted, depends.exe (comes with > VS2005...was removed in VS2008 for unknown reasons) will list the DLLs > required by an executable. Another Visual Studio tool, "dumpbin > /imports " works, too. Silly me, I hadn't even thought of the Windows event log either... thanks much ! fp From highcar at gmail.com Sat Oct 17 08:59:51 2009 From: highcar at gmail.com (elca) Date: Sat, 17 Oct 2009 05:59:51 -0700 (PDT) Subject: using cx_freeze on linux In-Reply-To: <7jtsrhF36i9bqU1@mid.uni-berlin.de> References: <25936152.post@talk.nabble.com> <7jtsrhF36i9bqU1@mid.uni-berlin.de> Message-ID: <25938094.post@talk.nabble.com> thanks diez paul, Diez B. Roggisch-2 wrote: > > james27 schrieb: >> hi.. >> im new to cx_freeze . >> i was searched much of time.but i couldn't found solution. >> i want to make exe file which can run on windows. >> is it possible to make windows exe file by use cx_freeze on linux? >> if anyone can help ,much appreciate. > > No, that's what py2exe *on windows* is for. > > Diez > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/using-cx_freeze-on-linux-tp25936152p25938094.html Sent from the Python - python-list mailing list archive at Nabble.com. From yves at zioup.com Sat Oct 17 09:17:28 2009 From: yves at zioup.com (Yves) Date: Sat, 17 Oct 2009 07:17:28 -0600 Subject: executing a function/method from a variable In-Reply-To: References: Message-ID: Carsten Haese wrote: >> Right now I use an eval, but I'm wondering if there isn't a better way: > > There's (almost) always a better way than using eval. Yes I agree, every time I use eval, I feel like the language is missing something, fair enough to shell type languages, but usually not the case in python. That's why I asked, > In this case, you > should use getattr(). Right ! I always forget about get/set attr. Thanks ! > > Here's your simplified example modified to use getattr: Tried it, and it works if we remove the self from the name of course: import sys class dummy(object): def __init__(self, arg): self.todo = 'print' + arg; def printa(self): print 'a' def printb(self): print 'b' def doit(self): #func = getattr(self, self.todo) #func() getattr(self, self.todo)() o = dummy(sys.argv[1]) o.doit() Thanks again, -- Yves. http://www.SollerS.ca From rsreese at gmail.com Sat Oct 17 09:31:19 2009 From: rsreese at gmail.com (Stephen Reese) Date: Sat, 17 Oct 2009 09:31:19 -0400 Subject: Rename file if it exists. In-Reply-To: <4AD946C4.5040407@ieee.org> References: <7js33pF37i47eU1@mid.uni-berlin.de> <665172f40910161812n3057effbk6fdeeb26d4205279@mail.gmail.com> <4AD946C4.5040407@ieee.org> Message-ID: <665172f40910170631j635d3f7at2bbf28d3bfe896bf@mail.gmail.com> > Clearly, the os.path.isfile() is never returning true, because once it does, > you'll get a string error on the next line. ?You need > > ? ? fn = basename + "." + str(count) > > or something similar. > > Anyway, isfile() needs a complete path, there's no way it can guess what > directory you plan to use. > > To build a complete path, you need something like: > > ? fullname = os.path.join('/var/www/apache2-default/', fn) > > You ought to use that on the actual open as well. ?As long as you're > hard-coding the path prefix, plain string concatenation can work, but > eventually you'll get the string from somewhere else, and what happens if > someone forgets the trailing slash? > Thanks Dave, got it working with the following bit: # strip leading path from file name to avoid directory traversal attacks fn = os.path.basename(fileitem.filename) path = '/var/www/apache2-default/' filepath = path + fn # Include date in filename. filepath = filepath + "_" + datetime.datetime.now().strftime("%Y-%m-%d") count = 0 tmp_fn = filepath + "." + str(count) while os.path.isfile(tmp_fn): count += 1 tmp_fn = filepath + "." + str(count) filepath = tmp_fn # Get the new filename for the users viewing pleasure displaypath, tmp_split = os.path.split(filepath) # Open the file for writing f = open(filepath , 'wb', 10000) From arian.kuschki at googlemail.com Sat Oct 17 09:54:57 2009 From: arian.kuschki at googlemail.com (Arian Kuschki) Date: Sat, 17 Oct 2009 17:24:57 +0330 Subject: umlauts Message-ID: <20091017135457.GA6335@alittleroisin> Hi all this has been bugging me for a long time and I do not seem to be able to understand what to do. I always have problems when dealing input text that contains umlauts. Consider the following: In [1]: import urllib In [2]: f = urllib.urlopen("http://www.google.de/ig/api?weather=Muenchen") In [3]: xml = f.read() In [4]: f.close() In [5]: print xml ------> print(xml) As you can see the umlauts in the XML are not displayed properly. When I want to process this text (for example with xml.sax), I get error messages because the parses can't read this. I've tried to read up on this and there is a lot of information on the web, but nothing seems to work for me. For example setting the coding to UTF like this: # -*- coding: utf-8 -*- or using the decode() string method. I always have this kind of problem when input contains umlauts, not just in this case. My locale (on Ubuntu) is en_GB.UTF-8. Cheers Arian From davea at ieee.org Sat Oct 17 10:02:27 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 17 Oct 2009 10:02:27 -0400 Subject: print() In-Reply-To: <4ad9a40e$0$1094$4fafbaef@reader3.news.tin.it> References: <4ad8dfc8$0$1112$4fafbaef@reader2.news.tin.it> <4ad9a40e$0$1094$4fafbaef@reader3.news.tin.it> Message-ID: <4AD9CE73.2020108@ieee.org> mattia wrote: > Il Fri, 16 Oct 2009 21:04:08 +0000, mattia ha scritto: > > >> Is there a way to print to an unbuffered output (like stdout)? I've seen >> that something like sys.stdout.write("hello") works but it also prints >> the number of characters! >> > > Another question (always py3). How can I print only the first number > after the comma of a division? > e.g. print(8/3) --> 2.66666666667 > I just want 2.6 (or 2.66) > > Thanks, Mattia > > Just as sys.stdout.write() is preferable to print() for your previous question, understanding str.format() is important to having good control over what your output looks like. It's certainly not the only way, but the docs seem to say it's the preferred way in version 3.x It was introduced in 2.6, so there are other approaches you might want if you need to work in 2.5 or earlier. x = 8/3 dummy0=dummy1=dummy2=42 s = "The answer is approx. {3:07.2f} after rounding".format(dummy0, dummy1, dummy2, x) print(s) will print out the following: The answer is approx. 0002.67 after rounding A brief explanation of the format string {3:07.2f} is as follows: 3 selects argument 3 of the function, which is x 0 means to zero-fill the value after conversion 7 means 7 characters total width (this helps determine who many zeroes are inserted) 2 means 2 digits after the decimal f means fixed point format You can generally leave out the parts you don't need, but this gives you lots of control over what things should look like. There are lots of other parts, but this is most of what you might need for controlled printing of floats. The only difference from what you asked is that this rounds, where you seemed (!) to be asking for truncation of the extra columns. If you really need to truncate, I'd recommend using str() to get a string, then use index() to locate the decimal separator, and then slice it yourself. DaveA From benjamin.kaplan at case.edu Sat Oct 17 10:14:09 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Sat, 17 Oct 2009 10:14:09 -0400 Subject: py2exe and croatian letters In-Reply-To: References: Message-ID: On Sat, Oct 17, 2009 at 4:58 AM, Samir Alukovic at work wrote: > I am making a simple program in Croatian. In the beginning I set "# - > *- coding: cp1250 -*-" code and when i run it in Python shell it comes > out fine, but when i compile it with py2exe he doesn't print out > croatian letters but he prints out tottaly other letters. I checked > the CMD and it supports croatian letters. If you want an source code > and setup file send me an e-mail at a.dexterlab at gmail.com > What's the encoding of cmd? Just because it supports Croatian letters doesn't mean it's cp1250. The American version of Windows usually uses cp1252 but cmd is still cp843. -- > http://mail.python.org/mailman/listinfo/python-list > From deets at nospam.web.de Sat Oct 17 10:27:38 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sat, 17 Oct 2009 16:27:38 +0200 Subject: sqlite question In-Reply-To: References: <2bc8095b-c851-472a-8c6f-25a18dbf7ab5@f16g2000yqm.googlegroups.com> Message-ID: <7ju2irF36ovsnU1@mid.uni-berlin.de> Roger Binns schrieb: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Dennis Lee Bieber wrote: >> I suspect, besides building an sqlite3.dll (if Windows), you might >> have to modify the pysqlite DB-API adapter to support whatever new >> arguments have been added to various calls (most like the db.connect() >> parameters have changed to require an encryption password) > > The extension requires an extra C api call after the database is opened to > set the encryption key. (There is also another API to change the key.) > > This means that it is not possible for the same pysqlite to work against > SQLite built with and without the encryption extension. The changes needed > in pysqlite are a simple matter of programming although you'll want them > incorporated back into the core so you don't have to maintain them (wrapped > in some sort of ifdef). It might be possible to wrap the needed call using ctypes, if the DLL/SO can be loaded explicitly. Which it should be I'd say. Diez From aahz at pythoncraft.com Sat Oct 17 10:27:44 2009 From: aahz at pythoncraft.com (Aahz) Date: 17 Oct 2009 07:27:44 -0700 Subject: restriction on sum: intentional bug? References: Message-ID: In article <0062f568$0$26941$c3e8da3 at news.astraweb.com>, Steven D'Aprano wrote: > >Mathematically, sum() is defined as the repeated application of the + >operator. In Python, the + operator is well-defined for strings and lists >as well as numbers. Since you can say "ab" + "cd" + "ef" and get a >sensible result, then sum() should be able to do the same thing. > >And indeed, if you pass a list-of-lists to sum(), it does: > >>>> sum([[1,2], ['a',None], [1,'b']], []) >[1, 2, 'a', None, 1, 'b'] > >(For the record, summing lists is O(N**2), and unlike strings, there's no >optimization in CPython to avoid the slow behaviour.) Are you sure? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "To me vi is Zen. To use vi is to practice zen. Every command is a koan. Profound to the user, unintelligible to the uninitiated. You discover truth everytime you use it." --reddy at lion.austin.ibm.com From aahz at pythoncraft.com Sat Oct 17 10:30:55 2009 From: aahz at pythoncraft.com (Aahz) Date: 17 Oct 2009 07:30:55 -0700 Subject: The rap against "while True:" loops References: <01ccc46d-5ea9-4dfe-ba22-699c6b859f00@v36g2000yqv.googlegroups.com> <0062fc72$0$26941$c3 Message-ID: In article , Tim Rowe wrote: > >The point is that an exception causes a change in program flow, so of >course they're used for flow control. It's what they do. The question >is in what cases it's appropriate to use them. Standard Python idiom: try: d[key] += value except KeyError: d[key] = value Maybe you need to re-think "appropriate". -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "To me vi is Zen. To use vi is to practice zen. Every command is a koan. Profound to the user, unintelligible to the uninitiated. You discover truth everytime you use it." --reddy at lion.austin.ibm.com From davea at ieee.org Sat Oct 17 10:38:55 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 17 Oct 2009 10:38:55 -0400 Subject: print() In-Reply-To: <4ad99cc8$0$1093$4fafbaef@reader3.news.tin.it> References: <4ad8dfc8$0$1112$4fafbaef@reader2.news.tin.it> <4AD93C7A.5030507@ieee.org> <4ad99cc8$0$1093$4fafbaef@reader3.news.tin.it> Message-ID: <4AD9D6FF.1000603@ieee.org> mattia wrote: > Il Fri, 16 Oct 2009 22:40:34 -0700, Dennis Lee Bieber ha scritto: > > >> On Fri, 16 Oct 2009 23:39:38 -0400, Dave Angel >> declaimed the following in gmane.comp.python.general: >> >> >> >>> You're presumably testing this in the interpreter, which prints extra >>> stuff. In particular, it prints the result value of any expressions >>> entered at the interpreter prompt. So if you type >>> >>> sys.stdout.write("hello") >>> >>> then after the write() method is done, the return value of the method >>> (5) will get printed by the interpreter. >>> >>> >> I was about to respond that way myself, but before doing so I >> > wanted > >> to produce an example in the interpreter window... But no effect? >> >> C:\Documents and Settings\Dennis Lee Bieber>python ActivePython 2.5.2.2 >> (ActiveState Software Inc.) based on Python 2.5.2 (r252:60911, Mar 27 >> 2008, 17:57:18) [MSC v.1310 32 bit (Intel)] on win32 >> Type "help", "copyright", "credits" or "license" for more information. >> >>>>> import sys >>>>> sys.stdout.write("hello") >>>>> >> hello>>> >> >> >> PythonWin 2.5.2 (r252:60911, Mar 27 2008, 17:57:18) [MSC v.1310 32 bit >> (Intel)] on win32. >> Portions Copyright 1994-2006 Mark Hammond - see 'Help/About PythonWin' >> for further copyright information. >> >>>>> import sys >>>>> sys.stdout.write("This is a test") >>>>> >> This is a test >> >>>>> print sys.stdout.write("Hello") >>>>> >> HelloNone >> >>>>> >> No count shows up... neither PythonWin or Windows command line/ >> > shell > > Indeed I'm using py3. But now everythong is fine. Everything I just > wanted to know was just to run this simple script (I've also sent the msg > 'putchar(8)' to the newsgroup): > > import time > import sys > > val = ("|", "/", "-", "\\", "|", "/", "-", "\\") > for i in range(100+1): > print("=", end="") > # print("| ", end="") > print(val[i%len(val)], " ", sep="", end="") > print(i, "%", sep="", end="") > sys.stdout.flush() > time.sleep(0.1) > if i > 9: > print("\x08"*5, " "*5, "\x08"*5, sep="", end="") > else: > print("\x08"*4, " "*4, "\x08"*4, sep="", end="") > print(" 100%\nDownload complete!") > > Seems to me you're spending too much energy defeating the things that print() is automatically doing for you. The whole point of write() is that it doesn't do anything but ship your string to the file/device. So if you want control, do your own formatting. Consider: import time, sys, itertools val = ("|", "/", "-", "\\", "|", "/", "-", "\\") sys.stdout.write(" ") pattern = "\x08"*8 + " {0}{1:02d}%" for percentage, string in enumerate(itertools.cycle(val)): if percentage>99 : break paddednum = pattern.format(string, percentage) sys.stdout.write(paddednum) sys.stdout.flush() time.sleep(0.1) print("\x08\x08\x08\x08 100%\nDownload complete!") Note the use of cycle() which effectively repeats a list indefinitely. And enumerate, which makes an index for you automatically when you're iterating through a list. And str.format() that builds our string, including using 0 padding so the percentages are always two digits. DaveA From weasley_wx at sina.com Sat Oct 17 10:40:47 2009 From: weasley_wx at sina.com (StarWing) Date: Sat, 17 Oct 2009 07:40:47 -0700 (PDT) Subject: slicing return iter? Message-ID: hello everyone, I'm new here :-) sometimes I want to iterate a part of a sequence. but don't want to copy it. i.e. a = list(...) # now a is a list, and a[:] is another list, and so a[m:n] # now we do something with the 0~len(a)-3 elements of a for val in a[:-2]: #do something.... but, this will cause a copy on a, has some convenience way to get a iter to iterate on a part of list? i made this: class iterslice: def __init__(self, list): self.list = list def __len__(self): return len(self.list) def __getitem__(self, slice): import itertools listlen = len(self.list) range = (((slice.start + listlen) % listlen) if slice.start else 0, ((slice.stop + listlen) % listlen) if slice.stop else listlen, slice.step) return itertools.islice(self.list, *range) a = [1,2,3,4] for i in iterslice(a)[:-1:2]: print i my question is: - are there any *Standard* way to do this? (a buit-in function? a module?) - are there any better implements? thanks for attention :-) From victorsubervi at gmail.com Sat Oct 17 11:18:17 2009 From: victorsubervi at gmail.com (Victor Subervi) Date: Sat, 17 Oct 2009 11:18:17 -0400 Subject: More & More Fun w/ Pics & MySQL In-Reply-To: References: <4dc0cfea0910161223q326f1306jb24b7d62ab124d30@mail.gmail.com> <4dc0cfea0910161439v4401d1b8h1f0c325bf809251d@mail.gmail.com> <4dc0cfea0910161721v4812822arfcb42743c1359dd7@mail.gmail.com> Message-ID: <4dc0cfea0910170818w37a47580qb49290a5b5546bfa@mail.gmail.com> Let me clarify. This prints out "all sorts of crap", which means an image string, the image as a string, to the screen: print 'Content-type: image/jpeg' print 'Content-Encoding: base64' print print pic().encode('base64') print '' The following once upon a time printed images, but now it doesn't. Why would that be? I would refresh the screen, and it would print. I'd change a line and it wouldn't. I'd change it back to what it was and it would no longer the image to the screen. Why is that? The same happens with or without the base64 stuff. Commenting out a line seemed to make a difference! print 'Content-type: text/plain' #print 'Content-type: image/jpeg' print print pic() print '' The above prints out a broken image of correct dimensions. Of course I try and figure out how things work once they get working. Sometimes, however, there is __no__ logic to it __at__all__. Sorry. After years of struggling with python I've come to realize that even though I may not be a good programmer, it isn't me. It may not be python itself. It may be the crappy hardware the server farms use. It may be the way they tweak their python interpreter. But it isn't just me. It would be nice if I could get this code printing images to the screen as it was before I had to put out another unnecessary fire when code that previously uploaded the images in the first place inexplicably no longer worked. Then, lo and behold, when I came back to this code that I hadn't touched and was working fine earlier this week, it no longer works. Lovely. My "plug-and-play" program has devoured two weeks of my time and I'm still up the creek without a paddle. Sure would appreciate any help you can give. V On Fri, Oct 16, 2009 at 8:54 PM, Carsten Haese wrote: > Victor Subervi wrote: > > I'm sorry. These scripts worked fine before and should have been > > plug-and-play. I have wasted 2 frustrating weeks trying to figure out > > why they don't work only to discover things that make no sense at all > > that do the trick. I thought programming was straight-forward and > > "logical"...boy, am I disappointed. Sorry for the jade. > > Clearly, you're not jaded enough. When my code does things that I don't > understand, I try to understand what's going on. Your approach seems to > be to randomly mutate your code until it works. There is nothing > inherently wrong with that as long as you then try to understand *why* > it works once you get it work. You seem to have omitted this step. Since > you never understood why your code worked, consequently now you don't > understand why your code has stopped working. > > > What I mean is that the code supplied merely posts a broken image, with > > the correct dimensions, interestingly enough. It makes no difference at > > all if one adds the line "print ''" or not. I also believe > > (and hope) my initial post is pretty clear. > > Actually, your original post was not clear at all. Your original post > said "This will print all sorts of crap to the screen." This is not > useful at all. The only reason why I responded at all was because I > noticed the line in your code that clearly didn't have > any business being there. The fact that you even considered the > possibility that that line could help shows just how little you know > about what's going on in your code. > > By the way, you now have changed your story from "random crap" to "a > broken image". Which one is it? > > The only way we have a fighting chance to help you in figuring out > what's going in is if you post the *exact* code you're running (and by > that I mean the actual code that you know your server is executing, and > not just some code that somewhat resembles the code that the server > might be executing), a detailed description of the result you're > expecting, and a detailed description of the result you're getting instead. > > -- > Carsten Haese > http://informixdb.sourceforge.net > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From lie.1296 at gmail.com Sat Oct 17 11:21:11 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 18 Oct 2009 02:21:11 +1100 Subject: print() In-Reply-To: <4ad9a40e$0$1094$4fafbaef@reader3.news.tin.it> References: <4ad8dfc8$0$1112$4fafbaef@reader2.news.tin.it> <4ad9a40e$0$1094$4fafbaef@reader3.news.tin.it> Message-ID: <4ad9e0e6$1@dnews.tpgi.com.au> mattia wrote: > Another question (always py3). How can I print only the first number > after the comma of a division? > e.g. print(8/3) --> 2.66666666667 > I just want 2.6 (or 2.66) Are you sure you don't want that to be 2.7 or 2.67? Then you can use: n = int(n * 10**2) / 10**2 else if 2.7 pr 2.67 is what you wanted, you could use: n = round(n, 2) From carsten.haese at gmail.com Sat Oct 17 11:33:38 2009 From: carsten.haese at gmail.com (Carsten Haese) Date: Sat, 17 Oct 2009 11:33:38 -0400 Subject: More & More Fun w/ Pics & MySQL In-Reply-To: <4dc0cfea0910170818w37a47580qb49290a5b5546bfa@mail.gmail.com> References: <4dc0cfea0910161223q326f1306jb24b7d62ab124d30@mail.gmail.com> <4dc0cfea0910161439v4401d1b8h1f0c325bf809251d@mail.gmail.com> <4dc0cfea0910161721v4812822arfcb42743c1359dd7@mail.gmail.com> <4dc0cfea0910170818w37a47580qb49290a5b5546bfa@mail.gmail.com> Message-ID: Victor Subervi wrote: > Let me clarify. This prints out "all sorts of crap", which means an > image string, the image as a string, to the screen: > > print 'Content-type: image/jpeg' > print 'Content-Encoding: base64' > print > print pic().encode('base64') > print '' This has no hope of working, since you're sending data that's not valid base64 code and pretend that it's base64-encoded. A browser trying to display this result will produce something anywhere between unpredictable and garbage. > The following once upon a time printed images, but now it doesn't. Why > would that be? I would refresh the screen, and it would print. I'd > change a line and it wouldn't. I'd change it back to what it was and it > would no longer the image to the screen. Why is that? The same happens > with or without the base64 stuff. Commenting out a line seemed to make a > difference! Why would turning a comment into a statement NOT make a difference?!? > > print 'Content-type: text/plain' > #print 'Content-type: image/jpeg' > print > print pic() > print '' This should produce plain text, not an image. > The above prints out a broken image of correct dimensions. What the heck is a "broken image of correct dimensions"? Are you looking at the output of the script itself, or are you looking at an html page that uses this script as src-attribute for an img-tag? In the latter case, if you're also giving the img-tag width and height attributes, it shouldn't be surprising that you're getting the correct dimensions, and you're getting a broken image because your src is producing plain text instead of an image. > Of course I try and figure out how things work once they get working. > Sometimes, however, there is __no__ logic to it __at__all__. Sorry. > After years of struggling with python I've come to realize that even > though I may not be a good programmer, it isn't me. It may not be python > itself. It may be the crappy hardware the server farms use. It may be > the way they tweak their python interpreter. But it isn't just me. I suppose it's possible that there is something quirky about the way the server has been set up, but so far, the more likely explanation is that you don't really know what you're doing. You can prove whether it's your server farm or you by running the same code on a different server. > It would be nice if I could get this code printing images to the screen > as it was before I had to put out another unnecessary fire when code > that previously uploaded the images in the first place inexplicably no > longer worked. Then, lo and behold, when I came back to this code that I > hadn't touched and was working fine earlier this week, it no longer > works. Lovely. My "plug-and-play" program has devoured two weeks of my > time and I'm still up the creek without a paddle. Sure would appreciate > any help you can give. Again, you have to help us help you. I have listed the three things you need to post together, and you haven't done that. You're giving us bits and pieces, but nothing that's sufficiently cohesive to do any meaningful troubleshooting on. -- Carsten Haese http://informixdb.sourceforge.net From deets at nospam.web.de Sat Oct 17 11:51:49 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sat, 17 Oct 2009 17:51:49 +0200 Subject: umlauts In-Reply-To: References: Message-ID: <7ju7gmF373tuhU1@mid.uni-berlin.de> Arian Kuschki schrieb: > Hi all > > this has been bugging me for a long time and I do not seem to be able to > understand what to do. I always have problems when dealing input text that > contains umlauts. Consider the following: > > In [1]: import urllib > > In [2]: f = urllib.urlopen("http://www.google.de/ig/api?weather=Muenchen") > > In [3]: xml = f.read() > > In [4]: f.close() > > In [5]: print xml > ------> print(xml) > tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" >> y data="Munich, BY"/> data=""/> data="2009-10-17"/> data="SI"/> umidity data="Feuchtigkeit: 87?%"/> data="/ig/images/weather/mostly_cloudy.gif"/> ent_conditions> data="1"/> data="/ig/images/weather/chance_of_rain.gif"/> data="So."/> data="/ig/images/weather/chance_of_sno > w.gif"/> data="Mo."/> data="Di."/> /> data="Klar"/> > > As you can see the umlauts in the XML are not displayed properly. When I want > to process this text (for example with xml.sax), I get error messages because > the parses can't read this. > > I've tried to read up on this and there is a lot of information on the web, but > nothing seems to work for me. For example setting the coding to UTF like this: > # -*- coding: utf-8 -*- or using the decode() string method. The encoding of the python-source-file has nothing to do with this. It's only relevant for unicode-literals (in python 2.x, that's u"...") > > I always have this kind of problem when input contains umlauts, not just in > this case. My locale (on Ubuntu) is en_GB.UTF-8. If we assume the data on the website is correct (it appears to be when I open it in FF), then your problem is most probably your display/terminal. What does this show you in your interactive interpreter? >>> print "\xc3\xb6" ? For me, it's o-umlaut, ?. This is because the above bytes are the sequence for ? in utf-8. If this shows something else, you need to adjust your terminal settings. Diez From arnodel at googlemail.com Sat Oct 17 11:56:46 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Sat, 17 Oct 2009 08:56:46 -0700 (PDT) Subject: slicing return iter? References: Message-ID: On Oct 17, 3:40?pm, StarWing wrote: > hello everyone, I'm new here :-) > > sometimes I want to iterate a part of a sequence. but don't want to > copy it. i.e. > > a = list(...) > # now a is a list, and a[:] is another list, and so a[m:n] > # now we do something with the 0~len(a)-3 elements of a > for val in a[:-2]: > ? ? #do something.... > > but, this will cause a copy on a, has some convenience way to get a > iter to iterate on a part of list? > > i made this: > class iterslice: > ? ? def __init__(self, list): > ? ? ? ? self.list = list > > ? ? def __len__(self): > ? ? ? ? return len(self.list) > > ? ? def __getitem__(self, slice): > ? ? ? ? import itertools > ? ? ? ? listlen = len(self.list) > ? ? ? ? range = (((slice.start + listlen) % listlen) if slice.start > else 0, > ? ? ? ? ? ? ? ? ((slice.stop + listlen) % listlen) if slice.stop else > listlen, > ? ? ? ? ? ? ? ? slice.step) > ? ? ? ? return itertools.islice(self.list, *range) > > a = [1,2,3,4] > for i in iterslice(a)[:-1:2]: > ? ? print i > > my question is: > ? - are there any *Standard* way to do this? (a buit-in function? a > module?) > ? - are there any better implements? > > thanks for attention :-) Check the itertools module. HTH -- Arnaud From weasley_wx at sina.com Sat Oct 17 12:11:46 2009 From: weasley_wx at sina.com (StarWing) Date: Sat, 17 Oct 2009 09:11:46 -0700 (PDT) Subject: umlauts References: Message-ID: <25ab29ae-eb30-452a-9595-22cb976657e8@f20g2000prn.googlegroups.com> On 10?17?, ??9?54?, Arian Kuschki wrote: > Hi all > > this has been bugging me for a long time and I do not seem to be able to > understand what to do. I always have problems when dealing input text that > contains umlauts. Consider the following: > > In [1]: import urllib > > In [2]: f = urllib.urlopen("http://www.google.de/ig/api?weather=Muenchen") > > In [3]: xml = f.read() > > In [4]: f.close() > > In [5]: print xml > ------> print(xml) > tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0"> > y data="Munich, BY"/> data=""/> data="2009-10-17"/> data="SI"/> umidity data="Feuchtigkeit: 87 %"/> data="/ig/images/weather/mostly_cloudy.gif"/> ent_conditions> data="1"/> data="/ig/images/weather/chance_of_rain.gif"/> data="So."/> data="/ig/images/weather/chance_of_sno > w.gif"/> data="Mo."/> data="Di."/> /> data="Klar"/> > > As you can see the umlauts in the XML are not displayed properly. When I want > to process this text (for example with xml.sax), I get error messages because > the parses can't read this. > > I've tried to read up on this and there is a lot of information on the web, but > nothing seems to work for me. For example setting the coding to UTF like this: > # -*- coding: utf-8 -*- or using the decode() string method. > > I always have this kind of problem when input contains umlauts, not just in > this case. My locale (on Ubuntu) is en_GB.UTF-8. > > Cheers > Arian try this? # vim: set fencoding=utf-8: import urllib import xml.sax as sax, xml.sax.handler as handler f = urllib.urlopen("http://www.google.de/ig/api?weather=Muenchen") xml = f.read() xml = xml.decode("cp1252") f.close() class my_handler(handler.ContentHandler): def startElement(self, name, attrs): print "begin:", name, attrs def endElement(self, name): print "end:", name sax.parseString(xml, my_handler()) From python at mrabarnett.plus.com Sat Oct 17 12:14:11 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 17 Oct 2009 17:14:11 +0100 Subject: umlauts In-Reply-To: <20091017135457.GA6335@alittleroisin> References: <20091017135457.GA6335@alittleroisin> Message-ID: <4AD9ED53.3010803@mrabarnett.plus.com> Arian Kuschki wrote: > Hi all > > this has been bugging me for a long time and I do not seem to be able to > understand what to do. I always have problems when dealing input text that > contains umlauts. Consider the following: > > In [1]: import urllib > > In [2]: f = urllib.urlopen("http://www.google.de/ig/api?weather=Muenchen") > > In [3]: xml = f.read() > > In [4]: f.close() > > In [5]: print xml > ------> print(xml) > tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" >> y data="Munich, BY"/> data=""/> data="2009-10-17"/> data="SI"/> umidity data="Feuchtigkeit: 87?%"/> data="/ig/images/weather/mostly_cloudy.gif"/> ent_conditions> data="1"/> data="/ig/images/weather/chance_of_rain.gif"/> data="So."/> data="/ig/images/weather/chance_of_sno > w.gif"/> data="Mo."/> data="Di."/> /> data="Klar"/> > > As you can see the umlauts in the XML are not displayed properly. When I want > to process this text (for example with xml.sax), I get error messages because > the parses can't read this. > > I've tried to read up on this and there is a lot of information on the web, but > nothing seems to work for me. For example setting the coding to UTF like this: > # -*- coding: utf-8 -*- or using the decode() string method. > > I always have this kind of problem when input contains umlauts, not just in > this case. My locale (on Ubuntu) is en_GB.UTF-8. > The string you received from the website is a bytestring and you're just printing it to your console, which is configured for UTF-8. However, the bytestring isn't valid UTF-8, so the console is replacing the invalid parts with the funny characters. You should decode the bytestring to Unicode and then re-encode it to UTF-8. I don't know what encoding the website is actually using; here I'm assuming ISO-8859-1: print xml.decode("iso-8859-1").encode("utf-8") From metolone+gmane at gmail.com Sat Oct 17 12:24:52 2009 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Sat, 17 Oct 2009 09:24:52 -0700 Subject: py2exe and croatian letters References: Message-ID: "Samir Alukovic at work" wrote in message news:ab6475d0-133c-478d-8f08-eafea0733701 at j39g2000yqh.googlegroups.com... >I am making a simple program in Croatian. In the beginning I set "# - > *- coding: cp1250 -*-" code and when i run it in Python shell it comes > out fine, but when i compile it with py2exe he doesn't print out > croatian letters but he prints out tottaly other letters. I checked > the CMD and it supports croatian letters. If you want an source code > and setup file send me an e-mail at a.dexterlab at gmail.com Not knowing what were considered Croatian characters, I found http://www.geocities.com/click2speak/unicode/chars_hr.html for my testing. Run 'chcp' from CMD and see what code page you are in. I was able to output correctly by changing the codepage from 437 (my U.S. default), to 1250. I also had to change my console window font from 'Raster Fonts' to 'Lucida Console', since the former supported the cp437 character set only. -Mark From victorsubervi at gmail.com Sat Oct 17 12:31:23 2009 From: victorsubervi at gmail.com (Victor Subervi) Date: Sat, 17 Oct 2009 12:31:23 -0400 Subject: More & More Fun w/ Pics & MySQL In-Reply-To: References: <4dc0cfea0910161223q326f1306jb24b7d62ab124d30@mail.gmail.com> <4dc0cfea0910161439v4401d1b8h1f0c325bf809251d@mail.gmail.com> <4dc0cfea0910161721v4812822arfcb42743c1359dd7@mail.gmail.com> <4dc0cfea0910170818w37a47580qb49290a5b5546bfa@mail.gmail.com> Message-ID: <4dc0cfea0910170931x58d92075g104c35dfaff02fec@mail.gmail.com> in line... On Sat, Oct 17, 2009 at 11:33 AM, Carsten Haese wrote: > Victor Subervi wrote: > > Let me clarify. This prints out "all sorts of crap", which means an > > image string, the image as a string, to the screen: > > > > print 'Content-type: image/jpeg' > > print 'Content-Encoding: base64' > > print > > print pic().encode('base64') > > print '' > > This has no hope of working, since you're sending data that's not valid > base64 code and pretend that it's base64-encoded. A browser trying to > display this result will produce something anywhere between > unpredictable and garbage. > This was recommended to me previously on this list, so I tried it. > > > The following once upon a time printed images, but now it doesn't. Why > > would that be? I would refresh the screen, and it would print. I'd > > change a line and it wouldn't. I'd change it back to what it was and it > > would no longer the image to the screen. Why is that? The same happens > > with or without the base64 stuff. Commenting out a line seemed to make a > > difference! > > Why would turning a comment into a statement NOT make a difference?!? > You misunderstood. Leaving in the __commented__ line __commented__ and __not__uncommented__ made a difference. Why? > > > > > print 'Content-type: text/plain' > > #print 'Content-type: image/jpeg' > > print > > print pic() > > print '' > > This should produce plain text, not an image. > > > The above prints out a broken image of correct dimensions. > > What the heck is a "broken image of correct dimensions"? http://allpointsmarinevi.com/stxresort/cart/getpic1.py > Are you looking > at the output of the script itself, or are you looking at an html page > that uses this script as src-attribute for an img-tag? In the latter > case, if you're also giving the img-tag width and height attributes, it > shouldn't be surprising that you're getting the correct dimensions, and > you're getting a broken image because your src is producing plain text > instead of an image. > Before, when it produced plain text it printed the same to screen. Now it does not. > > > Of course I try and figure out how things work once they get working. > > Sometimes, however, there is __no__ logic to it __at__all__. Sorry. > > After years of struggling with python I've come to realize that even > > though I may not be a good programmer, it isn't me. It may not be python > > itself. It may be the crappy hardware the server farms use. It may be > > the way they tweak their python interpreter. But it isn't just me. > > I suppose it's possible that there is something quirky about the way the > server has been set up, but so far, the more likely explanation is that > you don't really know what you're doing. > Well, if I did, I wouldn't be asking for your help. I was hoping you could help me. > > You can prove whether it's your server farm or you by running the same > code on a different server. > Which one? > > > It would be nice if I could get this code printing images to the screen > > as it was before I had to put out another unnecessary fire when code > > that previously uploaded the images in the first place inexplicably no > > longer worked. Then, lo and behold, when I came back to this code that I > > hadn't touched and was working fine earlier this week, it no longer > > works. Lovely. My "plug-and-play" program has devoured two weeks of my > > time and I'm still up the creek without a paddle. Sure would appreciate > > any help you can give. > > Again, you have to help us help you. I have listed the three things you > need to post together, and you haven't done that. You're giving us bits > and pieces, but nothing that's sufficiently cohesive to do any > meaningful troubleshooting on. > Forgive me. I must have misunderstood. I have deleted the old message. Would you be so kind as to re-post those three things you need me to provide you? TIA, V > > -- > Carsten Haese > http://informixdb.sourceforge.net > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From christophedeze at gmail.com Sat Oct 17 12:34:46 2009 From: christophedeze at gmail.com (Toff) Date: Sat, 17 Oct 2009 09:34:46 -0700 (PDT) Subject: help to convert c++ fonction in python Message-ID: hello I'm trying to convert 2 c++ functions in python they come from wpkg client https://wpkg.svn.sourceforge.net/svnroot/wpkg/wpkg-client/Sources/Components/XmlSettings.cpp they are CString CXmlSettings::Crypt(CString str) CString CXmlSettings::Decrypt(CString str) CAn someone help me? i d'ont know much about c++ thanks From metolone+gmane at gmail.com Sat Oct 17 12:46:50 2009 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Sat, 17 Oct 2009 09:46:50 -0700 Subject: how to write a unicode string to a file ? References: <4AD7B3B2.2040208@gmail.com><7a9c25c20910151659k5fa15ff8s111cb8bf8b3affb2@mail.gmail.com><4AD90ADD.4060407@gmail.com><7a9c25c20910161749j38d7a0ebx15e28d3dfab74892@mail.gmail.com> Message-ID: "Mark Tolonen" wrote in message news:hbbo6d$6ue$1 at ger.gmane.org... > > "Kee Nethery" wrote in message > news:AAAB63C6-6E44-4C07-B119-972D4F49E511 at kagi.com... >> >> On Oct 16, 2009, at 5:49 PM, Stephen Hansen wrote: >> >>> On Fri, Oct 16, 2009 at 5:07 PM, Stef Mientki >>> wrote: >> >> snip >> >>> The thing is, I'd be VERY surprised (neigh, shocked!) if Excel can't >>> open a file that is in UTF8-- it just might need to be TOLD that its >>> utf8 when you go and open the file, as UTF8 looks just like ASCII -- >>> until it contains characters that can't be expressed in ASCII. But I >>> don't know what type of file it is you're saving. >> >> We found that UTF-16 was required for Excel. It would not "do the right >> thing" when presented with UTF-8. > > Excel seems to expect a UTF-8-encoded BOM (byte order mark) to correctly > decide a file is written in UTF-8. This worked for me: > > f=codecs.open('test.csv','wb','utf-8') > f.write(u'\ufeff') # write a BOM > f.write(u'??,testing,123\r\n') > f.close() > > When opened in Excel without the BOM (\ufeff), I got gibberish, but with > the BOM the Chinese characters were displayed correctly. Also, it turns out the python 'utf-16' encoder adds a BOM for you, which is probably why UTF-16 worked for you and UTF-8 didn't: >>> u'\u0102'.encode('utf-16-be') # explicit big-endian, no BOM '\x01\x02' >>> u'\u0102'.encode('utf-16-le') # explicit little-endian, no BOM '\x02\x01' >>> u'\u0102'.encode('utf-16') # machine native-endian, with BOM '\xff\xfe\x02\x01' >>> u'\u0102'.encode('utf-8') # no BOM '\xc4\x82' >>> u'\ufeff\u0102'.encode('utf-8') # explicit BOM '\xef\xbb\xbf\xc4\x82' -Mark From deets at nospam.web.de Sat Oct 17 12:50:41 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sat, 17 Oct 2009 18:50:41 +0200 Subject: umlauts In-Reply-To: <25ab29ae-eb30-452a-9595-22cb976657e8@f20g2000prn.googlegroups.com> References: <25ab29ae-eb30-452a-9595-22cb976657e8@f20g2000prn.googlegroups.com> Message-ID: <4AD9F5E1.7060507@nospam.web.de> StarWing schrieb: > On 10?17?, ??9?54?, Arian Kuschki > wrote: >> Hi all >> >> this has been bugging me for a long time and I do not seem to be able to >> understand what to do. I always have problems when dealing input text that >> contains umlauts. Consider the following: >> >> In [1]: import urllib >> >> In [2]: f = urllib.urlopen("http://www.google.de/ig/api?weather=Muenchen") >> >> In [3]: xml = f.read() >> >> In [4]: f.close() >> >> In [5]: print xml >> ------> print(xml) >> > tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">> >> y data="Munich, BY"/>> data=""/>> data="2009-10-17"/>> data="SI"/>> umidity data="Feuchtigkeit: 87 %"/>> data="/ig/images/weather/mostly_cloudy.gif"/>> ent_conditions>> data="1"/>> data="/ig/images/weather/chance_of_rain.gif"/>> data="So."/>> data="/ig/images/weather/chance_of_sno >> w.gif"/>> data="Mo."/>> data="Di."/>> />> data="Klar"/> >> >> As you can see the umlauts in the XML are not displayed properly. When I want >> to process this text (for example with xml.sax), I get error messages because >> the parses can't read this. >> >> I've tried to read up on this and there is a lot of information on the web, but >> nothing seems to work for me. For example setting the coding to UTF like this: >> # -*- coding: utf-8 -*- or using the decode() string method. >> >> I always have this kind of problem when input contains umlauts, not just in >> this case. My locale (on Ubuntu) is en_GB.UTF-8. >> >> Cheers >> Arian > > try this? > > # vim: set fencoding=utf-8: > import urllib > import xml.sax as sax, xml.sax.handler as handler > > f = urllib.urlopen("http://www.google.de/ig/api?weather=Muenchen") > xml = f.read() > xml = xml.decode("cp1252") > f.close() > > class my_handler(handler.ContentHandler): > def startElement(self, name, attrs): > print "begin:", name, attrs > > def endElement(self, name): > print "end:", name > > sax.parseString(xml, my_handler()) This is wrong. XML is a *byte*-based format, which explicitly states encodings. So decoding a byte-string to a unicode-object and then passing it to a parser is not working in the very moment you have data that - is outside your default-system-encoding (ususally ascii) - the system-encoding and the declared decoding differ Besides, I don't see where the whole SAX-stuff is supposed to do anything the direct print and the decode() don't do - smells like cargo-cult to me. Diez From deets at nospam.web.de Sat Oct 17 12:50:53 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sat, 17 Oct 2009 18:50:53 +0200 Subject: umlauts In-Reply-To: <25ab29ae-eb30-452a-9595-22cb976657e8@f20g2000prn.googlegroups.com> References: <25ab29ae-eb30-452a-9595-22cb976657e8@f20g2000prn.googlegroups.com> Message-ID: <7juavdF37divlU2@mid.uni-berlin.de> StarWing schrieb: > On 10?17?, ??9?54?, Arian Kuschki > wrote: >> Hi all >> >> this has been bugging me for a long time and I do not seem to be able to >> understand what to do. I always have problems when dealing input text that >> contains umlauts. Consider the following: >> >> In [1]: import urllib >> >> In [2]: f = urllib.urlopen("http://www.google.de/ig/api?weather=Muenchen") >> >> In [3]: xml = f.read() >> >> In [4]: f.close() >> >> In [5]: print xml >> ------> print(xml) >> > tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">> >> y data="Munich, BY"/>> data=""/>> data="2009-10-17"/>> data="SI"/>> umidity data="Feuchtigkeit: 87 %"/>> data="/ig/images/weather/mostly_cloudy.gif"/>> ent_conditions>> data="1"/>> data="/ig/images/weather/chance_of_rain.gif"/>> data="So."/>> data="/ig/images/weather/chance_of_sno >> w.gif"/>> data="Mo."/>> data="Di."/>> />> data="Klar"/> >> >> As you can see the umlauts in the XML are not displayed properly. When I want >> to process this text (for example with xml.sax), I get error messages because >> the parses can't read this. >> >> I've tried to read up on this and there is a lot of information on the web, but >> nothing seems to work for me. For example setting the coding to UTF like this: >> # -*- coding: utf-8 -*- or using the decode() string method. >> >> I always have this kind of problem when input contains umlauts, not just in >> this case. My locale (on Ubuntu) is en_GB.UTF-8. >> >> Cheers >> Arian > > try this? > > # vim: set fencoding=utf-8: > import urllib > import xml.sax as sax, xml.sax.handler as handler > > f = urllib.urlopen("http://www.google.de/ig/api?weather=Muenchen") > xml = f.read() > xml = xml.decode("cp1252") > f.close() > > class my_handler(handler.ContentHandler): > def startElement(self, name, attrs): > print "begin:", name, attrs > > def endElement(self, name): > print "end:", name > > sax.parseString(xml, my_handler()) This is wrong. XML is a *byte*-based format, which explicitly states encodings. So decoding a byte-string to a unicode-object and then passing it to a parser is not working in the very moment you have data that - is outside your default-system-encoding (ususally ascii) - the system-encoding and the declared decoding differ Besides, I don't see where the whole SAX-stuff is supposed to do anything the direct print and the decode() don't do - smells like cargo-cult to me. Diez From deets at nospam.web.de Sat Oct 17 12:54:10 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sat, 17 Oct 2009 18:54:10 +0200 Subject: umlauts In-Reply-To: References: <20091017135457.GA6335@alittleroisin> Message-ID: <4AD9F6B2.9090506@nospam.web.de> MRAB schrieb: > Arian Kuschki wrote: >> Hi all >> >> this has been bugging me for a long time and I do not seem to be able >> to understand what to do. I always have problems when dealing input >> text that contains umlauts. Consider the following: >> >> In [1]: import urllib >> >> In [2]: f = >> urllib.urlopen("http://www.google.de/ig/api?weather=Muenchen") >> >> In [3]: xml = f.read() >> >> In [4]: f.close() >> >> In [5]: print xml >> ------> print(xml) >> > tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" >>> > y data="Munich, BY"/>> data=""/>> data="2009-10-17"/>> data="SI"/>> data="Meistens bew?kt"/>> umidity data="Feuchtigkeit: 87?%"/>> data="/ig/images/weather/mostly_cloudy.gif"/>> data="Wind: W mit Windgeschwindigkeiten von 13 km/h"/>> ent_conditions>> data="1"/>> data="/ig/images/weather/chance_of_rain.gif"/>> data="So."/>> data="/ig/images/weather/chance_of_sno >> w.gif"/>> data="Mo."/>> data="Di."/>> />> data="Klar"/> >> >> As you can see the umlauts in the XML are not displayed properly. When >> I want to process this text (for example with xml.sax), I get error >> messages because the parses can't read this. >> >> I've tried to read up on this and there is a lot of information on the >> web, but nothing seems to work for me. For example setting the coding >> to UTF like this: # -*- coding: utf-8 -*- or using the decode() string >> method. >> >> I always have this kind of problem when input contains umlauts, not >> just in this case. My locale (on Ubuntu) is en_GB.UTF-8. >> > The string you received from the website is a bytestring and you're just > printing it to your console, which is configured for UTF-8. However, the > bytestring isn't valid UTF-8, so the console is replacing the invalid > parts with the funny characters. This is wierd. I looked at the site in FireFox - and it was displayed correctly, including umlauts. Bringing up the info-dialog claims the page is UTF-8, the XML itself says so as well (implicit, through the missing declaration of an encoding) - but it clearly is *not* utf-8. One would expect google to be better at this... Diez From deets at nospam.web.de Sat Oct 17 12:54:19 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sat, 17 Oct 2009 18:54:19 +0200 Subject: umlauts In-Reply-To: References: <20091017135457.GA6335@alittleroisin> Message-ID: <7jub5rF37divlU4@mid.uni-berlin.de> MRAB schrieb: > Arian Kuschki wrote: >> Hi all >> >> this has been bugging me for a long time and I do not seem to be able >> to understand what to do. I always have problems when dealing input >> text that contains umlauts. Consider the following: >> >> In [1]: import urllib >> >> In [2]: f = >> urllib.urlopen("http://www.google.de/ig/api?weather=Muenchen") >> >> In [3]: xml = f.read() >> >> In [4]: f.close() >> >> In [5]: print xml >> ------> print(xml) >> > tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" >>> > y data="Munich, BY"/>> data=""/>> data="2009-10-17"/>> data="SI"/>> data="Meistens bew?kt"/>> umidity data="Feuchtigkeit: 87?%"/>> data="/ig/images/weather/mostly_cloudy.gif"/>> data="Wind: W mit Windgeschwindigkeiten von 13 km/h"/>> ent_conditions>> data="1"/>> data="/ig/images/weather/chance_of_rain.gif"/>> data="So."/>> data="/ig/images/weather/chance_of_sno >> w.gif"/>> data="Mo."/>> data="Di."/>> />> data="Klar"/> >> >> As you can see the umlauts in the XML are not displayed properly. When >> I want to process this text (for example with xml.sax), I get error >> messages because the parses can't read this. >> >> I've tried to read up on this and there is a lot of information on the >> web, but nothing seems to work for me. For example setting the coding >> to UTF like this: # -*- coding: utf-8 -*- or using the decode() string >> method. >> >> I always have this kind of problem when input contains umlauts, not >> just in this case. My locale (on Ubuntu) is en_GB.UTF-8. >> > The string you received from the website is a bytestring and you're just > printing it to your console, which is configured for UTF-8. However, the > bytestring isn't valid UTF-8, so the console is replacing the invalid > parts with the funny characters. This is wierd. I looked at the site in FireFox - and it was displayed correctly, including umlauts. Bringing up the info-dialog claims the page is UTF-8, the XML itself says so as well (implicit, through the missing declaration of an encoding) - but it clearly is *not* utf-8. One would expect google to be better at this... Diez From weasley_wx at sina.com Sat Oct 17 12:55:24 2009 From: weasley_wx at sina.com (StarWing) Date: Sat, 17 Oct 2009 09:55:24 -0700 (PDT) Subject: umlauts References: <20091017135457.GA6335@alittleroisin> Message-ID: On 10?18?, ??12?14?, MRAB wrote: > Arian Kuschki wrote: > > Hi all > > > this has been bugging me for a long time and I do not seem to be able to > > understand what to do. I always have problems when dealing input text that > > contains umlauts. Consider the following: > > > In [1]: import urllib > > > In [2]: f = urllib.urlopen("http://www.google.de/ig/api?weather=Muenchen") > > > In [3]: xml = f.read() > > > In [4]: f.close() > > > In [5]: print xml > > ------> print(xml) > > > tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" > >> > y data="Munich, BY"/> > data=""/> > data="2009-10-17"/> > data="SI"/> > umidity data="Feuchtigkeit: 87 %"/> > data="/ig/images/weather/mostly_cloudy.gif"/> > ent_conditions> > data="1"/> > data="/ig/images/weather/chance_of_rain.gif"/> > data="So."/> > data="/ig/images/weather/chance_of_sno > > w.gif"/> > data="Mo."/> > data="Di."/> > /> > data="Klar"/> > > > As you can see the umlauts in the XML are not displayed properly. When I want > > to process this text (for example with xml.sax), I get error messages because > > the parses can't read this. > > > I've tried to read up on this and there is a lot of information on the web, but > > nothing seems to work for me. For example setting the coding to UTF like this: > > # -*- coding: utf-8 -*- or using the decode() string method. > > > I always have this kind of problem when input contains umlauts, not just in > > this case. My locale (on Ubuntu) is en_GB.UTF-8. > > The string you received from the website is a bytestring and you're just > printing it to your console, which is configured for UTF-8. However, the > bytestring isn't valid UTF-8, so the console is replacing the invalid > parts with the funny characters. > > You should decode the bytestring to Unicode and then re-encode it to > UTF-8. I don't know what encoding the website is actually using; here > I'm assuming ISO-8859-1: > > print xml.decode("iso-8859-1").encode("utf-8") in 2.6, str.decode return unicode, so you can directly print it. in 3.1, str.encode return bytes, so you can also directly print it. so, just decode("cp1252"), it's enough. From weasley_wx at sina.com Sat Oct 17 13:02:20 2009 From: weasley_wx at sina.com (StarWing) Date: Sat, 17 Oct 2009 10:02:20 -0700 (PDT) Subject: umlauts References: <25ab29ae-eb30-452a-9595-22cb976657e8@f20g2000prn.googlegroups.com> <4AD9F5E1.7060507@nospam.web.de> Message-ID: <36acb7c2-ea98-476a-af4c-1ea4d2f24934@g1g2000pra.googlegroups.com> On 10?18?, ??12?50?, "Diez B. Roggisch" wrote: > StarWing schrieb: > > > > > On 10?17?, ??9?54?, Arian Kuschki > > wrote: > >> Hi all > > >> this has been bugging me for a long time and I do not seem to be able to > >> understand what to do. I always have problems when dealing input text that > >> contains umlauts. Consider the following: > > >> In [1]: import urllib > > >> In [2]: f = urllib.urlopen("http://www.google.de/ig/api?weather=Muenchen") > > >> In [3]: xml = f.read() > > >> In [4]: f.close() > > >> In [5]: print xml > >> ------> print(xml) > >> >> tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0"> > >> y data="Munich, BY"/> >> data=""/> >> data="2009-10-17"/> >> data="SI"/> >> umidity data="Feuchtigkeit: 87 %"/> >> data="/ig/images/weather/mostly_cloudy.gif"/> >> ent_conditions> >> data="1"/> >> data="/ig/images/weather/chance_of_rain.gif"/> >> data="So."/> >> data="/ig/images/weather/chance_of_sno > >> w.gif"/> >> data="Mo."/> >> data="Di."/> >> /> >> data="Klar"/> > > >> As you can see the umlauts in the XML are not displayed properly. When I want > >> to process this text (for example with xml.sax), I get error messages because > >> the parses can't read this. > > >> I've tried to read up on this and there is a lot of information on the web, but > >> nothing seems to work for me. For example setting the coding to UTF like this: > >> # -*- coding: utf-8 -*- or using the decode() string method. > > >> I always have this kind of problem when input contains umlauts, not just in > >> this case. My locale (on Ubuntu) is en_GB.UTF-8. > > >> Cheers > >> Arian > > > try this? > > > # vim: set fencoding=utf-8: > > import urllib > > import xml.sax as sax, xml.sax.handler as handler > > > f = urllib.urlopen("http://www.google.de/ig/api?weather=Muenchen") > > xml = f.read() > > xml = xml.decode("cp1252") > > f.close() > > > class my_handler(handler.ContentHandler): > > ? ? def startElement(self, name, attrs): > > ? ? ? ? print "begin:", name, attrs > > > ? ? def endElement(self, name): > > ? ? ? ? print "end:", name > > > sax.parseString(xml, my_handler()) > > This is wrong. XML is a *byte*-based format, which explicitly states > encodings. So decoding a byte-string to a unicode-object and then > passing it to a parser is not working in the very moment you have data that > > ? - is outside your default-system-encoding (ususally ascii) > ? - the system-encoding and the declared decoding differ > > Besides, I don't see where the whole SAX-stuff is supposed to do > anything the direct print ?and the decode() don't do - smells like > cargo-cult to me. > > Diez yes, XML is a *byte*-based format, and so as utf-8 and code-page (cp936, cp1252, etc.). so usually XML will sign its coding at head. but this didn't work now. in Python2.6, sys.getdefaultcoding() return 'ascii', and I can't use sys.setdefaultcoding(), and f.read() return a str. so it must be a undecoded, byte-base format (i.e. raw XML data). so use the right code- page to decode it is safe.(notice the webpage is google.de). in Python3.1, read() returns a bytes object. so we *must* decode it, nor we can't pass it into a parser. From weasley_wx at sina.com Sat Oct 17 13:05:52 2009 From: weasley_wx at sina.com (StarWing) Date: Sat, 17 Oct 2009 10:05:52 -0700 (PDT) Subject: slicing return iter? References: Message-ID: On 10?17?, ??11?56?, Arnaud Delobelle wrote: > On Oct 17, 3:40 pm, StarWing wrote: > > > > > hello everyone, I'm new here :-) > > > sometimes I want to iterate a part of a sequence. but don't want to > > copy it. i.e. > > > a = list(...) > > # now a is a list, and a[:] is another list, and so a[m:n] > > # now we do something with the 0~len(a)-3 elements of a > > for val in a[:-2]: > > #do something.... > > > but, this will cause a copy on a, has some convenience way to get a > > iter to iterate on a part of list? > > > i made this: > > class iterslice: > > def __init__(self, list): > > self.list = list > > > def __len__(self): > > return len(self.list) > > > def __getitem__(self, slice): > > import itertools > > listlen = len(self.list) > > range = (((slice.start + listlen) % listlen) if slice.start > > else 0, > > ((slice.stop + listlen) % listlen) if slice.stop else > > listlen, > > slice.step) > > return itertools.islice(self.list, *range) > > > a = [1,2,3,4] > > for i in iterslice(a)[:-1:2]: > > print i > > > my question is: > > - are there any *Standard* way to do this? (a buit-in function? a > > module?) > > - are there any better implements? > > > thanks for attention :-) > > Check the itertools module. > > HTH > > -- > Arnaud I had checked it for serval times. maybe it's my inattention :-(. but what i could find the nearest thing is itertools.islice. but it can't process negative index -- that's supported by slice. so I need something, bind object with slice, and return a iter. I can find anything like it...:-( From carsten.haese at gmail.com Sat Oct 17 13:31:59 2009 From: carsten.haese at gmail.com (Carsten Haese) Date: Sat, 17 Oct 2009 13:31:59 -0400 Subject: More & More Fun w/ Pics & MySQL In-Reply-To: <4dc0cfea0910170931x58d92075g104c35dfaff02fec@mail.gmail.com> References: <4dc0cfea0910161223q326f1306jb24b7d62ab124d30@mail.gmail.com> <4dc0cfea0910161439v4401d1b8h1f0c325bf809251d@mail.gmail.com> <4dc0cfea0910161721v4812822arfcb42743c1359dd7@mail.gmail.com> <4dc0cfea0910170818w37a47580qb49290a5b5546bfa@mail.gmail.com> <4dc0cfea0910170931x58d92075g104c35dfaff02fec@mail.gmail.com> Message-ID: Victor Subervi wrote: > in line... > > On Sat, Oct 17, 2009 at 11:33 AM, Carsten Haese > wrote: > Why would turning a comment into a statement NOT make a difference?!? > > > You misunderstood. Leaving in the __commented__ line __commented__ and > __not__uncommented__ made a difference. Why? Alright, so let me rephrase the question: Why would turning a statement into a comment not make a difference? Answer: The only way commenting out a line of code makes no difference is if the line of code was superfluous to begin with. The fact that commenting it out *does* make a difference therefore means that the line of code is not superfluous. > What the heck is a "broken image of correct dimensions"? > > > http://allpointsmarinevi.com/stxresort/cart/getpic1.py That doesn't look like a broken image to me. When I try that URL, I get a 750x900 pixel JPEG image that appears to be a line-drawing of sorts. Of course I have no idea whether that's the expected result because for some reason you decided to keep that a secret. > You can prove whether it's your server farm or you by running the same > code on a different server. > > > Which one? Which one what? Which code? Try all versions you have, until you find one that works. When you do, run that on your server farm to see if it still works. > Again, you have to help us help you. I have listed the three things you > need to post together, and you haven't done that. You're giving us bits > and pieces, but nothing that's sufficiently cohesive to do any > meaningful troubleshooting on. > > > Forgive me. I must have misunderstood. I have deleted the old message. > Would you be so kind as to re-post those three things you need me to > provide you? Here's what I said earlier: """ The only way we have a fighting chance to help you in figuring out what's going in is if you post the *exact* code you're running (and by that I mean the actual code that you know your server is executing, and not just some code that somewhat resembles the code that the server might be executing), a detailed description of the result you're expecting, and a detailed description of the result you're getting instead. """ So, the three things I'm asking for: * Show us EXACTLY the code you're running. * Tell us the result you're expecting. * Tell us the result you're getting instead. -- Carsten Haese http://informixdb.sourceforge.net From arian.kuschki at googlemail.com Sat Oct 17 13:50:47 2009 From: arian.kuschki at googlemail.com (Arian Kuschki) Date: Sat, 17 Oct 2009 21:20:47 +0330 Subject: umlauts In-Reply-To: <7jug1rF371nkpU1@mid.uni-berlin.de> References: <25ab29ae-eb30-452a-9595-22cb976657e8@f20g2000prn.googlegroups.com> <4AD9F5E1.7060507@nospam.web.de> <36acb7c2-ea98-476a-af4c-1ea4d2f24934@g1g2000pra.googlegroups.com> <7jug1rF371nkpU1@mid.uni-berlin.de> Message-ID: <20091017175047.GA23108@alittleroisin> Whoa, that was quick! Thanks for all the answers, I'll try to recapitulate >What does this show you in your interactive interpreter? > >>>> print "\xc3\xb6" >? > >For me, it's o-umlaut, ?. This is because the above bytes are the >sequence for ? in utf-8. > >If this shows something else, you need to adjust your terminal settings. for me it also prints the correct o-umlaut (?), so that was not the problem. All of the below result in xml that shows all umlauts correctly when printed: xml.decode("cp1252") xml.decode("cp1252").encode("utf-8") xml.decode("iso-8859-1") xml.decode("iso-8859-1").encode("utf-8") But when I want to parse the xml then, it only works if I do both decode and encode. If I only decode, I get the following error: SAXParseException: :1:1: not well-formed (invalid token) Do I understand right that since the encoding was not specified in the xml response, it should have been utf-8 by default? And that if it had indeed been utf-8 I would not have had the encoding problem in the first place? Anyway, thanks everybody, this has helped me a lot. Arian On Sat 17, 20:17 +0200, Diez B. Roggisch wrote: > StarWing schrieb: > >On 10?18?, ??12?50?, "Diez B. Roggisch" wrote: > >>StarWing schrieb: > >> > >> > >> > >>>On 10?17?, ??9?54?, Arian Kuschki > >>>wrote: > >>>>Hi all > >>>>this has been bugging me for a long time and I do not seem to be able to > >>>>understand what to do. I always have problems when dealing input text that > >>>>contains umlauts. Consider the following: > >>>>In [1]: import urllib > >>>>In [2]: f = urllib.urlopen("http://www.google.de/ig/api?weather=Muenchen") > >>>>In [3]: xml = f.read() > >>>>In [4]: f.close() > >>>>In [5]: print xml > >>>>------> print(xml) > >>>> >>>>tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0"> >>>>y data="Munich, BY"/> >>>>data=""/> >>>>data="2009-10-17"/> >>>>data="SI"/> >>>>umidity data="Feuchtigkeit: 87 %"/> >>>>data="/ig/images/weather/mostly_cloudy.gif"/> >>>>ent_conditions> >>>>data="1"/> >>>>data="/ig/images/weather/chance_of_rain.gif"/> >>>>data="So."/> >>>>data="/ig/images/weather/chance_of_sno > >>>>w.gif"/> >>>>data="Mo."/> >>>>data="Di."/> >>>>/> >>>>data="Klar"/> > >>>>As you can see the umlauts in the XML are not displayed properly. When I want > >>>>to process this text (for example with xml.sax), I get error messages because > >>>>the parses can't read this. > >>>>I've tried to read up on this and there is a lot of information on the web, but > >>>>nothing seems to work for me. For example setting the coding to UTF like this: > >>>># -*- coding: utf-8 -*- or using the decode() string method. > >>>>I always have this kind of problem when input contains umlauts, not just in > >>>>this case. My locale (on Ubuntu) is en_GB.UTF-8. > >>>>Cheers > >>>>Arian > >>>try this? > >>># vim: set fencoding=utf-8: > >>>import urllib > >>>import xml.sax as sax, xml.sax.handler as handler > >>>f = urllib.urlopen("http://www.google.de/ig/api?weather=Muenchen") > >>>xml = f.read() > >>>xml = xml.decode("cp1252") > >>>f.close() > >>>class my_handler(handler.ContentHandler): > >>> def startElement(self, name, attrs): > >>> print "begin:", name, attrs > >>> def endElement(self, name): > >>> print "end:", name > >>>sax.parseString(xml, my_handler()) > >>This is wrong. XML is a *byte*-based format, which explicitly states > >>encodings. So decoding a byte-string to a unicode-object and then > >>passing it to a parser is not working in the very moment you have data that > >> > >> - is outside your default-system-encoding (ususally ascii) > >> - the system-encoding and the declared decoding differ > >> > >>Besides, I don't see where the whole SAX-stuff is supposed to do > >>anything the direct print and the decode() don't do - smells like > >>cargo-cult to me. > >> > >>Diez > > > >yes, XML is a *byte*-based format, and so as utf-8 and code-page > >(cp936, cp1252, etc.). so usually XML will sign its coding at head. > >but this didn't work now. > > > >in Python2.6, sys.getdefaultcoding() return 'ascii', and I can't use > >sys.setdefaultcoding(), and f.read() return a str. so it must be a > >undecoded, byte-base format (i.e. raw XML data). so use the right code- > >page to decode it is safe.(notice the webpage is google.de). > > > >in Python3.1, read() returns a bytes object. so we *must* decode it, > >nor we can't pass it into a parser. > > You didn't get my point. A XML-parser only *takes* a byte-string. > Decoding is it's business. So your above last sentence is wrong. > > Because regardless of the python-version, if you feed the parser a > unicode-object, python will first encode that to a byte-string, > possibly giving a UnicodeError (maybe this automated conversion has > gone in Py3K, but then you get a type-error instead). > > So to make the above work (if one wants to parse the xml), the > proper thing to do would be > > xml = xml.decode("cp1252").encode("utf-8") > > and then feed that. Of course the really good thing would be to fix > the webpage, but that's beyond our capabilities I fear... > > Diez > -- > http://mail.python.org/mailman/listinfo/python-list -- From arian.kuschki at googlemail.com Sat Oct 17 13:54:59 2009 From: arian.kuschki at googlemail.com (Arian Kuschki) Date: Sat, 17 Oct 2009 21:24:59 +0330 Subject: umlauts In-Reply-To: <4ada1393@news.x-privat.org> References: <20091017135457.GA6335@alittleroisin> <4AD9F6B2.9090506@nospam.web.de> <4ada1393@news.x-privat.org> Message-ID: <20091017175459.GA26912@alittleroisin> I just checked and I see the following in the headers: Content-Type text/xml; charset=UTF-8 Where does it say ISO-8859-1? On Sat 17, 20:57 +0200, I V wrote: > On Sat, 17 Oct 2009 18:54:10 +0200, Diez B. Roggisch wrote: > > > This is wierd. I looked at the site in FireFox - and it was displayed > > correctly, including umlauts. Bringing up the info-dialog claims the > > page is UTF-8, the XML itself says so as well (implicit, through the > > missing declaration of an encoding) - but it clearly is *not* utf-8. > > The headers correctly identify it as ISO-8859-1, which overrides the > implicit specification of UTF-8. I'm not sure why Firefox is reporting it > as UTF-8 (it does that for me, too); I can see the umlauts, so it's > clearly processing it as ISO-8859-1. > -- > http://mail.python.org/mailman/listinfo/python-list -- From lie.1296 at gmail.com Sat Oct 17 14:08:05 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 18 Oct 2009 05:08:05 +1100 Subject: More & More Fun w/ Pics & MySQL In-Reply-To: References: <4dc0cfea0910161223q326f1306jb24b7d62ab124d30@mail.gmail.com> Message-ID: <4ada0805@dnews.tpgi.com.au> Carsten Haese wrote: > Victor Subervi wrote: >> [snip...] >> >> print 'Content-type: image/jpeg' >> print 'Content-Encoding: base64' >> print >> print pic().encode('base64') >> print '' >> >> [snip...] > > Why are you printing "" at the end of a page that is > supposed to be a base64-encoded JPEG file? I'm testing my "psychic" mode today. Your old script produces the line drawing image in http://allpointsmarinevi.com/stxresort/cart/getpic1.py; a JPEG image to be exact. Now, you want to embed this image into a webpage. You thought: "well I could simply put the code I used previously to the code that produced the HTML" Let's say, you used something like this to produce the image: ########################################### # in file getpic.py # for simplicity, the image is pre-generated def foo(): img = open('myimg.jpg') print 'some header information' print img.read() ########################################### While your HTML-producing code is something like this: ######################## # note that I avoided putting an exact working code # so you don't start copy and pasting without # digesting and understanding what you're doing import getpic print 'html header' getpic.foo() print 'html header end' ######################## You thought... this should work, I just called a code that previously works. Well, if my psychic prediction capability doesn't have bugs in it, and the weather is fine today, and the moon is full, and the 42nd road is not jammed today; then, as Carsten Haese remarked before, you have no idea why getpic.py works but calling a function it doesn't. First off; HTML doesn't have a way to "embed" image information[1]. What it can do, and what you should do is to "link" the image (unless you want to invite neverending troubles including poor browser support and a standard that practically nobody uses). First and foremost though, please understand how HTML works; particularly tag. This is how you "link" image inside a webpage: Since you don't actually use a static image but a script-generated image; as long as the server is configured to handle python script, you can use this as well: [1] actually, there is a standard for image embedding; but browser support is flaky, at best. Especially for IE. Hint: From arnodel at googlemail.com Sat Oct 17 14:11:48 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Sat, 17 Oct 2009 11:11:48 -0700 (PDT) Subject: slicing return iter? References: Message-ID: On Oct 17, 6:05 pm, StarWing wrote: > On 10?17?, ??11?56?, Arnaud Delobelle wrote: > > thanks for attention :-) > > > Check the itertools module. > > > HTH > > > -- > > Arnaud > > I had checked it for serval times. maybe it's my inattention :-(. but > what i could find the nearest thing is itertools.islice. but it can't > process negative index -- that's supported by slice. so I need > something, bind object with slice, and return a iter. I can find > anything like it...:-( Sorry, I read your post too quickly. I don't think there's anything in the standard library. Your solution is fine! -- Arnaud From deets at nospam.web.de Sat Oct 17 14:17:31 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sat, 17 Oct 2009 20:17:31 +0200 Subject: umlauts In-Reply-To: <36acb7c2-ea98-476a-af4c-1ea4d2f24934@g1g2000pra.googlegroups.com> References: <25ab29ae-eb30-452a-9595-22cb976657e8@f20g2000prn.googlegroups.com> <4AD9F5E1.7060507@nospam.web.de> <36acb7c2-ea98-476a-af4c-1ea4d2f24934@g1g2000pra.googlegroups.com> Message-ID: <7jug1rF371nkpU1@mid.uni-berlin.de> StarWing schrieb: > On 10?18?, ??12?50?, "Diez B. Roggisch" wrote: >> StarWing schrieb: >> >> >> >>> On 10?17?, ??9?54?, Arian Kuschki >>> wrote: >>>> Hi all >>>> this has been bugging me for a long time and I do not seem to be able to >>>> understand what to do. I always have problems when dealing input text that >>>> contains umlauts. Consider the following: >>>> In [1]: import urllib >>>> In [2]: f = urllib.urlopen("http://www.google.de/ig/api?weather=Muenchen") >>>> In [3]: xml = f.read() >>>> In [4]: f.close() >>>> In [5]: print xml >>>> ------> print(xml) >>>> >>> tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">>>> y data="Munich, BY"/>>>> data=""/>>>> data="2009-10-17"/>>>> data="SI"/>>>> umidity data="Feuchtigkeit: 87 %"/>>>> data="/ig/images/weather/mostly_cloudy.gif"/>>>> ent_conditions>>>> data="1"/>>>> data="/ig/images/weather/chance_of_rain.gif"/>>>> data="So."/>>>> data="/ig/images/weather/chance_of_sno >>>> w.gif"/>>>> data="Mo."/>>>> data="Di."/>>>> />>>> data="Klar"/> >>>> As you can see the umlauts in the XML are not displayed properly. When I want >>>> to process this text (for example with xml.sax), I get error messages because >>>> the parses can't read this. >>>> I've tried to read up on this and there is a lot of information on the web, but >>>> nothing seems to work for me. For example setting the coding to UTF like this: >>>> # -*- coding: utf-8 -*- or using the decode() string method. >>>> I always have this kind of problem when input contains umlauts, not just in >>>> this case. My locale (on Ubuntu) is en_GB.UTF-8. >>>> Cheers >>>> Arian >>> try this? >>> # vim: set fencoding=utf-8: >>> import urllib >>> import xml.sax as sax, xml.sax.handler as handler >>> f = urllib.urlopen("http://www.google.de/ig/api?weather=Muenchen") >>> xml = f.read() >>> xml = xml.decode("cp1252") >>> f.close() >>> class my_handler(handler.ContentHandler): >>> def startElement(self, name, attrs): >>> print "begin:", name, attrs >>> def endElement(self, name): >>> print "end:", name >>> sax.parseString(xml, my_handler()) >> This is wrong. XML is a *byte*-based format, which explicitly states >> encodings. So decoding a byte-string to a unicode-object and then >> passing it to a parser is not working in the very moment you have data that >> >> - is outside your default-system-encoding (ususally ascii) >> - the system-encoding and the declared decoding differ >> >> Besides, I don't see where the whole SAX-stuff is supposed to do >> anything the direct print and the decode() don't do - smells like >> cargo-cult to me. >> >> Diez > > yes, XML is a *byte*-based format, and so as utf-8 and code-page > (cp936, cp1252, etc.). so usually XML will sign its coding at head. > but this didn't work now. > > in Python2.6, sys.getdefaultcoding() return 'ascii', and I can't use > sys.setdefaultcoding(), and f.read() return a str. so it must be a > undecoded, byte-base format (i.e. raw XML data). so use the right code- > page to decode it is safe.(notice the webpage is google.de). > > in Python3.1, read() returns a bytes object. so we *must* decode it, > nor we can't pass it into a parser. You didn't get my point. A XML-parser only *takes* a byte-string. Decoding is it's business. So your above last sentence is wrong. Because regardless of the python-version, if you feed the parser a unicode-object, python will first encode that to a byte-string, possibly giving a UnicodeError (maybe this automated conversion has gone in Py3K, but then you get a type-error instead). So to make the above work (if one wants to parse the xml), the proper thing to do would be xml = xml.decode("cp1252").encode("utf-8") and then feed that. Of course the really good thing would be to fix the webpage, but that's beyond our capabilities I fear... Diez From ivlenin at gmail.com Sat Oct 17 14:57:23 2009 From: ivlenin at gmail.com (I V) Date: 17 Oct 2009 20:57:23 +0200 Subject: umlauts References: <20091017135457.GA6335@alittleroisin> <4AD9F6B2.9090506@nospam.web.de> Message-ID: <4ada1393@news.x-privat.org> On Sat, 17 Oct 2009 18:54:10 +0200, Diez B. Roggisch wrote: > This is wierd. I looked at the site in FireFox - and it was displayed > correctly, including umlauts. Bringing up the info-dialog claims the > page is UTF-8, the XML itself says so as well (implicit, through the > missing declaration of an encoding) - but it clearly is *not* utf-8. The headers correctly identify it as ISO-8859-1, which overrides the implicit specification of UTF-8. I'm not sure why Firefox is reporting it as UTF-8 (it does that for me, too); I can see the umlauts, so it's clearly processing it as ISO-8859-1. From arian.kuschki at googlemail.com Sat Oct 17 15:07:01 2009 From: arian.kuschki at googlemail.com (Arian Kuschki) Date: Sat, 17 Oct 2009 22:37:01 +0330 Subject: umlauts In-Reply-To: References: <20091017135457.GA6335@alittleroisin> <7jub5rF37divlU4@mid.uni-berlin.de> Message-ID: <20091017190701.GA25467@alittleroisin> Hm yes, that is true. In Firefox on the other hand, the response header is "Content-Type text/xml; charset=UTF-8" On Sat 17, 13:16 -0700, Mark Tolonen wrote: > > "Diez B. Roggisch" wrote in message > news:7jub5rF37divlU4 at mid.uni-berlin.de... > [snip] > >This is wierd. I looked at the site in FireFox - and it was > >displayed correctly, including umlauts. Bringing up the > >info-dialog claims the page is UTF-8, the XML itself says so as > >well (implicit, through the missing declaration of an encoding) - > >but it clearly is *not* utf-8. > > > >One would expect google to be better at this... > > > >Diez > > According to the XML 1.0 specification: > > "Although an XML processor is required to read only entities in the > UTF-8 and UTF-16 encodings, it is recognized that other encodings > are used around the world, and it may be desired for XML processors > to read entities that use them. In the absence of external character > encoding information (such as MIME headers), parsed entities which > are stored in an encoding other than UTF-8 or UTF-16 must begin with > a text declaration..." > > So UTF-8 and UTF-16 are the defaults supported without an xml > declaration in the absence of external encoding information. But we > have external character encoding information: > > >>>f = urllib.urlopen("http://www.google.de/ig/api?weather=Muenchen") > >>>f.headers.dict['content-type'] > 'text/xml; charset=ISO-8859-1' > > So the page seems correct. > > -Mark > > > -- > http://mail.python.org/mailman/listinfo/python-list -- From ivlenin at gmail.com Sat Oct 17 15:56:07 2009 From: ivlenin at gmail.com (I V) Date: 17 Oct 2009 21:56:07 +0200 Subject: umlauts References: <20091017135457.GA6335@alittleroisin> <4AD9F6B2.9090506@nospam.web.de> <4ada1393@news.x-privat.org> Message-ID: <4ada2157$1@news.x-privat.org> On Sat, 17 Oct 2009 21:24:59 +0330, Arian Kuschki wrote: > I just checked and I see the following in the headers: Content-Type > text/xml; charset=UTF-8 > > Where does it say ISO-8859-1? In the headers returned via urllib (and via wget). But checking in Firefox, it does indeed specify UTF-8 in the content type. Using wget, but specifying the same User-Agent header that Firefox uses, I get the same UTF-8 Content-Type that I see in Firefox. How bizarre. From neo at picture-art.eu Sat Oct 17 15:57:25 2009 From: neo at picture-art.eu (Neo) Date: Sat, 17 Oct 2009 21:57:25 +0200 Subject: More & More Fun w/ Pics & MySQL In-Reply-To: <4dc0cfea0910170818w37a47580qb49290a5b5546bfa@mail.gmail.com> References: <4dc0cfea0910161223q326f1306jb24b7d62ab124d30@mail.gmail.com> <4dc0cfea0910161439v4401d1b8h1f0c325bf809251d@mail.gmail.com> <4dc0cfea0910161721v4812822arfcb42743c1359dd7@mail.gmail.com> <4dc0cfea0910170818w37a47580qb49290a5b5546bfa@mail.gmail.com> Message-ID: <4ADA21A5.5010408@picture-art.eu> Hi, Victor Subervi schrieb: > Let me clarify. This prints out "all sorts of crap", which means an > image string, the image as a string, to the screen: > > print 'Content-type: image/jpeg' > print 'Content-Encoding: base64' > print > print pic().encode('base64') > print '' > > The following once upon a time printed images, but now it doesn't. Why > would that be? I would refresh the screen, and it would print. I'd > change a line and it wouldn't. I'd change it back to what it was and it > would no longer the image to the screen. Why is that? The same happens > with or without the base64 stuff. Commenting out a line seemed to make a > difference! > > print 'Content-type: text/plain' > #print 'Content-type: image/jpeg' > print > print pic() > print '' > > The above prints out a broken image of correct dimensions. Only with a broken client (I assume Internetexplorer here?) Any you don't know what it does in guessing and caching (Try shift-reload when you test things) You should work from a static page and a static image and look what is transferred by the server (wireshark or tcpflow are of great help, but nc | hexdump -C | more is probably more easy if you work out the HTTP protocol (see rfc2616) You script needs to do the same as a server would do serving a static resource. Just look how it works and rebuild it. You will quickly find out, that there is obviously no reason to have at the end of a binary resource an Image is. Also correct mime type is important unless you deal with broken clients. There are a lot more HTTP-Headers you want to understand and use but the simple usage above, provided pic() indeed returns the binary data of the image unaltered, should work (w/o the html crap at the end). You would not use print since it adds a linefeed at the end which is not part of the data. sys.stdout.write() should work better (and can be used incremental to avoid returning the whole image data from memory) > Of course I try and figure out how things work once they get working. > Sometimes, however, there is __no__ logic to it __at__all__. Sorry. > After years of struggling with python I've come to realize that even > though I may not be a good programmer, it isn't me. It may not be python > itself. It may be the crappy hardware the server farms use. It may be > the way they tweak their python interpreter. But it isn't just me. Sorry to tell you that but its just you. There is no magic in that. Ok, there is one thing: what makes you sure the data in the database is really unaltered image data? I mean, you are using mysql... for a first thing I'd write the output into a file and try to open it in a graphic viewer. > It would be nice if I could get this code printing images to the screen > as it was before I had to put out another unnecessary fire when code Images are not "printed to screen" the whole idea is just wrong. > that previously uploaded the images in the first place inexplicably no > longer worked. Then, lo and behold, when I came back to this code that I > hadn't touched and was working fine earlier this week, it no longer > works. Lovely. My "plug-and-play" program has devoured two weeks of my > time and I'm still up the creek without a paddle. Sure would appreciate > any help you can give. I see "plug and play" and read cargo-cult. You need to read a bit about the basics on the matter or you will constantly run uphill - and even if it seems to work it has no value if you don't know _why_. Regards Tino From http Sat Oct 17 16:13:11 2009 From: http (Paul Rubin) Date: 17 Oct 2009 13:13:11 -0700 Subject: The rap against "while True:" loops References: <01ccc46d-5ea9-4dfe-ba22-699c6b859f00@v36g2000yqv.googlegroups.com> <0062fc72$0$26941$c3 Message-ID: <7xmy3peq3s.fsf@ruckus.brouhaha.com> aahz at pythoncraft.com (Aahz) writes: > Standard Python idiom: > > try: > d[key] += value > except KeyError: > d[key] = value > > Maybe you need to re-think "appropriate". But more recent style prefers: d = collections.defaultdict(int) ... d[key] += value From metolone+gmane at gmail.com Sat Oct 17 16:16:45 2009 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Sat, 17 Oct 2009 13:16:45 -0700 Subject: umlauts References: <20091017135457.GA6335@alittleroisin> <7jub5rF37divlU4@mid.uni-berlin.de> Message-ID: "Diez B. Roggisch" wrote in message news:7jub5rF37divlU4 at mid.uni-berlin.de... [snip] > This is wierd. I looked at the site in FireFox - and it was displayed > correctly, including umlauts. Bringing up the info-dialog claims the page > is UTF-8, the XML itself says so as well (implicit, through the missing > declaration of an encoding) - but it clearly is *not* utf-8. > > One would expect google to be better at this... > > Diez According to the XML 1.0 specification: "Although an XML processor is required to read only entities in the UTF-8 and UTF-16 encodings, it is recognized that other encodings are used around the world, and it may be desired for XML processors to read entities that use them. In the absence of external character encoding information (such as MIME headers), parsed entities which are stored in an encoding other than UTF-8 or UTF-16 must begin with a text declaration..." So UTF-8 and UTF-16 are the defaults supported without an xml declaration in the absence of external encoding information. But we have external character encoding information: >>> f = urllib.urlopen("http://www.google.de/ig/api?weather=Muenchen") >>> f.headers.dict['content-type'] 'text/xml; charset=ISO-8859-1' So the page seems correct. -Mark From fordhaivat at gmail.com Sat Oct 17 16:26:35 2009 From: fordhaivat at gmail.com (Someone Something) Date: Sat, 17 Oct 2009 16:26:35 -0400 Subject: Threading from a class Message-ID: I'm trying to write a IRC client that has to have a method inside class Client that has to start a new thread that goes to run() which is in the same class. I'm not really understanding all the threading tutorials i've found. Can someone help? p.s. trying to use the threading module, not the thread module. -------------- next part -------------- An HTML attachment was scrubbed... URL: From aahz at pythoncraft.com Sat Oct 17 16:27:03 2009 From: aahz at pythoncraft.com (Aahz) Date: 17 Oct 2009 13:27:03 -0700 Subject: The rap against "while True:" loops References: <01ccc46d-5ea9-4dfe-ba22-699c6b859f00@v36g2000yqv.googlegroups.com> <7xmy3peq3s.fsf@ruckus.brouhaha.com> Message-ID: In article <7xmy3peq3s.fsf at ruckus.brouhaha.com>, Paul Rubin wrote: >aahz at pythoncraft.com (Aahz) writes: >> >> Standard Python idiom: >> >> try: >> d[key] += value >> except KeyError: >> d[key] = value >> >> Maybe you need to re-think "appropriate". > >But more recent style prefers: > > d = collections.defaultdict(int) > ... > d[key] += value That was a trivial example; non-trivial examples not addressed by defaultdict are left as an exercise for the reader. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "To me vi is Zen. To use vi is to practice zen. Every command is a koan. Profound to the user, unintelligible to the uninitiated. You discover truth everytime you use it." --reddy at lion.austin.ibm.com From dickinsm at gmail.com Sat Oct 17 16:32:32 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Sat, 17 Oct 2009 13:32:32 -0700 (PDT) Subject: restriction on sum: intentional bug? References: Message-ID: <7e905311-c561-4b93-9414-f873e6fee533@j19g2000yqk.googlegroups.com> On Oct 17, 3:27?pm, a... at pythoncraft.com (Aahz) wrote: > In article <0062f568$0$26941$c3e8... at news.astraweb.com>, > Steven D'Aprano ? wrote: > > (For the record, summing lists is O(N**2), and unlike strings, there's no > > optimization in CPython to avoid the slow behaviour.) > > Are you sure? The O(N**2) claim surprised me too, but it certainly looks that way. Here's a script to produce some timings: def concat1(list_of_lists): return sum(list_of_lists, []) def concat2(list_of_lists): acc = [] for l in list_of_lists: acc = acc + l return acc def concat3(list_of_lists): acc = [] for l in list_of_lists: acc += l return acc def concat4(list_of_lists): acc = [] for l in list_of_lists: acc.extend(l) return acc test_list = [[i] for i in xrange(100000)] from timeit import Timer for fn in ["concat1", "concat2", "concat3", "concat4"]: t = Timer(fn + "(test_list)", "from __main__ import test_list, " + fn) print fn, t.timeit(number=3)/3.0 On my machine (OS X 10.5/Core 2 Duo), under Python 2.7 svn I get: newton:trunk dickinsm$ ./python.exe ~/time_list_sum.py concat1 48.1459733645 concat2 48.4200883706 concat3 0.0146766503652 concat4 0.0184679826101 For some reason that I don't really understand, the CPython source does the equivalent of concat2 instead of concat3. See the builtin_sum function in http://svn.python.org/view/python/trunk/Python/bltinmodule.c?view=markup and scroll past the special cases for ints and floats. After a one- line source change, replacing the PyNumber_Add call with PyNumber_InPlaceAdd, I get the following results: newton:trunk dickinsm$ ./python.exe ~/time_list_sum.py concat1 0.0106019973755 concat2 48.0212899844 concat3 0.0138022899628 concat4 0.0179653167725 -- Mark From aahz at pythoncraft.com Sat Oct 17 16:49:30 2009 From: aahz at pythoncraft.com (Aahz) Date: 17 Oct 2009 13:49:30 -0700 Subject: restriction on sum: intentional bug? References: <0062f568$0$26941$c3e8da3@news.astraweb.com> <7e905311-c561-4b93-9414-f873e6fee533@j19g2000yqk.googlegroups.com> Message-ID: In article <7e905311-c561-4b93-9414-f873e6fee533 at j19g2000yqk.googlegroups.com>, Mark Dickinson wrote: > >For some reason that I don't really understand, the CPython source does >the equivalent of concat2 instead of concat3. See the builtin_sum >function in > >http://svn.python.org/view/python/trunk/Python/bltinmodule.c?view=3Dmarkup > >and scroll past the special cases for ints and floats. After a >one- line source change, replacing the PyNumber_Add call with >PyNumber_InPlaceAdd, Ahhh, I vaguely remember there being some discussion of this when sum() was introduced -- I think that using InPlaceAdd would have caused bad behavior when the initial list was referred to by multiple names. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "To me vi is Zen. To use vi is to practice zen. Every command is a koan. Profound to the user, unintelligible to the uninitiated. You discover truth everytime you use it." --reddy at lion.austin.ibm.com From dickinsm at gmail.com Sat Oct 17 17:00:36 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Sat, 17 Oct 2009 14:00:36 -0700 (PDT) Subject: restriction on sum: intentional bug? References: <0062f568$0$26941$c3e8da3@news.astraweb.com> <7e905311-c561-4b93-9414-f873e6fee533@j19g2000yqk.googlegroups.com> Message-ID: On Oct 17, 9:49?pm, a... at pythoncraft.com (Aahz) wrote: > Ahhh, I vaguely remember there being some discussion of this when sum() > was introduced -- I think that using InPlaceAdd would have caused bad > behavior when the initial list was referred to by multiple names. Ah yes. Good point. With my one-line modification, I get: Python 2.7a0 (trunk:75468M, Oct 17 2009, 21:57:02) [GCC 4.0.1 (Apple Inc. build 5493)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> acc = [] >>> sum(([i] for i in range(10)), acc) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> acc [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] There should probably be a test for this somewhere, to make sure that no-one else is tempted to make this change. -- Mark From davea at ieee.org Sat Oct 17 17:04:25 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 17 Oct 2009 17:04:25 -0400 Subject: print() In-Reply-To: <4ad9e0e6$1@dnews.tpgi.com.au> References: <4ad8dfc8$0$1112$4fafbaef@reader2.news.tin.it> <4ad9a40e$0$1094$4fafbaef@reader3.news.tin.it> <4ad9e0e6$1@dnews.tpgi.com.au> Message-ID: <4ADA3159.1000104@ieee.org> Lie Ryan wrote: > mattia wrote: >> Another question (always py3). How can I print only the first number >> after the comma of a division? >> e.g. print(8/3) --> 2.66666666667 >> I just want 2.6 (or 2.66) > > Are you sure you don't want that to be 2.7 or 2.67? Then you can use: > n = int(n * 10**2) / 10**2 > else if 2.7 pr 2.67 is what you wanted, you could use: > n = round(n, 2) > > Bad idea to use round() to make numbers format properly. No guarantees that the print logic will then print the number rounded the way you want. It could just as easily do 2.69999999999999 when you finally go to print it. Best to use format(), the way it was intended. Round to decimal while converting to decimal. Otherwise surprises await in dark corners. DaveA From http Sat Oct 17 17:05:50 2009 From: http (Paul Rubin) Date: 17 Oct 2009 14:05:50 -0700 Subject: The rap against "while True:" loops References: <01ccc46d-5ea9-4dfe-ba22-699c6b859f00@v36g2000yqv.googlegroups.com> <7xmy3peq3s.fsf@ruckus.brouhaha.com> Message-ID: <7xbpk57mtt.fsf@ruckus.brouhaha.com> aahz at pythoncraft.com (Aahz) writes: > > d = collections.defaultdict(int) > > ... > > d[key] += value > > That was a trivial example; non-trivial examples not addressed by > defaultdict are left as an exercise for the reader. Even in the "nontrivial" examples, I think avoiding the exception is stylistically preferable: d[key] = value + d.get(key, 0) It might be worth handling the exception in an inner loop where you want to avoid the cost of looking up key in the dictionary twice, but even that requires profiling to be sure. From alan.isaac at gmail.com Sat Oct 17 17:22:11 2009 From: alan.isaac at gmail.com (Alan G Isaac) Date: Sat, 17 Oct 2009 17:22:11 -0400 Subject: restriction on sum: intentional bug? In-Reply-To: References: Message-ID: On 10/17/2009 7:06 AM, Carl Banks wrote: > I'm basically saying here is, by shutting out strings from sum, > you don't really lose much in terms of duck typing, because duck > typing wouldn't have been that helpful anyway. That boils down to an argument for type checking whenever you cannot imagine my use case. I hope you do not write your own code that way... Here is a use case you might have ruled out with that approach. A PyX `path` defines `__add__` so that paths can be combined as subpaths of a single path. This is **VERY USEFUL**. Now if I have a list of paths, it is useful to to combine them: sum(pathlst,path()). Alan Isaac From nyamatongwe+thunder at gmail.com Sat Oct 17 17:43:01 2009 From: nyamatongwe+thunder at gmail.com (Neil Hodgson) Date: Sat, 17 Oct 2009 21:43:01 GMT Subject: umlauts In-Reply-To: References: <20091017135457.GA6335@alittleroisin> <7jub5rF37divlU4@mid.uni-berlin.de> Message-ID: The server is sniffing the User-Agent header to decide whether to send UTF-8 or ISO-8859-1. Try this code: import urllib2 r = urllib2.Request("http://www.google.de/ig/api?weather=Muenchen", None, {"User-Agent":"Mozilla/5.0"}) f = urllib2.urlopen(r) i = f.info() print(i) xml = f.read() f.close() print(xml) Neil From pavlovevidence at gmail.com Sat Oct 17 17:59:55 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Sat, 17 Oct 2009 14:59:55 -0700 (PDT) Subject: restriction on sum: intentional bug? References: Message-ID: <821c85fb-6029-4d26-a337-39ac35a56f3b@x5g2000prf.googlegroups.com> On Oct 17, 2:22?pm, Alan G Isaac wrote: > On 10/17/2009 7:06 AM, Carl Banks wrote: > > > I'm basically saying here is, by shutting out strings from sum, > > you don't really lose much in terms of duck typing, because duck > > typing wouldn't have been that helpful anyway. > > That boils down to an argument for type checking > whenever you cannot imagine my use case. ?I hope > you do not write your own code that way... No, it doesn't. It's an argument that the type check, IN THIS CASE, doesn't cost you much since duck typing isn't that useful here, IN THIS CASE, to begin with. Nothing more. Extrapolating this argument to be a general defense of type checking is something you did. > Here is a use case you might have ruled out with > that approach. ?A PyX `path` defines `__add__` > so that paths can be combined as subpaths of a > single path. This is ?**VERY USEFUL**. > Now if I have a list of paths, it is useful to > to combine them: sum(pathlst,path()). That's a fair counterargument. It definitely seems a lot more likely that some code could be written that works for a list of strings or a list of path components, meaning that it'd be desirable to have a single operation which works on both, for which sum() seems like it'd be a convenient candidate. Consider my argument weakened somewhat. Carl Banks From dickinsm at gmail.com Sat Oct 17 18:00:25 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Sat, 17 Oct 2009 15:00:25 -0700 (PDT) Subject: restriction on sum: intentional bug? References: <0062f568$0$26941$c3e8da3@news.astraweb.com> <7e905311-c561-4b93-9414-f873e6fee533@j19g2000yqk.googlegroups.com> Message-ID: On Oct 17, 9:49?pm, a... at pythoncraft.com (Aahz) wrote: > Ahhh, I vaguely remember there being some discussion of this when sum() > was introduced -- I think that using InPlaceAdd would have caused bad > behavior when the initial list was referred to by multiple names. Thanks for the pointer: I've now found the thread. It looks like Alex Martelli made this exact change 6 years ago, and then had to revert it because it changed behaviour: http://mail.python.org/pipermail/python-dev/2003-October/039511.html I've just checked in an extra test to test_builtin.py to make sure this bright idea isn't repeated. :) -- Mark From tjreedy at udel.edu Sat Oct 17 18:02:20 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 17 Oct 2009 18:02:20 -0400 Subject: slicing return iter? In-Reply-To: References: Message-ID: StarWing wrote: > I had checked it for serval times. maybe it's my inattention :-(. but > what i could find the nearest thing is itertools.islice. but it can't > process negative index A negative index -n is an abbreviation for len(sequence) - n. Since iterables in general do not have a length, islice(iterable) cannot handle the abbreviation. If you pass a sequence to islice, just un-abbreviate negative indexes. From timr at probo.com Sat Oct 17 18:09:08 2009 From: timr at probo.com (Tim Roberts) Date: Sat, 17 Oct 2009 15:09:08 -0700 Subject: help to convert c++ fonction in python References: Message-ID: Toff wrote: > >I'm trying to convert 2 c++ functions in python > >they come from wpkg client >https://wpkg.svn.sourceforge.net/svnroot/wpkg/wpkg-client/Sources/Components/XmlSettings.cpp > >they are >CString CXmlSettings::Crypt(CString str) >CString CXmlSettings::Decrypt(CString str) > >CAn someone help me? >i d'ont know much about c++ It's possible this solution is a bit too clever. import base64 import itertools key = ( 0x50, 0xF7, 0x82, 0x69, 0xEA, 0x2D, 0xDD, 0x2D, 0x6A, 0xB4, 0x33, 0x8F, 0xD5, 0xC7, 0x90, 0x9C, 0x22, 0x95, 0x61, 0xE5, 0x65, 0xF6, 0xB0, 0x4B, 0x94, 0x47, 0xB0, 0xBD, 0x73, 0x58, 0x56, 0x87, 0x79, 0x7B, 0xE6, 0xB0, 0xD2, 0x20, 0x28, 0xE1 ) def Crypt( s ): return base64.encode( ''.join( chr(ord(x)^y) for x,y in itertools.izip(s,itertools.cycle(key)) ) ) def Decrypt( s ) s1 = base64.decode( s ) return ''.join( chr(ord(x)^y) for x,y in itertools.izip(s1,itertools.cycle(key)) ) -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From timr at probo.com Sat Oct 17 18:15:51 2009 From: timr at probo.com (Tim Roberts) Date: Sat, 17 Oct 2009 15:15:51 -0700 Subject: help to convert c++ fonction in python References: Message-ID: <9egkd5151sokfi15a8j39qe939ho7ajfvo@4ax.com> Toff wrote: > >I'm trying to convert 2 c++ functions in python > >they come from wpkg client >https://wpkg.svn.sourceforge.net/svnroot/wpkg/wpkg-client/Sources/Components/XmlSettings.cpp > >they are >CString CXmlSettings::Crypt(CString str) >CString CXmlSettings::Decrypt(CString str) > >CAn someone help me? >i d'ont know much about c++ I should have tested before I posted. These work. There is one significant difference between my code and the C++ original: my code will not explode if the string to be encrypted is longer than 768 characters. Theirs will. key = ( 0x50, 0xF7, 0x82, 0x69, 0xEA, 0x2D, 0xDD, 0x2D, 0x6A, 0xB4, 0x33, 0x8F, 0xD5, 0xC7, 0x90, 0x9C, 0x22, 0x95, 0x61, 0xE5, 0x65, 0xF6, 0xB0, 0x4B, 0x94, 0x47, 0xB0, 0xBD, 0x73, 0x58, 0x56, 0x87, 0x79, 0x7B, 0xE6, 0xB0, 0xD2, 0x20, 0x28, 0xE1 ) import base64 import itertools def Crypt( s ): return base64.b64encode( ''.join( chr(ord(x)^y) for x,y in itertools.izip(s,itertools.cycle(key)) ) ) def Decrypt( s ): s1 = base64.b64decode( s ) return ''.join( chr(ord(x)^y) for x,y in itertools.izip(s,itertools.cycle(key)) ) s = 'Hello, there' print s t = Crypt(s) print t u = Decrypt(t) print s -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From tjreedy at udel.edu Sat Oct 17 18:17:36 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 17 Oct 2009 18:17:36 -0400 Subject: The rap against "while True:" loops In-Reply-To: <7xmy3peq3s.fsf@ruckus.brouhaha.com> References: <01ccc46d-5ea9-4dfe-ba22-699c6b859f00@v36g2000yqv.googlegroups.com> <0062fc72$0$26941$c3 <7xmy3peq3s.fsf@ruckus.brouhaha.com> Message-ID: Paul Rubin wrote: > aahz at pythoncraft.com (Aahz) writes: >> Standard Python idiom: >> >> try: >> d[key] += value >> except KeyError: >> d[key] = value >> >> Maybe you need to re-think "appropriate". > > But more recent style prefers: > > d = collections.defaultdict(int) > ... > d[key] += value Yes, the motivation was to reduce 4 lines to 1 line for a common use case, and not because of any sense of 'inappropriateness'. tjr From tjreedy at udel.edu Sat Oct 17 18:18:40 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 17 Oct 2009 18:18:40 -0400 Subject: The rap against "while True:" loops In-Reply-To: <0062fc72$0$26941$c3e8da3@news.astraweb.com> References: <01ccc46d-5ea9-4dfe-ba22-699c6b859f00@v36g2000yqv.googlegroups.com> <7jg84fF3536pjU1@mid.individual.net> <4cdf9c03-d8e1-406c-b960-1a5b535f9430@m38g2000yqd.googlegroups.com> <005fde84$0$26920$c3e8da3@news.astraweb.com> <0062fc72$0$26941$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Fri, 16 Oct 2009 18:30:50 +0100, Tim Rowe wrote: > >>> Also, using exceptions this way is a structured form of GOTO -- it's >>> easy to abuse and turn it into spaghetti code. Actually, not that easy >>> to abuse, because you can't jump back into the try block. It's more >>> like a multi-level break outside of a loop than a general GOTO. >> I don't think it's *only* the performance thing, it's also clarity. The >> understood meaning of throwing an exception is to say "something >> happened that shouldn't have". If one uses it when something has >> happened that *should* have, because it happens to have the right >> behaviour (even if the overhead doesn't matter), then one is >> misrepresenting the program logic. > > No, you have a fundamental misunderstanding. They're called exceptions, > not errors, because they represent exceptional cases. Often errors are > exceptional cases, but they're not the only sort of exceptional case. > > Python uses exceptions for flow control: e.g. for-loops swallow > StopIteration or IndexError to indicate the end of the loop. In the > context of a for-loop, StopIteration or IndexError doesn't represent an > error. It doesn't represent an unexpected case. It represents an > expected, but exceptional (special) case: we expect that most sequences > are finite, and it is normal to eventually reach the end of the sequence, > after which the loop must change behaviour. > > Similarly, it's hardly an *error* for [1, 2, 3].index(5) to fail -- who > is to say that the list is supposed to have 5 in it? ValueError (a > slightly misleading name in this situation) is used to indicate an > exceptional, but not unexpected, occurrence. > > Likewise, KeyboardInterrupt is used to allow the user to halt processing; > SystemExit is used to shut down the Python virtual machine; and warnings > are implemented using exceptions. There may be others among the built-ins > and standard library, but even if there aren't, there is plenty of > precedence for us to do the same. Nicely put. Programmers are exceptional people, but not erroneous, in spite of nerd stereotypes ;-). tjr From http Sat Oct 17 18:22:59 2009 From: http (Paul Rubin) Date: 17 Oct 2009 15:22:59 -0700 Subject: The rap against "while True:" loops References: <01ccc46d-5ea9-4dfe-ba22-699c6b859f00@v36g2000yqv.googlegroups.com> <0062fc72$0$26941$c3 <7xmy3peq3s.fsf@ruckus.brouhaha.com> Message-ID: <7xws2tace4.fsf@ruckus.brouhaha.com> Terry Reedy writes: > > d[key] += value > > Yes, the motivation was to reduce 4 lines to 1 line for a common use > case, and not because of any sense of 'inappropriateness'. Reducing 4 confusing lines to 1 clear one is almost always appropriate. From tjreedy at udel.edu Sat Oct 17 18:24:09 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 17 Oct 2009 18:24:09 -0400 Subject: which "dictionary with attribute-style access"? In-Reply-To: References: Message-ID: Aahz wrote: > In article , > Andreas Balogh wrote: >> My question to the Python specialists: which one is the most correct? >> Are there restrictions with regards to pickling or copy()? >> Which one should I choose? > > What's your goal? I'd probably do the dirt simple myself: > > class AttrDict(dict): > def __getattr__(self, attr): > if attr in self: > return self[attr] > else: > raise AttributeError Why the double lookup? Harking to another thread on using exceptions, try: return self[attr] except KeyError: raise AttributeError(attr) > def __setattr__(self, attr, value): > self[attr] = value > > d = AttrDict() > d.foo = 'bar' > print d.foo From aahz at pythoncraft.com Sat Oct 17 18:29:13 2009 From: aahz at pythoncraft.com (Aahz) Date: 17 Oct 2009 15:29:13 -0700 Subject: id( ) function question References: <46d12d880910140346p718877c0k4cbaa027f8716bf3@mail.gmail.com> <50697b2c0910141619k55df8857jeb1e71c037dc1668@mail.gmail.com> <4AD65FA0.2060201@cheimes.de> Message-ID: In article , Laszlo Nagy wrote: > >All right, I see your point now. So can we say, that the id function can >be used to tell if two mutable objects are different as long as they are >both alive during the comparison? Yes -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "To me vi is Zen. To use vi is to practice zen. Every command is a koan. Profound to the user, unintelligible to the uninitiated. You discover truth everytime you use it." --reddy at lion.austin.ibm.com From aahz at pythoncraft.com Sat Oct 17 18:32:45 2009 From: aahz at pythoncraft.com (Aahz) Date: 17 Oct 2009 15:32:45 -0700 Subject: which "dictionary with attribute-style access"? References: Message-ID: In article , Terry Reedy wrote: >Aahz wrote: >> In article , >> Andreas Balogh wrote: >>> >>> My question to the Python specialists: which one is the most correct? >>> Are there restrictions with regards to pickling or copy()? >>> Which one should I choose? >> >> What's your goal? I'd probably do the dirt simple myself: >> >> class AttrDict(dict): >> def __getattr__(self, attr): >> if attr in self: >> return self[attr] >> else: >> raise AttributeError > >Why the double lookup? Harking to another thread on using exceptions, > >try: > return self[attr] >except KeyError: > raise AttributeError(attr) For this purpose, it's almost entirely a stylistic difference; I happen to prefer using the test, but the other way is fine, too. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "To me vi is Zen. To use vi is to practice zen. Every command is a koan. Profound to the user, unintelligible to the uninitiated. You discover truth everytime you use it." --reddy at lion.austin.ibm.com From aahz at pythoncraft.com Sat Oct 17 18:34:14 2009 From: aahz at pythoncraft.com (Aahz) Date: 17 Oct 2009 15:34:14 -0700 Subject: restriction on sum: intentional bug? References: <7e905311-c561-4b93-9414-f873e6fee533@j19g2000yqk.googlegroups.com> Message-ID: In article , Mark Dickinson wrote: >On Oct 17, 9:49=A0pm, a... at pythoncraft.com (Aahz) wrote: >> >> Ahhh, I vaguely remember there being some discussion of this when sum() >> was introduced -- I think that using InPlaceAdd would have caused bad >> behavior when the initial list was referred to by multiple names. > >Thanks for the pointer: I've now found the thread. It looks like >Alex Martelli made this exact change 6 years ago, and then had to >revert it because it changed behaviour: > >http://mail.python.org/pipermail/python-dev/2003-October/039511.html > >I've just checked in an extra test to test_builtin.py to make sure >this bright idea isn't repeated. :) Thanks! I hope you added a comment to the code, too. ;-) And wow, sometimes my memory amazes me... -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "To me vi is Zen. To use vi is to practice zen. Every command is a koan. Profound to the user, unintelligible to the uninitiated. You discover truth everytime you use it." --reddy at lion.austin.ibm.com From tjreedy at udel.edu Sat Oct 17 18:40:37 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 17 Oct 2009 18:40:37 -0400 Subject: print() In-Reply-To: References: <4ad8dfc8$0$1112$4fafbaef@reader2.news.tin.it> Message-ID: Gabriel Genellina wrote: > Presumably he's using Python 3: And apparently not IDLE > Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit > (Intel)] on win32 > Type "help", "copyright", "credits" or "license" for more information. > p3> import sys > p3> sys.stdout.write("hello") > hello5 > > See http://bugs.python.org/issue6345 IDLE Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit (Intel)] on win32 >>> import sys >>> sys.stdout.write('abc') abc reported, for better or worse, in http://bugs.python.org/issue7163 Since interactive users do not usually use sys.stdout.write (versus print), the mixed output is not usually a problem. tjr From tjreedy at udel.edu Sat Oct 17 18:46:57 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 17 Oct 2009 18:46:57 -0400 Subject: executing a function/method from a variable In-Reply-To: References: Message-ID: Yves wrote: > What is the best way to execute a function which name is stored in a > variable ? One standard way is a dict mapping names to function objects. >>> numfuncs = {'int':int, 'float':float} >>> f='int' >>> numfuncs[f]('33') 33 Since attributes gets mapped to a dict, this is similar to previous answer. tjr From tjreedy at udel.edu Sat Oct 17 19:03:22 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 17 Oct 2009 19:03:22 -0400 Subject: restriction on sum: intentional bug? In-Reply-To: References: <7a9c25c20910160900p35600587v164b1c55c64718f1@mail.gmail.com> <4AD8A67C.2000508@tim.thechases.com> <7a9c25c20910161031q67342767pd744d455714f00aa@mail.gmail.com> Message-ID: Alan G Isaac wrote: > On 10/16/2009 8:16 PM, Terry Reedy wrote: >> The fact that two or three people who agree on something agree on the >> thing that they agree on confirms nothing. If you disagree with this, I think *you* are being silly. >> One could just as well argue >> that summing anything but numbers is semantically incoherent, not >> correct. Certainly, my dictionary points in that direction. Ditto. > Come on now, that is just a silly argument. And I think this is a silly response ;-). > And dictionaries are obviously irrelevant; Not when talking about the semantics of English words. > The only serious reason that has been offered > for the current behavior is that people who do > not know better will sum strings instead of > joining them, which is more efficient. That is > a pretty weak argument for breaking expectations > and so refusing to do duck typing that an error > is raise. Especially in a language like Python. > (As Tim and Peter make clear.) The absence of other responses is not the same as the absence of other possible responses. Some people have better things to do than rehash all the details of a past discussion. Nothing I have said bears on whether I would have voted for or against the current behavior. I have only addressed your to-me silly claim to have 'confirmed' the correctness of one position. Terry Jan Reedy From python at mrabarnett.plus.com Sat Oct 17 19:42:05 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 18 Oct 2009 00:42:05 +0100 Subject: restriction on sum: intentional bug? In-Reply-To: <821c85fb-6029-4d26-a337-39ac35a56f3b@x5g2000prf.googlegroups.com> References: <821c85fb-6029-4d26-a337-39ac35a56f3b@x5g2000prf.googlegroups.com> Message-ID: <4ADA564D.1070302@mrabarnett.plus.com> Carl Banks wrote: > On Oct 17, 2:22 pm, Alan G Isaac wrote: >> On 10/17/2009 7:06 AM, Carl Banks wrote: >> >>> I'm basically saying here is, by shutting out strings from sum, >>> you don't really lose much in terms of duck typing, because duck >>> typing wouldn't have been that helpful anyway. >> That boils down to an argument for type checking >> whenever you cannot imagine my use case. I hope >> you do not write your own code that way... > > No, it doesn't. It's an argument that the type check, IN THIS CASE, > doesn't cost you much since duck typing isn't that useful here, IN > THIS CASE, to begin with. Nothing more. > > Extrapolating this argument to be a general defense of type checking > is something you did. > > >> Here is a use case you might have ruled out with >> that approach. A PyX `path` defines `__add__` >> so that paths can be combined as subpaths of a >> single path. This is **VERY USEFUL**. >> Now if I have a list of paths, it is useful to >> to combine them: sum(pathlst,path()). > > That's a fair counterargument. It definitely seems a lot more likely > that some code could be written that works for a list of strings or a > list of path components, meaning that it'd be desirable to have a > single operation which works on both, for which sum() seems like it'd > be a convenient candidate. Consider my argument weakened somewhat. > Here's a suggestion: add a new special method '__sum__' for summing sequences. If 'sum' isn't given an initial value then the first value in the sequence is used as the initial value. The (remainder of) the sequence is passed to initial_value.__sum__ to perform the summation. If __sum__ isn't defined then the __add__ method is used repeatedly as at present. The 'str' and 'unicode' classes (although I'd expected it would be the 'bytes' and 'str' classes in Python 3) would define __sum__ as an efficient summation. Other classes like 'list' could do likewise. No more special cases! From debatem1 at gmail.com Sat Oct 17 19:48:46 2009 From: debatem1 at gmail.com (geremy condra) Date: Sat, 17 Oct 2009 19:48:46 -0400 Subject: help to convert c++ fonction in python In-Reply-To: <9egkd5151sokfi15a8j39qe939ho7ajfvo@4ax.com> References: <9egkd5151sokfi15a8j39qe939ho7ajfvo@4ax.com> Message-ID: On Sat, Oct 17, 2009 at 6:15 PM, Tim Roberts wrote: > Toff wrote: >> >>I'm trying to convert ?2 c++ functions ?in python >> >>they come from wpkg client >>https://wpkg.svn.sourceforge.net/svnroot/wpkg/wpkg-client/Sources/Components/XmlSettings.cpp >> >>they are >>CString CXmlSettings::Crypt(CString str) >>CString CXmlSettings::Decrypt(CString str) >> >>CAn someone help me? >>i d'ont know much about c++ > > I should have tested before I posted. ?These work. ?There is one > significant difference between my code and the C++ original: my code will > not explode if the string to be encrypted is longer than 768 characters. > Theirs will. > > > key = ( > ? ?0x50, 0xF7, 0x82, 0x69, 0xEA, 0x2D, 0xDD, 0x2D, 0x6A, 0xB4, > ? ?0x33, 0x8F, 0xD5, 0xC7, 0x90, 0x9C, 0x22, 0x95, 0x61, 0xE5, > ? ?0x65, 0xF6, 0xB0, 0x4B, 0x94, 0x47, 0xB0, 0xBD, 0x73, 0x58, > ? ?0x56, 0x87, 0x79, 0x7B, 0xE6, 0xB0, 0xD2, 0x20, 0x28, 0xE1 > ) > > import base64 > import itertools > > def Crypt( s ): > ? ?return base64.b64encode( > ? ? ? ?''.join( > ? ? ? ? ? ?chr(ord(x)^y) for x,y in itertools.izip(s,itertools.cycle(key)) > ? ? ? ?) > ? ?) > > def Decrypt( s ): > ? ?s1 = base64.b64decode( s ) > ? ?return ''.join( > ? ? ? ?chr(ord(x)^y) for x,y in itertools.izip(s,itertools.cycle(key)) > ? ?) > > s = 'Hello, there' > print s > t = Crypt(s) > print t > u = Decrypt(t) > print s > -- > Tim Roberts, timr at probo.com > Providenza & Boekelheide, Inc. > -- > http://mail.python.org/mailman/listinfo/python-list > For the love of baby kittens, please, please, please tell me that you do not believe this securely encrypts your data. Geremy Condra From debatem1 at gmail.com Sat Oct 17 20:13:42 2009 From: debatem1 at gmail.com (geremy condra) Date: Sat, 17 Oct 2009 20:13:42 -0400 Subject: help to convert c++ fonction in python In-Reply-To: References: <9egkd5151sokfi15a8j39qe939ho7ajfvo@4ax.com> <4eb0089f0910171657q5c85df9ew4cb9699032f49c85@mail.gmail.com> Message-ID: On Sat, Oct 17, 2009 at 7:57 PM, David Robinow wrote: > On Sat, Oct 17, 2009 at 7:48 PM, geremy condra wrote: >> For the love of baby kittens, please, please, please tell me that >> you do not believe this securely encrypts your data. > ?Yeah, I think it's pretty good. > Can you do better? > Trivially. Use AES, 3DES, any standard cryptosystem- there are literally dozens of excellent, well-studied implementations in both C++ and Python, and hardware implementations on many processors. The cipher listed will fall in a single round of chosen plaintext attacks or chosen ciphertext attacks, and with a keylength of 40 bytes against a message length of 768 will give me roughly 19 windows on a single encryption. Frequency analysis is therefore going to be extremely profitable, not to mention trivially easy. Geremy Condra From fordhaivat at gmail.com Sat Oct 17 21:07:45 2009 From: fordhaivat at gmail.com (Someone Something) Date: Sat, 17 Oct 2009 21:07:45 -0400 Subject: Threading from a class In-Reply-To: References: Message-ID: anyone? On Sat, Oct 17, 2009 at 4:26 PM, Someone Something wrote: > I'm trying to write a IRC client that has to have a method inside class > Client that has to start a new thread that goes to run() which is in the > same class. I'm not really understanding all the threading tutorials i've > found. Can someone help? > > p.s. trying to use the threading module, not the thread module. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at rcn.com Sat Oct 17 21:09:21 2009 From: python at rcn.com (Raymond Hettinger) Date: Sat, 17 Oct 2009 18:09:21 -0700 (PDT) Subject: slicing return iter? References: Message-ID: [StarWing] > > > sometimes I want to iterate a part of a sequence. but don't want to > > > copy it. i.e. . . . > I had checked it for serval times. maybe it's my inattention :-(. but > what i could find the nearest thing is itertools.islice. but it can't > process negative index -- that's supported by slice. so I need > something, bind object with slice, and return a iter. I can find > anything like it...:-( If it really is a sequence (with len and getitem), you can write your own indexing iterator: def myslice(seq, start, stop, step): 'Allow forward or backwards iteration over a subslice' for i in range(start, stop, step): yield seq[i] Raymond From metolone+gmane at gmail.com Sat Oct 17 21:13:27 2009 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Sat, 17 Oct 2009 18:13:27 -0700 Subject: print() References: <4ad8dfc8$0$1112$4fafbaef@reader2.news.tin.it> Message-ID: "Terry Reedy" wrote in message news:hbdh51$g0f$1 at ger.gmane.org... > Gabriel Genellina wrote: > >> Presumably he's using Python 3: > > And apparently not IDLE > >> Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit >> (Intel)] on win32 >> Type "help", "copyright", "credits" or "license" for more information. >> p3> import sys >> p3> sys.stdout.write("hello") >> hello5 >> >> See http://bugs.python.org/issue6345 > > IDLE > > Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit (Intel)] > on win32 > > >>> import sys > >>> sys.stdout.write('abc') > abc > > reported, for better or worse, in http://bugs.python.org/issue7163 > > Since interactive users do not usually use sys.stdout.write (versus > print), the mixed output is not usually a problem. > > tjr Apparently, Pythonwin has the same "problem": PythonWin 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit (Intel)] on win32. Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin' for further copyright information. >>> import sys >>> sys.stdout.write('hello') hello >>> print(sys.stdout.write('hello')) helloNone -Mark From python at mrabarnett.plus.com Sat Oct 17 21:27:35 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 18 Oct 2009 02:27:35 +0100 Subject: Threading from a class In-Reply-To: References: Message-ID: <4ADA6F07.4000704@mrabarnett.plus.com> Someone Something wrote: > anyone? > > On Sat, Oct 17, 2009 at 4:26 PM, Someone Something > wrote: > > I'm trying to write a IRC client that has to have a method inside > class Client that has to start a new thread that goes to run() which > is in the same class. I'm not really understanding all the threading > tutorials i've found. Can someone help? > > p.s. trying to use the threading module, not the thread module. > # Written for Python v2.6 import threading import time class Client(object): def test_thread(self): print "starting thread" self.thread = threading.Thread(target=self.run) self.thread.start() print "sleeping for 6 seconds" time.sleep(6) print "woken up" self.thread.join() def run(self): for i in range(5): print i time.sleep(1) c = Client() c.test_thread() From deets at nospam.web.de Sat Oct 17 21:28:40 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sun, 18 Oct 2009 03:28:40 +0200 Subject: umlauts In-Reply-To: References: <25ab29ae-eb30-452a-9595-22cb976657e8@f20g2000prn.googlegroups.com> <4AD9F5E1.7060507@nospam.web.de> <36acb7c2-ea98-476a-af4c-1ea4d2f24934@g1g2000pra.googlegroups.com> <7jug1rF371nkpU1@mid.uni-berlin.de> Message-ID: <7jv9a9F363gkqU1@mid.uni-berlin.de> Arian Kuschki schrieb: > Whoa, that was quick! Thanks for all the answers, I'll try to recapitulate > >> What does this show you in your interactive interpreter? >> >>>>> print "\xc3\xb6" >> ? >> >> For me, it's o-umlaut, ?. This is because the above bytes are the >> sequence for ? in utf-8. >> >> If this shows something else, you need to adjust your terminal settings. > > for me it also prints the correct o-umlaut (?), so that was not the problem. > > > All of the below result in xml that shows all umlauts correctly when printed: > > xml.decode("cp1252") > xml.decode("cp1252").encode("utf-8") > xml.decode("iso-8859-1") > xml.decode("iso-8859-1").encode("utf-8") > > But when I want to parse the xml then, it only works if I > do both decode and encode. If I only decode, I get the following error: > SAXParseException: :1:1: not well-formed (invalid token) > > Do I understand right that since the encoding was not specified in the xml > response, it should have been utf-8 by default? And that if it had indeed been utf-8 I > would not have had the encoding problem in the first place? Yes. XML without explicit encoding is implicitly UTF-8, and the page is borked using cp* or latin* without saying so. Diez From deets at nospam.web.de Sat Oct 17 21:41:03 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sun, 18 Oct 2009 03:41:03 +0200 Subject: umlauts In-Reply-To: <7jv9a9F363gkqU1@mid.uni-berlin.de> References: <25ab29ae-eb30-452a-9595-22cb976657e8@f20g2000prn.googlegroups.com> <4AD9F5E1.7060507@nospam.web.de> <36acb7c2-ea98-476a-af4c-1ea4d2f24934@g1g2000pra.googlegroups.com> <7jug1rF371nkpU1@mid.uni-berlin.de> <7jv9a9F363gkqU1@mid.uni-berlin.de> Message-ID: <7jva1gF37el21U1@mid.uni-berlin.de> Diez B. Roggisch schrieb: > Arian Kuschki schrieb: >> Whoa, that was quick! Thanks for all the answers, I'll try to >> recapitulate >> >>> What does this show you in your interactive interpreter? >>> >>>>>> print "\xc3\xb6" >>> ? >>> >>> For me, it's o-umlaut, ?. This is because the above bytes are the >>> sequence for ? in utf-8. >>> >>> If this shows something else, you need to adjust your terminal settings. >> >> for me it also prints the correct o-umlaut (?), so that was not the >> problem. >> >> >> All of the below result in xml that shows all umlauts correctly when >> printed: >> >> xml.decode("cp1252") >> xml.decode("cp1252").encode("utf-8") >> xml.decode("iso-8859-1") >> xml.decode("iso-8859-1").encode("utf-8") >> >> But when I want to parse the xml then, it only works if I >> do both decode and encode. If I only decode, I get the following error: >> SAXParseException: :1:1: not well-formed (invalid token) >> >> Do I understand right that since the encoding was not specified in the >> xml response, it should have been utf-8 by default? And that if it had >> indeed been utf-8 I would not have had the encoding problem in the >> first place? > > Yes. XML without explicit encoding is implicitly UTF-8, and the page is > borked using cp* or latin* without saying so. Ok, after reading some other posts in this thread this assumption seems not to hold. HTTP-protocol allows for other encodings to be implicitly given. Which I think is an atrocity. Diez From fordhaivat at gmail.com Sat Oct 17 21:58:06 2009 From: fordhaivat at gmail.com (Someone Something) Date: Sat, 17 Oct 2009 21:58:06 -0400 Subject: Multiple files Message-ID: I was trying to write a program with just one class and it was working fine. Then, I seperated that class into two different files and made the objects and called the methods in a third (client.py IO.py main.py). Now, when I use the command: python client.py IO.py main.py Nothing prints. I think its only interpreting client.py (the objects are declared in main.py) How can I fix this? -------------- next part -------------- An HTML attachment was scrubbed... URL: From benjamin.kaplan at case.edu Sat Oct 17 22:05:53 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Sat, 17 Oct 2009 22:05:53 -0400 Subject: Multiple files In-Reply-To: References: Message-ID: On Sat, Oct 17, 2009 at 9:58 PM, Someone Something wrote: > I was trying to write a program with just one class and it was working fine. > Then, I seperated that class into two different files and made the objects > and called the methods in a third (client.py IO.py main.py). Now, when I use > the command: > > python client.py IO.py main.py > > Nothing prints. I think its only interpreting client.py (the objects are > declared in main.py) > > How can I fix this? Python is a scripting language that compiles files as needed. You don't need to declare every module you're using Run only the script you need. python main.py When the script is run, the current directory is automatically added to the python path, the list of directories python looks through for modules (stored in sys.path). When python sees "import client", it looks through that path for client.py and then compiles it if it's not already been compiled. > > -- > http://mail.python.org/mailman/listinfo/python-list > > From turian at gmail.com Sat Oct 17 22:54:23 2009 From: turian at gmail.com (Joseph Turian) Date: Sat, 17 Oct 2009 19:54:23 -0700 (PDT) Subject: SimpleXMLRPCServer clobbering sys.stderr? (2.5.2) Message-ID: I was having a mysterious problem with SimpleXMLRPCServer. (I am using Python 2.5.2) The request handlers were sometimes failing without any error message to the log output. What I discovered was perplexing. I had some 'print' statements in the handers that, assuming the request would be handled, would print just fine. When I switched to 'print >> sys.stderr', the request handlers would just fail completely, and not make the sys.stderr output that I desired. It seems that SimpleXMLRPCServer is clobbering stderr in some bizarre and silent-error-causing way. I can't really find any documentation of explanation of this phenomenon. Could someone please illuminate it for me? Best, Joseph From rylesny at gmail.com Sat Oct 17 22:55:14 2009 From: rylesny at gmail.com (ryles) Date: Sat, 17 Oct 2009 19:55:14 -0700 (PDT) Subject: Installation question 2.5.4 References: <27d8850b-df32-49cd-a52c-6df870cdc80d@37g2000yqm.googlegroups.com> Message-ID: On Sat, Oct 17, 2009 at 9:55 PM, JimR wrote: > Thanks. As it turned out, I needed /usr/local/python instead of /usr/local > as the prefix. After setting that, all worked as it should. From alan.isaac at gmail.com Sat Oct 17 23:04:05 2009 From: alan.isaac at gmail.com (Alan G Isaac) Date: Sat, 17 Oct 2009 23:04:05 -0400 Subject: restriction on sum: intentional bug? In-Reply-To: References: <7a9c25c20910160900p35600587v164b1c55c64718f1@mail.gmail.com> <4AD8A67C.2000508@tim.thechases.com> <7a9c25c20910161031q67342767pd744d455714f00aa@mail.gmail.com> Message-ID: <-emdnQiiKcg2GEfXnZ2dnUVZ_hWdnZ2d@rcn.net> >> On 10/16/2009 8:16 PM, Terry Reedy wrote: >>> The fact that two or three people who agree on something agree on the >>> thing that they agree on confirms nothing. On 10/17/2009 7:03 PM, Terry Reedy wrote: > If you disagree with this, I think *you* are being silly. Well, ... > Alan G Isaac wrote: >> Of course the numbers do not matter. >> The *reasons* matter. But you cut that... It is a good idea not to quote too selectively if you want to look like you are actually engaging the argument. Anything else looks very self-serving. On 10/17/2009 7:03 PM, Terry Reedy wrote: > Nothing I have said bears on whether I would have voted for or against > the current behavior. While that certainly provides you with convenient shelter, it does not move the discussion forward. Cheers, Alan Isaac From jabba.laci at gmail.com Sat Oct 17 23:24:01 2009 From: jabba.laci at gmail.com (Jabba Laci) Date: Sat, 17 Oct 2009 23:24:01 -0400 Subject: list to tuple and vice versa Message-ID: <310fbb00910172024x10695160h1100b7a24aec77d8@mail.gmail.com> Hi, I have some difficulties with list -> tuple conversion: t = ('a', 'b') li = list(t) # tuple -> list, works print li # ['a', 'b'] tu = tuple(li) # list -> tuple, error print tu # what I'd expect: ('a', 'b') The error message is: "TypeError: 'tuple' object is not callable". Thanks, Laszlo From clp2 at rebertia.com Sat Oct 17 23:32:55 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 17 Oct 2009 20:32:55 -0700 Subject: list to tuple and vice versa In-Reply-To: <310fbb00910172024x10695160h1100b7a24aec77d8@mail.gmail.com> References: <310fbb00910172024x10695160h1100b7a24aec77d8@mail.gmail.com> Message-ID: <50697b2c0910172032j4a6f60c7p46f38347123a03d9@mail.gmail.com> On Sat, Oct 17, 2009 at 8:24 PM, Jabba Laci wrote: > Hi, > > I have some difficulties with list -> tuple conversion: > > t = ('a', 'b') > li = list(t) ? # tuple -> list, works > print li ? # ['a', 'b'] > > tu = tuple(li) ? # list -> tuple, error > print tu ? # what I'd expect: ('a', 'b') > > The error message is: "TypeError: 'tuple' object is not callable". You created a variable named "tuple" somewhere, which is shadowing the built-in type. Rename that variable to something else. Cheers, Chris -- http://blog.rebertia.com From ben+python at benfinney.id.au Sat Oct 17 23:33:17 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 18 Oct 2009 14:33:17 +1100 Subject: list to tuple and vice versa References: Message-ID: <87pr8lml4y.fsf@benfinney.id.au> Jabba Laci writes: > Hi, > > I have some difficulties with list -> tuple conversion: > > t = ('a', 'b') > li = list(t) # tuple -> list, works > print li # ['a', 'b'] > > tu = tuple(li) # list -> tuple, error > print tu # what I'd expect: ('a', 'b') Works fine for me: Python 2.5.4 (r254:67916, Sep 26 2009, 11:00:02) [GCC 4.3.4] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> t = ('a', 'b') >>> li = list(t) >>> print li ['a', 'b'] >>> >>> tu = tuple(li) >>> print tu ('a', 'b') > The error message is: "TypeError: 'tuple' object is not callable". Nothing in your example code is calling a tuple object. This makes me suspect that what you showed us is not what you actually tried. You might want to try the above session yourself and paste the resulting session *literally* (rather than re-typing) as I did above, so we can see what you're seeing. -- \ ?If you ever fall off the Sears Tower, just go real limp, | `\ because maybe you'll look like a dummy and people will try to | _o__) catch you because, hey, free dummy.? ?Jack Handey | Ben Finney From jabba.laci at gmail.com Sat Oct 17 23:36:48 2009 From: jabba.laci at gmail.com (Jabba Laci) Date: Sat, 17 Oct 2009 23:36:48 -0400 Subject: list to tuple and vice versa In-Reply-To: <50697b2c0910172032j4a6f60c7p46f38347123a03d9@mail.gmail.com> References: <310fbb00910172024x10695160h1100b7a24aec77d8@mail.gmail.com> <50697b2c0910172032j4a6f60c7p46f38347123a03d9@mail.gmail.com> Message-ID: <310fbb00910172036t146b9aebuea1eb4b5c561a4a5@mail.gmail.com> >> The error message is: "TypeError: 'tuple' object is not callable". > > You created a variable named "tuple" somewhere, which is shadowing the > built-in type. Rename that variable to something else. Right, it was my bad. After removal the tuple() function works perfectly. Thanks, Laszlo From ben+python at benfinney.id.au Sun Oct 18 00:19:00 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 18 Oct 2009 15:19:00 +1100 Subject: list to tuple and vice versa References: <310fbb00910172024x10695160h1100b7a24aec77d8@mail.gmail.com> <50697b2c0910172032j4a6f60c7p46f38347123a03d9@mail.gmail.com> Message-ID: <87hbtxmj0r.fsf@benfinney.id.au> Jabba Laci writes: > Right, it was my bad. After removal the tuple() function works > perfectly. Note that, though it is callable, ?tuple? is not a function but a type: >>> tuple >>> len You can use the built-in ?type? type to get the type of any object: >>> foo = 12 >>> type(foo) >>> bar = 1, 2, 3 >>> type(bar) >>> type(tuple) >>> type(len) >>> type(type) -- \ ?Pinky, are you pondering what I'm pondering?? ?Uh, I think so | `\ Brain, but this time, you wear the tutu.? ?_Pinky and The Brain_ | _o__) | Ben Finney From gagsl-py2 at yahoo.com.ar Sun Oct 18 00:52:27 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sun, 18 Oct 2009 02:52:27 -0200 Subject: SimpleXMLRPCServer clobbering sys.stderr? (2.5.2) References: Message-ID: En Sat, 17 Oct 2009 23:54:23 -0300, Joseph Turian escribi?: > I was having a mysterious problem with SimpleXMLRPCServer. (I am using > Python 2.5.2) I'd start updating Python to the latest 2.5 release: 2.5.4 > The request handlers were sometimes failing without any error message > to the log output. > > What I discovered was perplexing. > I had some 'print' statements in the handers that, assuming the > request would be handled, would print just fine. When I switched to > 'print >> sys.stderr', the request handlers would just fail > completely, and not make the sys.stderr output that I desired. Perhaps you need to flush the file also? sys.stderr.flush() > It seems that SimpleXMLRPCServer is clobbering stderr in some bizarre > and silent-error-causing way. > I can't really find any documentation of explanation of this > phenomenon. > > Could someone please illuminate it for me? XMLRPCServer doesn't reassign or alter sys.stderr, just uses it in the log_message method. I'd look in some other place... -- Gabriel Genellina From steve at REMOVE-THIS-cybersource.com.au Sun Oct 18 00:58:21 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 18 Oct 2009 04:58:21 GMT Subject: restriction on sum: intentional bug? References: <0062f568$0$26941$c3e8da3@news.astraweb.com> Message-ID: <02ea8f24$0$1384$c3e8da3@news.astraweb.com> On Sat, 17 Oct 2009 07:27:44 -0700, Aahz wrote: >>(For the record, summing lists is O(N**2), and unlike strings, there's >>no optimization in CPython to avoid the slow behaviour.) > > Are you sure? Not 100% -- I haven't read the CPython source code. But I have done timing tests for repeated concatenation of strings, and demonstrated to my own satisfaction that CPython can avoid O(N**2) behaviour under certain circumstances (but not all). I've repeated those same tests using lists instead of strings, and seen O(N**2) behaviour under all circumstances. This was under Python 2.5 or 2.6, not 3.1. The situation may have changed. -- Steven From weasley_wx at sina.com Sun Oct 18 01:02:27 2009 From: weasley_wx at sina.com (StarWing) Date: Sat, 17 Oct 2009 22:02:27 -0700 (PDT) Subject: slicing return iter? References: Message-ID: <6ab68e78-1272-4226-9027-51627a9096bf@x25g2000prf.googlegroups.com> On 10?18?, ??9?09?, Raymond Hettinger wrote: > [StarWing] > > > > > sometimes I want to iterate a part of a sequence. but don't want to > > > > copy it. i.e. > . . . > > I had checked it for serval times. maybe it's my inattention :-(. but > > what i could find the nearest thing is itertools.islice. but it can't > > process negative index -- that's supported by slice. so I need > > something, bind object with slice, and return a iter. I can find > > anything like it...:-( > > If it really is a sequence (with len and getitem), you can write your > own indexing iterator: > > def myslice(seq, start, stop, step): > 'Allow forward or backwards iteration over a subslice' > for i in range(start, stop, step): > yield seq[i] > > Raymond Thank you. but it can't support negative index :-( Terry Reedy is right. since a range (or xrange or slice etc.) don't have a length, so it can't support a negative index. so the best way to do it is that binding a range with a object. and return a iter. I think, why standard library didn't have anything like that, that will be very useful. maybe we should have a builtin functoin itertools.bslice (stands for bind slice)... From weasley_wx at sina.com Sun Oct 18 01:08:54 2009 From: weasley_wx at sina.com (StarWing) Date: Sat, 17 Oct 2009 22:08:54 -0700 (PDT) Subject: list to tuple and vice versa References: <310fbb00910172024x10695160h1100b7a24aec77d8@mail.gmail.com> <50697b2c0910172032j4a6f60c7p46f38347123a03d9@mail.gmail.com> <87hbtxmj0r.fsf@benfinney.id.au> Message-ID: <7f68983f-2a87-4cbd-9c6a-58923d439a13@m3g2000pri.googlegroups.com> On 10?18?, ??12?19?, Ben Finney wrote: > Jabba Laci writes: > > Right, it was my bad. After removal the tuple() function works > > perfectly. > > Note that, though it is callable, ?tuple? is not a function but a type: > > ? ? >>> tuple > ? ? > ? ? >>> len > ? ? > > You can use the built-in ?type? type to get the type of any object: > > ? ? >>> foo = 12 > ? ? >>> type(foo) > ? ? > ? ? >>> bar = 1, 2, 3 > ? ? >>> type(bar) > ? ? > ? ? >>> type(tuple) > ? ? > ? ? >>> type(len) > ? ? > ? ? >>> type(type) > ? ? > > -- > ?\ ? ? ??Pinky, are you pondering what I'm pondering?? ?Uh, I think so | > ? `\ ?Brain, but this time, you wear the tutu.? ?_Pinky and The Brain_ | > _o__) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| > Ben Finney A type is always callable. call a type will call its __init__ special method (or and __new__ special method together). From steve at REMOVE-THIS-cybersource.com.au Sun Oct 18 01:18:20 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 18 Oct 2009 05:18:20 GMT Subject: The rap against "while True:" loops References: <01ccc46d-5ea9-4dfe-ba22-699c6b859f00@v36g2000yqv.googlegroups.com> <7xmy3peq3s.fsf@ruckus.brouhaha.com> <7xws2tace4.fsf@ruckus.brouhaha.com> Message-ID: <02ea93d3$0$1384$c3e8da3@news.astraweb.com> On Sat, 17 Oct 2009 15:22:59 -0700, Paul Rubin wrote: > Terry Reedy writes: >> > d[key] += value >> >> Yes, the motivation was to reduce 4 lines to 1 line for a common use >> case, and not because of any sense of 'inappropriateness'. > > Reducing 4 confusing lines to 1 clear one is almost always appropriate. This is true, but there is nothing confusing about "Asking for forgiveness is better than asking for permission". For the record, the four lines Paul implies are "confusing" are: try: d[key] += value except KeyError: d[key] = value Paul, if that confuses you, perhaps you should consider a change of career. *wink* On the other hand: d = collections.defaultdict(int) d[key] += value is confusing, at least to me. I would expect the argument to defaultdict to be the value used as a default, not a callable. In other words, I would expect the above to try adding the type `int` to the integer `value` and fail, and wonder why it wasn't written as: d = collections.defaultdict(0) d[key] += value Having thought about it, I can see why defaultdict is done that way, instead of this: class MyDefaultDict(dict): def __init__(self, default=None): self._default = default def __getitem__(self, key): if key in self: return self[key] else: return self._default And here's why this doesn't work too well: >>> d = MyDefaultDict([]) >>> d['a'] = [1,2] >>> d['b'] = [3,4,5] >>> d {'a': [1, 2], 'b': [3, 4, 5]} >>> d['c'] += [6,7] >>> d {'a': [1, 2], 'c': [6, 7], 'b': [3, 4, 5]} So far so good. But wait: >>> d['d'] += [8] >>> d {'a': [1, 2], 'c': [6, 7, 8], 'b': [3, 4, 5], 'd': [6, 7, 8]} Oops. So even though it's initially surprising and even confusing, the API for collections.defaultdict functionally more useful. -- Steven From half.italian at gmail.com Sun Oct 18 01:19:07 2009 From: half.italian at gmail.com (Sean DiZazzo) Date: Sat, 17 Oct 2009 22:19:07 -0700 (PDT) Subject: ftplib connection fails with multiple nics References: <9adf4434-321d-46f5-89e2-1341d5f7d7d6@w37g2000prg.googlegroups.com> Message-ID: <2724c192-c005-426a-b108-b6f7fefcb6a3@i12g2000prg.googlegroups.com> On Oct 16, 4:51?pm, Sean DiZazzo wrote: > Hi all, > > I'm trying to connect to an ftp site from a windows machine with two > nics going to two different networks, but I keep getting the below > exception: > > Traceback (most recent call last): > ? File "ftp.pyo", line 70, in connect > ? File "ftp.pyo", line 17, in __init__ > ? File "ftplib.pyo", line 131, in connect > ? File "socket.pyo", line 498, in create_connection > gaierror: [Errno 10093] getaddrinfo failed > > I think it is because of the two nics, because the code runs fine on > other machines. ?Any ideas on how to fix this? > > TIA. > > ~Sean What does socket.getaddrinfo() rely on?? From weasley_wx at sina.com Sun Oct 18 01:22:55 2009 From: weasley_wx at sina.com (StarWing) Date: Sat, 17 Oct 2009 22:22:55 -0700 (PDT) Subject: a gap of do....while? Message-ID: <74900bb0-fa04-4d34-893d-b109c628fc81@r24g2000prf.googlegroups.com> okay, I think somethings do....while is useful, but why python didn't have it? in lisp, we can (while (progn ....)) and in all other language we have do...while. but in python, we only can: cond = 1 while cond: cond = 0 ..... if ....: cond = 1 has any polite way to handle this? From aahz at pythoncraft.com Sun Oct 18 01:23:24 2009 From: aahz at pythoncraft.com (Aahz) Date: 17 Oct 2009 22:23:24 -0700 Subject: python along or bash combined with python (for manipulating files) References: <38890afc-c542-478a-bbe7-9a63dc6c9bed@j9g2000vbp.googlegroups.com> Message-ID: In article <38890afc-c542-478a-bbe7-9a63dc6c9bed at j9g2000vbp.googlegroups.com>, TerryP wrote: > >Very sophisticated scripts are possible using bash and ksh, there is >even a form of ksh that has tk capabilities! (tksh). The Python and >Bourne-derived languages are however fundamentally different >creatures, and use very different data models. You should **not** >write Python (or Perl) scripts as if they were shell scripts -- doing >so is very bad practice. When you want a shell script, write a shell >script. When you write a Python script, write a Python script. It >really is that simple. Oh, well, I guess I follow bad practice a lot. Shame on me. (That is, I disagree that it's bad practice to use Python as if it were a straight scripting language, os.system() and all. I prefer using Python because it makes it easy to upgrade scripts as needed.) -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "To me vi is Zen. To use vi is to practice zen. Every command is a koan. Profound to the user, unintelligible to the uninitiated. You discover truth everytime you use it." --reddy at lion.austin.ibm.com From ben+python at benfinney.id.au Sun Oct 18 01:32:53 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 18 Oct 2009 16:32:53 +1100 Subject: list to tuple and vice versa References: <310fbb00910172024x10695160h1100b7a24aec77d8@mail.gmail.com> <50697b2c0910172032j4a6f60c7p46f38347123a03d9@mail.gmail.com> <87hbtxmj0r.fsf@benfinney.id.au> <7f68983f-2a87-4cbd-9c6a-58923d439a13@m3g2000pri.googlegroups.com> Message-ID: <87d44lmflm.fsf@benfinney.id.au> StarWing writes: > On 10?18?, ??12?19?, Ben Finney wrote: > > Jabba Laci writes: > > > Right, it was my bad. After removal the tuple() function works > > > perfectly. > > > > Note that, though it is callable, ?tuple? is not a function but a > > type: > > A type is always callable. Yes (modulo perversions like explicitly making a non-callable type). This matches what I said above. > call a type will call its __init__ special method (or and __new__ > special method together). No . -- \ ?Anyone who believes exponential growth can go on forever in a | `\ finite world is either a madman or an economist.? ?Kenneth | _o__) Boulding | Ben Finney From apt.shansen at gmail.com Sun Oct 18 01:34:06 2009 From: apt.shansen at gmail.com (Stephen Hansen) Date: Sat, 17 Oct 2009 22:34:06 -0700 Subject: a gap of do....while? In-Reply-To: <74900bb0-fa04-4d34-893d-b109c628fc81@r24g2000prf.googlegroups.com> References: <74900bb0-fa04-4d34-893d-b109c628fc81@r24g2000prf.googlegroups.com> Message-ID: <7a9c25c20910172234r10d27f8ft48d6deecb6d3aab4@mail.gmail.com> On Sat, Oct 17, 2009 at 10:22 PM, StarWing wrote: > okay, I think somethings do....while is useful, but why python didn't > have it? > > in lisp, we can (while (progn ....)) > and in all other language we have do...while. > but in python, we only can: > cond = 1 > while cond: > cond = 0 > ..... > if ....: cond = 1 > > has any polite way to handle this? > Although recently there's been a big thread about some people thinking its bad, the idiomatic way to do it in Python is: while True: ... if : break Basically, the answer to "why doesn't Python have a do...while" statement is-- there's no universally satisfactory way to spell it with Python's indent-based syntax. Every once in awhile someone proposes a particular way to do it, but no one has ever really been able to come up with a syntax everyone likes. See PEP315 (http://www.python.org/dev/peps/pep-0315/) for the deferred proposal to add such a construct and links to extensive discussions about it. HTH, --S -------------- next part -------------- An HTML attachment was scrubbed... URL: From clp2 at rebertia.com Sun Oct 18 01:34:22 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 17 Oct 2009 22:34:22 -0700 Subject: a gap of do....while? In-Reply-To: <74900bb0-fa04-4d34-893d-b109c628fc81@r24g2000prf.googlegroups.com> References: <74900bb0-fa04-4d34-893d-b109c628fc81@r24g2000prf.googlegroups.com> Message-ID: <50697b2c0910172234w34463725kd931c42d6e3367e3@mail.gmail.com> On Sat, Oct 17, 2009 at 10:22 PM, StarWing wrote: > okay, I think somethings do....while is useful, but why python didn't > have it? For simplicity of syntax and less duplication among the basic syntactic constructs. > in lisp, we can (while (progn ....)) > and in all other language we have do...while. > but in python, we only can: > cond = 1 > while cond: > ? ?cond = 0 > ? ?..... > ? ?if ....: cond = 1 > > has any polite way to handle this? It's essentially the same: while True: ... if not cond: break This is considered idiomatic; and FWIW, I see do-while as having no significant advantage over this. Cheers, Chris -- http://blog.rebertia.com From clp2 at rebertia.com Sun Oct 18 01:36:49 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 17 Oct 2009 22:36:49 -0700 Subject: a gap of do....while? In-Reply-To: <50697b2c0910172234w34463725kd931c42d6e3367e3@mail.gmail.com> References: <74900bb0-fa04-4d34-893d-b109c628fc81@r24g2000prf.googlegroups.com> <50697b2c0910172234w34463725kd931c42d6e3367e3@mail.gmail.com> Message-ID: <50697b2c0910172236j15f90950g93663466b5463bc1@mail.gmail.com> On Sat, Oct 17, 2009 at 10:34 PM, Chris Rebert wrote: > On Sat, Oct 17, 2009 at 10:22 PM, StarWing wrote: >> but in python, we only can: >> cond = 1 >> while cond: >> ? ?cond = 0 >> ? ?..... >> ? ?if ....: cond = 1 >> >> has any polite way to handle this? > > It's essentially the same: > > while True: > ? ?... > ? ?if not cond: break Substituting in the appropriate conditional expression for cond of course (your use of cond for the flag caught me off guard). Cheers, Chris From weasley_wx at sina.com Sun Oct 18 01:41:56 2009 From: weasley_wx at sina.com (StarWing) Date: Sat, 17 Oct 2009 22:41:56 -0700 (PDT) Subject: a gap of do....while? References: <74900bb0-fa04-4d34-893d-b109c628fc81@r24g2000prf.googlegroups.com> <50697b2c0910172234w34463725kd931c42d6e3367e3@mail.gmail.com> Message-ID: On 10?18?, ??1?36?, Chris Rebert wrote: > On Sat, Oct 17, 2009 at 10:34 PM, Chris Rebert wrote: > > On Sat, Oct 17, 2009 at 10:22 PM, StarWing wrote: > > >> but in python, we only can: > >> cond = 1 > >> while cond: > >> ? ?cond = 0 > >> ? ?..... > >> ? ?if ....: cond = 1 > > >> has any polite way to handle this? > > > It's essentially the same: > > > while True: > > ? ?... > > ? ?if not cond: break > > Substituting in the appropriate conditional expression for cond of > course (your use of cond for the flag caught me off guard). > > Cheers, > Chris Thank you, this is nice than mine :-) From dieter at handshake.de Sun Oct 18 01:47:01 2009 From: dieter at handshake.de (Dieter Maurer) Date: 18 Oct 2009 07:47:01 +0200 Subject: restriction on sum: intentional bug? In-Reply-To: References: Message-ID: Christian Heimes writes on Fri, 16 Oct 2009 17:58:29 +0200: > Alan G Isaac schrieb: > > I expected this to be fixed in Python 3: > > > >>>> sum(['ab','cd'],'') > > Traceback (most recent call last): > > File "", line 1, in > > TypeError: sum() can't sum strings [use ''.join(seq) instead] > > > > Of course it is not a good way to join strings, > > but it should work, should it not? Naturally, > > It's not a bug. sum() doesn't work on strings deliberately. ''.join() > *is* the right and good way to concatenate strings. Apparently, "sum" special cases 'str' in order to teach people to use "join". It would have been as much work and much more friendly, to just use "join" internally to implement "sum" when this is possible. Dieter From notontheweb at noisp.com Sun Oct 18 01:47:19 2009 From: notontheweb at noisp.com (Jive Dadson) Date: Sat, 17 Oct 2009 22:47:19 -0700 Subject: weekdays in range Message-ID: (Sorry if this shows up twice.) Can someone think of an easy way to calculate the number of weekdays between two calendar dates (in Python)? Thankee. From weasley_wx at sina.com Sun Oct 18 01:52:37 2009 From: weasley_wx at sina.com (StarWing) Date: Sat, 17 Oct 2009 22:52:37 -0700 (PDT) Subject: list to tuple and vice versa References: <310fbb00910172024x10695160h1100b7a24aec77d8@mail.gmail.com> <50697b2c0910172032j4a6f60c7p46f38347123a03d9@mail.gmail.com> <87hbtxmj0r.fsf@benfinney.id.au> <7f68983f-2a87-4cbd-9c6a-58923d439a13@m3g2000pri.googlegroups.com> <87d44lmflm.fsf@benfinney.id.au> Message-ID: On 10?18?, ??1?32?, Ben Finney wrote: > StarWing writes: > > On 10?18?, ??12?19?, Ben Finney wrote: > > > Jabba Laci writes: > > > > Right, it was my bad. After removal the tuple() function works > > > > perfectly. > > > > Note that, though it is callable, ?tuple? is not a function but a > > > type: > > > A type is always callable. > > Yes (modulo perversions like explicitly making a non-callable type). > This matches what I said above. > > > call a type will call its __init__ special method (or and __new__ > > special method together). > > No > . > > -- > ?\ ? ? ??Anyone who believes exponential growth can go on forever in a | > ? `\ ? ? ? ?finite world is either a madman or an economist.? ?Kenneth | > _o__) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Boulding | > Ben Finney Sorry for my ambiguity. I exactly mean that, but thanks for the docs :-) From christophedeze at gmail.com Sun Oct 18 02:13:18 2009 From: christophedeze at gmail.com (Toff) Date: Sat, 17 Oct 2009 23:13:18 -0700 (PDT) Subject: help to convert c++ fonction in python References: <9egkd5151sokfi15a8j39qe939ho7ajfvo@4ax.com> <4eb0089f0910171657q5c85df9ew4cb9699032f49c85@mail.gmail.com> Message-ID: On 18 oct, 02:13, geremy condra wrote: > On Sat, Oct 17, 2009 at 7:57 PM, David Robinow wrote: > > On Sat, Oct 17, 2009 at 7:48 PM, geremy condra wrote: > >> For the love of baby kittens, please, please, please tell me that > >> you do not believe this securely encrypts your data. > > ?Yeah, I think it's pretty good. > > Can you do better? > > Trivially. Use AES, 3DES, any standard cryptosystem- there are > literally dozens of excellent, well-studied implementations in > both C++ and Python, and hardware implementations on many > processors. > > The cipher listed will fall in a single round of chosen plaintext > attacks or chosen ciphertext attacks, and with a keylength of > 40 bytes against a message length of 768 will give me roughly > 19 windows on a single encryption. Frequency analysis is > therefore going to be extremely profitable, not to mention > trivially easy. > > Geremy Condra Thanks a lot Tim ! @Geremy : this is not a methode to encrypt data it is more a methode to encode /decode strings for exemple to store passwords that need to be used by others programs yes it 's insecure but there is no secure way to store password that 's need to be retrieve PS : sorry for my english From ben+python at benfinney.id.au Sun Oct 18 02:22:29 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 18 Oct 2009 17:22:29 +1100 Subject: weekdays in range References: Message-ID: <878wf9mday.fsf@benfinney.id.au> Jive Dadson writes: > Can someone think of an easy way to calculate the number of weekdays > between two calendar dates (in Python)? That depends on what you mean by ?weekdays?. >>> import datetime >>> begin_date = datetime.date(2009, 10, 9) >>> end_date = datetime.date(2009, 10, 22) >>> import calendar >>> print calendar.month(2009, 10) October 2009 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 >>> end_date - begin_date datetime.timedelta(13) If you're expecting to exclude Saturday and Sunday (i.e. if you expect the above result to be 9 days instead of 13), you can use other functions of the ?calendar? module; try starting with: >>> friday_weekday = 4 >>> len([ ... date for date in ( ... begin_date + datetime.timedelta(days) ... for days in range((end_date - begin_date).days)) ... if calendar.weekday(date.year, date.month, date.day) <= friday_weekday]) 9 -- \ ?Selfish, adj. Devoid of consideration for the selfishness of | `\ others.? ?Ambrose Bierce, _The Devil's Dictionary_, 1906 | _o__) | Ben Finney From weasley_wx at sina.com Sun Oct 18 02:30:02 2009 From: weasley_wx at sina.com (StarWing) Date: Sat, 17 Oct 2009 23:30:02 -0700 (PDT) Subject: How to check the exists of a name? Message-ID: Sometimes I want to make a simple flags. and i need to check there is a name in current scope or not (that is, we can visit this name, no matter where is it). and how to do that in python? From http Sun Oct 18 02:37:51 2009 From: http (Paul Rubin) Date: 17 Oct 2009 23:37:51 -0700 Subject: The rap against "while True:" loops References: <01ccc46d-5ea9-4dfe-ba22-699c6b859f00@v36g2000yqv.googlegroups.com> <7xmy3peq3s.fsf@ruckus.brouhaha.com> <7xws2tace4.fsf@ruckus.brouhaha.com> <02ea93d3$0$1384$c3e8da3@news.astraweb.com> Message-ID: <7x4opx2on4.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > For the record, the four lines Paul implies are "confusing" are: > > try: > d[key] += value > except KeyError: > d[key] = value Consider what happens if the computation of "key" or "value" itself raises KeyError. From clp2 at rebertia.com Sun Oct 18 02:37:55 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 17 Oct 2009 23:37:55 -0700 Subject: How to check the exists of a name? In-Reply-To: References: Message-ID: <50697b2c0910172337k2d9cf15bub7c0571915aaccea@mail.gmail.com> On Sat, Oct 17, 2009 at 11:30 PM, StarWing wrote: > Sometimes I want to make a simple flags. and i need to check there is > a name in current scope or not (that is, we can visit this name, no > matter where is it). and how to do that in python? You should avoid needing to do that in the first place. Common alternatives include using a dict instead of variables, depending on your circumstances. Cheers, Chris -- http://blog.rebertia.com From 71david at libero.it Sun Oct 18 02:43:26 2009 From: 71david at libero.it (David) Date: Sun, 18 Oct 2009 08:43:26 +0200 Subject: How to check the exists of a name? References: Message-ID: Il Sat, 17 Oct 2009 23:30:02 -0700 (PDT), StarWing ha scritto: > Sometimes I want to make a simple flags. and i need to check there is > a name in current scope or not (that is, we can visit this name, no > matter where is it). and how to do that in python? Just use it in a try..except block. try: print myname except NameError: print "myname does not exists" D. From weasley_wx at sina.com Sun Oct 18 02:43:36 2009 From: weasley_wx at sina.com (StarWing) Date: Sat, 17 Oct 2009 23:43:36 -0700 (PDT) Subject: How to check the exists of a name? References: Message-ID: <07529e61-7ed1-48e4-84d7-d61b11bc2e1f@x5g2000prf.googlegroups.com> On 10?18?, ??2?37?, Chris Rebert wrote: > On Sat, Oct 17, 2009 at 11:30 PM, StarWing wrote: > > Sometimes I want to make a simple flags. and i need to check there is > > a name in current scope or not (that is, we can visit this name, no > > matter where is it). and how to do that in python? > > You should avoid needing to do that in the first place. Common > alternatives include using a dict instead of variables, depending on > your circumstances. > > Cheers, > Chris > --http://blog.rebertia.com Okay... Thank you... But if I want to do that, what shall I do? I got a idea, use a try...except statement. there are another way to do it ? (I just curious now, because I solve my problem in another way :-) From 71david at libero.it Sun Oct 18 02:46:00 2009 From: 71david at libero.it (David) Date: Sun, 18 Oct 2009 08:46:00 +0200 Subject: How to check the exists of a name? References: <07529e61-7ed1-48e4-84d7-d61b11bc2e1f@x5g2000prf.googlegroups.com> Message-ID: <10mhebg6yca6e$.15p2f0flh92p9.dlg@40tude.net> Il Sat, 17 Oct 2009 23:43:36 -0700 (PDT), StarWing ha scritto: > I got a idea, use a try...except statement. there are another way to > do it ? > > (I just curious now, because I solve my problem in another way :-) locals().has_key(myname) globals().has_key(myname) D. From clp2 at rebertia.com Sun Oct 18 02:54:37 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 17 Oct 2009 23:54:37 -0700 Subject: How to check the exists of a name? In-Reply-To: <07529e61-7ed1-48e4-84d7-d61b11bc2e1f@x5g2000prf.googlegroups.com> References: <07529e61-7ed1-48e4-84d7-d61b11bc2e1f@x5g2000prf.googlegroups.com> Message-ID: <50697b2c0910172354i66a2b332n2f3565e54a46247@mail.gmail.com> On Sat, Oct 17, 2009 at 11:43 PM, StarWing wrote: > On 10?18?, ??2?37?, Chris Rebert wrote: >> On Sat, Oct 17, 2009 at 11:30 PM, StarWing wrote: >> > Sometimes I want to make a simple flags. and i need to check there is >> > a name in current scope or not (that is, we can visit this name, no >> > matter where is it). and how to do that in python? >> >> You should avoid needing to do that in the first place. Common >> alternatives include using a dict instead of variables, depending on >> your circumstances. > > Okay... Thank you... But if I want to do that, what shall I do? > > I got a idea, use a try...except statement. there are another way to > do it ? > > (I just curious now, because I solve my problem in another way :-) Perhaps if you could explain why there's the possibility these variables might not be defined... Cheers, Chris -- http://blog.rebertia.com From ben+python at benfinney.id.au Sun Oct 18 03:00:06 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 18 Oct 2009 18:00:06 +1100 Subject: How to check the exists of a name? References: <07529e61-7ed1-48e4-84d7-d61b11bc2e1f@x5g2000prf.googlegroups.com> <10mhebg6yca6e$.15p2f0flh92p9.dlg@40tude.net> Message-ID: <874opxmbk9.fsf@benfinney.id.au> David <71david at libero.it> writes: > Il Sat, 17 Oct 2009 23:43:36 -0700 (PDT), StarWing ha scritto: > > > I got a idea, use a try...except statement. there are another way to > > do it ? > > > > (I just curious now, because I solve my problem in another way :-) > > locals().has_key(myname) > globals().has_key(myname) The ?dict.has_key? method is deprecated, and is removed in Python 3. The ?in? operator now works on dict objects: 'foo' in locals() 'foo' in globals() -- \ ?We must become the change we want to see.? ?Mahatma Gandhi | `\ | _o__) | Ben Finney From notontheweb at noisp.com Sun Oct 18 04:01:18 2009 From: notontheweb at noisp.com (Jive Dadson) Date: Sun, 18 Oct 2009 01:01:18 -0700 Subject: weekdays in range In-Reply-To: <878wf9mday.fsf@benfinney.id.au> References: <878wf9mday.fsf@benfinney.id.au> Message-ID: Ben Finney wrote: > Jive Dadson writes: > >> Can someone think of an easy way to calculate the number of weekdays >> between two calendar dates (in Python)? > > That depends on what you mean by ?weekdays?. > > >>> import datetime > >>> begin_date = datetime.date(2009, 10, 9) > >>> end_date = datetime.date(2009, 10, 22) > >>> import calendar > >>> print calendar.month(2009, 10) > October 2009 > Mo Tu We Th Fr Sa Su > 1 2 3 4 > 5 6 7 8 9 10 11 > 12 13 14 15 16 17 18 > 19 20 21 22 23 24 25 > 26 27 28 29 30 31 > >>> end_date - begin_date > datetime.timedelta(13) > > If you're expecting to exclude Saturday and Sunday (i.e. if you expect > the above result to be 9 days instead of 13), you can use other > functions of the ?calendar? module; try starting with: > > >>> friday_weekday = 4 > >>> len([ > ... date for date in ( > ... begin_date + datetime.timedelta(days) > ... for days in range((end_date - begin_date).days)) > ... if calendar.weekday(date.year, date.month, date.day) <= friday_weekday]) > 9 > Thanks for your help. For a non-expert at Python, that last compound statement is pretty inscrutable. I am trying to scrute it. Wish me luck. From hendrik at microcorp.co.za Sun Oct 18 04:08:18 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Sun, 18 Oct 2009 10:08:18 +0200 Subject: The rap against "while True:" loops In-Reply-To: References: <01ccc46d-5ea9-4dfe-ba22-699c6b859f00@v36g2000yqv.googlegroups.com> Message-ID: <200910181008.18219.hendrik@microcorp.co.za> On Saturday, 17 October 2009 16:30:55 Aahz wrote: > In article , > > Tim Rowe wrote: > >The point is that an exception causes a change in program flow, so of > >course they're used for flow control. It's what they do. The question > >is in what cases it's appropriate to use them. > > Standard Python idiom: > > try: > d[key] += value > except KeyError: > d[key] = value > > Maybe you need to re-think "appropriate". Standard Python idiom: if key in d: d[key] += value else: d[key] = value Maybe you need to re-think "appropriate". - Hendrik From lie.1296 at gmail.com Sun Oct 18 04:17:22 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 18 Oct 2009 19:17:22 +1100 Subject: a gap of do....while? In-Reply-To: References: <74900bb0-fa04-4d34-893d-b109c628fc81@r24g2000prf.googlegroups.com> Message-ID: <4adacf11@dnews.tpgi.com.au> Chris Rebert wrote: > On Sat, Oct 17, 2009 at 10:22 PM, StarWing wrote: >> okay, I think somethings do....while is useful, but why python didn't >> have it? > > For simplicity of syntax and less duplication among the basic > syntactic constructs. Less language features means less decisions to make. From steve at REMOVE-THIS-cybersource.com.au Sun Oct 18 04:19:50 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 18 Oct 2009 08:19:50 GMT Subject: How to check the exists of a name? References: Message-ID: <02eabe5c$0$1384$c3e8da3@news.astraweb.com> On Sat, 17 Oct 2009 23:30:02 -0700, StarWing wrote: > Sometimes I want to make a simple flags. and i need to check there is a > name in current scope or not (that is, we can visit this name, no matter > where is it). and how to do that in python? (1) Use a sentinel: myname = None # always exists ... # much later if myname is None: print "initialising myname" else: process(myname) (2) Try it and see: try: myname except NameError: print "myname does not exist" else: print "myname exists" (3) Look Before You Leap: # inside a class: 'myname' in self.__dict__ or self.__class__.__dict__ # better, because it uses inheritance and supports __slots__: hasattr(self, 'myname') # inside a function 'myname' in locals() 'myname' in globals() -- Steven From ben+python at benfinney.id.au Sun Oct 18 04:30:14 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 18 Oct 2009 19:30:14 +1100 Subject: weekdays in range References: <878wf9mday.fsf@benfinney.id.au> Message-ID: <87zl7pkstl.fsf@benfinney.id.au> Jive Dadson writes: > Ben Finney wrote: > > >>> friday_weekday = 4 > > >>> len([ > > ... date for date in ( > > ... begin_date + datetime.timedelta(days) > > ... for days in range((end_date - begin_date).days)) > > ... if calendar.weekday(date.year, date.month, date.day) <= friday_weekday]) > > 9 > > > > Thanks for your help. For a non-expert at Python, that last compound > statement is pretty inscrutable. I am trying to scrute it. Wish me > luck. I'll help you by showing (liberally editing to make me look reptrospectively clever) how I built it up: >>> import datetime >>> import calendar >>> import pprint >>> begin_date = datetime.date(2009, 10, 9) >>> end_date = datetime.date(2009, 10, 22) >>> end_date - begin_date datetime.timedelta(13) >>> (end_date - begin_date).days 13 >>> range((end_date - begin_date).days) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] >>> begin_date + datetime.timedelta(0) datetime.date(2009, 10, 9) >>> begin_date + datetime.timedelta(1) datetime.date(2009, 10, 10) >>> (begin_date + datetime.timedelta(days) ... for days in range((end_date - begin_date).days)) >>> [ ... date for date in ( ... begin_date + datetime.timedelta(days) ... for days in range((end_date - begin_date).days)) ... ] [datetime.date(2009, 10, 9), datetime.date(2009, 10, 10), datetime.date(2009, 10, 11), datetime.date(2009, 10, 12), datetime.date(2009, 10, 13), datetime.date(2009, 10, 14), datetime.date(2009, 10, 15), datetime.date(2009, 10, 16), datetime.date(2009, 10, 17), datetime.date(2009, 10, 18), datetime.date(2009, 10, 19), datetime.date(2009, 10, 20), datetime.date(2009, 10, 21)] >>> pprint.pprint([ ... date for date in ( ... begin_date + datetime.timedelta(days) ... for days in range((end_date - begin_date).days)) ... ]) [datetime.date(2009, 10, 9), datetime.date(2009, 10, 10), datetime.date(2009, 10, 11), datetime.date(2009, 10, 12), datetime.date(2009, 10, 13), datetime.date(2009, 10, 14), datetime.date(2009, 10, 15), datetime.date(2009, 10, 16), datetime.date(2009, 10, 17), datetime.date(2009, 10, 18), datetime.date(2009, 10, 19), datetime.date(2009, 10, 20), datetime.date(2009, 10, 21)] >>> len([ ... date for date in ( ... begin_date + datetime.timedelta(days) ... for days in range((end_date - begin_date).days)) ... ]) 13 >>> friday_weekday = 4 >>> calendar.weekday(begin_date.year, begin_date.month, begin_date.day) 4 >>> calendar.weekday(end_date.year, end_date.month, end_date.day) 3 >>> [ ... date for date in ( ... begin_date + datetime.timedelta(days) ... for days in range((end_date - begin_date).days)) ... if calendar.weekday(date.year, date.month, date.day) <= friday_weekday] [datetime.date(2009, 10, 9), datetime.date(2009, 10, 12), datetime.date(2009, 10, 13), datetime.date(2009, 10, 14), datetime.date(2009, 10, 15), datetime.date(2009, 10, 16), datetime.date(2009, 10, 19), datetime.date(2009, 10, 20), datetime.date(2009, 10, 21)] >>> pprint.pprint([ ... date for date in ( ... begin_date + datetime.timedelta(days) ... for days in range((end_date - begin_date).days)) ... if calendar.weekday(date.year, date.month, date.day) <= friday_weekday]) [datetime.date(2009, 10, 9), datetime.date(2009, 10, 12), datetime.date(2009, 10, 13), datetime.date(2009, 10, 14), datetime.date(2009, 10, 15), datetime.date(2009, 10, 16), datetime.date(2009, 10, 19), datetime.date(2009, 10, 20), datetime.date(2009, 10, 21)] >>> len([ ... date for date in ( ... begin_date + datetime.timedelta(days) ... for days in range((end_date - begin_date).days)) ... if calendar.weekday(date.year, date.month, date.day) <= friday_weekday]) 9 -- \ ?We are all agreed that your theory is crazy. The question that | `\ divides us is whether it is crazy enough to have a chance of | _o__) being correct.? ?Niels Bohr (to Wolfgang Pauli), 1958 | Ben Finney From notontheweb at noisp.com Sun Oct 18 04:41:18 2009 From: notontheweb at noisp.com (Jive Dadson) Date: Sun, 18 Oct 2009 01:41:18 -0700 Subject: weekdays in range In-Reply-To: <87zl7pkstl.fsf@benfinney.id.au> References: <878wf9mday.fsf@benfinney.id.au> <87zl7pkstl.fsf@benfinney.id.au> Message-ID: Wow. It's a danged tutorial. Thanks again. Take a break. From john_re at fastmail.us Sun Oct 18 04:45:38 2009 From: john_re at fastmail.us (john_re) Date: Sun, 18 Oct 2009 01:45:38 -0700 Subject: Sunday 18th 12N-3P PDST Global Python Mtg - BerkeleyTIP - for forwarding Message-ID: <1255855538.20643.1340652361@webmail.messagingengine.com> Join the global Free SW HW & Culture meeting online via VOIP & IRC. Sunday Oct 18, 12N-3P Pacific Daylight Savings Time (UTC-8), 3P-6P Eastern, (7P-10P UTC?) http://sites.google.com/site/berkeleytip/remote-attendance Or, come to the UCBerkeley Free Speech Cafe. Discuss the videos, work on your own projects & share them with others, & help work on the group projects. Get a VOIP headset, & join the #berkeleytip channel on irc.freenode.net We'll help you install VOIP client SW, & join the global meeting. Join with the meeting from your home via VOIP, or create a local meeting at your local college wifi cafe. http://sites.google.com/site/berkeleytip/schedule I finally got some posters up around campus. :) ===== Talk/Video October: Open Source-and-Mac Other OSs http://www.eweek.com/c/a/Linux-and-Open-Source/OpenSource-and-Mac-Alternatives-to-Windows-7-649233/ http://sites.google.com/site/berkeleytip/talk-videos ===== Some Topics for discussion: How get better WiFi at FSC - talk to UCB net services? FreeSwitch install on UCB box KUbuntu 9.10 out this month Plan for a UCB installfest - leaders, helpers, announcements Flyers post throughout campus beginning November? SSL & OpenVPN on UCB box ===== Join the mailing lists & say hi, tell us what you are interested in. http://groups.google.com/group/BerkTIPGlobal You are invited to forward this message anywhere it would be welcomed. :) From steve at REMOVE-THIS-cybersource.com.au Sun Oct 18 05:07:14 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 18 Oct 2009 09:07:14 GMT Subject: How to check the exists of a name? References: <07529e61-7ed1-48e4-84d7-d61b11bc2e1f@x5g2000prf.googlegroups.com> Message-ID: <02eac977$0$1384$c3e8da3@news.astraweb.com> On Sat, 17 Oct 2009 23:54:37 -0700, Chris Rebert wrote: > Perhaps if you could explain why there's the possibility these variables > might not be defined... If I have to support older versions of Python: try: bin except NameError: # Define my own. def bin(arg): ... But for my own variables? No way. I just make sure they're defined. -- Steven From steve at REMOVE-THIS-cybersource.com.au Sun Oct 18 05:07:41 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 18 Oct 2009 09:07:41 GMT Subject: The rap against "while True:" loops References: <01ccc46d-5ea9-4dfe-ba22-699c6b859f00@v36g2000yqv.googlegroups.com> <7xmy3peq3s.fsf@ruckus.brouhaha.com> <7xws2tace4.fsf@ruckus.brouhaha.com> <02ea93d3$0$1384$c3e8da3@news.astraweb.com> <7x4opx2on4.fsf@ruckus.brouhaha.com> Message-ID: <02eac993$0$1384$c3e8da3@news.astraweb.com> On Sat, 17 Oct 2009 23:37:51 -0700, Paul Rubin wrote: > Steven D'Aprano writes: >> For the record, the four lines Paul implies are "confusing" are: >> >> try: >> d[key] += value >> except KeyError: >> d[key] = value > > Consider what happens if the computation of "key" or "value" itself > raises KeyError. How does using a defaultdict for d save you from that problem? table = {101: 'x', 202: 'y'} data = {'a': 1, 'b': 2} d = collections.defaultdict(int) d[table[303]] += data['c'] It may not be appropriate to turn table and data into defaultdicts -- there may not be a legitimate default you can use, and the key-lookup failure may be a fatal error. So defaultdict doesn't solve your problem. If you need to distinguish between multiple expressions that could raise exceptions, you can't use a single try to wrap them all. If you need to make that distinction, then the following is no good: try: key = keytable[s] value = datatable[t] d[key] += value except KeyError: print "An exception occurred somewhere" But if you need to treat all three possible KeyErrors identically, then the above is a perfectly good solution. -- Steven From lie.1296 at gmail.com Sun Oct 18 05:21:31 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 18 Oct 2009 20:21:31 +1100 Subject: The rap against "while True:" loops In-Reply-To: <7x4opx2on4.fsf@ruckus.brouhaha.com> References: <01ccc46d-5ea9-4dfe-ba22-699c6b859f00@v36g2000yqv.googlegroups.com> <7xmy3peq3s.fsf@ruckus.brouhaha.com> <7xws2tace4.fsf@ruckus.brouhaha.com> <02ea93d3$0$1384$c3e8da3@news.astraweb.com> <7x4opx2on4.fsf@ruckus.brouhaha.com> Message-ID: <4adade1a$1@dnews.tpgi.com.au> Paul Rubin wrote: > Steven D'Aprano writes: >> For the record, the four lines Paul implies are "confusing" are: >> >> try: >> d[key] += value >> except KeyError: >> d[key] = value > > Consider what happens if the computation of "key" or "value" itself > raises KeyError. Isn't key and value just a simple variables/names? Why should it ever raises KeyError? The only other error that try-block code could ever possibly throw are NameError and possibly MemoryError. From http Sun Oct 18 05:31:19 2009 From: http (Paul Rubin) Date: 18 Oct 2009 02:31:19 -0700 Subject: The rap against "while True:" loops References: <01ccc46d-5ea9-4dfe-ba22-699c6b859f00@v36g2000yqv.googlegroups.com> Message-ID: <7x1vl1nj4o.fsf@ruckus.brouhaha.com> Hendrik van Rooyen writes: > Standard Python idiom: > > if key in d: > d[key] += value > else: > d[key] = value The issue is that uses two lookups. If that's ok, the more usual idiom is: d[key] = value + d.get(key, 0) From ben+python at benfinney.id.au Sun Oct 18 05:43:16 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 18 Oct 2009 20:43:16 +1100 Subject: The rap against "while True:" loops References: <01ccc46d-5ea9-4dfe-ba22-699c6b859f00@v36g2000yqv.googlegroups.com> <7xmy3peq3s.fsf@ruckus.brouhaha.com> <7xws2tace4.fsf@ruckus.brouhaha.com> <02ea93d3$0$1384$c3e8da3@news.astraweb.com> <7x4opx2on4.fsf@ruckus.brouhaha.com> <4adade1a$1@dnews.tpgi.com.au> Message-ID: <87vdidkpfv.fsf@benfinney.id.au> Lie Ryan writes: > Paul Rubin wrote: > > Steven D'Aprano writes: > >> For the record, the four lines Paul implies are "confusing" are: > >> > >> try: > >> d[key] += value > >> except KeyError: > >> d[key] = value > > > > Consider what happens if the computation of "key" or "value" itself > > raises KeyError. > > Isn't key and value just a simple variables/names? In that example, yes. Paul is encouraging the reader to think of more complex cases where they are compound expressions, that can therefore raise other errors depending on what those expressions contain. -- \ ?Don't be afraid of missing opportunities. Behind every failure | `\ is an opportunity somebody wishes they had missed.? ?Jane | _o__) Wagner, via Lily Tomlin | Ben Finney From davea at ieee.org Sun Oct 18 06:30:09 2009 From: davea at ieee.org (Dave Angel) Date: Sun, 18 Oct 2009 06:30:09 -0400 Subject: restriction on sum: intentional bug? In-Reply-To: References: Message-ID: <4ADAEE31.9010204@ieee.org> Dieter Maurer wrote: > Christian Heimes writes on Fri, 16 Oct 2009 17:58:29 +0200: > >> Alan G Isaac schrieb: >> >>> I expected this to be fixed in Python 3: >>> >>> >>>>>> sum(['ab','cd'],'') >>>>>> >>> Traceback (most recent call last): >>> File "", line 1, in >>> TypeError: sum() can't sum strings [use ''.join(seq) instead] >>> >>> Of course it is not a good way to join strings, >>> but it should work, should it not? Naturally, >>> >> It's not a bug. sum() doesn't work on strings deliberately. ''.join() >> *is* the right and good way to concatenate strings. >> > > Apparently, "sum" special cases 'str' in order to teach people to use "join". > It would have been as much work and much more friendly, to just use "join" > internally to implement "sum" when this is possible. > > Dieter > > > Earlier, I would have agreed with you. I assumed that this could be done invisibly, with the only difference being performance. But you can't know whether join will do the trick without error till you know that all the items are strings or Unicode strings. And you can't check that without going through the entire iterator. At that point it's too late to change your mind, as you can't back up an iterator. So the user who supplies a list with mixed strings and other stuff will get an unexpected error, one that join generates. To put it simply, I'd say that sum() should not dispatch to join() unless it could be sure that no errors might result. DaveA From davea at ieee.org Sun Oct 18 06:51:07 2009 From: davea at ieee.org (Dave Angel) Date: Sun, 18 Oct 2009 06:51:07 -0400 Subject: weekdays in range In-Reply-To: References: <878wf9mday.fsf@benfinney.id.au> <87zl7pkstl.fsf@benfinney.id.au> Message-ID: <4ADAF31B.1000609@ieee.org> Jive Dadson wrote: >
Wow. > It's a danged tutorial. Thanks again. Take a break. > >
> Ben Finney's method is a very good approach, and an experienced Python programmer would consider it straightforward. But I have to ask whether the range of dates you might be considering could be large. For example, if you want to know how many week-days there are between a date in 1904 and 2207, it'd build a list of some 110 thousand items. There are other approaches which would be faster, consume much less memory, and be much harder to read. I'm not offering to debug it, but here's such an approach, subject to the old plus/minus one bugs. def epochweekdays (datetimeobj) """Given an arbitrary Monday long ago, figure out how many weekdays have occurred between that day and the argument""" subtract to get total_days return int(totaldays/7) * 5 + max(totaldays%7, 5) Now, your answer is just epochweekdays(b) - epochweekdays(a) DaveA From notontheweb at noisp.com Sun Oct 18 07:04:38 2009 From: notontheweb at noisp.com (Jive Dadson) Date: Sun, 18 Oct 2009 04:04:38 -0700 Subject: weekdays in range In-Reply-To: References: <878wf9mday.fsf@benfinney.id.au> <87zl7pkstl.fsf@benfinney.id.au> Message-ID: I'm using weekdays as a proxy for days when the US stock market is open. (I'll miss holidays.) The application is pricing CALL and PUT options. Speed is not a problem. The number of days will typically be between 1 and 254. From steve at REMOVE-THIS-cybersource.com.au Sun Oct 18 07:28:09 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 18 Oct 2009 11:28:09 GMT Subject: The rap against "while True:" loops References: <01ccc46d-5ea9-4dfe-ba22-699c6b859f00@v36g2000yqv.googlegroups.com> <4cdf9c03-d8e1-406c-b960-1a5b535f9430@m38g2000yqd.googlegroups.com> <005fde84$0$26920$c3e8da3@news.astraweb.com> <0062fc72$0$26941$c3e8da3@news.astraweb.com> Message-ID: <02eaea7e$0$1384$c3e8da3@news.astraweb.com> On Sat, 17 Oct 2009 13:12:52 +0100, Tim Rowe wrote: > 2009/10/17 Steven D'Aprano : > >> No, you have a fundamental misunderstanding. They're called exceptions, >> not errors, because they represent exceptional cases. Often errors are >> exceptional cases, but they're not the only sort of exceptional case. > > The whole reason for the mechanism, across all languages that have it, > is to deal with situations that you don't know how to deal with locally. That confuses me. If I call: y = mydict[x] how does my knowledge of what to do if x is not a key relate to whether the language raises an exception, returns an error code, dumps core, or prints "He's not the Messiah, he's a very naughty boy" to stderr? You seem to be making a distinction of *intent* which, as far as I can tell, doesn't actually exist. What's the difference in intent between these? y = mydict[x] if y == KeyErrorCode: handle_error_condition() process(y) and this? try: y = mydict[x] except KeyError: handle_error_condition() process(y) Neither assumes more or less knowledge of what to do in handle_error_condition(). Neither case assumes that the failure of x to be a key is an error: try: y = mydict[x] except KeyError: process() # working as expected else: print 'found x in dict, it shouldn't be there' sys.exit() Either way, whether the language uses error codes or exceptions, the decision of what to do in an exceptional situation is left to the user. If you'll excuse me pointing out the bleedin' obvious, there are differences between error codes and exceptions, but they aren't one of intention. Error codes put the onus on the caller to check the code after every single call which might fail (or have a buggy program), while exceptions use a framework that do most of the heavy lifting. > That's why they have the overhead that they do. Exceptions don't have one common overhead across all languages that use them. They have different overhead in different languages -- they're very heavyweight in C++ and Java, but lightweight in Python. The Perl Exception::Base module claims to be lightweight. The overhead of exceptions is related to the implementation of the language. >> Python uses exceptions for flow control: > > Yes, and in some cases I think that's a serious language wart. Not > enough to put me off the language, but a serious wart nevertheless. I disagree with that. I think exceptions are a beautiful and powerful way of dealing with flow control, much better than returning a special code, and much better than having to check some status function or global variable, as so often happens in C. They're more restricted, and therefore safer, than goto. They're not a panacea but they're very useful. >> Similarly, it's hardly an *error* for [1, 2, 3].index(5) to fail -- who >> is to say that the list is supposed to have 5 in it? ValueError (a >> slightly misleading name in this situation) is used to indicate an >> exceptional, but not unexpected, occurrence. > > That one is, I think, a legitimate use of an exception. The result > returned by index is defined if the index is in bounds. If not, index > doesn't know whether it was supposed to be in bounds or not, and so > can't handle the case locally. It could suggest an error or merely > (IMHO) poor programming. Because index cannot know what the proper > action is, an exception is the appropriate response. I think you're confused about what list.index(obj) does. You seem to me to be assuming that [1,2,3].index(5) should return the item in position 5 of the list, and since there isn't one (5 is out of bounds), raise an exception. But that's not what it does. It searches the list and returns the position at which 5 is found. Of course list.index() could have returned an error code instead, like str.find() does. But str also has an index() method, which raises an exception -- when handling strings, you can Look Before You Leap or Ask For Forgiveness Instead Of Permission, whichever you prefer. >> Likewise, KeyboardInterrupt is used to allow the user to halt >> processing; SystemExit is used to shut down the Python virtual machine; >> and warnings are implemented using exceptions. > > Again, I think it's fair to treat a program being killed from outside as > an exception as far as the program is concerned. No, it's not being killed from outside the program -- it's being *interrupted* from *inside* the program by the user. What you do in response to that interrupt is up to you -- it doesn't necessarily mean "kill the program". If you kill the program from outside, using (say) kill or the TaskManager or something, you don't necessarily get an exception. With kill -9 on POSIX systems you won't get anything, because the OS will just yank the carpet out from under your program's feet and then drop a safe on it to be sure. -- Steven From gervaz at gmail.com Sun Oct 18 08:25:40 2009 From: gervaz at gmail.com (mattia) Date: 18 Oct 2009 12:25:40 GMT Subject: print() References: <4ad8dfc8$0$1112$4fafbaef@reader2.news.tin.it> <4AD93C7A.5030507@ieee.org> <4ad99cc8$0$1093$4fafbaef@reader3.news.tin.it> Message-ID: <4adb0944$0$1107$4fafbaef@reader3.news.tin.it> Il Sat, 17 Oct 2009 10:38:55 -0400, Dave Angel ha scritto: > mattia wrote: >> Il Fri, 16 Oct 2009 22:40:34 -0700, Dennis Lee Bieber ha scritto: >> >> >>> On Fri, 16 Oct 2009 23:39:38 -0400, Dave Angel >>> declaimed the following in gmane.comp.python.general: >>> >>> >>> >>>> You're presumably testing this in the interpreter, which prints extra >>>> stuff. In particular, it prints the result value of any expressions >>>> entered at the interpreter prompt. So if you type >>>> >>>> sys.stdout.write("hello") >>>> >>>> then after the write() method is done, the return value of the method >>>> (5) will get printed by the interpreter. >>>> >>>> >>> I was about to respond that way myself, but before doing so I >>> >> wanted >> >>> to produce an example in the interpreter window... But no effect? >>> >>> C:\Documents and Settings\Dennis Lee Bieber>python ActivePython >>> 2.5.2.2 (ActiveState Software Inc.) based on Python 2.5.2 (r252:60911, >>> Mar 27 2008, 17:57:18) [MSC v.1310 32 bit (Intel)] on win32 Type >>> "help", "copyright", "credits" or "license" for more information. >>> >>>>>> import sys >>>>>> sys.stdout.write("hello") >>>>>> >>> hello>>> >>> >>> >>> PythonWin 2.5.2 (r252:60911, Mar 27 2008, 17:57:18) [MSC v.1310 32 bit >>> (Intel)] on win32. >>> Portions Copyright 1994-2006 Mark Hammond - see 'Help/About PythonWin' >>> for further copyright information. >>> >>>>>> import sys >>>>>> sys.stdout.write("This is a test") >>>>>> >>> This is a test >>> >>>>>> print sys.stdout.write("Hello") >>>>>> >>> HelloNone >>> >>> >>> No count shows up... neither PythonWin or Windows command line/ >>> >> shell >> >> Indeed I'm using py3. But now everythong is fine. Everything I just >> wanted to know was just to run this simple script (I've also sent the >> msg 'putchar(8)' to the newsgroup): >> >> import time >> import sys >> >> val = ("|", "/", "-", "\\", "|", "/", "-", "\\") for i in range(100+1): >> print("=", end="") >> # print("| ", end="") >> print(val[i%len(val)], " ", sep="", end="") print(i, "%", sep="", >> end="") >> sys.stdout.flush() >> time.sleep(0.1) >> if i > 9: >> print("\x08"*5, " "*5, "\x08"*5, sep="", end="") >> else: >> print("\x08"*4, " "*4, "\x08"*4, sep="", end="") >> print(" 100%\nDownload complete!") >> >> > Seems to me you're spending too much energy defeating the things that > print() is automatically doing for you. The whole point of write() is > that it doesn't do anything but ship your string to the file/device. So > if you want control, do your own formatting. > > Consider: > > import time, sys, itertools > > val = ("|", "/", "-", "\\", "|", "/", "-", "\\") sys.stdout.write(" > ") > pattern = "\x08"*8 + " {0}{1:02d}%" for percentage, string in > enumerate(itertools.cycle(val)): > if percentage>99 : break > paddednum = pattern.format(string, percentage) > sys.stdout.write(paddednum) > sys.stdout.flush() > time.sleep(0.1) > print("\x08\x08\x08\x08 100%\nDownload complete!") > > > Note the use of cycle() which effectively repeats a list indefinitely. > And enumerate, which makes an index for you automatically when you're > iterating through a list. And str.format() that builds our string, > including using 0 padding so the percentages are always two digits. > > > DaveA It's always good to learn something new, thanks! From gervaz at gmail.com Sun Oct 18 08:35:34 2009 From: gervaz at gmail.com (mattia) Date: 18 Oct 2009 12:35:34 GMT Subject: print() References: <4ad8dfc8$0$1112$4fafbaef@reader2.news.tin.it> <4ad9a40e$0$1094$4fafbaef@reader3.news.tin.it> Message-ID: <4adb0b96$0$1107$4fafbaef@reader3.news.tin.it> Il Sat, 17 Oct 2009 10:02:27 -0400, Dave Angel ha scritto: > mattia wrote: >> Il Fri, 16 Oct 2009 21:04:08 +0000, mattia ha scritto: >> >> >>> Is there a way to print to an unbuffered output (like stdout)? I've >>> seen that something like sys.stdout.write("hello") works but it also >>> prints the number of characters! >>> >>> >> Another question (always py3). How can I print only the first number >> after the comma of a division? >> e.g. print(8/3) --> 2.66666666667 >> I just want 2.6 (or 2.66) >> >> Thanks, Mattia >> >> > Just as sys.stdout.write() is preferable to print() for your previous > question, understanding str.format() is important to having good control > over what your output looks like. It's certainly not the only way, but > the docs seem to say it's the preferred way in version 3.x It was > introduced in 2.6, so there are other approaches you might want if you > need to work in 2.5 or earlier. > > x = 8/3 > dummy0=dummy1=dummy2=42 > s = "The answer is approx. {3:07.2f} after rounding".format(dummy0, > dummy1, dummy2, x) > print(s) > > > will print out the following: > > The answer is approx. 0002.67 after rounding > > A brief explanation of the format string {3:07.2f} is as follows: > 3 selects argument 3 of the function, which is x 0 means to > zero-fill the value after conversion 7 means 7 characters total > width (this helps determine who many > zeroes are inserted) > 2 means 2 digits after the decimal > f means fixed point format > > You can generally leave out the parts you don't need, but this gives you > lots of control over what things should look like. There are lots of > other parts, but this is most of what you might need for controlled > printing of floats. > > The only difference from what you asked is that this rounds, where you > seemed (!) to be asking for truncation of the extra columns. If you > really need to truncate, I'd recommend using str() to get a string, then > use index() to locate the decimal separator, and then slice it yourself. > > DaveA Yes, reading the doc I've come up with s = "%(0)03.02f%(1)s done" % {"0": 100.0-100.0*(size/tot), "1": "%"} but to it is not a good idea to use a dict here.. From bigboss1964 at gmail.com Sun Oct 18 11:20:05 2009 From: bigboss1964 at gmail.com (TerryP) Date: Sun, 18 Oct 2009 08:20:05 -0700 (PDT) Subject: print() References: <4ad8dfc8$0$1112$4fafbaef@reader2.news.tin.it> <4ad9a40e$0$1094$4fafbaef@reader3.news.tin.it> <4adb0b96$0$1107$4fafbaef@reader3.news.tin.it> Message-ID: On Oct 18, 12:35?pm, mattia wrote: > Yes, reading the doc I've come up with > s = "%(0)03.02f%(1)s done" % {"0": 100.0-100.0*(size/tot), "1": "%"} > but to it is not a good idea to use a dict here.. Also look at the new str.format() From t100ss at gmail.com Sun Oct 18 11:29:34 2009 From: t100ss at gmail.com (nerak99) Date: Sun, 18 Oct 2009 08:29:34 -0700 (PDT) Subject: dav caldav webdav Message-ID: <887bcaf5-4632-47bc-a74a-71f15f8814f0@k4g2000yqb.googlegroups.com> I want to write a script to add events to a groupware calendar. I am using sogo and i have a test server tom play with. I want to be able to script a process of accessing a file of ~10^5 events into a calendaring system so that I can prepopulate a load of calendars with the users' timetables at the beginning of an academic year. It does not seem to be too difficult according to some > 2009/10/6 Helge Hess > > On 06.10.2009, at 09:35, Brian Lockwood wrote: >> >>> In order to use Python to do bulk calendar updates I need the API for the >>> Python libraries involved. Any idea where I can find them? >>> Brian >>> >> >> http://caldav.calconnect.org/implementations/librariestools.html >> >> But you can also just use httplib and contruct/parse the PROPFINDs >> manually. Not that hard. Helge may be correct but I need a little more help to get started. I have come to the conclusion that I need a library with an appropriate api and have found zanshin. It would be incredibly helpful to find some code snippets or examples or an api to help get started on this. From xnews2 at fredp.lautre.net Sun Oct 18 12:11:59 2009 From: xnews2 at fredp.lautre.net (Fred Pacquier) Date: 18 Oct 2009 16:11:59 GMT Subject: Tracking down DLL load errors in Windows ? References: Message-ID: Fred P said : > Hi, a bit of platform-specific advice sought here... I'm trying to > diagnose one of those mysteries Windows is so fond of... > Say that I have code that imports some binary Python module from site- > packages (in this case, libpyexiv2.pyd through pyexiv2.py, could be > anythng else). > On three Windows boxes I've tried it (XP Pro or Home, Python 2.5) > everything works fine. On a fourth, importing the module fails with a > traceback like this : > Traceback (most recent call last): > File "", line 1, in > File "c:\python\lib\site-packages\pyexiv2.py", line 60, in > import libpyexiv2 > ImportError: DLL load failed: This application could not start > because its configuration is incorrect. Reinstalling it might solve > the problem. > I have tried long and hard to spot a meaningful difference between the > first three systems and the last, without success. > Is there any tool and/or methodology I could use to at least pinpoint > the exact DLL that libpyexiv2 is failing to load, and ideally also the > reason why ?... Thanks to Mark and Christian : dependency-walker confirmed the VC++ issue. After installing the MS redistributable DLL package -- actually I had to install *three* before hitting the right one (2005SP1) -- libpyexiv2 finally loads all its dependencies. Of course I've still no idea why/how it ran on the first three systems (and most others, from the look of it) without doing anything, and not on this one... but at least now it works :-) From dmcclouds at gmail.com Sun Oct 18 12:22:07 2009 From: dmcclouds at gmail.com (D) Date: Sun, 18 Oct 2009 09:22:07 -0700 (PDT) Subject: Spawning Cmd Window via Subprocess References: <766f9620-7e18-4b1d-b541-a0c27a054c94@x37g2000yqj.googlegroups.com> Message-ID: On Oct 16, 5:26?pm, TerryP wrote: > On Oct 16, 8:15?pm, D wrote: > > > Hello, > > > I would like to be able to spawn a new CMD window (specifing size, > > color and placement of the window), ?and write to it separately. > > Specifically, I have a backup program that displays each file backed > > up in the main window, and I would like to spawn and continually > > update a second CMD window that will display the current status (i.e. > > number of files backed up, amount of data backed up). ?Also, I only > > want to display the update messages, don't want to display any command > > prompts. ?I'm thinking I should be able to do this using subprocess, > > but I haven't been able to find out how. ?Any help would be greatly > > appreciated! > > you'll likely want to fiddle with subprocess.Popen with the arguments > set to suitable values to invoke a cmd window and establish pipes for > communication; see the documentation. If that doesn't work, it would > probably be time to muck with the Windows API. Thanks, TerryP..I briefly played around with subprocess.Popen, but so far no luck (certainly not to say I haven't missed something). You could be right that the Win API is needed.. I try to avoid whenever possible though. :) From victorsubervi at gmail.com Sun Oct 18 12:23:17 2009 From: victorsubervi at gmail.com (Victor Subervi) Date: Sun, 18 Oct 2009 11:23:17 -0500 Subject: More & More Fun w/ Pics & MySQL In-Reply-To: <4ADA21A5.5010408@picture-art.eu> References: <4dc0cfea0910161223q326f1306jb24b7d62ab124d30@mail.gmail.com> <4dc0cfea0910161439v4401d1b8h1f0c325bf809251d@mail.gmail.com> <4dc0cfea0910161721v4812822arfcb42743c1359dd7@mail.gmail.com> <4dc0cfea0910170818w37a47580qb49290a5b5546bfa@mail.gmail.com> <4ADA21A5.5010408@picture-art.eu> Message-ID: <4dc0cfea0910180923r63708c68w84a9c3b1f4ce6e1b@mail.gmail.com> Thank you all. I have __no__idea__ why the link I sent you, that you tested, __didn't__ work for me. Was it a problem with the browser? Why did it render other images fine? Strange! Well, glad it works *today*. Hopefully it'll work tomorrow, when I try and pick up a check from my client! Regarding Carsten's misunderstanding of my mentioning of commenting out statements, are the following two statements identical or not? print 'Content-type: image/jpeg' print 'Content-type: image/jpeg' #print 'Content-type: text/plain' This is what I mean. Sorry it was so difficult to communicate that to you. Thanks again, V On Sat, Oct 17, 2009 at 2:57 PM, Neo wrote: > Hi, > > Victor Subervi schrieb: > > Let me clarify. This prints out "all sorts of crap", which means an > > image string, the image as a string, to the screen: > > > > print 'Content-type: image/jpeg' > > print 'Content-Encoding: base64' > > print > > print pic().encode('base64') > > print '' > > > > The following once upon a time printed images, but now it doesn't. Why > > would that be? I would refresh the screen, and it would print. I'd > > change a line and it wouldn't. I'd change it back to what it was and it > > would no longer the image to the screen. Why is that? The same happens > > with or without the base64 stuff. Commenting out a line seemed to make a > > difference! > > > > print 'Content-type: text/plain' > > #print 'Content-type: image/jpeg' > > print > > print pic() > > print '' > > > > The above prints out a broken image of correct dimensions. > > Only with a broken client (I assume Internetexplorer here?) > Any you don't know what it does in guessing and caching (Try > shift-reload when you test things) > > You should work from a static page and a static image and look what > is transferred by the server (wireshark or tcpflow are of great help, > but nc | hexdump -C | more is probably more easy if you work out the > HTTP protocol (see rfc2616) > > You script needs to do the same as a server would do serving a static > resource. Just look how it works and rebuild it. > > You will quickly find out, that there is obviously no reason to have > at the end of a binary resource an Image is. > Also correct mime type is important unless you deal with broken clients. > > There are a lot more HTTP-Headers you want to understand and use but the > simple usage above, provided pic() indeed returns the binary data of the > image unaltered, should work (w/o the html crap at the end). > You would not use print since it adds a linefeed at the end which is not > part of the data. sys.stdout.write() should work better (and can be used > incremental to avoid returning the whole image data from memory) > > > Of course I try and figure out how things work once they get working. > > Sometimes, however, there is __no__ logic to it __at__all__. Sorry. > > After years of struggling with python I've come to realize that even > > though I may not be a good programmer, it isn't me. It may not be python > > itself. It may be the crappy hardware the server farms use. It may be > > the way they tweak their python interpreter. But it isn't just me. > > Sorry to tell you that but its just you. There is no magic in that. > Ok, there is one thing: what makes you sure the data in the database > is really unaltered image data? I mean, you are using mysql... > for a first thing I'd write the output into a file and try to open it > in a graphic viewer. > > > It would be nice if I could get this code printing images to the screen > > as it was before I had to put out another unnecessary fire when code > > Images are not "printed to screen" the whole idea is just wrong. > > > that previously uploaded the images in the first place inexplicably no > > longer worked. Then, lo and behold, when I came back to this code that I > > hadn't touched and was working fine earlier this week, it no longer > > works. Lovely. My "plug-and-play" program has devoured two weeks of my > > time and I'm still up the creek without a paddle. Sure would appreciate > > any help you can give. > > I see "plug and play" and read cargo-cult. You need to read a bit about > the basics on the matter or you will constantly run uphill - and even if > it seems to work it has no value if you don't know _why_. > > Regards > Tino > -------------- next part -------------- An HTML attachment was scrubbed... URL: From thom1948 at gmail.com Sun Oct 18 12:48:11 2009 From: thom1948 at gmail.com (Thomas) Date: Sun, 18 Oct 2009 09:48:11 -0700 (PDT) Subject: help to convert c++ fonction in python References: <9egkd5151sokfi15a8j39qe939ho7ajfvo@4ax.com> <4eb0089f0910171657q5c85df9ew4cb9699032f49c85@mail.gmail.com> Message-ID: If you change your last line from: print s to: print u you'll get different results :) TC From carsten.haese at gmail.com Sun Oct 18 13:16:10 2009 From: carsten.haese at gmail.com (Carsten Haese) Date: Sun, 18 Oct 2009 13:16:10 -0400 Subject: More & More Fun w/ Pics & MySQL In-Reply-To: <4dc0cfea0910180923r63708c68w84a9c3b1f4ce6e1b@mail.gmail.com> References: <4dc0cfea0910161223q326f1306jb24b7d62ab124d30@mail.gmail.com> <4dc0cfea0910161439v4401d1b8h1f0c325bf809251d@mail.gmail.com> <4dc0cfea0910161721v4812822arfcb42743c1359dd7@mail.gmail.com> <4dc0cfea0910170818w37a47580qb49290a5b5546bfa@mail.gmail.com> <4ADA21A5.5010408@picture-art.eu> <4dc0cfea0910180923r63708c68w84a9c3b1f4ce6e1b@mail.gmail.com> Message-ID: Victor Subervi wrote: > Thank you all. I have __no__idea__ why the link I sent you, that you > tested, __didn't__ work for me. Was it a problem with the browser? My Magic 8-Ball says "Unclear. Ask again later." > Regarding Carsten's misunderstanding of my mentioning of commenting out > statements, are the following two statements identical or not? > > print 'Content-type: image/jpeg' > > > print 'Content-type: image/jpeg' > #print 'Content-type: text/plain' Those two sets of lines are functionally identical, and they *will* *always* produce the same results in the same context. If they don't, then your server isn't executing the code that you think it's executing. -- Carsten Haese http://informixdb.sourceforge.net From lie.1296 at gmail.com Sun Oct 18 13:30:18 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 19 Oct 2009 04:30:18 +1100 Subject: The rap against "while True:" loops In-Reply-To: <87vdidkpfv.fsf@benfinney.id.au> References: <01ccc46d-5ea9-4dfe-ba22-699c6b859f00@v36g2000yqv.googlegroups.com> <7xmy3peq3s.fsf@ruckus.brouhaha.com> <7xws2tace4.fsf@ruckus.brouhaha.com> <02ea93d3$0$1384$c3e8da3@news.astraweb.com> <7x4opx2on4.fsf@ruckus.brouhaha.com> <4adade1a$1@dnews.tpgi.com.au> <87vdidkpfv.fsf@benfinney.id.au> Message-ID: <4adb50aa$1@dnews.tpgi.com.au> Ben Finney wrote: > Lie Ryan writes: > >> Paul Rubin wrote: >>> Steven D'Aprano writes: >>>> For the record, the four lines Paul implies are "confusing" are: >>>> >>>> try: >>>> d[key] += value >>>> except KeyError: >>>> d[key] = value >>> Consider what happens if the computation of "key" or "value" itself >>> raises KeyError. >> Isn't key and value just a simple variables/names? > > In that example, yes. Paul is encouraging the reader to think of more > complex cases where they are compound expressions, that can therefore > raise other errors depending on what those expressions contain. If key and value had been anything but simple variable/name, then definitely you're writing the try-block the wrong way. try-block must be kept as simple as possible. Here is a simple, and effective way to mitigate the concern about compound expressions: key = complex_compound_expression_to_calculate_key value = complex_compound_expression_to_calculate_value try: d[key] += value except KeyError: d[key] = value The point is: "complex expressions and exceptions are used for different purpose" From http Sun Oct 18 13:53:41 2009 From: http (Paul Rubin) Date: 18 Oct 2009 10:53:41 -0700 Subject: The rap against "while True:" loops References: <01ccc46d-5ea9-4dfe-ba22-699c6b859f00@v36g2000yqv.googlegroups.com> <7xmy3peq3s.fsf@ruckus.brouhaha.com> <7xws2tace4.fsf@ruckus.brouhaha.com> <02ea93d3$0$1384$c3e8da3@news.astraweb.com> <7x4opx2on4.fsf@ruckus.brouhaha.com> <4adade1a$1@dnews.tpgi.com.au> <87vdidkpfv.fsf@benfinney.id.au> <4adb50aa$1@dnews.tpgi.com.au> Message-ID: <7x8wf88u6y.fsf@ruckus.brouhaha.com> Lie Ryan writes: > If key and value had been anything but simple variable/name, then > definitely you're writing the try-block the wrong way. try-block must > be kept as simple as possible. Avoiding the try-block completely is the simplest possibility of them all. From contact at pythonxy.com Sun Oct 18 14:11:20 2009 From: contact at pythonxy.com (Pierre Raybaut) Date: Sun, 18 Oct 2009 20:11:20 +0200 Subject: [ANN] Python(x,y) 2.6.3.0 released Message-ID: <4ADB5A48.1030605@pythonxy.com> Hi all, I'm quite pleased (and relieved) to announce that Python(x,y) version 2.6.3.0 has been released. It is the first release based on Python 2.6 -- note that Python(x,y) version number will now follow the included Python version (Python(x,y) vX.Y.Z.N will be based on Python vX.Y.Z). Python(x,y) is a free Python distribution providing a ready-to-use scientific development software for numerical computations, data analysis and data visualization based on Python programming language, Qt graphical user interfaces (and development framework), Eclipse integrated development environment and Spyder interactive development environment. Its purpose is to help scientific programmers used to interpreted languages (such as MATLAB or IDL) or compiled languages (C/C++ or Fortran) to switch to Python. It is now available for Windows XP/Vista/7 (as well as for Ubuntu through the pythonxy-linux project -- note that included software may differs from the Windows version): http://www.pythonxy.com Major changes since v2.1.17: * Python 2.6.3 * Spyder 1.0.0 -- the Scientific PYthon Development EnviRonment, a powerful MATLAB-like development environment introducing exclusive features in the scientific Python community (http://packages.python.org/spyder/) * MinGW 4.4.0 -- including gcc 4.4.0 and gfortran * Pydev 1.5.0 -- now including the powerful code analysis features of Pydev Extensions (formerly available as a commercial extension to the free Pydev plugin) * Enthought Tool Suite 3.3.0 * PyQt 4.5.4 and PyQwt 5.2.0 * VTK 5.4.2 * ITK 3.16 -- Built for Python 2.6 thanks to the help of Charl Botha, DeVIDE (Delft Visualisation and Image processing Development Environment) Complete release notes: http://www.pythonxy.com/download.php - Pierre From contact at pythonxy.com Sun Oct 18 14:11:27 2009 From: contact at pythonxy.com (Pierre Raybaut) Date: Sun, 18 Oct 2009 20:11:27 +0200 Subject: [ANN] Spyder v1.0.0 released Message-ID: <4ADB5A4F.7010504@pythonxy.com> Hi all, I'm pleased to announce here that Spyder version 1.0.0 has been released: http://packages.python.org/spyder Previously known as Pydee, Spyder (Scientific PYthon Development EnviRonment) is a free open-source Python development environment providing MATLAB-like features in a simple and light-weighted software, available for Windows XP/Vista/7, GNU/Linux and MacOS X: * advanced code editing features (code analysis, ...) * interactive console with MATLAB-like workpace (with GUI-based list, dictionary, tuple, text and array editors -- screenshots: http://packages.python.org/spyder/console.html#the-workspace) and integrated matplotlib figures * external console to open an interpreter or run a script in a separate process (with a global variable explorer providing the same features as the interactive console's workspace) * code analysis with pyflakes and pylint * search in files features * documentation viewer: automatically retrieves docstrings or source code of the function/class called in the interactive/external console * integrated file/directories explorer * MATLAB-like path management ...and more! Spyder is part of spyderlib, a Python module based on PyQt4 and QScintilla2 which provides powerful console-related PyQt4 widgets. I would like to thanks here all the Spyder users and especially the beta testers and contributors: without them, Spyder wouldn't be as stable, easy-to-use and full-featured as it is. - Pierre From lie.1296 at gmail.com Sun Oct 18 14:25:07 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 19 Oct 2009 05:25:07 +1100 Subject: help to convert c++ fonction in python In-Reply-To: References: <9egkd5151sokfi15a8j39qe939ho7ajfvo@4ax.com> <4eb0089f0910171657q5c85df9ew4cb9699032f49c85@mail.gmail.com> Message-ID: <4adb5d84$1@dnews.tpgi.com.au> Thomas wrote: > If you change your last line from: > > print s > > to: > > print u > > you'll get different results :) if you change: s1 = base64.b64decode( s ) into s = base64.b64decode( s ) you'll get the same result again. From necronymouse at gmail.com Sun Oct 18 14:28:57 2009 From: necronymouse at gmail.com (Necronymouse) Date: Sun, 18 Oct 2009 11:28:57 -0700 (PDT) Subject: problem with re.MULTILINE Message-ID: <8a0a0ea2-baac-4063-9840-75f5fac6fc50@x37g2000yqj.googlegroups.com> Hello i ?ve got a little problem: I ?ve this text: http://openpaste.org/en/secret/17343/pass-python and I need to parse it. So i wrote this: >>> patternNode = re.compile(""" # Node (\w*).* (.*)""", re.MULTILINE) with open("test.msg", "r") as file: testData = file.read() for Node in re.findall(patternNode, testData): print "Node:", Node[0] print Node <<< but it prints only one line from text. If i am using re.DOTALL it wouldn?t print anything.. So don?t you know whre the problem is? Sorry for my English - it?s not my native language... From benjamin.the.violinist at gmail.com Sun Oct 18 14:34:09 2009 From: benjamin.the.violinist at gmail.com (Benjamin Middaugh) Date: Sun, 18 Oct 2009 14:34:09 -0400 Subject: Reverse Iteration Through Integers Message-ID: <4ADB5FA1.5020008@gmail.com> I'm trying to make an integer that is the reverse of an existing integer such that 169 becomes 961. I guess I don't know enough yet to figure out how to do this without a ton of awkward-looking code. I've tried for loops without much success. I guess I need a good way of figuring out the length of the input integer so my loop can iterate that many times to reverse the number, but I keep getting errors that say "TypeError: 'Int' object is not iterable". Any help would be much appreciated, Benjamin "Self-Proclaimed Python Newbie" From ubershmekel at gmail.com Sun Oct 18 14:44:35 2009 From: ubershmekel at gmail.com (Yuvgoog Greenle) Date: Sun, 18 Oct 2009 20:44:35 +0200 Subject: (from stdlib-sig) ctypes or struct from an h file Message-ID: <9d153b7c0910181144t37e66beajae57e02db29babbb@mail.gmail.com> Is there a way that Python and C can have a shared definition for a binary data structure? It could be nice if: 1. struct or ctypes had a function that could parse a .h/.c/.cpp file to auto-generate constructors or 2. a ctypes definition could be exported to a .h file. So my question is - is there a way to do this in the std-lib or even pypi? --yuv ps If this doesn't exist, then I'm probably going to open a project and would like some tips/ideas. From benjamin.the.violinist at gmail.com Sun Oct 18 15:13:21 2009 From: benjamin.the.violinist at gmail.com (Benjamin Middaugh) Date: Sun, 18 Oct 2009 15:13:21 -0400 Subject: Checking a Number for Palindromic Behavior Message-ID: <4ADB68D1.4010308@gmail.com> Thanks to everyone who helped with my query on reversing integers. I have one more simple problem I'm having trouble solving. I want to check a number for palindromic behavior (reading the same backwards and forwards). So if I have an integer 1457 it can tell me this is not the same from both ends but 1551 is. I think the simplest way would be to work inwards from both ends checking digits for equality, but I don't know enough (yet) to do this. All help is much appreciated. Benjamin From python at mrabarnett.plus.com Sun Oct 18 15:20:20 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 18 Oct 2009 20:20:20 +0100 Subject: problem with re.MULTILINE In-Reply-To: <8a0a0ea2-baac-4063-9840-75f5fac6fc50@x37g2000yqj.googlegroups.com> References: <8a0a0ea2-baac-4063-9840-75f5fac6fc50@x37g2000yqj.googlegroups.com> Message-ID: <4ADB6A74.5020501@mrabarnett.plus.com> Necronymouse wrote: > Hello i ?ve got a little problem: I ?ve this text: > http://openpaste.org/en/secret/17343/pass-python and I need to parse > it. So i wrote this: > > patternNode = re.compile(""" > # Node (\w*).* > (.*)""", re.MULTILINE) > > > with open("test.msg", "r") as file: > testData = file.read() > > for Node in re.findall(patternNode, testData): > print "Node:", Node[0] > print Node > <<< > > but it prints only one line from text. If i am using re.DOTALL it > wouldn?t print anything.. So don?t you know whre the problem is? > I assume you mean that it's giving you only the first line of text of each node. "(.*)" will capture a single (and possibly empty) line of text. "(.+\n)" will capture a single non-empty line of text ending with a newline. I think you want to capture multiple non-empty lines, each line ending with a newline: patternNode = re.compile(""" # Node (\w*).* ((?:.+\n)*)""", re.MULTILINE) > Sorry for my English - it?s not my native language... It's better than my Czech/Slovak (depending on what Google says)! :-) From lie.1296 at gmail.com Sun Oct 18 15:38:13 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 19 Oct 2009 06:38:13 +1100 Subject: More & More Fun w/ Pics & MySQL In-Reply-To: References: <4dc0cfea0910161223q326f1306jb24b7d62ab124d30@mail.gmail.com> <4dc0cfea0910161439v4401d1b8h1f0c325bf809251d@mail.gmail.com> <4dc0cfea0910161721v4812822arfcb42743c1359dd7@mail.gmail.com> <4dc0cfea0910170818w37a47580qb49290a5b5546bfa@mail.gmail.com> <4ADA21A5.5010408@picture-art.eu> <4dc0cfea0910180923r63708c68w84a9c3b1f4ce6e1b@mail.gmail.com> Message-ID: <4adb6ea6@dnews.tpgi.com.au> Carsten Haese wrote: > Victor Subervi wrote: >> Thank you all. I have __no__idea__ why the link I sent you, that you >> tested, __didn't__ work for me. Was it a problem with the browser? > > My Magic 8-Ball says "Unclear. Ask again later." > >> Regarding Carsten's misunderstanding of my mentioning of commenting out >> statements, are the following two statements identical or not? >> >> print 'Content-type: image/jpeg' >> >> >> print 'Content-type: image/jpeg' >> #print 'Content-type: text/plain' > > Those two sets of lines are functionally identical, and they *will* > *always* produce the same results in the same context. If they don't, > then your server isn't executing the code that you think it's executing. Only in HTML commented line can sometimes not be ignored and is parsed depending on the time of the day (specifically the in a