Python-URL! - weekly Python news and links (Nov 28)
QOTW: Given that C++ has pointers and typecasts, it's really hard to have a serious conversation about type safety with a C++ programmer and keep a straight face. It's kind of like having a guy who juggles chainsaws wearing body armor arguing with a guy who juggles rubber chickens wearing a T-shirt about who's in more danger. - Roy Smith, c.l.py, 2004.05.23 [R]esumable functions are a honking great idea. - Alan Kennedy Making all equal instances the same (so a==b implies a is b): http://groups.google.com/group/comp.lang.python/browse_thread/thread/c82c7d1cfb49c1d4 A good way of organizing packages and project layout: http://groups.google.com/group/comp.lang.python/browse_thread/thread/8a064827f4d2afc7 Enumerating all the running processes (cross-platform): http://groups.google.com/group/comp.lang.python/browse_thread/thread/f9f0c6726f6e0211 Numerical tricks from number-crunching people: http://groups.google.com/group/comp.lang.python/browse_thread/thread/f67b7c777c0d6915 How to obtain the next floating point number: http://groups.google.com/group/comp.lang.python/browse_thread/thread/0d992d01db3bde1a Python variables and what they mean: http://groups.google.com/group/comp.lang.python/browse_thread/thread/26e4fe70c858e156 Catching a segfault in a third party library: http://groups.google.com/group/comp.lang.python/browse_thread/thread/d505960679df17d2 Why files don't have an EOF attribute - and an alternative method to process files in chunks http://groups.google.com/group/comp.lang.python/browse_thread/thread/4916ce38c3ca0e00 Everything Python-related you want is probably one or two clicks away in these pages: Python.org's Python Language Website is the traditional center of Pythonia http://www.python.org Notice especially the master FAQ http://www.python.org/doc/FAQ.html PythonWare complements the digest you're reading with the marvelous daily python url http://www.pythonware.com/daily Mygale is a news-gathering webcrawler that specializes in (new) World-Wide Web articles related to Python. http://www.awaretek.com/nowak/mygale.html While cosmetically similar, Mygale and the Daily Python-URL are utterly different in their technologies and generally in their results. Just beginning with Python? This page is a great place to start: http://wiki.python.org/moin/BeginnersGuide/Programmers The Python Papers aims to publish the efforts of Python enthusiats: http://pythonpapers.org/ The Python Magazine is a technical monthly devoted to Python: http://pythonmagazine.com Readers have recommended the Planet sites: http://planetpython.org http://planet.python.org comp.lang.python.announce announces new Python software. Be sure to scan this newsgroup weekly. http://groups.google.com/groups?oi=djqas_ugroup=comp.lang.python.announce Python411 indexes podcasts ... to help people learn Python ... Updates appear more-than-weekly: http://www.awaretek.com/python/index.html Steve Bethard continues the marvelous tradition early borne by Andrew Kuchling, Michael Hudson, Brett Cannon, Tony Meyer, and Tim Lesher of intelligently summarizing action on the python-dev mailing list once every other week. http://www.python.org/dev/summary/ The Python Package Index catalogues packages. http://www.python.org/pypi/ The somewhat older Vaults of Parnassus ambitiously collects references to all sorts of Python resources. http://www.vex.net/~x/parnassus/ Much of Python's real work takes place on Special-Interest Group mailing lists http://www.python.org/sigs/ Python Success Stories--from air-traffic control to on-line match-making--can inspire you or decision-makers to whom you're subject with a vision of what the language makes practical. http://www.pythonology.com/success The Python Software Foundation (PSF) has replaced the Python Consortium as an independent nexus of activity. It has official responsibility for Python's development and maintenance. http://www.python.org/psf/ Among the ways you can support PSF is with a donation. http://www.python.org/psf/donate.html Kurt B. Kaiser publishes a weekly report on faults and patches. http://www.google.com/groups?as_usubject=weekly%20python%20patch Although unmaintained since 2002, the Cetus collection of Python hyperlinks retains a few gems. http://www.cetus-links.org/oo_python.html Python FAQTS http://python.faqts.com/ The Cookbook is a collaborative effort to capture useful and interesting recipes.
ANN: PyTables PyTables Pro 2.0.2 are out!
Hi everyone, We at Carabos are happy to announce the simultaneous release of the new 2.0.2 versions of both PyTables and PyTables Pro. They are mainly bugfix releases, and users of previous versions are encouraged to upgrade. And now the official announce: Announcing PyTables and PyTables Pro 2.0.2 PyTables is a library for managing hierarchical datasets and designed to efficiently cope with extremely large amounts of data with support for full 64-bit file addressing. PyTables runs on top of the HDF5 library and NumPy package for achieving maximum throughput and convenient use. PyTables Pro adds OPSI, a powerful indexing engine for executing very fast queries in large tables. In this version, some bugs have been fixed, being the most important a problem when moving or renaming a group. Some small improvements have been added as well. Besides, a *critical* bug has been fixed in the Pro version (the problem arose when doing repeated queries using the same index). Because of this, an upgrade is strongly recommended. In case you want to know more in detail what has changed in this version, have a look at ``RELEASE_NOTES.txt``. Find the HTML version for this document at: http://www.pytables.org/moin/ReleaseNotes/Release_2.0.2 You can download a source package of the version 2.0.2 with generated PDF and HTML docs and binaries for Windows from http://www.pytables.org/download/stable/ For an on-line version of the manual, visit: http://www.pytables.org/docs/manual-2.0.2 Migration Notes for PyTables 1.x users == If you are a user of PyTables 1.x, probably it is worth for you to look at ``MIGRATING_TO_2.x.txt`` file where you will find directions on how to migrate your existing PyTables 1.x apps to the 2.x versions. You can find an HTML version of this document at http://www.pytables.org/moin/ReleaseNotes/Migrating_To_2.x Resources = Go to the PyTables web site for more details: http://www.pytables.org About the HDF5 library: http://hdfgroup.org/HDF5/ About NumPy: http://numpy.scipy.org/ To know more about the company behind the development of PyTables, see: http://www.carabos.com/ Acknowledgments === Thanks to many users who provided feature improvements, patches, bug reports, support and suggestions. See the ``THANKS`` file in the distribution package for a (incomplete) list of contributors. Many thanks also to SourceForge who have helped to make and distribute this package! And last, but not least thanks a lot to the HDF5 and NumPy (and numarray!) makers. Without them, PyTables simply would not exist. Share your experience = Let us know of any bugs, suggestions, gripes, kudos, etc. you may have. **Enjoy data!** -- The PyTables Team :: Ivan Vilata i Balaguer qo http://www.carabos.com/ Cárabos Coop. V. V V Enjoy Data signature.asc Description: Digital signature -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations.html
Wing IDE 3.0.2 released
Hi, We're happy to announce version 3.0.2 of Wing IDE, an advanced development environment for the Python programming language. It is available from: http://wingware.com/downloads This release focuses on fixing usability issues found in Wing 3.0.1, including missed Search in Project matches, failure to offer auto-completion for code in extension modules, and about 25 other bugs. See the change log for details: http://wingware.com/pub/wingide/3.0.2/CHANGELOG.txt It is a free upgrade for all Wing 3.x users. *About Wing IDE* Wing IDE is an integrated development environment for the Python programming language. It provides powerful debugging, editing, code intelligence, testing, and search capabilities that reduce development and debugging time, cut down on coding errors, and make it easier to understand and navigate Python code. New features added in Wing 3.0 include: * Multi-threaded debugger * Debug value tooltips in editor, debug probe, and interactive shell * Autocompletion and call tips in debug probe and interactive shell * Automatically updating project directories * Testing tool, currently supporting unittest derived tests (*) * OS Commands tool for executing and interacting with external commands (*) * Rewritten indentation analysis and conversion (*) * Introduction of Wing IDE 101, a free edition for beginning programmers * Available as a .deb package for Debian and Ubuntu * Support for Stackless Python * Support for 64 bit Python on Windows and Linux (*)'d items are available in Wing IDE Professional only. System requirements are Windows 2000 or later, OS X 10.3.9 or later for PPC or Intel (requires X11 Server), or a recent Linux system (either 32 or 64 bit). *Purchasing Upgrading* Wing IDE Professional Wing IDE Personal are commercial software and require a license to run. To upgrade a 2.x license or purchase a new 3.x license: Upgradehttps://wingware.com/store/upgrade Purchase https://wingware.com/store/purchase Any 2.x license sold after May 2nd 2006 is free to upgrade; others cost 1/2 the normal price to upgrade. -- The Wingware Team Wingware | Python IDE Advancing Software Development www.wingware.com -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations.html
Re: the annoying, verbose self
On Mon, 26 Nov 2007 21:48:36 +0100, Ton van Vliet wrote: On Mon, 26 Nov 2007 20:14:50 +0100, Bruno Desthuilliers [EMAIL PROTECTED] wrote: However, I was more thinking in terms of attributes only Too bad : in Python, everything's an object, so 'methods' are attributes too. Right, but I'm sure *you* know a way to distinguish between them (I'm just a beginner ;-) All methods are attributes. Not all attributes are methods. The usual way to see if something is a method is to try calling it and see what happens, but if you want a less informal test, try type(): type(''.join) type 'builtin_function_or_method' type(Foo().foo) # with the obvious definition of Foo type 'instancemethod' -- Steven -- http://mail.python.org/mailman/listinfo/python-list
FREE AIR TICKET
AIR TICKET FOR SWIZ ANSWER SIMPLE QUESTIONhttp://rexmier.blogspot.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Rss Feed Creator
Shane Geiger wrote: James Matthews wrote: I am looking for a library that will create Rss/Atom feeds in python. It needs to format the XML in a readable format! Does anyone have any suggestions? http://trac.openplans.org/openplans/browser/TaggerClient/trunk/taggerclient/atom.py?rev=6717 The usage is mainly the same as in standard ElementTree. http://codespeak.net/lxml/dev/ Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: Rss Feed Creator
On 11/27/07, James Matthews [EMAIL PROTECTED] wrote: I am looking for a library that will create Rss/Atom feeds in python. It needs to format the XML in a readable format! Does anyone have any suggestions? You can also use some xml-based template engine (Kid or Genshi). -- Vladimir Rusinov GreenMice Solutions: IT-решения на базе Linux http://greenmice.info/ -- http://mail.python.org/mailman/listinfo/python-list
Re: the annoying, verbose self
Steven D'Aprano a écrit : On Mon, 26 Nov 2007 21:48:36 +0100, Ton van Vliet wrote: On Mon, 26 Nov 2007 20:14:50 +0100, Bruno Desthuilliers [EMAIL PROTECTED] wrote: However, I was more thinking in terms of attributes only Too bad : in Python, everything's an object, so 'methods' are attributes too. Right, but I'm sure *you* know a way to distinguish between them Yes : reading the doc. But that's something the compiler will have hard time doing. (I'm just a beginner ;-) All methods are attributes. Not all attributes are methods. The usual way to see if something is a method is to try calling it and see what happens, but if you want a less informal test, try type(): type(''.join) type 'builtin_function_or_method' type(Foo().foo) # with the obvious definition of Foo type 'instancemethod' Fine. Now since Python let you define your own callable types and your own descriptors, you can as well have an attribute that behave just like a method without being an instance of any of the method types - so the above test defeats duck typing. And since you can have callable attributes that are definitively not methods, you can't rely on the fact that an attribute is callable neither. -- http://mail.python.org/mailman/listinfo/python-list
Why are class methods not classmethods?
There's some subtle behaviour going on here that I don't really follow. Class methods apparently aren't classmethods. class Parrot(object): ... def method(self, *args): ... return self, args ... @classmethod ... def cmethod(cls, *args): ... return cls, args ... type(parrot.method) # as expected type 'instancemethod' type(parrot.cmethod) # I don't expect this result type 'instancemethod' type(classmethod(parrot.method)) type 'classmethod' parrot.cm = classmethod(parrot.method) type(parrot.cm) # I expect this type 'classmethod' Parrot.CM = classmethod(parrot.method) type(Parrot.CM) # but not this type 'instancemethod' Can anyone explain why class methods bound to a class are instancemethods rather than classmethods? -- Steven. -- http://mail.python.org/mailman/listinfo/python-list
Re: gnosis XML objectify
Wang, Harry wrote: Test Suite Started @ 2007-11-26 11:34:46.617000 Traceback (most recent call last): File C:\UDR2\UDRxmlGateway.py, line 370, in module ParseAll() File C:\UDR2\UDRxmlGateway.py, line 286, in ParseAll py_obj = gnosis.xml.objectify.XML_Objectify(InputFile).make_instance() File C:\python25\Lib\site-packages\gnosis\xml\objectify\_objectify.py, line 160, in make_instance o = self.ParseFile(self._fh) File C:\python25\Lib\site-packages\gnosis\xml\objectify\_objectify.py, line 190, in ParseFile self._myparser.ParseFile(file) xml.parsers.expat.ExpatError: not well-formed (invalid token): line 68, column 0 You XML input is broken. This is not a gnosis problem. Look at line 68 in your XML file. Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: the annoying, verbose self
In article [EMAIL PROTECTED], Bruno Desthuilliers [EMAIL PROTECTED] wrote: Steven D'Aprano a écrit : On Mon, 26 Nov 2007 21:48:36 +0100, Ton van Vliet wrote: On Mon, 26 Nov 2007 20:14:50 +0100, Bruno Desthuilliers [EMAIL PROTECTED] wrote: However, I was more thinking in terms of attributes only Too bad : in Python, everything's an object, so 'methods' are attributes too. Right, but I'm sure *you* know a way to distinguish between them Yes : reading the doc. But that's something the compiler will have hard time doing. (I'm just a beginner ;-) All methods are attributes. Not all attributes are methods. The usual way to see if something is a method is to try calling it and see what happens, but if you want a less informal test, try type(): type(''.join) type 'builtin_function_or_method' type(Foo().foo) # with the obvious definition of Foo type 'instancemethod' Fine. Now since Python let you define your own callable types and your own descriptors, you can as well have an attribute that behave just like a method without being an instance of any of the method types - so the above test defeats duck typing. And since you can have callable attributes that are definitively not methods, you can't rely on the fact that an attribute is callable neither. If you want to have a little fun: class peverse: def __call__(self): raise AttributeError (peverse instance has no __call__ method) x = peverse() x() -- http://mail.python.org/mailman/listinfo/python-list
Re: Why are class methods not classmethods?
On Tue, 27 Nov 2007 09:12:28 +, Steven D'Aprano wrote: Can anyone explain why class methods bound to a class are instancemethods rather than classmethods? I'd say because they are bound to the class which is the instance that is passed as first argument. Just like unbound methods turn into bound methods when you access them on normal objects and change from functions to instance methods, accessing class methods make them instance methods too. If you bypass the dot operator you get the still unwrapped class method: In [83]: Parrot.cmethod Out[83]: bound method type.cmethod of class '__main__.Parrot' In [84]: type(Parrot.cmethod) Out[84]: type 'instancemethod' In [85]: Parrot.__dict__['cmethod'] Out[85]: classmethod object at 0x9b26434 In [86]: type(Parrot.__dict__['cmethod']) Out[86]: type 'classmethod' Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
Re: the annoying, verbose self
On Nov 27, 3:20 am, Roy Smith [EMAIL PROTECTED] wrote: If you want to have a little fun: class peverse: def __call__(self): raise AttributeError (peverse instance has no __call__ method) x = peverse() x() That is peverse, but still... from types import FunctionType type(x) == FunctionType # False And you can't (easily?) subclass FunctionType: Error when calling the metaclass bases type 'function' is not an acceptable base type ;) Regards, Jordan -- http://mail.python.org/mailman/listinfo/python-list
Re: Rss Feed Creator
Thank you everyone! On Nov 27, 2007 10:13 AM, Vladimir Rusinov [EMAIL PROTECTED] wrote: On 11/27/07, James Matthews [EMAIL PROTECTED] wrote: I am looking for a library that will create Rss/Atom feeds in python. It needs to format the XML in a readable format! Does anyone have any suggestions? You can also use some xml-based template engine (Kid or Genshi). -- Vladimir Rusinov GreenMice Solutions: IT-решения на базе Linux http://greenmice.info/ -- http://search.goldwatches.com/?Search=Movado+Watches http://www.goldwatches.com/coupons http://www.jewelerslounge.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Find Replace hyperlinks in a string
On Nov 27, 1:37 am, Nico Grubert [EMAIL PROTECTED] wrote: Hi there, I have a string containing some hyperlinks. I'd like to replace every hyperlink with a HTML style link. Example: Replace 'http://www.foo.com/any_url' with 'a href=http://www.foo.com/any_url;http://www.foo.com/any_url/a' What's the best way to do this if I have a few hundret strings to check? Thanks in advance, Nico Well, this isn't the most robust and someone will probably say not to use regular expressions, but the QD way is: import re fixed = re.sub(r'(http:[^\s\n\r]+)', r'a href=\1\1/a', your_string) NB. If the URLs are malformed (like have spaces in them, or are broken over several lines) this won't work right. Regards, Jordan -- http://mail.python.org/mailman/listinfo/python-list
vanga machan vanga
http://goast.50webs.com/ http://bigchurch.com/go/g912753-pmem http://indianfriendfinder.com/go/g912753-pmem -- http://mail.python.org/mailman/listinfo/python-list
Re: the annoying, verbose self
Colin J. Williams a écrit : Bruno Desthuilliers wrote: [snip] Too bad : in Python, everything's an object, so 'methods' are attributes too. What do you see as a problem here? You snipped too much... Tony wrote However, I was more thinking in terms of attributes only (implying: attributes != methods). So the too bad meant won't work here. Surely it gives useful flexibility. Indeed. But that's not the problem here. -- http://mail.python.org/mailman/listinfo/python-list
Re: the annoying, verbose self
MonkeeSage a écrit : On Nov 27, 3:20 am, Roy Smith [EMAIL PROTECTED] wrote: If you want to have a little fun: class peverse: def __call__(self): raise AttributeError (peverse instance has no __call__ method) x = peverse() x() print callable(x) = True That is peverse, but still... from types import FunctionType type(x) == FunctionType # False And you can't (easily?) subclass FunctionType: Error when calling the metaclass bases type 'function' is not an acceptable base type ;) You don't have to subclass function to define a callable type that implements the descriptor protocol so it behaves just like a function in the context of an attribute lookup. -- http://mail.python.org/mailman/listinfo/python-list
Re: spawning a process with subprocess
MonkeeSage [EMAIL PROTECTED] wrote: Couple of things. You should use poll() on the Popen instance, and should check it explicitly against None (since a 0 return code, meaning exit successfully, will be treated as a false condition the same as None). Also, in your second example, you block the program when you call readlines on the pipe, since readlines blocks until it reaches eof (i.e., until pipe closes stdout, i.e., process is complete). Oh, and you don't have to split the input to the args option yourself, you can just pass a string. Though passing an array is good practice if you want to avoid passing user data through the shell. So, putting it all together, you want something like: import subprocess, time cmd = cat somefile proc = subprocess.Popen(args=cmd, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True) while 1: time.sleep(1) if proc.poll() != None: break else: print waiting on child... print returncode =, proc.returncode This works fine unless the command generates a lot of output (more than 64k on linux) when the output pipe will fill up and the process will block until it is emptied. If you run the below with `seq 1` then it works fine but as written the subprocess will block forever writing its output pipe (under linux 2.6.23). # import subprocess, time cmd = for i in `seq 2`; do echo $i done exit 42 proc = subprocess.Popen(args=cmd, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True) while 1: time.sleep(1) if proc.poll() != None: break else: print waiting on child... print returncode =, proc.returncode lines = 0 total = 0 for line in proc.stdout: lines += 1 total += len(line) print Received %d lines of %d bytes total % (lines, total) # So you do need to read stuff from your subprocess, but there isn't a way in the standard library to do that without potentially blocking. There are a few solutions 1) use the python expect module (not windows) http://pexpect.sourceforge.net/ 2) set your file descriptors non blocking. The following recipe shows a cross platform module to do it. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/440554 Or just do it with the fcntl module 3) Use a thread to read stuff from your subprocess and allow it to block on proc.stdout.read() Here is an example of 2) # import subprocess, time, os from fcntl import fcntl, F_GETFL, F_SETFL from errno import EAGAIN cmd = for i in `seq 10`; do echo $i done exit 42 proc = subprocess.Popen(args=cmd, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True) # Set non blocking (unix only) fcntl(proc.stdout, F_SETFL, fcntl(proc.stdout, F_GETFL) | os.O_NONBLOCK) def read_all(fd): out = while 1: try: bytes = fd.read(4096) except IOError, e: if e[0] != EAGAIN: raise break if not bytes: break out += bytes return out rx = while 1: time.sleep(1) if proc.poll() != None: break else: print waiting on child... rx += read_all(proc.stdout) rx += read_all(proc.stdout) print returncode =, proc.returncode lines = 0 total = 0 for line in rx.split(\n): lines += 1 total += len(line) print Received %d lines of %d bytes total % (lines, total) # Which runs like this on my machine $ python subprocess-shell-nb.py waiting on child... waiting on child... waiting on child... waiting on child... waiting on child... waiting on child... waiting on child... waiting on child... returncode = 42 Received 11 lines of 488895 bytes total -- Nick Craig-Wood [EMAIL PROTECTED] -- http://www.craig-wood.com/nick -- http://mail.python.org/mailman/listinfo/python-list
Re: trouble selling twisted
[EMAIL PROTECTED] [EMAIL PROTECTED] wrote: i have a lot of trouble selling twisted a a client lib for network access (on embedded platform) the group i'm a member of wants to write some unmaintainable threaded blocking junk in c--. Finish the code in the time it takes them to work out what they are going to do is the best way of selling it to them ;-) Twisted may be hard to understand but it doesn't produce the kind of monstrously difficult to debug threading problems that you get with the other proposed solution. If you can't sell them twisted you could try http://monkey.org/~provos/libevent/ which is similar in concept to a lot of twisted but as a C library. does anyone can give me an idea how many kilobytes extra would it take to have a pyqt linked in and a running qtreactor? (qtreactor natirally depends on the qt bindings) Presumably you wouldn't use a qtreactor unless you were already using QT - there are plenty of other reactors. So you are asking about the overhead of pyqt only? In terms of disk space it is potentially quite large. As for memory I suggest you write the minimal pyqt program on your platform (using a shared QT) and see how much extra RAM it uses. is there an exaample of going thru a passworded proxy using twisted client classes? i have trouble understanding how to adapt the proxy example on page 58 in the twisted book to my needs. I advise looking at the twisted source code! -- Nick Craig-Wood [EMAIL PROTECTED] -- http://www.craig-wood.com/nick -- http://mail.python.org/mailman/listinfo/python-list
Re: Find Replace hyperlinks in a string
You mean in Python? 'cause if it is a one time shot kind of thing, I would simply open the file in my favorite editor (NEdit) and use a Search and Replace, check the regexp box and type my expression...something along the lines of ([^:]+)://([^:/]+)(: ([0-9]+))?(/.*) to find URLs and then replace with a href=\0\0/ a ...if I remember correctly that \0 is the entire match, \1 the first parenthesised match, etc. gsal -- http://mail.python.org/mailman/listinfo/python-list
Re: spawning a process with subprocess
On Nov 27, 4:30 am, Nick Craig-Wood [EMAIL PROTECTED] wrote: MonkeeSage [EMAIL PROTECTED] wrote: Couple of things. You should use poll() on the Popen instance, and should check it explicitly against None (since a 0 return code, meaning exit successfully, will be treated as a false condition the same as None). Also, in your second example, you block the program when you call readlines on the pipe, since readlines blocks until it reaches eof (i.e., until pipe closes stdout, i.e., process is complete). Oh, and you don't have to split the input to the args option yourself, you can just pass a string. Though passing an array is good practice if you want to avoid passing user data through the shell. Well, he was setting shell=True, but I guess being explicit (about that) is better than implicit. ;) So, putting it all together, you want something like: import subprocess, time cmd = cat somefile proc = subprocess.Popen(args=cmd, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True) while 1: time.sleep(1) if proc.poll() != None: break else: print waiting on child... print returncode =, proc.returncode This works fine unless the command generates a lot of output (more than 64k on linux) when the output pipe will fill up and the process will block until it is emptied. If you run the below with `seq 1` then it works fine but as written the subprocess will block forever writing its output pipe (under linux 2.6.23). # import subprocess, time cmd = for i in `seq 2`; do echo $i done exit 42 proc = subprocess.Popen(args=cmd, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True) while 1: time.sleep(1) if proc.poll() != None: break else: print waiting on child... print returncode =, proc.returncode lines = 0 total = 0 for line in proc.stdout: lines += 1 total += len(line) print Received %d lines of %d bytes total % (lines, total) # So you do need to read stuff from your subprocess, but there isn't a way in the standard library to do that without potentially blocking. There are a few solutions 1) use the python expect module (not windows) http://pexpect.sourceforge.net/ 2) set your file descriptors non blocking. The following recipe shows a cross platform module to do it. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/440554 Or just do it with the fcntl module 3) Use a thread to read stuff from your subprocess and allow it to block on proc.stdout.read() Here is an example of 2) # import subprocess, time, os from fcntl import fcntl, F_GETFL, F_SETFL from errno import EAGAIN cmd = for i in `seq 10`; do echo $i done exit 42 proc = subprocess.Popen(args=cmd, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True) # Set non blocking (unix only) fcntl(proc.stdout, F_SETFL, fcntl(proc.stdout, F_GETFL) | os.O_NONBLOCK) def read_all(fd): out = while 1: try: bytes = fd.read(4096) except IOError, e: if e[0] != EAGAIN: raise break if not bytes: break out += bytes return out rx = while 1: time.sleep(1) if proc.poll() != None: break else: print waiting on child... rx += read_all(proc.stdout) rx += read_all(proc.stdout) print returncode =, proc.returncode lines = 0 total = 0 for line in rx.split(\n): lines += 1 total += len(line) print Received %d lines of %d bytes total % (lines, total) # Which runs like this on my machine $ python subprocess-shell-nb.py waiting on child... waiting on child... waiting on child... waiting on child... waiting on child... waiting on child... waiting on child... waiting on child... returncode = 42 Received 11 lines of 488895 bytes total -- Nick Craig-Wood [EMAIL PROTECTED] --http://www.craig-wood.com/nick Nice. Thanks for the recipe link too. Regards, Jordan -- http://mail.python.org/mailman/listinfo/python-list
Re: spawning a process with subprocess
bhunter [EMAIL PROTECTED] writes: Hi, I've used subprocess with 2.4 several times to execute a process, wait for it to finish, and then look at its output. Now I want to spawn the process separately, later check to see if it's finished, and if it is look at its output. I may want to send a signal at some point to kill the process. This seems straightforward, but it doesn't seem to be working. Here's my test case: import subprocess, time cmd = cat somefile thread = subprocess.Popen(args=cmd.split(), shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True) while(1): time.sleep(1) if(thread.returncode): break else: print thread.returncode print returncode = , thread.returncode for line in thread.stdout: print stdout:\t,line This will just print the returncode of None forever until I Ctrl-C it. Of course, the program works fine if I call thread.communicate(), but since this waits for the process to finish, that's not what I want. Any help would be appreciated. Reading documentation for subprocess, it mentions that On UNIX, with shell=False (default): In this case, the Popen class uses os.execvp() to execute the child program. args should normally be a sequence. A string will be treated as a sequence with the string as the only item (the program to execute). On UNIX, with shell=True: If args is a string, it specifies the command string to execute through the shell. If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional shell arguments. Since you have specified shell = True, and since you pass a sequence as args, you will efficiently invoke the cat process through the shell and then pass somefile as an extra argument to she shell (not the cat command) That is probably not what you intended. This can be solved by either - Not splitting the cmd, in which case you will pass the whole cmd string to the shell for execution - Or setting shell to False. This is what I would have done, since I can't see any reason for going via the shell. Please note that if setting shell to False, you must then split the cmd. Please also note that your test for the returncode might not work since a normal returncode is 0. Your code will only detect non-0 values. Also, it is good practice to call wait() on the subprocess in order to avoid zombie-processes. Finally, I find it somewhat misleading to use the name thread for the variable used to represent a sub-process. Threads and processes are not exactly the same Hence, the following code should works as expected cmd = cat somefile proc = subprocess.Popen( args = cmd.split(), shell = False, stdin = None, stdout= subprocess.PIPE, stderr= subprocess.STDOUT, close_fds = True) while True: rc = proc.poll() if rc != None: break print rc time.sleep(1) lno = 1 for lin in proc.stdout: print '%i: %s' % (lno,lin.rstrip('\n')) lno += 1 rc = proc.wait() print rc = %i % rc /Ove -- http://mail.python.org/mailman/listinfo/python-list
Re: the annoying, verbose self
On Nov 27, 4:22 am, Bruno Desthuilliers You don't have to subclass function to define a callable type that implements the descriptor protocol so it behaves just like a function in the context of an attribute lookup. I'm aware, and I understand that python's types (as with other duck- typed languages) are basically just an object's capabilities; I was just pointing out that, if needed (though I can't think of why offhand...mabye some kind of sandbox?), you can detect built-in functions. Hmm...but then again, it seems that shadowing a built-in function still reports as it's type as FunctionType... In [1]: from types import FunctionType In [2]: def len(x): ...: # mischief ...: pass ...: In [3]: type(len) == FunctionType Out[3]: True Regards, Jordan -- http://mail.python.org/mailman/listinfo/python-list
Re: the annoying, verbose self
On Nov 27, 9:20 am, Roy Smith [EMAIL PROTECTED] wrote: In article [EMAIL PROTECTED], Bruno Desthuilliers [EMAIL PROTECTED] wrote: Steven D'Aprano a écrit : On Mon, 26 Nov 2007 21:48:36 +0100, Ton van Vliet wrote: On Mon, 26 Nov 2007 20:14:50 +0100, Bruno Desthuilliers [EMAIL PROTECTED] wrote: However, I was more thinking in terms of attributes only Too bad : in Python, everything's an object, so 'methods' are attributes too. Right, but I'm sure *you* know a way to distinguish between them Yes : reading the doc. But that's something the compiler will have hard time doing. (I'm just a beginner ;-) All methods are attributes. Not all attributes are methods. The usual way to see if something is a method is to try calling it and see what happens, but if you want a less informal test, try type(): type(''.join) type 'builtin_function_or_method' type(Foo().foo) # with the obvious definition of Foo type 'instancemethod' Fine. Now since Python let you define your own callable types and your own descriptors, you can as well have an attribute that behave just like a method without being an instance of any of the method types - so the above test defeats duck typing. And since you can have callable attributes that are definitively not methods, you can't rely on the fact that an attribute is callable neither. If you want to have a little fun: class peverse: def __call__(self): raise AttributeError (peverse instance has no __call__ method) x = peverse() x() Horrific cluge: -- def noself(func): def t(*args, **kwargs): self = args[0] g = globals() delete = [] for varname in dir(self): if not varname.startswith(__) and varname not in g: g[varname] = self.__getattribute__(varname) delete.append(varname) func(*args, **kwargs) for varname in delete: del(g[varname]) return t class Test(object): x = 1 @noself def test(self): print x foo = Test() foo.test() 1 -- FTR, I won't be using this :) I do like this syntax though: class Vector: def __init__(self, x, y, z): self.x = x self.y = y self.z = z def abs(self): using self: return math.sqrt(.x*.x + .y*.y + .z*.z) Iain -- http://mail.python.org/mailman/listinfo/python-list
Re: the annoying, verbose self
MonkeeSage a écrit : On Nov 27, 4:22 am, Bruno Desthuilliers You don't have to subclass function to define a callable type that implements the descriptor protocol so it behaves just like a function in the context of an attribute lookup. I'm aware, and I understand that python's types (as with other duck- typed languages) are basically just an object's capabilities; I was just pointing out that, if needed (though I can't think of why offhand...mabye some kind of sandbox?), you can detect built-in functions. Ok. Hmm...but then again, it seems that shadowing a built-in function still reports as it's type as FunctionType... Of course (at least as long as you sahdow it with another function) - Why would it be otherwise ? -- http://mail.python.org/mailman/listinfo/python-list
see most important news
see most important news http://earnmac.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: the annoying, verbose self
Iain King [EMAIL PROTECTED] wrote: FTR, I won't be using this :) I do like this syntax though: class Vector: def __init__(self, x, y, z): self.x = x self.y = y self.z = z def abs(self): using self: return math.sqrt(.x*.x + .y*.y + .z*.z) It is a bit verbose though. This variant is shorter on my system[*]: class Vector: def __init__(self, x, y, z): self.x = x self.y = y self.z = z def abs(self): return math.sqrt(self.x*self.x + self.y*self.y + self.z*self.z) [*] Windows, they are the same length on Linux. :) -- http://mail.python.org/mailman/listinfo/python-list
Re: the annoying, verbose self
On Nov 27, 12:03 pm, Duncan Booth [EMAIL PROTECTED] wrote: Iain King [EMAIL PROTECTED] wrote: FTR, I won't be using this :) I do like this syntax though: class Vector: def __init__(self, x, y, z): self.x = x self.y = y self.z = z def abs(self): using self: return math.sqrt(.x*.x + .y*.y + .z*.z) It is a bit verbose though. This variant is shorter on my system[*]: class Vector: def __init__(self, x, y, z): self.x = x self.y = y self.z = z def abs(self): return math.sqrt(self.x*self.x + self.y*self.y + self.z*self.z) [*] Windows, they are the same length on Linux. :) Yeah, in this example. Another would be using NetworkConnection: .address = 127.0.0.1 .port = 8080 .connect() using .connection while .read(): do something .disconnect() I doubt anything like this will take though, since you can write similar code with 'with' and assigning a long name to a short one. Iain -- http://mail.python.org/mailman/listinfo/python-list
It works! Was: Installing Python 3000 on Leopard (Mac OS) fails...
On Nov 26, 9:59 pm, André [EMAIL PROTECTED] wrote: While I made some progress in trying to install Py3k from source (for the first time), it has failed... Here are the steps I went through (not necessarily in that order - except for those that matter). 1. After installing Leopard, install Xcode tools from the dvd - even if you had done so with a previous version (they need to be updated - trust me :-) 2. Download Python 3.0a1 3. Unpack the archive. 4. Go to /usr/local and make a directory sudo mkdir py3k (This is probably not needed, but that's what I did). 5. From the directory where the Python 3.0a1 was unpacked run ./configure --prefix=/usr/local/py3k 6. run make This last step failed with the following error message: gcc -fno-strict-aliasing -Wno-long-double -no-cpp-precomp -mno-fused- madd -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -I. -I./Include - DPy_BUILD_CORE -c ./Modules/posixmodule.c -o Modules/posixmodule.o ./Modules/posixmodule.c: In function 'posix_setpgrp': ./Modules/posixmodule.c:3769: error: too few arguments to function 'setpgrp' make: *** [Modules/posixmodule.o] Error 1 Any suggestions? André Following Martin v Löwis's suggestion, I looked at http://bugs.python.org/issue1358 and added the line #define SETPGRP_HAVE_ARG by hand to pyconfig.h (after it was created by configure). Then 6. run make 7. run make test (one test failed; this step likely unnecessary) 8. sudo make altinstall 9. sudo ln /usr/local/bin/py3k/python3.0 /usr/bin/python3.0 10. type python 11. print(Hello world!) 12. Be happy! André, hoping this report might help some other newbie. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to Teach Python Variables
On 2007-11-26, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Hrvoje Niksic wrote: greg [EMAIL PROTECTED] writes: none wrote: IIRC, I once saw an explanation how Python doesn't have variables in the sense that, say, C does, and instead has bindings from names to objects. IMHO, this is nonsense. All that variables are (in any language) are bindings for names. Pretending python does anything novel with regard to variables is confusing and, ultimately, simply results in a redefinition of terms programmers are already familiar with. Someone who says Python identifiers aren't variables, as in C, is thinking of C variables, which behave quite differently than Python identifiers. I mean, it's kind of like saying my computer is not a computer but is actually a device that follows input directions. Of course that description may be true (and I may even use it to explain to students *what* a computer is), but it is no less a computer for it. Your analogy is begging the question. Noone is claiming that a variable is not a variable. Python identifiers are variables in a broad sense. But they aren't like C variables. If you're talking to C programmers, just tell them that Python variables always contain pointers. That should give them the right mental model to build on. That is a convenient shortcut when it works, but in my experience it tends to confuse the issue. The reason is that one of the main uses of pointers in C is implementing pass-by-reference. A C programmer told that Python variables internally hold pointers expects this code: I think most C programmers are smart enough to figure out the supposed differences, with only a little additional explanation on the part of the instructor. Python's variable model is practically identical to that of Java, after all, and I don't recall any discussion of cataclysmic proportions over the differences between C pointers and Java references. There are differences (or more accurately points of emphasis), but of the sort that take a paragraph or two of explanation/clarification -- not a completely new model. There are more differences than similarities. Pointers are are a low-level mechanism suitable for many purposes, referencing values amongst them. Python identifiers are a high-level machanism, suitable for only one purpose. -- Neil Cerutti -- http://mail.python.org/mailman/listinfo/python-list
reading/writing files
Hi, I am trying to read a file and write into other file. if I do it for simple text file, it works well. but for pdfs or some other mime types, its failing. actually main problem is i am uploading file using cgi, in this process I am getting content of file, and I am trying to save the file. I think I need to specify mimetype of file somehow..? any quick solutions? or need to google. sandip -- http://mail.python.org/mailman/listinfo/python-list
Keyword passing to superclass written in C
Hi I am getting slightly frustrated and wonder if you can help me. Consider a Python class Foo implemented in C++. I have declared an initialization method like this static int Foo_init(P4Adapter *self, PyObject *args, PyObject *kwds) { } and I am subclassing Foo in Python, which works fine in principle, except for passing keywords. Here is the subclass: class Bar(Foo): def __init__(self, *args, **kwlist): Foo.__init__(self, args, kwlist) I have tested Foo and Bar as Python classes and passing the keywords is not a problem. If I create Foo in C++ and call it directly, I can pass the keywords no bother. But if I call Bar with keywords, it calls the C++ version of Foo with all arguments bunched together in args and not separated in args and keywords. What am doing wrong? This is Python 2.5.1 - on Ubuntu 7.0.4 if that makes any difference. Thanks Sven Erik This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. Please note that any views or opinions presented in this email are solely those of the author and do not necessarily represent those of Perforce Software. Finally, the recipient should check this email and any attachments for the presence of viruses. Perforce Software accepts no liability for any damage caused by any virus transmitted by this email. Perforce Software UK Ltd is registered in England and Wales as company no. 3816019 at the following address: West Forest Gate, Wellington Road, Wokingham, RG40 2AQ, UK -- http://mail.python.org/mailman/listinfo/python-list
Re: reading/writing files
f1= open(file1.pdf, rb) x = f1.read() open(file2.pdf, wb).write(x) works... thanks sandip On Nov 27, 5:43 pm, sandipm [EMAIL PROTECTED] wrote: Hi, I am trying to read a file and write into other file. if I do it for simple text file, it works well. but for pdfs or some other mime types, its failing. actually main problem is i am uploading file using cgi, in this process I am getting content of file, and I am trying to save the file. I think I need to specify mimetype of file somehow..? any quick solutions? or need to google. sandip -- http://mail.python.org/mailman/listinfo/python-list
Re: How can I create customized classes that have similar properties as 'str'?
On Nov 27, 10:45 am, Steven D'Aprano [EMAIL PROTECTED] wrote: On Sun, 25 Nov 2007 02:42:36 -0800, Licheng Fang wrote: I mentioned trigram counting as an illustrative case. In fact, you'll often need to define patterns more complex than that, and tens of megabytes of text may generate millions of them, and I've observed they quickly ate up the 8G memory of a workstation in a few minutes. Manipulating these patterns can be tricky, you can easily waste a lot of memory without taking extra care. I just thought if I define my pattern class with this 'atom' property, coding efforts could be easier later. I'm just not getting the same results as you when I try this. I'm finding that with no extra effort at all, it just works. The size of your corpus is not important. Neither is the complexity of how you generate the patterns. What's important is the number of patterns you produce, and millions isn't that huge a number, even without interning the strings. Obviously I'm not running your code, but I can build a dict with millions of patterns, from hundreds of megabytes of text, on a PC with just 1GB of memory and not run into any serious problems. I've just processed roughly 240MB of random emails, generating n-grams up to length 5. The emails include binary attachments and HTML etc., so I'm getting lots of patterns that don't normally exist in natural languages (e.g. 71 occurrences of 'qqq', and 5 of ''). As I said, my PC has only 1GB, and that's being shared with about a dozen other apps (including such memory hogs as Firefox). Results? 64939962 patterns found, of which 17031467 are unique. There's paging, yes, and my PC runs a little slow when I try to switch from one running application to another, but nothing unusable. Opening a dozen YouTube videos at once impacts performance worse. I can't think what you're doing to use up 8GB of RAM for merely millions of strings, even if you are keeping two, three, ten redundant copies. Assuming an average length of twenty bytes per pattern (you said trigrams, but I'd rather over-estimate than under), and even assuming that only half the 8GB are available to Python, you should be able to store something of the order of one hundred million patterns easily: My task is identifying sequences of tokens (phrases) that are possible tranlations of each other from a bilingual corpus. I need to check all the subsequences of a sentence to get the possible phrase pairs. This makes the problem different from n-gram counting in that the number of possible phrases doesn't grow linearly with n, but approximately with n^2. (n being the sentence length.) My first version of the program consumes almost twice as much memory as the current one because I discovered in doing different statistics I was regenerating the patterns, and the counting dictionaries ended up with duplicated pattern keys (a == b, yet a is not b). Wouldn't it be convenient if I can restrict the pattern class to not generate identical instances? So I can avoid such subtle but significant bugs. 4 bytes for a pointer plus 20 bytes for the string = 24 bytes 4*1024**3 / 24 = 178,956,970 (This is a ballpark figure. Real Python strings will have additional overhead.) If you're running into problems with merely millions of patterns, then you're doing worse by probably two orders of magnitude. I don't think that the problem lies where you think it does. If you have a dict with millions of keys, it doesn't matter how many times each pattern exists in the corpus because the key only exists *once* in the dict. Duplicate the dict, or generate it from scratch even, and at worse you double the memory used by the keys. And probably not even that. The only thing I can think of that might explain why you're using so much memory is if you are generating *all* the patterns up front, say in a list, before adding them to the dict: # Generate one massive list of patterns containing many duplicates patterns = make_patterns(text) # returns a massive list like ['fre', 'req', 'equ', 'que' ...] d = {} for pattern in patterns: d[pattern] = d.get(pattern, 0) + 1 No, I wasn't doing that. BTW, do you think the pattern counting code can avoid hashing the pattern twice? Is there a way to do that when the dictionary values are of a primitive type? Notice that the real killer in the above algorithm is that you need enough storage, not just for the unique patterns, but for EVERY separate occurrence of each pattern. Nothing to do with how dicts operate, and everything to do with the algorithm you (hypothetically) are using. If that's what you're doing, then no wonder you're running out of memory. With 200MB of text, you have 209715198 trigrams in your list. The pointers alone will take almost a gigabyte, assuming 32-bit pointers. If this is your approach, interning the strings won't save you. You almost certainly should change to a lazy approach, and use a
Different kinds of Import Errors
If you look at this code, you see there are two kind of ImportErrors: 1. app_name has no attribute or file managment.py: That's OK. 2. managment.py exists, but raises an ImportError: That's not OK: reraise # Import the 'management' module within each installed app, to register # dispatcher events. for app_name in settings.INSTALLED_APPS: try: __import__(app_name + '.management', {}, {}, ['']) except ImportError, exc: if exc.args[0]!='No module named management': raise I am searching a better solution, since in a future version of python the string 'No module namend management' might be changed. Any better solution? -- http://mail.python.org/mailman/listinfo/python-list
Re: Best ways of managing text encodings in source/regexes?
On Tue, Nov 27, 2007 at 01:30:15AM +0200, tinker barbet wrote regarding Re: Best ways of managing text encodings in source/regexes?: Hi Thanks for your responses, as I said on the reply I posted I thought later it was a bit long so I'm grateful you held out! I should have said (but see comment about length) that I'd tried joining a unicode and a byte string in the interpreter and got that working, but wondered whether it was safe (I'd seen the odd warning about mixing byte strings and unicode). Anyway what I was concerned about was what python does with source files rather than what happens from the interpreter, since I don't know if it's possible to change the encoding of the terminal without messing with site.py (and what else). Aren't both ASCII and ISO-8859-1 subsets of UTF-8? Can't you then use chars from either of those charsets in a file saved as UTF-8 by one's editor, with a # -*- coding: utf-8 -*- pseudo-declaration for python, without problems? You seem to disagree. I do disagree. Unicode is a superset of ISO-8859-1, but UTF-8 is a specific encoding, which changes many of the binary values. UTF-8 was designed specifically not to change the values of ascii characters. 0x61 (lower case a) in ascii is encoded with the bits 0110 0001. In UTF-8 it is also encoded 0110 0001. However, ?, latin small letter n with tilde, is unicode/iso-8859-1 character 0xf1. In ISO-8859-1, this is represented by the bits 0001. UTF-8 gets a little tricky here. In order to be extensible beyond 8 bits, it has to insert control bits at the beginning, so this character actually requires 2 bytes to represent instead of just one. In order to show that UTF-8 will be using two bytes to represent the character, the first byte begins with 110 (1110 is used when three bytes are needed). Each successive byte begins with 10 to show that it is not the beginning of a character. Then the code-point value is packed into the remaining free bits, as far to the right as possible. So in this case, the control bits are 110x 10xx . The character value, 0xf1, or: 0001 gets inserted as follows: 110x xx{11} 10{11 0001} and the remaining free x-es get replaced by zeroes. 1100 0011 1011 0001. Note that the python interpreter agrees: py x = u'\u00f1' py x.encode('utf-8') '\xc3\xb1' (Conversion from binary to hex is left as an exercise for the reader) So while ASCII is a subset of UTF-8, ISO-8859-1 is definitely not. As others have said many times when this issue periodically comes up: UTF-8 is not unicode. Hopefully this will help explain exactly why. Note that with other encodings, like UTF-16, even ascii is not a subset. See the wikipedia article on UTF-8 for a more complete explanation and external references to official documentation (http://en.wikipedia.org/wiki/UTF-8). The reason all this arose was that I was using ISO-8859-1/Latin-1 with all the right declarations, but then I needed to match a few chars outside of that range. So I didn't need to use u before, but now I do in some regexes, and I was wondering if this meant that /all/ my regexes had to be constructed from u strings or whether I could just do the selected ones, either using literals (and saving the file as UTF-8) or unicode escape sequences (and leaving the file as ASCII -- I already use hex escape sequences without problems but that doesn't work past the end of ISO-8859-1). Do you know about unicode escape sequences? py u'\xf1' == u'\u00f1' True Thanks again for your feedback. Best wishes Tim No problem. It took me a while to wrap my head around it, too. Cheers, Cliff -- http://mail.python.org/mailman/listinfo/python-list
Re: Different kinds of Import Errors
On Nov 27, 7:35 am, Thomas Guettler [EMAIL PROTECTED] wrote: If you look at this code, you see there are two kind of ImportErrors: 1. app_name has no attribute or file managment.py: That's OK. 2. managment.py exists, but raises an ImportError: That's not OK: reraise # Import the 'management' module within each installed app, to register # dispatcher events. for app_name in settings.INSTALLED_APPS: try: __import__(app_name + '.management', {}, {}, ['']) except ImportError, exc: if exc.args[0]!='No module named management': raise I am searching a better solution, since in a future version of python the string 'No module namend management' might be changed. Any better solution? I would assume that in the future versions of Python, it would still mention the word management. In that case, you could do something like this in the except clause: # untested args = exc.args[0] if args.find('management') != -1: raise Mike -- http://mail.python.org/mailman/listinfo/python-list
Re: Best ways of managing text encodings in source/regexes?
On Nov 26, 2007 4:27 PM, Martin v. Löwis [EMAIL PROTECTED] wrote: myASCIIRegex = re.compile('[A-Z]') myUniRegex = re.compile(u'\u2013') # en-dash then read the source file into a unicode string with codecs.read(), then expect re to match against the unicode string using either of those regexes if the string contains the relevant chars? Or do I need to do make all my regex patterns unicode strings, with u? It will work fine if the regular expression restricts itself to ASCII, and doesn't rely on any of the locale-specific character classes (such as \w). If it's beyond ASCII, or does use such escapes, you better make it a Unicode expression. yes, you have to be careful when writing unicode-senstive regular expressions: http://effbot.org/zone/unicode-objects.htm You can apply the same pattern to either 8-bit (encoded) or Unicode strings. To create a regular expression pattern that uses Unicode character classes for \w (and \s, and \b), use the (?u) flag prefix, or the re.UNICODE flag: pattern = re.compile((?u)pattern) pattern = re.compile(pattern, re.UNICODE) I'm not actually sure what precisely the semantics is when you match an expression compiled from a byte string against a Unicode string, or vice versa. I believe it operates on the internal representation, so \xf6 in a byte string expression matches with \u00f6 in a Unicode string; it won't try to convert one into the other. Regards, Martin -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
you want?
Dear Friends The Shoplifes.com belongs to Shoplife Limited Company who mainly sell personal stylish electronic consumable products such as Mobile phones, Laptops, Digital Cameras, Digital Videos,Mp3,Mp4 and bulk products such as LCD TV, Motorcycles and so on. The specific item please visit our company website www.shoplifes.com. As a larger wholesaler we pursue a goal which offer our customer competitive price, highly efficient delivery and perfect customer service. In China we have own warehouse and stores, and our clients all over the world. The best foundation and long-term business relation lead to our company gain highly reputation. We expect to please our honest customer through our product and service. If you have any comments about our products or service please contact us we are appreciated and will improve our service. Hope we can have cooperate with great pleasure and establish long-term business relationship. Website: www.shoplifes.com E-mail: [EMAIL PROTECTED] MSN:[EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: the annoying, verbose self
On 2007-11-26, Bruno Desthuilliers [EMAIL PROTECTED] wrote: Patrick Mullen a écrit : (snip) Still an unnecessary lookup on tmp though :) And it would be useless to use it for one assignment, the idea is to eliminate all the typing with this: self.var1 = 5 self.var2 = a value self.var3 = stuff self.var4 = [2,54,7,7] self.var5 = dingaling self.var6 = 6.4 self.var7 = 1 self.var8 = False self.var9 = True self.__dict__.update(dict(var1=5, var2=a value, var3 = stuff, etc)) Someone else ? Or are we done with this DeadHorse ? I was really upset about having to type 'self' all the time, until I learned that in ABC, one of Python's precursors, you had to use '__anInstanceOf_This_TYPE_or_Maybe_A__SubClassOfItInstead_arg0_'. -- Neil Cerutti Sermon Outline: I. Delineate your fear II. Disown your fear III. Displace your rear --Church Bulletin Blooper -- http://mail.python.org/mailman/listinfo/python-list
Re: How to Teach Python Variables
On Nov 25, 5:31 pm, none atavory\@(none) wrote: Aurélien Campéas wrote: none a écrit : Hello, IIRC, I once saw an explanation how Python doesn't have variables in the sense that, say, C does, and instead has bindings from names to objects. Does anyone have a link? Thanks, Ami That's something I've often heard and I don't get it. Somehow I don't understand how C variables are not like python bindings (the differences being that C variables are statically typed and completely disappear at run-time; are these differences important enough to warrant such a shift in terminology ? (yes there are some other differences, but then the question is asked in a context of pedagogy, where the audience is introduced to the basics)) I mean : aren't C variables also bindings from names to objects ? Or what ? Thanks. It's very possible you're right - I don't know. There seem to be some differences however. To name a few: 1. A C variable exists regardless of whether you're storing something in it. Not so for a python variable - so it's a bit really more like a name for something that exists independently. 2. C variables (or C++ objects) completely disappear when out of scope, but that's not necessarily true of Python objects. 3. C++ references have the semantics that if a = b, and you write a.c = 3, then b.c == 3. This is also true in Python. But then if a = b, and then you write b = 5, then a is still bound to the original value of b, so it's not exactly like a reference. 4. Etc. So on the one hand, you're obviously right, and maybe there's no room for a paradigm shift. OTOH, if the simplest explanation is it's like a C/C++ variable/reference/pointer except for 1, 2, 3, 4,..., then maybe it is different. I just don't know, hence my question. (If I say something wrong, please raise your hand) For me, python variables are _exactly_ the same as reference counted (or garbage collected) C++ pointers. It's just that the assignment operator in python has distinctive semantics. for example: the following code in python a = 3 print a + 2 print len(a) b = a print id(a), id(b) print a + b a = 5 is similar to the following pseudo-code in C++ object *a, *b; // a = 3 a = refnewInt(3); // print a + 2 print(a-add(*refnewInt(2))); // print len(a) cout len(*a) '\n'; // b = a b = refnewobject(a); // print id(a), id(b) print(*refnewList(a, b)) // print a + b print(a-add(*b)); // a = 5 refdel(a); a = refnewInt(5); So, we can see that, in python: - attributions operate in the pointer - method calls (that is, everything else, except id()) operate in the object And that's it. I think that there is confusion because everything we do with python variables are pointer dereferences, except for the attribution, that instead of dereferencing and operating the object, just changes the pointer. The scope is just a matter of using automatic memory management or not. -- http://mail.python.org/mailman/listinfo/python-list
read/write to java socket in python
Hi all, I have a problem with reading from a Java server after I have written to it - it just hangs. It works fine if I just write to the server and not try to write. I have read the HOWTO on sockets - and it states that there is a problem (something about flushing), but not what the solutions is. Nor do google. Can somebody please help? A few lines down you can see the example code that sums up the problem. Just change the name of the Python HOST-variable. Thanks Mads This is the client in Python: #! /usr/bin/env python import sys from socket import * PORT = 3122 HOST = 'app-5' SUCCESS = 'Success' FAILURE = 'Failure' s = socket(AF_INET, SOCK_STREAM) s.connect((HOST, PORT)) s.send(Hi Java Server); print Have written, waiting to recieve.. print s.recv(1014) s.close() And this the server in Java: import java.io.*; import java.net.*; public class Server{ public static void main(String args[]){ int port = 3122; int backLog = 50; ServerSocket ss = null; try{ InetAddress localhost = InetAddress.getLocalHost(); ss = new ServerSocket(port, backLog, localhost); while(true){ final Socket client = ss.accept(); new Thread(){ public void run(){ try{ InputStream is = client.getInputStream(); BufferedReader buf = new BufferedReader(new InputStreamReader(is)); print(buf.readLine()); PrintWriter out = new PrintWriter(client.getOutputStream()); out.write(Hi Python Client.); out.flush(); client.close(); }catch(Exception e) {print(e);} } }.start(); } }catch(Exception e){print(e);} } private static void print(Object o){System.out.println(o);} } -- http://mail.python.org/mailman/listinfo/python-list
Re: reading/writing files
On Nov 27, 7:14 am, sandipm [EMAIL PROTECTED] wrote: f1= open(file1.pdf, rb) x = f1.read() open(file2.pdf, wb).write(x) works... thanks sandip You might also like: http://pybrary.net/pyPdf/ -- http://mail.python.org/mailman/listinfo/python-list
Re: read/write to java socket in python
On Tue, 27 Nov 2007 07:08:04 -0800 (PST), [EMAIL PROTECTED] wrote: Hi all, I have a problem with reading from a Java server after I have written to it - it just hangs. It works fine if I just write to the server and not try to write. I have read the HOWTO on sockets - and it states that there is a problem (something about flushing), but not what the solutions is. Nor do google. Can somebody please help? A few lines down you can see the example code that sums up the problem. Just change the name of the Python HOST-variable. Thanks Mads [snip] s.send(Hi Java Server); Here, you sent some bytes (maybe - you didn't check the return value of send, so it's possible nothing at all was sent) but you didn't send a new line indicator of any sort. [snip] print(buf.readLine()); Here, you tried you read a line from the socket. The peer never sent a whole line though, so readLine will never succeed. Jean-Paul -- http://mail.python.org/mailman/listinfo/python-list
Re: read/write to java socket in python
On Nov 27, 1:08 pm, [EMAIL PROTECTED] wrote: Hi all, I have a problem with reading from a Java server after I have written to it - it just hangs. It works fine if I just write to the server and not try to write. I have read the HOWTO on sockets - and it states that there is a problem (something about flushing), but not what the solutions is. Nor do google. Can somebody please help? A few lines down you can see the example code that sums up the problem. Just change the name of the Python HOST-variable. Thanks Mads This is the client in Python: #! /usr/bin/env python import sys from socket import * PORT = 3122 HOST = 'app-5' SUCCESS = 'Success' FAILURE = 'Failure' s = socket(AF_INET, SOCK_STREAM) s.connect((HOST, PORT)) s.send(Hi Java Server); print Have written, waiting to recieve.. print s.recv(1014) s.close() And this the server in Java: import java.io.*; import java.net.*; public class Server{ public static void main(String args[]){ int port = 3122; int backLog = 50; ServerSocket ss = null; try{ InetAddress localhost = InetAddress.getLocalHost(); ss = new ServerSocket(port, backLog, localhost); while(true){ final Socket client = ss.accept(); new Thread(){ public void run(){ try{ InputStream is = client.getInputStream(); BufferedReader buf = new BufferedReader(new InputStreamReader(is)); print(buf.readLine()); PrintWriter out = new PrintWriter(client.getOutputStream()); out.write(Hi Python Client.); out.flush(); client.close(); }catch(Exception e) {print(e);} } }.start(); } }catch(Exception e){print(e);} } private static void print(Object o){System.out.println(o);} } I don't know, but it's amazing to compare the python client with the java server. -- http://mail.python.org/mailman/listinfo/python-list
A bug in Python's regular expression engine?
This won't compile for me: regex = re.compile('(.*\\).*') I get the error: sre_constants.error: unbalanced parenthesis I'm running Python 2.5 on WinXP. I've tried this expression with another RE engine in another language and it works just fine which leads me to believe the problem is Python. Can anyone confirm or deny this bug? Thank you... -- http://mail.python.org/mailman/listinfo/python-list
Re: Why are class methods not classmethods?
On Nov 27, 2007 3:12 AM, Steven D'Aprano [EMAIL PROTECTED] wrote: There's some subtle behaviour going on here that I don't really follow. Class methods apparently aren't classmethods. class Parrot(object): ... def method(self, *args): ... return self, args ... @classmethod ... def cmethod(cls, *args): ... return cls, args ... type(parrot.method) # as expected type 'instancemethod' type(parrot.cmethod) # I don't expect this result type 'instancemethod' type(classmethod(parrot.method)) type 'classmethod' parrot.cm = classmethod(parrot.method) type(parrot.cm) # I expect this type 'classmethod' Parrot.CM = classmethod(parrot.method) type(Parrot.CM) # but not this type 'instancemethod' Can anyone explain why class methods bound to a class are instancemethods rather than classmethods? They're instancemethods bound to the type instance, rather than to an instance of the type: c = C() c.method bound method C.method of __main__.C object at 0x01B8F330 c.cmethod bound method type.cmethod of class '__main__.C' So rather than inventing special machinery for classmethods, Python uses the existing instancemethod machinery and just changes the object the instancemethod is bound to. -- http://mail.python.org/mailman/listinfo/python-list
Re: A bug in Python's regular expression engine?
Just Another Victim of the Ambient Morality wrote: This won't compile for me: regex = re.compile('(.*\\).*') I get the error: sre_constants.error: unbalanced parenthesis I'm running Python 2.5 on WinXP. I've tried this expression with another RE engine in another language and it works just fine which leads me to believe the problem is Python. Can anyone confirm or deny this bug? It pretty much says what the problem is - you escaped the closing parenthesis, resulting in an invalid rex. Either use raw-strings or put the proper amount of backslashes in your string: regex = re.compile(r'(.*\\).*') # raw string literal regex = re.compile('(.*).*') # two consecutive \es, meaning an escaped one Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Keyword passing to superclass written in C
On Nov 27, 2007 6:47 AM, Sven Erik Knop [EMAIL PROTECTED] wrote: Hi I am getting slightly frustrated and wonder if you can help me. Consider a Python class Foo implemented in C++. I have declared an initialization method like this static int Foo_init(P4Adapter *self, PyObject *args, PyObject *kwds) { } and I am subclassing Foo in Python, which works fine in principle, except for passing keywords. Here is the subclass: class Bar(Foo): def __init__(self, *args, **kwlist): Foo.__init__(self, args, kwlist) This passes the args tuple and the kw dict as 2 regular arguments. You need to expand them in the second call if you want them to be passed as args and kwargs - Foo.__init__(self, *args, **kwargs) -- http://mail.python.org/mailman/listinfo/python-list
Re: A bug in Python's regular expression engine?
On 2007-11-27, Just Another Victim of the Ambient Morality [EMAIL PROTECTED] wrote: This won't compile for me: regex = re.compile('(.*\\).*') I get the error: sre_constants.error: unbalanced parenthesis Hint 1: Always assume that errors are in your own code. Blaming library code and language implementations will get you nowhere most of the time. Hint 2: regular expressions and Python strings use the same escape character. Hint 3: Consult the Python documentation about raw strings, and what they are meant for. -- Neil Cerutti -- http://mail.python.org/mailman/listinfo/python-list
Re: A bug in Python's regular expression engine?
On Nov 27, 3:48 pm, Just Another Victim of the Ambient Morality [EMAIL PROTECTED] wrote: This won't compile for me: regex = re.compile('(.*\\).*') I get the error: sre_constants.error: unbalanced parenthesis I'm running Python 2.5 on WinXP. I've tried this expression with another RE engine in another language and it works just fine which leads me to believe the problem is Python. Can anyone confirm or deny this bug? Your code is equivalent to: regex = re.compile(r'(.*\).*') Written like this, it's easier to see that you've started a regular expression group with '(', but it's never closed since your closed parenthesis is escaped (which causes it to match a literal ')' when used). Hence the reported error (which isn't a bug). Perhaps you meant this? regex = re.compile(r'(.*\\).*') This matches any number of characters followed by a backslash (group 1), and then any number of characters. If you're using this for path splitting filenames under Windows, you should look at os.path.split instead of writing your own. HTH -- Paul Hankin -- http://mail.python.org/mailman/listinfo/python-list
Re: A bug in Python's regular expression engine?
Paul Hankin [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] On Nov 27, 3:48 pm, Just Another Victim of the Ambient Morality [EMAIL PROTECTED] wrote: This won't compile for me: regex = re.compile('(.*\\).*') I get the error: sre_constants.error: unbalanced parenthesis I'm running Python 2.5 on WinXP. I've tried this expression with another RE engine in another language and it works just fine which leads me to believe the problem is Python. Can anyone confirm or deny this bug? Your code is equivalent to: regex = re.compile(r'(.*\).*') Written like this, it's easier to see that you've started a regular expression group with '(', but it's never closed since your closed parenthesis is escaped (which causes it to match a literal ')' when used). Hence the reported error (which isn't a bug). Perhaps you meant this? regex = re.compile(r'(.*\\).*') This matches any number of characters followed by a backslash (group 1), and then any number of characters. If you're using this for path splitting filenames under Windows, you should look at os.path.split instead of writing your own. Indeed, I did end up using os.path functions, instead. I think I see what's going on. Backslash has special meaning in both the regular expression and Python string declarations. So, my version should have been something like this: regex = re.compile('(.*).*') That is funny. Thank you for your help... Just for clarification, what does the r in your code do? -- http://mail.python.org/mailman/listinfo/python-list
Re: It works! Was: Installing Python 3000
On Tuesday 27 November 2007 07:20, André wrote: On Nov 26, 9:59 pm, André [EMAIL PROTECTED] wrote: While I made some progress in trying to install Py3k from source (for the first time), it has failed... Here are the steps I went through (not necessarily in that order - except for those that matter). 1. After installing Leopard, install Xcode tools from the dvd - even if you had done so with a previous version (they need to be updated - trust me :-) 2. Download Python 3.0a1 3. Unpack the archive. 4. Go to /usr/local and make a directory sudo mkdir py3k (This is probably not needed, but that's what I did). 5. From the directory where the Python 3.0a1 was unpacked run ./configure --prefix=/usr/local/py3k 6. run make This last step failed with the following error message: gcc -fno-strict-aliasing -Wno-long-double -no-cpp-precomp -mno-fused- madd -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -I. -I./Include - DPy_BUILD_CORE -c ./Modules/posixmodule.c -o Modules/posixmodule.o ./Modules/posixmodule.c: In function 'posix_setpgrp': ./Modules/posixmodule.c:3769: error: too few arguments to function 'setpgrp' make: *** [Modules/posixmodule.o] Error 1 Any suggestions? André Following Martin v Löwis's suggestion, I looked at http://bugs.python.org/issue1358 and added the line #define SETPGRP_HAVE_ARG by hand to pyconfig.h (after it was created by configure). Then 6. run make 7. run make test (one test failed; this step likely unnecessary) 8. sudo make altinstall 9. sudo ln /usr/local/bin/py3k/python3.0 /usr/bin/python3.0 10. type python 11. print(Hello world!) 12. Be happy! André, hoping this report might help some other newbie. Bug fix excluded, After unpacking the compressed version of Python, look for a file named README. Open README and look for Installing. Make install and Make altinstall is explained. I don't like to read instructions but in the long run, it saves time. jim-on-linux http://www.inqvista.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Should proxy objects lie about their class name?
On Nov 26, 11:56 pm, Carl Banks [EMAIL PROTECTED] wrote: On Nov 20, 3:50 pm, [EMAIL PROTECTED] (John J. Lee) wrote: Not much to add to the subject line. I mean something like this: ProxyClass.__name__ = ProxiedClass.__name__ I've been told that this is common practice. Is it? Would this surprise you if you ran into it in a debugging session? One very real advantage that I can see is avoiding breaking existing doctests. Thanks in advance for your views Python 3.0 has a proposal, accepted I believe, to allow classes to control the behavior of issubclass and ininstance, so there appears to be tacit support from the language for mimicking the proxied classes in such ways. I guess for me it would be a judgment call on based how tight I wanted the proxy class to be--is it being the class, or is it just standing in? In Python 2 you can already lie to 'isinstance' and 'issubclass' by catching calls to the '__class__' and '__bases__' attribute. I'm not sure yet whether this is a good thing however. :-) I have a proxy class I want to extend and am facing similar questions. Michael http://www.manning.com/foord Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: How to Teach Python Variables
On Nov 27, 10:06 am, hdante [EMAIL PROTECTED] wrote: On Nov 25, 5:31 pm, none atavory\@(none) wrote: And that's it. I think that there is confusion because everything we do with python variables are pointer dereferences, except for the attribution, that instead of dereferencing and operating the object, just changes the pointer. The scope is just a matter of using automatic memory management or not. I hope the participants in this thread realize that this sort of discussion will cause any programming newbie to immediately melt into the floor. I would try to avoid talking in generalities about python variables versus C or lisp or whatever, unless I was teaching an upper division college programming languages survey class. Instead, I'd fire up the interactive interpreter and illustrate how things work via examples, avoiding the weird cases at all costs. If they ask about the relationship to C or java or whatever I would encourage them to not worry about it, and only go deeper if pressed. BTW, at the moment I'm facilitating an online C# class for fun and a very little profit in my spare time. I'm becoming quite convinced that especially the beginning programmers would have far less difficulty with Python, largely because there is less syntactic noise to confuse them and because they can fiddle around with everything up to and including advanced concepts using simple examples at the interactive prompt. But if you talk like the above to them, they will run for the hills... (no offense intended). -- Aaron Watters === http://www.xfeedme.com/nucular/pydistro.py/go?FREETEXT=evil+fish -- http://mail.python.org/mailman/listinfo/python-list
count pages in a pdf
is it possible to parse a pdf file in python? for starters, i would like to count the number of pages in a pdf file. i see there is a project called ReportLab, but it seems to be a pdf generator... i can't tell if i would be able to parse a pdf file programmically. thanks for any recommendations. bryan -- http://mail.python.org/mailman/listinfo/python-list
Re: How to Teach Python Variables
On Nov 27, 2007 10:25 AM, Aaron Watters [EMAIL PROTECTED] wrote: On Nov 27, 10:06 am, hdante [EMAIL PROTECTED] wrote: On Nov 25, 5:31 pm, none atavory\@(none) wrote: And that's it. I think that there is confusion because everything we do with python variables are pointer dereferences, except for the attribution, that instead of dereferencing and operating the object, just changes the pointer. The scope is just a matter of using automatic memory management or not. I hope the participants in this thread realize that this sort of discussion will cause any programming newbie to immediately melt into the floor. Well, this (part of) the conversation was specifically about using the knowledge that a C programmer already has and how best to teach them how Python variables work in that context. I think that even the people who disagree with my position would agree that, if your goal were to teach a new student about Python variables, teaching them C pointers first would be a really terrible way to go about it. I would try to avoid talking in generalities about python variables versus C or lisp or whatever, unless I was teaching an upper division college programming languages survey class. I disagree, although it's not really on topic for the thread. There's no reason why low level details need to be relegated to upper division college classes. It shouldn't be in Programming Languages 101, but it's something that I would expect a first year student who's planning to pursue a programming career or a comp sci degree to be exposed to. It's not out of place even in high school - that's where I first learned C. Instead, I'd fire up the interactive interpreter and illustrate how things work via examples, avoiding the weird cases at all costs. If they ask about the relationship to C or java or whatever I would encourage them to not worry about it, and only go deeper if pressed. BTW, at the moment I'm facilitating an online C# class for fun and a very little profit in my spare time. I'm becoming quite convinced that especially the beginning programmers would have far less difficulty with Python, largely because there is less syntactic noise to confuse them and because they can fiddle around with everything up to and including advanced concepts using simple examples at the interactive prompt. The idea of boxes with names is generally far more intuitive to absolute beginners than pointers, if only because you have to understand more of the underlying implementation to make use of pointers. There's other (very good) reasons to use Python as a first language for beginners, too. But if you talk like the above to them, they will run for the hills... (no offense intended). -- Aaron Watters -- http://mail.python.org/mailman/listinfo/python-list
Re: Should proxy objects lie about their class name?
Fuzzyman wrote: On Nov 26, 11:56 pm, Carl Banks [EMAIL PROTECTED] wrote: On Nov 20, 3:50 pm, [EMAIL PROTECTED] (John J. Lee) wrote: Not much to add to the subject line. I mean something like this: ProxyClass.__name__ = ProxiedClass.__name__ I've been told that this is common practice. Is it? Would this surprise you if you ran into it in a debugging session? One very real advantage that I can see is avoiding breaking existing doctests. Python 3.0 has a proposal, accepted I believe, to allow classes to control the behavior of issubclass and ininstance, so there appears to be tacit support from the language for mimicking the proxied classes in such ways. In Python 2 you can already lie to 'isinstance' and 'issubclass' by catching calls to the '__class__' and '__bases__' attribute. I'm not sure yet whether this is a good thing however. :-) The Python 3 machinery allows *other* classes to lie about whether or not your object is an instance or subclass of them, without requiring them to set your __class__ or __bases__. So, for example, you can create a class ``Integer`` and make ``issubclass(int, Integer)`` true. For more information see: http://www.python.org/dev/peps/pep-3119/ STeVe -- http://mail.python.org/mailman/listinfo/python-list
Re: A bug in Python's regular expression engine?
On Nov 27, 10:52 am, MonkeeSage [EMAIL PROTECTED] wrote: On Nov 27, 10:19 am, Just Another Victim of the Ambient Morality [EMAIL PROTECTED] wrote: That is funny. Thank you for your help... Just for clarification, what does the r in your code do? It means a raw string (as you know ruby, think of it like %w{}): This page explains about string literal prefixes (see especially the end-notes): http://docs.python.org/ref/strings.html HTH, Jordan Arg! %w{} should have said %q{} -- http://mail.python.org/mailman/listinfo/python-list
Re: Keyword passing to superclass written in C
Duh of course. Not sure what I had done in Python/Python. Now it works. Thanks a lot. Sven Erik Chris Mellon wrote: On Nov 27, 2007 6:47 AM, Sven Erik Knop [EMAIL PROTECTED] wrote: Hi I am getting slightly frustrated and wonder if you can help me. Consider a Python class Foo implemented in C++. I have declared an initialization method like this static int Foo_init(P4Adapter *self, PyObject *args, PyObject *kwds) { } and I am subclassing Foo in Python, which works fine in principle, except for passing keywords. Here is the subclass: class Bar(Foo): def __init__(self, *args, **kwlist): Foo.__init__(self, args, kwlist) This passes the args tuple and the kw dict as 2 regular arguments. You need to expand them in the second call if you want them to be passed as args and kwargs - Foo.__init__(self, *args, **kwargs) This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. Please note that any views or opinions presented in this email are solely those of the author and do not necessarily represent those of Perforce Software. Finally, the recipient should check this email and any attachments for the presence of viruses. Perforce Software accepts no liability for any damage caused by any virus transmitted by this email. Perforce Software UK Ltd is registered in England and Wales as company no. 3816019 at the following address: West Forest Gate, Wellington Road, Wokingham, RG40 2AQ, UK -- http://mail.python.org/mailman/listinfo/python-list
Re: Should proxy objects lie about their class name?
Steven Bethard wrote: ... The Python 3 machinery allows *other* classes to lie about whether or not your object is an instance or subclass of them, without requiring them to set your __class__ or __bases__. So, for example, you can create a class ``Integer`` and make ``issubclass(int, Integer)`` true. For more information see: http://www.python.org/dev/peps/pep-3119/ STeVe That will allow me to magically create instances which claim to be instances of working_class even though they're actually instances of reactionary_class thus turning all my modules into instances of class_war :) -- Robin Becker -- http://mail.python.org/mailman/listinfo/python-list
Re: How can I create customized classes that have similar properties as 'str'?
On Nov 27, 2007 7:16 AM, Licheng Fang [EMAIL PROTECTED] wrote: On Nov 27, 10:45 am, Steven D'Aprano [EMAIL PROTECTED] wrote: On Sun, 25 Nov 2007 02:42:36 -0800, Licheng Fang wrote: I mentioned trigram counting as an illustrative case. In fact, you'll often need to define patterns more complex than that, and tens of megabytes of text may generate millions of them, and I've observed they quickly ate up the 8G memory of a workstation in a few minutes. Manipulating these patterns can be tricky, you can easily waste a lot of memory without taking extra care. I just thought if I define my pattern class with this 'atom' property, coding efforts could be easier later. I'm just not getting the same results as you when I try this. I'm finding that with no extra effort at all, it just works. The size of your corpus is not important. Neither is the complexity of how you generate the patterns. What's important is the number of patterns you produce, and millions isn't that huge a number, even without interning the strings. Obviously I'm not running your code, but I can build a dict with millions of patterns, from hundreds of megabytes of text, on a PC with just 1GB of memory and not run into any serious problems. I've just processed roughly 240MB of random emails, generating n-grams up to length 5. The emails include binary attachments and HTML etc., so I'm getting lots of patterns that don't normally exist in natural languages (e.g. 71 occurrences of 'qqq', and 5 of ''). As I said, my PC has only 1GB, and that's being shared with about a dozen other apps (including such memory hogs as Firefox). Results? 64939962 patterns found, of which 17031467 are unique. There's paging, yes, and my PC runs a little slow when I try to switch from one running application to another, but nothing unusable. Opening a dozen YouTube videos at once impacts performance worse. I can't think what you're doing to use up 8GB of RAM for merely millions of strings, even if you are keeping two, three, ten redundant copies. Assuming an average length of twenty bytes per pattern (you said trigrams, but I'd rather over-estimate than under), and even assuming that only half the 8GB are available to Python, you should be able to store something of the order of one hundred million patterns easily: My task is identifying sequences of tokens (phrases) that are possible tranlations of each other from a bilingual corpus. I need to check all the subsequences of a sentence to get the possible phrase pairs. This makes the problem different from n-gram counting in that the number of possible phrases doesn't grow linearly with n, but approximately with n^2. (n being the sentence length.) My first version of the program consumes almost twice as much memory as the current one because I discovered in doing different statistics I was regenerating the patterns, and the counting dictionaries ended up with duplicated pattern keys (a == b, yet a is not b). Wouldn't it be convenient if I can restrict the pattern class to not generate identical instances? So I can avoid such subtle but significant bugs. Implement __hash__ and __eq__ on your pattern class. If the same pattern compares equal and hashes the same then it will be a matching key as far as the dict is concerned and will only be stored once. This is probably cheaper than explicit interning anyway (you don't need to search an intern table). The only thing I can think of that might explain why you're using so much memory is if you are generating *all* the patterns up front, say in a list, before adding them to the dict: # Generate one massive list of patterns containing many duplicates patterns = make_patterns(text) # returns a massive list like ['fre', 'req', 'equ', 'que' ...] d = {} for pattern in patterns: d[pattern] = d.get(pattern, 0) + 1 No, I wasn't doing that. BTW, do you think the pattern counting code can avoid hashing the pattern twice? Is there a way to do that when the dictionary values are of a primitive type? Hashing isn't really an expensive operation. On strings it's even cached on the object. If you implement your own __hash__ method you can do the same, but I wouldn't bother unless you benchmark it and discover that hashing is a bottleneck. -- http://mail.python.org/mailman/listinfo/python-list
os.path.islink documentation error?
os.path.islink documentation says: Return True if path refers to a directory entry that is a symbolic link. Always False if symbolic links are not supported. It's not clear to me why it is mentioned the DIRECTORY term. Shouldn't os.path.islink be used to just check if the *path* passed as argument is a symlink? In such case I would change it in: Return True if path refers to a symbolic link -- http://mail.python.org/mailman/listinfo/python-list
Re: count pages in a pdf
Tim Golden wrote: [EMAIL PROTECTED] wrote: is it possible to parse a pdf file in python? for starters, i would like to count the number of pages in a pdf file. i see there is a project called ReportLab, but it seems to be a pdf generator... i can't tell if i would be able to parse a pdf file programmically. http://groups.google.de/group/comp.lang.python/msg/6f304970b4ff40ce and following. Well the simple expedient of putting python count pages pdf into Google turned up the following link: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496837 h. There is a non-vanishing possibility that this pattern- matching can give you false positives - not reliable. Wishing a happy day, LOBI -- http://mail.python.org/mailman/listinfo/python-list
Re: count pages in a pdf
On 2007-11-27, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: is it possible to parse a pdf file in python? for starters, i would like to count the number of pages in a pdf file. i see there is a project called ReportLab, but it seems to be a pdf generator... i can't tell if i would be able to parse a pdf file programmically. http://pybrary.net/pyPdf/ -- http://mail.python.org/mailman/listinfo/python-list
Unsupported operator for Decimal: + (or -)
This seems to have come up earlier... http://mail.python.org/pipermail/python-list/2007-July/451187.html but no resolution. We're seeing the same thing. We're using Django's DecimalField type and when we try to add or subtract values--which should be decimal.Decimal objects--we occasionally get an error about the operator not being supported. It doesn't always happen and we can't seem to reproduce it when we try to. Has anybody else seen this or is it just the original poster and me? Todd -- http://mail.python.org/mailman/listinfo/python-list
Looking for a Python tutor
I'm looking for a Python programmar that is willing write simple functions, prices can be talked over. Contact me asap. -- http://mail.python.org/mailman/listinfo/python-list
Re: os.path.islink documentation error?
Giampaolo Rodola' schrieb: os.path.islink documentation says: Return True if path refers to a directory entry that is a symbolic link. Always False if symbolic links are not supported. It's not clear to me why it is mentioned the DIRECTORY term. Shouldn't os.path.islink be used to just check if the *path* passed as argument is a symlink? In such case I would change it in: Return True if path refers to a symbolic link But it does say so. It talks about entries in a directory being symbolic links. Not the entries being symbolic links to directories. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Unsupported operator for Decimal: + (or -)
Todd O'Bryan schrieb: This seems to have come up earlier... http://mail.python.org/pipermail/python-list/2007-July/451187.html but no resolution. We're seeing the same thing. We're using Django's DecimalField type and when we try to add or subtract values--which should be decimal.Decimal objects--we occasionally get an error about the operator not being supported. It doesn't always happen and we can't seem to reproduce it when we try to. Try putting an try/except clause around the problem that drops into the debugger: try: foo = a - b except TypeError: import pdb; pdb.set_trace() Then you can see what a b _really_ are. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: How to Teach Python Variables
In article [EMAIL PROTECTED], Aaron Watters [EMAIL PROTECTED] wrote: I would try to avoid talking in generalities about python variables versus C or lisp or whatever, unless I was teaching an upper division college programming languages survey class. Instead, I'd fire up the interactive interpreter and illustrate how things work via examples, avoiding the weird cases at all costs. If they ask about the relationship to C or java or whatever I would encourage them to not worry about it, and only go deeper if pressed. I don't know if that explains enough on its own - I suppose it depends on how ambitious your programmer is. But the key point is that by approaching it this way, you're teaching them how to teach themselves as required: write an example, see what happens. A programmer who does this by reflex and remains confused about how the language works is, in my opinion, not going to get very far anyway. (This may have changed somewhat in recent years as more esoteric junk has has been stuffed into the language, I haven't really been keeping track.) In contrast, I suspect that someone who learns Python concepts in terms of explanations like `boxes' or `pointers' or whatnot is at some disadvantage while that lasts, like translating a foreign language to your own instead of attaching meaning directly. Donn Cave, [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: Unsupported operator for Decimal: + (or -)
On Nov 27, 2007 11:45 AM, Todd O'Bryan [EMAIL PROTECTED] wrote: This seems to have come up earlier... http://mail.python.org/pipermail/python-list/2007-July/451187.html but no resolution. We're seeing the same thing. We're using Django's DecimalField type and when we try to add or subtract values--which should be decimal.Decimal objects--we occasionally get an error about the operator not being supported. It doesn't always happen and we can't seem to reproduce it when we try to. Has anybody else seen this or is it just the original poster and me? If __sub__ (or __add__, or whatever) returns NotImplemented that can raise this error. This should never happen between two Decimal instances, and the traceback should show you which two classes were used. The most likely cause is that something is getting passed into the comparison that's not a Decimal, an int, or a long. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to Teach Python Variables
On Nov 26, 7:49 am, Hrvoje Niksic [EMAIL PROTECTED] wrote: greg [EMAIL PROTECTED] writes: none wrote: IIRC, I once saw an explanation how Python doesn't have variables in the sense that, say, C does, and instead has bindings from names to objects. If you're talking to C programmers, just tell them that Python variables always contain pointers. That should give them the right mental model to build on. That is a convenient shortcut when it works, but in my experience it tends to confuse the issue. The reason is that one of the main uses of pointers in C is implementing pass-by-reference. A C programmer told that Python variables internally hold pointers expects this code: def func(a): a = 10 ... func(x) to change the value of x. This shouldn't confuse a C programmer if he understands that assignment changes the pointer address, instead of copying the value: void func(int *a) { int *tmp = malloc(sizeof(int)); *tmp = 10; a = tmp; free(tmp); } int *x = some_address; func(x); assert(x == some_address); The confusion is that assignment does not copy the object. Python variables are pointers and that's it. -- http://mail.python.org/mailman/listinfo/python-list
Re: read/write to java socket in python
On Nov 27, 4:21 pm, Jean-Paul Calderone [EMAIL PROTECTED] wrote: On Tue, 27 Nov 2007 07:08:04 -0800 (PST), [EMAIL PROTECTED] wrote: Hi all, I have a problem with reading from a Java server after I have written to it - it just hangs. It works fine if I just write to the server and not try to write. I have read the HOWTO on sockets - and it states that there is a problem (something about flushing), but not what the solutions is. Nor do google. Can somebody please help? A few lines down you can see the example code that sums up the problem. Just change the name of the Python HOST-variable. Thanks Mads [snip] s.send(Hi Java Server); Here, you sent some bytes (maybe - you didn't check the return value of send, so it's possible nothing at all was sent) but you didn't send a new line indicator of any sort. [snip] print(buf.readLine()); Here, you tried you read a line from the socket. The peer never sent a whole line though, so readLine will never succeed. Jean-Paul Thanks, sometimes the obvious causes the biggest problems ;) Thanks a lot! /Mads -- http://mail.python.org/mailman/listinfo/python-list
Pulling data from a .asps site
There's a government website which shows public data for banks. We'd like to pull the data down programmatically but the data is hidden behind .aspx... Is there anyway in Python to hook in directly to a browser (firefox or IE) to do the following... 1) Fill the search criteria 2) Press the Search button 3) Press another button (the CSV button) on the resulting page 4) Then grab the data out of the notepad file that pops up If this is a wild good chase, let me know... (or if there's a better way besides Python... I may have to explore writing a firefox plug-in or something)... -- http://mail.python.org/mailman/listinfo/python-list
Re: read/write to java socket in python
It's a socket interface it should be universal! On Nov 27, 2007 4:08 PM, [EMAIL PROTECTED] wrote: Hi all, I have a problem with reading from a Java server after I have written to it - it just hangs. It works fine if I just write to the server and not try to write. I have read the HOWTO on sockets - and it states that there is a problem (something about flushing), but not what the solutions is. Nor do google. Can somebody please help? A few lines down you can see the example code that sums up the problem. Just change the name of the Python HOST-variable. Thanks Mads This is the client in Python: #! /usr/bin/env python import sys from socket import * PORT = 3122 HOST = 'app-5' SUCCESS = 'Success' FAILURE = 'Failure' s = socket(AF_INET, SOCK_STREAM) s.connect((HOST, PORT)) s.send(Hi Java Server); print Have written, waiting to recieve.. print s.recv(1014) s.close() And this the server in Java: import java.io.*; import java.net.*; public class Server{ public static void main(String args[]){ int port = 3122; int backLog = 50; ServerSocket ss = null; try{ InetAddress localhost = InetAddress.getLocalHost(); ss = new ServerSocket(port, backLog, localhost); while(true){ final Socket client = ss.accept(); new Thread(){ public void run(){ try{ InputStream is = client.getInputStream(); BufferedReader buf = new BufferedReader(new InputStreamReader(is)); print(buf.readLine()); PrintWriter out = new PrintWriter(client.getOutputStream()); out.write(Hi Python Client.); out.flush(); client.close(); }catch(Exception e) {print(e);} } }.start(); } }catch(Exception e){print(e);} } private static void print(Object o){System.out.println(o);} } -- http://mail.python.org/mailman/listinfo/python-list -- http://search.goldwatches.com/?Search=Movado+Watches http://www.goldwatches.com/coupons http://www.jewelerslounge.com -- http://mail.python.org/mailman/listinfo/python-list
Fwd: Pulling data from a .asps site
-- Forwarded message -- From: Todd O'Bryan [EMAIL PROTECTED] Date: Nov 27, 2007 1:48 PM Subject: Re: Pulling data from a .asps site To: [EMAIL PROTECTED] Check out Selenium Remote Control! It's very easy to use. On Nov 27, 2007 1:37 PM, [EMAIL PROTECTED] wrote: There's a government website which shows public data for banks. We'd like to pull the data down programmatically but the data is hidden behind .aspx... Is there anyway in Python to hook in directly to a browser (firefox or IE) to do the following... 1) Fill the search criteria 2) Press the Search button 3) Press another button (the CSV button) on the resulting page 4) Then grab the data out of the notepad file that pops up If this is a wild good chase, let me know... (or if there's a better way besides Python... I may have to explore writing a firefox plug-in or something)... -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: How to Teach Python Variables
Aurélien Campéas [EMAIL PROTECTED] wrote (Sun, 25 Nov 2007 20:09:59 +0100): none a écrit : That's something I've often heard and I don't get it. Somehow I don't understand how C variables are not like python bindings (the differences being that C variables are statically typed and completely disappear at run-time; are these differences important enough to warrant such a shift in terminology ? (yes there are some other differences, but then the question is asked in a context of pedagogy, where the audience is introduced to the basics)) I mean : aren't C variables also bindings from names to objects ? Or what ? This is actually something that comes up a lot on #python on freenode irc. Usually we can explain it by calling things nametags that get bound to objects and such, but sometimes it help to show people globals() and locals() - that you can really just keep track of names with a dictionary (And this is indeed, as I understand it, what's actually being done). There's nothing that stops several names from being bound to the same object, but names need to be unique. It's quite a good illustration, I think. It certainly helped me. Oh, and there's the thing about people calling them references, which isn't really accurate in the C++/Java or pointer way. Considering a C++/Java reference can actually change what it, in that if you send a reference to a variable into a function/method, you can risk that your own copy will be pointing (For a lack of better words) to a different object. This won't happen in Python. Mutable objects make it seem very similar though. -- regards, Robin -- http://mail.python.org/mailman/listinfo/python-list
Re: spawning a process with subprocess
Wow, everyone. Great comments. Thanks so much! A few points on all of the above, just so I don't look too stupid: * The problem with the testcase, I believe, was the size of the file and the output pipe filling up, as Nick suggested. When run on a smaller file, with Jordan's suggestions, it works fine. With a larger file, it's necessary to do as Nick says. If the size of the file is unknown, its best to use this case as the default. This seems unfortunate to me, because it's quite a bit of code to do something that should be fairly straightforward--at least, that's what I think. * Using poll() and checking for None and not non-zero: Yes, I had both of those originally in my testcase, but when I re-wrote and re- wrote it after it initially didn't work those initial concepts got dropped. Thanks for reminding me. * Yes, I should use proc instead of thread as a variable. Good point, Ove. But your solution works on small files but chokes on larger files, too. Thanks again...and just to reiterate, I really think this could be more straightforward for the rest of us if Popen could do all of this on its own. Brian On Nov 27, 5:13 am, Ove Svensson [EMAIL PROTECTED] wrote: bhunter [EMAIL PROTECTED] writes: Hi, I've used subprocess with 2.4 several times to execute a process, wait for it to finish, and then look at its output. Now I want to spawn the process separately, later check to see if it's finished, and if it is look at its output. I may want to send a signal at some point to kill the process. This seems straightforward, but it doesn't seem to be working. Here's my test case: import subprocess, time cmd = cat somefile thread = subprocess.Popen(args=cmd.split(), shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True) while(1): time.sleep(1) if(thread.returncode): break else: print thread.returncode print returncode = , thread.returncode for line in thread.stdout: print stdout:\t,line This will just print the returncode of None forever until I Ctrl-C it. Of course, the program works fine if I call thread.communicate(), but since this waits for the process to finish, that's not what I want. Any help would be appreciated. Reading documentation for subprocess, it mentions that On UNIX, with shell=False (default): In this case, the Popen class uses os.execvp() to execute the child program. args should normally be a sequence. A string will be treated as a sequence with the string as the only item (the program to execute). On UNIX, with shell=True: If args is a string, it specifies the command string to execute through the shell. If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional shell arguments. Since you have specified shell = True, and since you pass a sequence as args, you will efficiently invoke the cat process through the shell and then pass somefile as an extra argument to she shell (not the cat command) That is probably not what you intended. This can be solved by either - Not splitting the cmd, in which case you will pass the whole cmd string to the shell for execution - Or setting shell to False. This is what I would have done, since I can't see any reason for going via the shell. Please note that if setting shell to False, you must then split the cmd. Please also note that your test for the returncode might not work since a normal returncode is 0. Your code will only detect non-0 values. Also, it is good practice to call wait() on the subprocess in order to avoid zombie-processes. Finally, I find it somewhat misleading to use the name thread for the variable used to represent a sub-process. Threads and processes are not exactly the same Hence, the following code should works as expected cmd = cat somefile proc = subprocess.Popen( args = cmd.split(), shell = False, stdin = None, stdout= subprocess.PIPE, stderr= subprocess.STDOUT, close_fds = True) while True: rc = proc.poll() if rc != None: break print rc time.sleep(1) lno = 1 for lin in proc.stdout: print '%i: %s' % (lno,lin.rstrip('\n')) lno += 1 rc = proc.wait() print rc = %i % rc /Ove -- http://mail.python.org/mailman/listinfo/python-list
Re: How to Teach Python Variables
On Nov 27, 2007, at Nov 27:1:21 PM, hdante wrote: This shouldn't confuse a C programmer if he understands that assignment changes the pointer address, instead of copying the value: Coming from C, I found the pointer analogy to work pretty well, but in my head I always felt that integers (or other numbers) were somehow different than lists, dicts and other classes. I think I found it a little weird to think that -- 1 -- is an object. You can't have all the integers as individual objects!, is what I thought...then I saw that is exactly how it is implemented. So when you say: a=1 it is *really* a pointer to a 1-object, and that b=1 points to the same 1-object. In [4]:id(a) Out[4]:25180552 In [5]:b=1 In [6]:id(b) Out[6]:25180552 bb -- Brian Blais [EMAIL PROTECTED] http://web.bryant.edu/~bblais -- http://mail.python.org/mailman/listinfo/python-list
Re: How to Teach Python Variables
On Nov 27, 11:52 am, Chris Mellon [EMAIL PROTECTED] wrote: I would try to avoid talking in generalities about python variables versus C or lisp or whatever, unless I was teaching an upper division college programming languages survey class. I disagree, although it's not really on topic for the thread. There's no reason why low level details need to be relegated to upper division college classes. It shouldn't be in Programming Languages 101, but it's something that I would expect a first year student who's planning to pursue a programming career or a comp sci degree to be exposed to. It's not out of place even in high school - that's where I first learned C. Well I learned PDP11 assembly and FORTRAN IV in high school and I'm still recovering (taking it day by day). Offline I would discuss anything the students wanted to talk about, but during lecture or in reading, etcetera I would try to stick to the pythonic way of looking at things without worrying about the underlying implementation, except in side references. Yes, some boxes and arrows would be very useful to explain shared side effects, but I'd keep the diagrams limited. I think thinking too much like C can lead to bad practices -- for example I've seen people use the global module name space more or less like any other hash table -- adding, deleting, changing freely -- and I think this was motivated by the C level understanding that it really is just another hash table. From a pythonic perspective you would never think of behaving this way except under extreme duress. -- Aaron Watters === http://www.xfeedme.com/nucular/pydistro.py/go?FREETEXT=revolting+delicate -- http://mail.python.org/mailman/listinfo/python-list
Re: read/write to java socket in python
On Nov 27, 4:29 pm, hdante [EMAIL PROTECTED] wrote: On Nov 27, 1:08 pm, [EMAIL PROTECTED] wrote: Hi all, I have a problem with reading from a Java server after I have written to it - it just hangs. It works fine if I just write to the server and not try to write. I have read the HOWTO on sockets - and it states that there is a problem (something about flushing), but not what the solutions is. Nor do google. Can somebody please help? A few lines down you can see the example code that sums up the problem. Just change the name of the Python HOST-variable. Thanks Mads This is the client in Python: #! /usr/bin/env python import sys from socket import * [Snip - a little Python] And this the server in Java: import java.io.*; import java.net.*; [Snip - a lot of Java] } I don't know, but it's amazing to compare the python client with the java server. Yes, Python is really handy :) /Mads -- http://mail.python.org/mailman/listinfo/python-list
Re: Looking for a Python tutor
Please post on the job section! On Nov 27, 2007 6:46 PM, hong2221 [EMAIL PROTECTED] wrote: I'm looking for a Python programmar that is willing write simple functions, prices can be talked over. Contact me asap. -- http://mail.python.org/mailman/listinfo/python-list -- http://search.goldwatches.com/?Search=Movado+Watches http://www.goldwatches.com/coupons http://www.jewelerslounge.com -- http://mail.python.org/mailman/listinfo/python-list
create pywintypes.CreateGuid() compatible guids on Linux ?
Hello, I created lots of guids via pywintypes.CreateGuid() on windows. Now I would like to run the same software on Linux / Solaris / FreeBSD. So I should produce compatible GUIDS on that systems. compatible having the meaining: Providing similiar likelehood of collisions. of course google python guid leads directly to http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/163604, but ... will it blend? any ideas what could help me to research? Harald -- http://mail.python.org/mailman/listinfo/python-list
Re: How to Teach Python Variables
On Nov 27, 2:25 pm, Aaron Watters [EMAIL PROTECTED] wrote: I hope the participants in this thread realize that this sort of discussion will cause any programming newbie to immediately melt into the floor. All right, answering the original question is good. :-P 1) If the students can't program and you have chosen to teach them by using python, then they don't have any assignment == copy hard-coded in their minds. Just teach them that python assignments are like creating aliases, or mirrors. That is, a = 2 Makes a an alias of 2. l = [1, 2, 3] Makes l[0] an alias of 1 Don't ever speak the word copy in class. 2) If the students come from different backgrounds, typically having programmed in high level languages, like pascal, java, C#, etc, then you may call python variables either aliases or references, but state clearly that assignment in python doesn't mean object copy, but reference change. 3) If you're teaching in college, always show the students the formal definition. -- Aaron Watters ===http://www.xfeedme.com/nucular/pydistro.py/go?FREETEXT=evil+fish -- http://mail.python.org/mailman/listinfo/python-list
Re: Looking for a Python tutor
On Nov 27, 5:46 pm, hong2221 [EMAIL PROTECTED] wrote: I'm looking for a Python programmar that is willing write simple ^^ functions, prices can be talked over. Contact me asap. You should start with a prospelling :) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: create pywintypes.CreateGuid() compatible guids on Linux ?
On Nov 27, 2007 1:09 PM, GHUM [EMAIL PROTECTED] wrote: Hello, I created lots of guids via pywintypes.CreateGuid() on windows. Now I would like to run the same software on Linux / Solaris / FreeBSD. So I should produce compatible GUIDS on that systems. compatible having the meaining: Providing similiar likelehood of collisions. of course google python guid leads directly to http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/163604, but ... will it blend? any ideas what could help me to research? The uuid module, in Python 2.5 and up, provides RFC compliant UUID generation. See the module docs and the RFC for details. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to Teach Python Variables
On Nov 27, 11:50 am, Donn Cave [EMAIL PROTECTED] wrote: In contrast, I suspect that someone who learns Python concepts in terms of explanations like `boxes' or `pointers' or whatnot is at some disadvantage while that lasts, like translating a foreign language to your own instead of attaching meaning directly. I agree with your point, but I think the distinction is this: pedagogical analogies should be truly *analogous* -- they should not be analogies that are only vaguely similar and require you to constantly say ...but ignore this difference and that difference...oh, and that one...you'll learn that later. I personally think the ignore this for now approach is detrimental. An example of a real analogy, which is very useful (imho), is in regards to C pointers. The analogy is to a real pointing device (nowadays, probably a laser pointer). The pointer (pointing device) is one thing, and it takes up its own dimensions of space; but it can point to something else, and that something takes up its own dimensions as well. They are not interchangeable; they have their own identities. But by following the pointer to the something else, and interpreting that something according to its own nature (text, a picture, c), you can find the value of the pointer. The pointer merely references that value. And you can exchange the value that is referred to with any other value of the same nature. You can, for example, point to a line of code on an overhead projector. Then you can change the slide. Now the value of the reference has changed, but the reference is the same. And you can go one step further and point at an arrow symbol, which in turn points to some other value in the context; the value turns out to be another pointer which can be followed to it's own meaning. Granted, it's an analogy, but I think it's a *good* analogy rather than a bad one; it helps map the meaning to familiar objects, rather than the trappings of the familiar preventing from grasping the concept. Regards, Jordan -- http://mail.python.org/mailman/listinfo/python-list
Re: It works! Was: Installing Python 3000
On Nov 27, 11:17 am, jim-on-linux [EMAIL PROTECTED] wrote: On Tuesday 27 November 2007 07:20, André wrote: On Nov 26, 9:59 pm, André [EMAIL PROTECTED] wrote: While I made some progress in trying to install Py3k from source (for the first time), it has failed... Here are the steps I went through (not necessarily in that order - except for those that matter). 1. After installing Leopard, install Xcode tools from the dvd - even if you had done so with a previous version (they need to be updated - trust me :-) 2. Download Python 3.0a1 3. Unpack the archive. 4. Go to /usr/local and make a directory sudo mkdir py3k (This is probably not needed, but that's what I did). 5. From the directory where the Python 3.0a1 was unpacked run ./configure --prefix=/usr/local/py3k 6. run make This last step failed with the following error message: gcc -fno-strict-aliasing -Wno-long-double -no-cpp-precomp -mno-fused- madd -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -I. -I./Include - DPy_BUILD_CORE -c ./Modules/posixmodule.c -o Modules/posixmodule.o ./Modules/posixmodule.c: In function 'posix_setpgrp': ./Modules/posixmodule.c:3769: error: too few arguments to function 'setpgrp' make: *** [Modules/posixmodule.o] Error 1 Any suggestions? André Following Martin v Löwis's suggestion, I looked at http://bugs.python.org/issue1358 and added the line #defineSETPGRP_HAVE_ARG by hand to pyconfig.h (after it was created by configure). Then 6. run make 7. run make test (one test failed; this step likely unnecessary) 8. sudo make altinstall 9. sudo ln /usr/local/bin/py3k/python3.0 /usr/bin/python3.0 10. type python Should have been python3.0 11. print(Hello world!) 12. Be happy! André, hoping this report might help some other newbie. Bug fix excluded, After unpacking the compressed version of Python, look for a file named README. Did that. Open README and look for Installing. Make install and Make altinstall is explained. make altinstall is mentioned (not explained) in very brief comment. This series of post followed from a previous one where I queried about how to install py3k without it becoming the default. Many useful suggestions were offered by others which I found very useful as I had *never* installed/configured/made something from source before (I always used .msi on Windows and, more recently, .dmg on Mac). Once you know what/why things like --prefix or --enable-framework or altinstall are for, the README file content becomes extremely clear. I don't like to read instructions but in the long run, it saves time. Actually, I do try and read instructions first usually. But sometimes the instructions use terms that are not clear for newbies. And, if I may, the normal way to create an alias/link for unsophisticated Mac users (like me) is to use the GUI (Finder) and ctrl-click on the file. However, /usr is hidden ... and using ln is not something that can be found in the README ... So that is why, to save time for others, I thought of writing this summary of what I did, so that it could be found by people searching this newsgroup (which is one of the other things I did first...) André jim-on-linuxhttp://www.inqvista.com -- http://mail.python.org/mailman/listinfo/python-list
chdir() problem
I am trying to get a hold of anyone at Fenix in Thailand. Can you be of any help? Pizza Hut London, Ont. Canada -- http://mail.python.org/mailman/listinfo/python-list
Re: read/write to java socket in python
[EMAIL PROTECTED] wrote: I have a problem with reading from a Java server after I have written to it - it just hangs. It works fine if I just write to the server and not try to write. Excuse me? I have read the HOWTO on sockets - and it states that there is a problem (something about flushing), but not what the solutions is. Nor do google. Can somebody please help? Spare yourself the trouble and do not use low level socket functions in Python directly. Try Twisted. http://twistedmatrix.com/projects/core/documentation/howto/clients.html As for that Java problem I recommend learning common debugging techniques: Use a protocol analyser (www.wireshark.org) and let your program(s) output a debug log. Regards, Björn -- BOFH excuse #16: somebody was calculating pi on the server -- http://mail.python.org/mailman/listinfo/python-list
Passing arguments to subclasses of unittest.TestCase
Hi, Is there a way to pass arguments to TestCases when running tests? I have a test suite that need to be configured slightly differently for 3 different products, and rather than do a hack I wondered if there was a canonical way to do it. I _know_ that py.test can do it trivially. I am rather stuck with unittest, as I have 84 testcases, and I have to make it work tomorrow. -- Tom Harris BeacyBooks beacybooksATbigpondDOTcom -- http://mail.python.org/mailman/listinfo/python-list
Re: Pulling data from a .asps site
On Tue, 27 Nov 2007 20:37:19 +0200, [EMAIL PROTECTED] wrote: There's a government website which shows public data for banks. We'd like to pull the data down programmatically but the data is hidden behind .aspx... Is there anyway in Python to hook in directly to a browser (firefox or IE) to do the following... 1) Fill the search criteria 2) Press the Search button 3) Press another button (the CSV button) on the resulting page 4) Then grab the data out of the notepad file that pops up If this is a wild good chase, let me know... (or if there's a better way besides Python... I may have to explore writing a firefox plug-in or something)... Well, Python supports moving the mouse pointer so it's should be quite simple to write such a program. -- http://mail.python.org/mailman/listinfo/python-list
Re: Running unmodified CGI scripts persistently under mod_wsgi.
On Nov 25, 1:55 am, Graham Dumpleton [EMAIL PROTECTED] wrote: The other question is whether there is even a demand for this. Do people want to be able to take unmodified Python CGI scripts and try to run them persistently in this way, or would they be better off converting them to proper WSGI applications. I think CGI will be with us for many decades. It will be awesome if mod_wsgi can run CGI without invoking python on each access. i. -- http://mail.python.org/mailman/listinfo/python-list
Re: spawning a process with subprocess
bhunter [EMAIL PROTECTED] wrote: * The problem with the testcase, I believe, was the size of the file and the output pipe filling up, as Nick suggested. When run on a smaller file, with Jordan's suggestions, it works fine. With a larger file, it's necessary to do as Nick says. If the size of the file is unknown, its best to use this case as the default. This seems unfortunate to me, because it's quite a bit of code to do something that should be fairly straightforward--at least, that's what I think. You may be interrested in the module 'asyncproc', which I wrote a couple of years ago to make it easier working with processes that would otherwise block on output. You can download it at http://www.lysator.liu.se/~bellman/download/asyncproc.py. It probably only works on Unix, but considering your use of cat as a test program, I suppose that isn't a problem for you. -- Thomas Bellman, Lysator Computer Club, Linköping University, Sweden God is real, but Jesus is an integer. ! bellman @ lysator.liu.se ! Make Love -- Nicht Wahr! -- http://mail.python.org/mailman/listinfo/python-list
Re: Should proxy objects lie about their class name?
Diez B. Roggisch [EMAIL PROTECTED] writes: John J. Lee schrieb: [EMAIL PROTECTED] (John J. Lee) writes: Not much to add to the subject line. I mean something like this: ProxyClass.__name__ = ProxiedClass.__name__ I've been told that this is common practice. Is it? Would this surprise you if you ran into it in a debugging session? Does nobody have an opinion on this? Pull your socks up, c.l.py! insert reference to argument sketch here I've written quite a few proxies, but none of them did that. IMHO this is similar to using isinstance overeagerly: it works against the duck-typing principle to guard code by requiring certain base-classes, instead of just relying on protocol. The same applies here. Sure. The push here was the doctest issue. There might be of course cases where you have code lying around that does do some distinctions based on the class-name (I'm certainly I've seen such stuff once or tiwce, but always regarded it a WTF). Then doing as you do above might help in getting things work. Right. The actual case I'm talking about here involved an exception class (dynamically created by subclassing at runtime, eugh) -- and a common one at that -- so it's a pretty common thing in doctests and any workaround on the doctest level would be very ugly (of course, alternatives include working around it by wrapping the code that raises the exception, or changing the doctest files to use the new exception name). What worries me is the potential for confusion. Again, would it confuse you (not just Diez, everybody)? John -- http://mail.python.org/mailman/listinfo/python-list
get mouse
I'm just trying to test and see if the get mouse statements are working in my program. If they are, in the console window, it should go back to the prompt. It doesn't for all of them, just the last rectangle...sometimes. Am I setting them up correctly? here's the relevant code...thx The first two numbers are x1,x2 and the last two are y1, y2. rectMathButton = CreateRect(4,6,7,8,grey) rectMathButton.draw(win) rectScienceButton = CreateRect(3.5,6.5,5,6,grey) rectScienceButton.draw(win) rectHistoryButton = CreateRect(3.5,6.5,3,4,grey) rectHistoryButton.draw(win) rectGeographyButton = CreateRect(3.1,7,1,2,grey) rectGeographyButton.draw(win) #math p1 = win.getMouse() while p1.getX() 6 or p1.getX() 4 or p1.getY() 8 or p1.getY() 7: p1 = win.getMouse() #science p2 = win.getMouse() while p2.getX() 6.5 or p2.getX() 3.5 or p2.getY() 6 or p2.getY() 5: p2 = win.getMouse() #history p3 = win.getMouse() while p3.getX() 6.5 or p3.getX() 3.5 or p3.getY() 4 or p3.getY() 3: p3 = win.getMouse() #geography p4 = win.getMouse() while p4.getX() 7 or p4.getX() 3.1 or p4.getY() 2 or p4.getY() 1.1: p4 = win.getMouse() -- http://mail.python.org/mailman/listinfo/python-list
Re: fork/exec with input redirection
./gobmk_base.linux_x86 --quiet --mode gtp 13x13.tst The only thing I could really think of to try was os.execv(./gobmk_base.linux_x86, [./gobmk_base.linux_x86, --quiet, --mode, gtp, , 13x13.tst]) but this apparently doesn't work. Is there some other way to accomplish what I'm going for? Thanks, -dan IIRC, if os.fork() == 0: new_stdin = os.open('13x13.tst') os.dup2(new_stdin, sys.stdin.fileno()) os.close(new_stdin) os.execv(./gobmk_base.linux_x86, [./gobmk_base.linux_x886, -- quiet, --mode, gtp]) Maybe a sys.stdin.flush() just to be sure ? Thanks, that did the trick (well, os.open('13x13.tst', os.O_RDONLY), but you know... close enough). -- http://mail.python.org/mailman/listinfo/python-list
Re: How to Teach Python Variables
On Nov 27, 2:20 pm, MonkeeSage [EMAIL PROTECTED] wrote: On Nov 27, 11:50 am, Donn Cave [EMAIL PROTECTED] wrote: I agree with your point, but I think the distinction is this: pedagogical analogies should be truly *analogous* -- they should not be analogies that are only vaguely similar and require you to constantly say ...but ignore this difference and that difference...oh, and that one...you'll learn that later. I personally think the ignore this for now approach is detrimental. Yes, in theory. And in theory, theory is the same as practice. In practice there is too much to understand all at once and in the beginning you have to say don't worry about that right now, consider it magic... Of course they should eventually understand it. Again, this is another place where Python shines, because the front end scary magic is limited -- you can start out with simple and useful imperative straight line scripts and then move into higher levels from there -- in contrast to C# and java which require you to declare a class with static methods even if you have no intention of writing any OO code. Beginners find this incredibly confusing and scary. Magic should never be permanent, however. In the case of Struts or Spring there is magic that no one is ever intended to truly understand. Danger Will Robinson! Danger! This is my problem also with most web application frameworks, even in Python -- too much strange magic floats around in the air -- usually in order to make things easy that I never thought were hard in the first place. -- Aaron Watters === http://www.xfeedme.com/nucular/pydistro.py/go?FREETEXT=lame+hack+wink -- http://mail.python.org/mailman/listinfo/python-list
Re: Pulling data from a .asps site
[EMAIL PROTECTED] schrieb: There's a government website which shows public data for banks. We'd like to pull the data down programmatically but the data is hidden behind .aspx... Is there anyway in Python to hook in directly to a browser (firefox or IE) to do the following... 1) Fill the search criteria 2) Press the Search button 3) Press another button (the CSV button) on the resulting page 4) Then grab the data out of the notepad file that pops up If this is a wild good chase, let me know... (or if there's a better way besides Python... I may have to explore writing a firefox plug-in or something)... There needs no browser to be involved - unless there is AJAX in the game, and even then - after alle, it's all HTTP-requests. Whichfor there are several options to work with - one popular is http://wwwsearch.sourceforge.net/mechanize/ Diez -- http://mail.python.org/mailman/listinfo/python-list