Re: How to escape # hash character in regex match strings
504cr...@gmail.com wrote: > I've encountered a problem with my RegEx learning curve -- how to > escape hash characters # in strings being matched, e.g.: > string = re.escape('123#abc456') match = re.match('\d+', string) print match > > <_sre.SRE_Match object at 0x00A6A800> print match.group() > > 123 > > The correct result should be: > > 123456 > > I've tried to escape the hash symbol in the match string without > result. > > Any ideas? Is the answer something I overlooked in my lurching Python > schooling? As you're not being clear on what you wanted, I'm just guessing this is what you wanted: >>> s = '123#abc456' >>> re.match('\d+', re.sub('#\D+', '', s)).group() '123456' >>> s = '123#this is a comment and is ignored456' >>> re.match('\d+', re.sub('#\D+', '', s)).group() '123456' -- http://mail.python.org/mailman/listinfo/python-list
Re: How should I compare two txt files separately coming from windows/dos and linux/unix
On Jun 11, 1:08 pm, John Machin wrote: > Chris Rebert rebertia.com> writes: > > > > > > > On Wed, Jun 10, 2009 at 8:11 PM, higer gmail.com> wrote: > > > I just want to compare two files,one from windows and the other from > > > unix. But I do not want to compare them through reading them line by > > > line. Then I found there is a filecmp module which is used as file and > > > directory comparisons. However,when I use two same files (one from > > > unix,one from windows,the content of them is the same) to test its cmp > > > function, filecmp.cmp told me false. > > > > Later, I found that windows use '\n\r' as new line flag but unix use > > > '\n', so filecmp.cmp think that they are different,then return false. > > > So, can anyone tell me that is there any method like IgnoreNewline > > > which can ignore the difference of new line flag in diffrent > > > platforms? If not,I think filecmp may be not a good file comparison > > > Nope, there's no such flag. You could run the files through either > > `dos2unix` or `unix2dos` beforehand though, which would solve the > > problem. > > Or you could write the trivial line comparison code yourself and just > > make sure to open the files in Universal Newline mode (add 'U' to the > > `mode` argument to `open()`). > > You could also file a bug (a patch to add newline insensitivity would > > probably be welcome). > > Or popen diff ... > > A /very/ /small/ part of the diff --help output: > > -E --ignore-tab-expansion Ignore changes due to tab expansion. > -b --ignore-space-change Ignore changes in the amount of white space. > -w --ignore-all-space Ignore all white space. > -B --ignore-blank-lines Ignore changes whose lines are all blank. > -I RE --ignore-matching-lines=RE Ignore changes whose lines all match RE. > --strip-trailing-cr Strip trailing carriage return on input. > > Cheers, > John Tool can certainly be used to compare two files,but I just want to compare them using Python code. -- http://mail.python.org/mailman/listinfo/python-list
Re: How should I compare two txt files separately coming from windows/dos and linux/unix
On Jun 11, 11:44 am, Chris Rebert wrote: > On Wed, Jun 10, 2009 at 8:11 PM, higer wrote: > > I just want to compare two files,one from windows and the other from > > unix. But I do not want to compare them through reading them line by > > line. Then I found there is a filecmp module which is used as file and > > directory comparisons. However,when I use two same files (one from > > unix,one from windows,the content of them is the same) to test its cmp > > function, filecmp.cmp told me false. > > > Later, I found that windows use '\n\r' as new line flag but unix use > > '\n', so filecmp.cmp think that they are different,then return false. > > So, can anyone tell me that is there any method like IgnoreNewline > > which can ignore the difference of new line flag in diffrent > > platforms? If not,I think filecmp may be not a good file comparison > > Nope, there's no such flag. You could run the files through either > `dos2unix` or `unix2dos` beforehand though, which would solve the > problem. > Or you could write the trivial line comparison code yourself and just > make sure to open the files in Universal Newline mode (add 'U' to the > `mode` argument to `open()`). > You could also file a bug (a patch to add newline insensitivity would > probably be welcome). > > Cheers, > Chris > --http://blog.rebertia.com Thank you very much. Adding 'U' argument can perfectly work, and I think it is definitely to report this as a bug to Python.org as you say. Cheers, higer -- http://mail.python.org/mailman/listinfo/python-list
Re: object reincarnation
On Jun 11, 5:34 am, Manavan wrote: > Since the real world objects often needs to be deleted even if they > have some reference from some other object [...] >From this it sounds like you're trying to implement some form of weak referencing. Are you familiar with weakref? "A weak reference to an object is not enough to keep the object alive: when the only remaining references to a referent are weak references, garbage collection is free to destroy the referent and reuse its memory for something else. A primary use for weak references is to implement caches or mappings holding large objects, where it’s desired that a large object not be kept alive solely because it appears in a cache or mapping." http://docs.python.org/library/weakref.html -- http://mail.python.org/mailman/listinfo/python-list
zipfile doesn't compress very good, are there other solutions ?
hello, I want to make a distro for Ubuntu, and run under Windows. I packed all sources with zipfile, but the compression doesn't seem to be very good. If run the created file through 7zip, it becomes anout half the size. Is there a way to accomplish the same task with zipfile ( the documentation isn't overwhelming). thanks, Stef Mientki -- http://mail.python.org/mailman/listinfo/python-list
Re: multi-core software
Matthias Blume wrote: > "Jeff M." writes: >> But, assuming that your program works and does what it's supposed to, >> I agree with Jon that performance needs to be right near the top of >> the list of concerns. Why? Performance isn't about looking good as a >> programmer, or having fun making a function run in 15 cycles instead >> of 24, or coming up with some neat bit packing scheme so that your app >> now only uses 20K instead of 200K. Performance is - pure and simple - >> about one thing only: money. > > Programmer time is vastly more expensive than CPU time, so the > money argument often leads to slow ("low performance") solutions as long > as they are "good enough" because developing a faster solution would > mean spending more valuable programmer time at a cost that cannot > be recovered over the life cycle of the product in question. In the context of commercial software, the money to fund developers to improve performance comes from the huge marketing budget because performance is usually more about marketing than anything else. -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. http://www.ffconsultancy.com/?u -- http://mail.python.org/mailman/listinfo/python-list
xml.dom.minidom losing the XML document type attribute
Hello group, when I read in a XML document with the xml.dom.minidom parser and write it out again, an attribute is lost: Input: [...] Output: How can I fix this? Python is Python 3.0rc2 (r30rc2:67114, Nov 16 2008, 15:24:36) Kind regards, Johannes -- "Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit, verlästerung von Gott, Bibel und mir und bewusster Blasphemie." -- Prophet und Visionär Hans Joss aka HJP in de.sci.physik <48d8bf1d$0$7510$54022...@news.sunrise.ch> -- http://mail.python.org/mailman/listinfo/python-list
Re: can it be shorter?
Paul Rubin writes: > url = url.rstrip('/') + '/' That's what I use: It has the (nice) side effect of ending the URL with a *single* slash. C -- http://mail.python.org/mailman/listinfo/python-list
Re: zipfile doesn't compress very good, are there other solutions ?
On Tue, Jun 9, 2009 at 3:07 PM, Stef Mientki wrote: > hello, > > I want to make a distro for Ubuntu, > and run under Windows. > > I packed all sources with zipfile, > but the compression doesn't seem to be very good. > > If run the created file through 7zip, it becomes anout half the size. Are you sure it's compressing it as a .zip? I know it has its own incompatible .7z format which tends compress things a bit better. > Is there a way to accomplish the same task with zipfile ( the documentation > isn't overwhelming). You can use the `tarfile` module's bz2 compression option; bzip2 tends to compress slightly better than zip. tarfile docs -- http://docs.python.org/library/tarfile.html Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: object reincarnation
On Jun 10, 12:34 pm, Manavan wrote: > Hello everyone, > Since the real world objects often needs to be deleted even if they > have some reference from some other object, I am going to use this > approach to better model this situation, by cleaning up the attributes > and assigning self.__class__ to a different class. > Any comment on this approach. > > class Deleted(object): > pass > > class RealWorldObj(object): > def __init__(self, *args): > self.attrs = args > def getAttrs(self,): > return self.attrs > def delete(self,): > del self.attrs > self.__class__ = Deleted > > >>> a = RealWorldObj(1,2,3) > >>> print a.attrs > (1, 2, 3) > >>> a.delete() > >>> a > > <__main__.Deleted object at 0x893ae2c a.attrs > > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'Deleted' object has no attribute 'attrs' Cute, but let me suggest that changing the class of the object would make it harder to track down the original error. So just delete the attributes. I'd also recommend using self.__dict__.clear() for that, it gets all of them (usually) and doesn't need to be updated with you add a new attribute to a class. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: installation in mac os x
Karl Jansson wrote: > Hi, > > I was doing the tutorial at http://www.python.org/doc/current/ > tutorial/, and I came across some code that did not work, and I got > the following error: AttributeError: 'str' object has no attribute > 'format'. > > So I downloaded a .dmg of python 2.6.2 and then I installed it. But > it's not working. Running the Python launcher, the "new" command in > the file menu is grey, and won't work, so I can't make any scripts. > > Does anyone know how to make python 2.6.2 work in mac os x? What happens if you work on the commandline (Terminal)? Python2.6 should be available from (out of my head) /Library/Frameworks/Python.framework/Versions/2.6/bin/python If that's working, it is successfully installed. Diez -- http://mail.python.org/mailman/listinfo/python-list
ANN: eGenix pyOpenSSL Distribution 0.9.0-0.9.8k
ANNOUNCING eGenix.com pyOpenSSL Distribution Version 0.9.0-0.9.8k An easy to install and use repackaged distribution of the pyOpenSSL Python interface for OpenSSL - available on Windows, Mac OS X and Unix platforms This announcement is also available on our web-site for online reading: http://www.egenix.com/company/news/eGenix-pyOpenSSL-Distribution-0.9.0-0.9.8k-1-GA.html INTRODUCTION The eGenix.com pyOpenSSL Distribution includes everything you need to get started with SSL in Python. It comes with an easy to use installer that includes the most recent OpenSSL library versions in pre-compiled form. pyOpenSSL is an open-source Python add-on (http://pyopenssl.sf.net/) that allows writing SSL aware networking applications as well as certificate management tools. OpenSSL is an open-source implementation of the SSL protocol (http://www.openssl.org/). For more information, please see the product page: http://www.egenix.com/products/python/pyOpenSSL/ NEWS This new release of the eGenix.com pyOpenSSL Distribution updates the included pyOpenSSL version to 0.9, which includes a new fix for a serious problem in pyOpenSSL 0.8 related to threaded applications. It also comes with an important bug-fix update of OpenSSL, now at version 0.9.8k. The problem causes invalid thread states in the Python interpreter which then result in random core dumps and seg faults when using pyOpenSSL 0.8.0 with multi-threaded applications. The new fix is slightly different than the one we included in 0.8.1 and based on a code analysis we did together with Jean-Paul Calderone to track down the cause of the problem. Binaries are available for Linux x86 and x64 as well as Windows x86 and Mac OS X PPC/Intel. They include both pyOpenSSL and the necessary OpenSSL libraries. For Plone users and friends of buildout scripts, we have added pre-built binaries for Windows. They install just like the Linux versions and allow easy integration of the archives into buildout scripts. For our Mac OS X users, we have included new pre-built binaries for Mac OS X PPC and Intel platforms. DOWNLOADS The download archives and instructions for installing the package can be found at: http://www.egenix.com/products/python/pyOpenSSL/ UPGRADING Before installing this version of pyOpenSSL, please make sure that you uninstall any previously installed pyOpenSSL version. Otherwise, you could end up not using the included OpenSSL libs. ___ SUPPORT Commercial support for these packages is available from eGenix.com. Please see http://www.egenix.com/services/support/ for details about our support offerings. Enjoy, -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Jun 11 2009) >>> Python/Zope Consulting and Support ...http://www.egenix.com/ >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/ 2009-06-29: EuroPython 2009, Birmingham, UK17 days to go ::: Try our new mxODBC.Connect Python Database Interface for free ! eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ -- http://mail.python.org/mailman/listinfo/python-list
Re: unladen swallow: python and llvm
Nick Craig-Wood wrote: > Luis M González wrote: >> I am very excited by this project (as well as by pypy) and I read all >> their plan, which looks quite practical and impressive. >> But I must confess that I can't understand why LLVM is so great for >> python and why it will make a difference. > > CPython uses a C compiler to compile the python code (written in C) > into native machine code. That would be Cython: compile Python code to (optimised) C code and then run a C compiler over that to get native machine code. http://cython.org/ CPython compiles Python code to *byte-code* and then *interprets* that in a virtual machine (which happens to be written in C, hence the name). Stefan -- http://mail.python.org/mailman/listinfo/python-list
Convert integer to fixed length binary string
Hi, I know the bin function converts an int into a binary string. Unfortunately, I need to know the length of the binary string when it is being read in and len(bin(x)) depends on x. Is there any way to limit it to 4 bytes? Thanks for your assistance, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: unladen swallow: python and llvm
bearophileh...@lycos.com wrote: > Kay Schluehr: >> Don't understand your Cython compliant. The only tricky part of Cython >> is the doublethink regarding Python types and C types. I attempted once >> to write a ShedSkin like code transformer from Python to Cython based on >> type recordings but never found the time for this because I have to work >> on EasyExtend on all fronts at the same time. > > I have tried to create a certain data structure with a recent version > of Pyrex on Windows, and I have wasted lot of time looking for missing > reference count updates that didn't happen, or memory that didn't get > freed. I wonder what you did then. Apparently, you didn't just compile a Python program, but tried to use Pyrex/Cython to avoid writing C code. That can work, but depends on your C expertise. If you only compile Python code, you will not get in touch with any ref-counting or memory leaks (any more than in CPython, that is). You only have to care about freeing memory if you manually allocate that memory through malloc(), in which case it's your own fault if it doesn't get freed. > I'm sure lot of people like Cython, but I prefer a more transparent > language, that doesn't hide me how it works inside. Cython doesn't hide anything. Most of the magic that happens is done in code tree transformations, which you can look up in the compiler code. The code generation is mostly just mapping the final code tree to C code, with some type specialisations. Cython will even copy your complete source code line-by-line into the C code it writes, so that you can easily read up what your code gets translated to. I admit that the generated C code is not always simple and obvious, as it contains lots of runtime type specialisations and optimisations. But that's the price you pay for fast code. And you can make Cython leave out most of the duplicated code (i.e. the pessimistic fallbacks) by adding type hints to your code. Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: Convert integer to fixed length binary string
On Jun 11, 10:29 am, casebash wrote: > Sorry, I didn't quite make it clear. The issue isn't about limiting > the length (as I won't be using integers bigger than this). The > problem is that sometimes the output is shorter. Is this what you're looking for? Python 2.6.2 (r262:71600, Jun 8 2009, 14:57:27) [GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> '{0:032b}'.format(12345) '001100111001' Mark -- http://mail.python.org/mailman/listinfo/python-list
Re: xml.dom.minidom losing the XML document type attribute
Stefan Behnel schrieb: > Johannes Bauer wrote: >> when I read in a XML document with the xml.dom.minidom parser and write >> it out again, an attribute is lost: >> >> Input: >> >> >> [...] >> >> Output: >> >> >> How can I fix this? > > You don't have to. UTF-8 is the default encoding, so the two lines above > are equivalent. Can I somehow force Python to generate it anyways? I have software which complains if an explicit encoding is missing... Kind regards, Johannes -- "Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit, verlästerung von Gott, Bibel und mir und bewusster Blasphemie." -- Prophet und Visionär Hans Joss aka HJP in de.sci.physik <48d8bf1d$0$7510$54022...@news.sunrise.ch> -- http://mail.python.org/mailman/listinfo/python-list
Re: xml.dom.minidom losing the XML document type attribute
Johannes Bauer wrote: > Stefan Behnel schrieb: >> Johannes Bauer wrote: >>> when I read in a XML document with the xml.dom.minidom parser and write >>> it out again, an attribute is lost: >>> >>> Input: >>> >>> >>> [...] >>> >>> Output: >>> >>> >>> How can I fix this? >> You don't have to. UTF-8 is the default encoding, so the two lines above >> are equivalent. > > Can I somehow force Python to generate it anyways? Did you try passing encoding='UTF-8' on serialisation? > I have software which > complains if an explicit encoding is missing... Well, to parse XML, it's best to use an XML parser. ;) Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: Compiling Python3.1
On Wed, Jun 10, 2009 at 10:33 PM, Johannes Bauer wrote: > Martin v. Löwis schrieb: > >> What can I do about that? > > > > Remove the non-ASCII characters from db.h. > > Ehh... > > $ find -type f | grep -i db.h > OT: find -type f -iname "db.h" -- http://mail.python.org/mailman/listinfo/python-list
Re: random number including 1 - i.e. [0,1]
Thanks everyone, I learned more than I expected about floats :-) and got some good explanations and ideas about all of this. Esmail -- http://mail.python.org/mailman/listinfo/python-list
Re: Convert integer to fixed length binary string
casebash wrote: > I know the bin function converts an int into a binary string. Binary string sounds ambiguous. Firstly, everything is binary. Secondly, strings are byte strings or Unicode strings. In any case, I'm not 100% sure what you mean - giving an example of input and output would help! > Unfortunately, I need to know the length of the binary string when it > is being read in and len(bin(x)) depends on x. Is there any way to > limit it to 4 bytes? If you need a piece of four bytes which contain a number in a packed format similar to the one used in memory, using bin(x) is the wrong way. Instead, take a look at the struct module: import struct struct.pack('=L', 255) Alternatively, also the array module might help. Uli -- Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932 -- http://mail.python.org/mailman/listinfo/python-list
Re: zipfile doesn't compress very good, are there other solutions ?
Stef Mientki wrote: > I packed all sources with zipfile, > but the compression doesn't seem to be very good. If you don't specify the compression, the files are not compressed at all. Just in case you didn't know... Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Convert integer to fixed length binary string
Sorry, I didn't quite make it clear. The issue isn't about limiting the length (as I won't be using integers bigger than this). The problem is that sometimes the output is shorter. -- http://mail.python.org/mailman/listinfo/python-list
OT: Periodic table gets a new element
Element 112 is to be named. Do you think we could persuade the scientists to name it "Pythonium"? :-) http://news.bbc.co.uk/1/hi/sci/tech/8093374.stm -- http://mail.python.org/mailman/listinfo/python-list
Re: retrieve bitwise float representation
On Jun 10, 8:41 am, Ulrich Eckhardt wrote: > I need to pack a floating point value into a vector of 32-bit unsigned > values in IEEE format. Further, I maintain a CRC32 checksum for integrity > checking. For the latter, I actually need the float as integral value. ... > What I'm wondering is whether there are any better or alternative ways to > achieve this, the overhead now seems enormous and unnecessary to me here. Numpy has support for this: import numpy as np a = np.arange(10.) # an array of floats b = a.view(dtype=np.uint32) print b array([ 0, 0, 1072693248, 0, 1073741824, 0, 1074266112, 0, 1074790400, 0, 1075052544, 0, 1075314688, 0, 1075576832, 0, 1075838976, 0, 1075970048, 0], dtype=uint32) b[0]=5 print a array([ 1.06099790e-313, 1.e+000, 2.e+000, 3.e+000, 4.e+000, 5.e+000, 6.e+000, 7.e+000, 8.e+000, 9.e+000]) -- http://mail.python.org/mailman/listinfo/python-list
Re: xml.dom.minidom losing the XML document type attribute
Stefan Behnel schrieb: >> Can I somehow force Python to generate it anyways? > > Did you try passing encoding='UTF-8' on serialisation? Uhm... nope - how can I do that? > >> I have software which >> complains if an explicit encoding is missing... > > Well, to parse XML, it's best to use an XML parser. ;) Well, I'm not speaking about my software :-) Actually it's Gnucash which complains if the tag is not explicitly set. This is because they appearently had a ancient version which did not specify the charset, but used a different one than UTF-8. Kind of annoying, but fixing my XML output seems to be easier than convincing the Gnucash people to change their software :-) Kind regards, Johannes -- "Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit, verlästerung von Gott, Bibel und mir und bewusster Blasphemie." -- Prophet und Visionär Hans Joss aka HJP in de.sci.physik <48d8bf1d$0$7510$54022...@news.sunrise.ch> -- http://mail.python.org/mailman/listinfo/python-list
Re: OT: Periodic table gets a new element
Not if this element is to end up in some further ultimate nuclear weapon :-) , unless you are using python to conquer the world (I've been told this is GVR main secret objective). MRAB wrote: Element 112 is to be named. Do you think we could persuade the scientists to name it "Pythonium"? :-) http://news.bbc.co.uk/1/hi/sci/tech/8093374.stm -- http://mail.python.org/mailman/listinfo/python-list
Re: xml.dom.minidom losing the XML document type attribute
Johannes Bauer wrote: > Stefan Behnel schrieb: > >>> Can I somehow force Python to generate it anyways? >> Did you try passing encoding='UTF-8' on serialisation? > > Uhm... nope - how can I do that? Well, depends on what your code currently does. Maybe you could use something like doc.xmlwrite(..., encoding='UTF-8') Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: OT: Periodic table gets a new element
MRAB wrote: Element 112 is to be named. Do you think we could persuade the scientists to name it "Pythonium"? :-) What did Python do to deserve this? I think 'Hofmannium' is a more appropriate name ;-) On the other hand, if the scientists used Python on their equipment with which they discovered the new element, the name 'Pythonium' is of course totally acceptable. Greetings, -- "The ability of the OSS process to collect and harness the collective IQ of thousands of individuals across the Internet is simply amazing." - Vinod Valloppillil http://www.catb.org/~esr/halloween/halloween4.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Programming language comparison examples?
pobox.com> writes: > > I thought there was a website which demonstrated how to program a bunch of > small problems in a number of different languages. I know about the > Programming Language Shootout: > > http://shootout.alioth.debian.org/ > > but that's not what I was thinking of. I thought there was a site with a > bunch of smaller examples. Rosetta Code has examples of common programming tasks in hundreds of languages: http://rosettacode.org/ -- http://mail.python.org/mailman/listinfo/python-list
FW: [Tutor] Multi-Threading and KeyboardInterrupt
I sent this to the Tutor mailing list and did not receive a response. Perhaps one of you might be able to offer some sagely wisdom or pointed remarks? Please reply off-list and thanks in advance. Code examples are below in plain text. -- Forwarded Message > From: Matthew Strax-Haber > Reply-To: Matthew Strax-Haber > Date: Tue, 9 Jun 2009 22:01:33 -0500 > To: Python Tutor > Subject: [Tutor] Multi-Threading and KeyboardInterrupt > > Hey everyone, > > I hope one of you can help me with this. This is my first foray into > multi-threaded programming. I have tried to boil my code down to it's > simplest demonstrative form. > > My program runs interactively by allowing the user to directly > interact with the python prompt. This program has a runAll() method > that runs a series of subprocesses with a cap on how many instances > are running at a time. My intent is to allow the user to use Ctrl-C to > break these subprocesses. Note that while not reflected in the demo > below, each subprocess needs to be able to run clean-up code before > shutting down (in single-threaded mode I just wrap in try-finally). > When I run DB.py, and interrupt it with Ctrl-C, things do not run so > cleanly. Please let me know what I can change to make this work > properly. My intent is to have the following output: > > 'key interrupt 1' > 'key interrupt 2' > 'key interrupt 3' > 'key interrupt 4' > '* stopped midway ' > > Here is the code for a demo: > ## > DB.py (run this): > ## #!/usr/bin/env python from subprocessimport Popen from threadingimport Thread, Semaphore MAX_SUBPROCS= 3 RUN_PERMISSION= Semaphore(MAX_SUBPROCS) def runSingle(i): with RUN_PERMISSION: Popen(['./Sub.py', str(i)]).wait() def runAll(): workers = [ Thread(target = runSingle, args = [i]) for i in xrange(MAX_SUBPROCS + 1) ] try: for w in workers: w.start() except KeyboardInterrupt: ## I want this to be shown on a KeyboardInterrupt print '* stopped midway ' for w in workers: w.join() runAll() > ## > Sub.py (called by DB.py): > ## #!/usr/bin/env python import sys, time try: while True: pass except KeyboardInterrupt: print 'key interrupt %s' % sys.argv[1] raise > ## > ___ > Tutor maillist - tu...@python.org > http://mail.python.org/mailman/listinfo/tutor > -- End of Forwarded Message -- http://mail.python.org/mailman/listinfo/python-list
Re: How to escape # hash character in regex match strings
On Jun 11, 2:01 am, Lie Ryan wrote: > 504cr...@gmail.com wrote: > > I've encountered a problem with my RegEx learning curve -- how to > > escape hash characters # in strings being matched, e.g.: > > string = re.escape('123#abc456') > match = re.match('\d+', string) > print match > > > <_sre.SRE_Match object at 0x00A6A800> > print match.group() > > > 123 > > > The correct result should be: > > > 123456 > > > I've tried to escape the hash symbol in the match string without > > result. > > > Any ideas? Is the answer something I overlooked in my lurching Python > > schooling? > > As you're not being clear on what you wanted, I'm just guessing this is > what you wanted: > > >>> s = '123#abc456' > >>> re.match('\d+', re.sub('#\D+', '', s)).group() > '123456' > >>> s = '123#this is a comment and is ignored456' > >>> re.match('\d+', re.sub('#\D+', '', s)).group() > > '123456' Sorry I wasn't more clear. I positively appreciate your reply. It provides half of what I'm hoping to learn. The hash character is actually a desirable hook to identify a data entity in a scraping routine I'm developing, but not a character I want in the scrubbed data. In my application, the hash makes a string of alphanumeric characters unique from other alphanumeric strings. The strings I'm looking for are actually manually-entered identifiers, but a real machine-created identifier shouldn't contain that hash character. The correct pattern should be 'A1234509', but is instead often merely entered as '#12345' when the first character, representing an alphabet sequence for the month, and the last two characters, representing a two-digit year, can be assumed. Identifying the hash character in a RegEx match is a way of trapping the string and transforming it into its correct machine- generated form. I'm surprised it's been so difficult to find an example of the hash character in a RegEx string -- for exactly this type of situation, since it's so common in the real world that people want to put a pound symbol in front of a number. Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: How to escape # hash character in regex match strings
On Jun 11, 9:22 am, Brian D wrote: > On Jun 11, 2:01 am, Lie Ryan wrote: > > > > > 504cr...@gmail.com wrote: > > > I've encountered a problem with my RegEx learning curve -- how to > > > escape hash characters # in strings being matched, e.g.: > > > string = re.escape('123#abc456') > > match = re.match('\d+', string) > > print match > > > > <_sre.SRE_Match object at 0x00A6A800> > > print match.group() > > > > 123 > > > > The correct result should be: > > > > 123456 > > > > I've tried to escape the hash symbol in the match string without > > > result. > > > > Any ideas? Is the answer something I overlooked in my lurching Python > > > schooling? > > > As you're not being clear on what you wanted, I'm just guessing this is > > what you wanted: > > > >>> s = '123#abc456' > > >>> re.match('\d+', re.sub('#\D+', '', s)).group() > > '123456' > > >>> s = '123#this is a comment and is ignored456' > > >>> re.match('\d+', re.sub('#\D+', '', s)).group() > > > '123456' > > Sorry I wasn't more clear. I positively appreciate your reply. It > provides half of what I'm hoping to learn. The hash character is > actually a desirable hook to identify a data entity in a scraping > routine I'm developing, but not a character I want in the scrubbed > data. > > In my application, the hash makes a string of alphanumeric characters > unique from other alphanumeric strings. The strings I'm looking for > are actually manually-entered identifiers, but a real machine-created > identifier shouldn't contain that hash character. The correct pattern > should be 'A1234509', but is instead often merely entered as '#12345' > when the first character, representing an alphabet sequence for the > month, and the last two characters, representing a two-digit year, can > be assumed. Identifying the hash character in a RegEx match is a way > of trapping the string and transforming it into its correct machine- > generated form. > > I'm surprised it's been so difficult to find an example of the hash > character in a RegEx string -- for exactly this type of situation, > since it's so common in the real world that people want to put a pound > symbol in front of a number. > > Thanks! By the way, other forms the strings can take in their manually created forms: A#12345 #1234509 Garbage in, garbage out -- I know. I wish I could tell the people entering the data how challenging it is to work with what they provide, but it is, after all, a screen-scraping routine. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to escape # hash character in regex match strings
On Jun 11, 2:01 am, Lie Ryan wrote: > 504cr...@gmail.com wrote: > > I've encountered a problem with my RegEx learning curve -- how to > > escape hash characters # in strings being matched, e.g.: > > string = re.escape('123#abc456') > match = re.match('\d+', string) > print match > > > <_sre.SRE_Match object at 0x00A6A800> > print match.group() > > > 123 > > > The correct result should be: > > > 123456 > > > I've tried to escape the hash symbol in the match string without > > result. > > > Any ideas? Is the answer something I overlooked in my lurching Python > > schooling? > > As you're not being clear on what you wanted, I'm just guessing this is > what you wanted: > > >>> s = '123#abc456' > >>> re.match('\d+', re.sub('#\D+', '', s)).group() > '123456' > >>> s = '123#this is a comment and is ignored456' > >>> re.match('\d+', re.sub('#\D+', '', s)).group() > > '123456'- Hide quoted text - > > - Show quoted text - Sorry I wasn't more clear. I positively appreciate your reply. It provides half of what I'm hoping to learn. The hash character is actually a desirable hook to identify a data entity in a scraping routine I'm developing, but not a character I want in the scrubbed data. In my application, the hash makes a string of alphanumeric characters unique from other alphanumeric strings. The strings I'm looking for are actually manually-entered identifiers, but a real machine-created identifier shouldn't contain that hash character. The correct pattern should be 'A1234509', but is instead often merely entered as '#12345' when the first character, representing an alphabet sequence for the month, and the last two characters, representing a two-digit year, can be assumed. Identifying the hash character in a RegEx match is a way of trapping the string and transforming it into its correct machine- generated form. Other patterns the strings can take in their manually-created form: A#12345 #1234509 Garbage in, garbage out -- I know. I wish I could tell the people entering the data how challenging it is to work with what they provide, but it is, after all, a screen-scraping routine. I'm surprised it's been so difficult to find an example of the hash character in a RegEx string -- for exactly this type of situation, since it's so common in the real world that people want to put a pound symbol in front of a number. Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: spawning a process using os.spawn
En Tue, 09 Jun 2009 15:38:53 -0300, rkmr...@gmail.com escribió: im spawning a script that runs for a long from a web app like this: os.spawnle(os.P_NOWAIT, "../bin/producenotify.py", "producenotify.py", "xx",os.environ) the script is spawned and it runs, but till it gets over i am not able to free the port that is used by the web app, or in other words i am not able to restart the web app. how do i spawn off a process and make it completely independent of the web app? Try subprocess.Popen instead, setting close_fds=True -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: preferring [] or () in list of error codes?
En Tue, 09 Jun 2009 05:02:33 -0300, Steven D'Aprano escribió: [...] As tuples are defined in Python, they quack like immutable lists, they walk like immutable lists, and they swim like immutable lists. Why shouldn't we treat them as immutable lists? Phillip Eby states that "Lists are intended to be homogeneous sequences, while tuples are heterogeneous data structures." (Notice the subtle shift there: lists are "intended", while tuples "are". But in fact, there's nothing to stop you from putting homogeneous data into a tuple, so Eby is wrong to say that tuples *are* heterogeneous.) Perhaps Eby intends lists to be homogeneous, perhaps Guido does too, but this is Python, where we vigorously defend the right to shoot ourselves in the foot. We strongly discourage class creators from trying to enforce their intentions by using private attributes, and even when we allow such a thing, the nature of Python is that nothing is truly private. Why should homogeneity and heterogeneity of lists and tuples be sacrosanct? Nothing stops me from putting hetereogeneous data into a list, or homogeneous data into a tuple, and there doesn't appear to be any ill- effects from doing so. Why give lose sleep over the alleged lack of purity? Yes - but in the past the distinction was very much stronger. I think that tuples didn't have *any* method until Python 2.0 -- so, even if someone could consider a tuple a "read-only list", the illusion disappeared as soon as she tried to write anything more complex that a[i]. Maybe tuples could quack like immutable lists, but they could not swim nor walk... With time, tuples gained more and more methods and are now very similar to lists - they even have an index() method (undocumented but obvious) which is absurd in the original context. Think of tuples as used in relational databases: there is no way in SQL to express the condition "search for this along all values in this tuple", because it usually doesn't make any sense at all (and probably, if it does make sense in a certain case, it's because the database is badly designed.) But *now*, you can express that operation in Python. So I'd say that *now*, the distinction between an "homogeneous container" vs "heterogeneous data structure" has vanished a lot, and it's hard to convince people that tuples aren't just immutable lists. That is, *I* would have used a list in this case: for delay in (0.01, 0.1, 0.5, 1, 2, 5, 10, 30, 60): do_something(delay) but I cannot find a *concrete* reason to support the assertion "list is better". So, for practical purposes, tuples act now as if they were immutable lists -- one should be aware of the different memory allocation strategies, but I see no other relevant differences. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
install older Python version parallel
Hi everybody, The situation: I wrote a GUI, based on Python, TkInter and Pmw. It runs perfectly fine with Python 2.4 (providing, TkInter and Pmw are installed). But it crashes with Python 2.6. I tried this on MacOSX11.4 and various Linux Distributions. Crashes occurs when I activate a Pmw.Diaog (I guess this is due to a bug in the installed blt package), also when I use setitems on Pmw.OptionMenu (I guess this is due to another package, associated with tcl/tk). The target: I have to get my GUI work under openSUSE 11.1 (x86_64). My plan: On my openSUSE 11.1 (x86_64), Python 2.6 is installed by default. I would like to know, how to install Python 2.4 along with TkInter and Pmw (and packages that are required by them), parallel to the existing Python 2.6. So that I do not break other software that depends on Python 2.6. If you can think of another plan, I would be also glad to discuss it. Cheers, Simon -- http://mail.python.org/mailman/listinfo/python-list
Re: .pth files and figuring out valid paths..
En Tue, 09 Jun 2009 20:30:06 -0300, rh0dium escribió: On Jun 9, 3:28 pm, Emile van Sebille wrote: On 6/9/2009 3:00 PM rh0dium said... > I have a .pth file which has some logic in it - but it isn't quite > enough... > It started with this.. > import os, site; site.addsitedir(os.path.join(os.environ["TECHROOT"], > "tools/python/modules")) > But that eventually evolved into.. > import os, site; site.addsitedir(os.path.join(os.environ.get > ("TECHROOT", "/home/tech"), "tools/python/modules")) Try it this way... import os, site smsc = os.environ.get("TECHROOT", "/home/tech") if not os.path.isdir(smsc): smsc = "/home/tech" site.addsitedir (os.path.join(smsc, "tools/python/Linux/%arch/lib/python2.5/site-packages")) No for .pth files this needs to be on a single line.. Try this instead: import os, site; smsc = os.environ.get("TECHROOT", ""); smsc = smsc if smsc and os.path.isdir(smsc) else "/home/tech"; site.addsitedir(...) > But now I want to check to make sure this directory exists or fall > back to "/home/tech". That was the point of the environ.get but what > if someone sets TECHROOT to /dev/null. Well that will break > things... I tried this but no go. Can someone help me out.. I'd add the directory unconditionally - then, when initializing the application, I'd check that the required modules can be imported, and inform the user of that specific problem if not. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: How to insert string in each match using RegEx iterator
On Jun 10, 10:13 am, Peter Otten <__pete...@web.de> wrote: > 504cr...@gmail.com wrote: > > I wonder if you (or anyone else) might attempt a different explanation > > for the use of the special sequence '\1' in the RegEx syntax. > > > The Python documentation explains: > > > \number > > Matches the contents of the group of the same number. Groups are > > numbered starting from 1. For example, (.+) \1 matches 'the the' or > > '55 55', but not 'the end' (note the space after the group). This > > special sequence can only be used to match one of the first 99 groups. > > If the first digit of number is 0, or number is 3 octal digits long, > > it will not be interpreted as a group match, but as the character with > > octal value number. Inside the '[' and ']' of a character class, all > > numeric escapes are treated as characters. > > > In practice, this appears to be the key to the key device to your > > clever solution: > > re.compile(r"(\d+)").sub(r"INSERT \1", string) > > > 'abc INSERT 123 def INSERT 456 ghi INSERT 789' > > re.compile(r"(\d+)").sub(r"INSERT ", string) > > > 'abc INSERT def INSERT ghi INSERT ' > > > I don't, however, precisely understand what is meant by "the group of > > the same number" -- or maybe I do, but it isn't explicit. Is this just > > a shorthand reference to match.group(1) -- if that were valid -- > > implying that the group match result is printed in the compile > > execution? > > If I understand you correctly you are right. Another example: > > >>> re.compile(r"([a-z]+)(\d+)").sub(r"number=\2 word=\1", "a1 zzz42") > > 'number=1 word=a number=42 word=zzz' > > For every match of "[a-z]+\d+" in the original string "\1" in > "number=\2 word=\1" is replaced with the actual match for "[a-z]+" and > "\2" is replaced with the actual match for "\d+". > > The result, e. g. "number=1 word=a", is then used to replace the actual > match for group 0, i. e. "a1" in the example. > > Peter- Hide quoted text - > > - Show quoted text - Wow! That is so cool. I had to process it for a little while to get it. >>> s = '111bbb333' >>> re.compile('(\d+)([b]+)(\d+)').sub(r'First string: \1 Second string: \2 >>> Third string: \3', s) 'First string: 111 Second string: bbb Third string: 333' MRI scans would no doubt reveal that people who attain a mastery of RegEx expressions must have highly developed areas of the brain. I wonder where the RegEx part of the brain might be located. That was a really clever teaching device. I really appreciate you taking the time to post it, Peter. I'm definitely getting a schooling on this list. Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: preferring [] or () in list of error codes?
> [In this tuple] >dodge_city = (1781, 1870, 1823) >(population, feet_above_sea_level, establishment_year) = dodge_city > each index in the sequence implies something very > different about each value. The semantic meaning > of each index is *more* than just the position in > the sequence; it matters *for interpreting that > component*, and that component would not mean the > same thing in a different index position. A tuple > is the right choice, for that reason. I think I would have difficulty holding a position that this should not be a class (or equivalent via namedtuple()) or a dict. It seems to me like a case could be made that there are far more situations where it makes sense to use tuples as immutable sequences than as objects whose attributes are named implicitly by an index. This dodge_city definitely does not seem to me like a good candidate for a plain tuple. -- http://mail.python.org/mailman/listinfo/python-list
Re: install older Python version parallel
Hi, I'm working on a project of mine that does creates python installation under /opt so they can be installed side by side with a system installed one. There's a web page: http://pyvm.sourceforge.net/ With links to the newest build plus all teh accompaining unitests. But you can use the sources rpm to create a version with python 2.4 (see http://download.opensuse.org/repositories/home:/cavallo71:/opt-python- interpreters/openSUSE_11.1/src/) Let me know if you need more help, Regards, Antoino On Thursday 11 June 2009 15:50:06 S. Dornseifer wrote: > Hi everybody, > > The situation: > I wrote a GUI, based on Python, TkInter and Pmw. > It runs perfectly fine with Python 2.4 (providing, TkInter and Pmw are > installed). But it crashes with Python 2.6. I tried this on MacOSX11.4 > and various Linux Distributions. > Crashes occurs when I activate a Pmw.Diaog (I guess this is due to a bug > in the installed blt package), also when I use setitems on > Pmw.OptionMenu (I guess this is due to another package, associated with > tcl/tk). > > The target: > I have to get my GUI work under openSUSE 11.1 (x86_64). > > My plan: > On my openSUSE 11.1 (x86_64), Python 2.6 is installed by default. > I would like to know, how to install Python 2.4 along with TkInter and > Pmw (and packages that are required by them), parallel to the existing > Python 2.6. So that I do not break other software that depends on Python > 2.6. > > If you can think of another plan, I would be also glad to discuss it. > > Cheers, > Simon -- http://mail.python.org/mailman/listinfo/python-list
Re: xml.dom.minidom losing the XML document type attribute
On Thu, Jun 11, 2009 at 9:20 AM, Johannes Bauer wrote: > Well, I'm not speaking about my software :-) Actually it's Gnucash which > complains if the tag is not explicitly set. This is because they > appearently had a ancient version which did not specify the charset, but > used a different one than UTF-8. Kind of annoying, but fixing my XML > output seems to be easier than convincing the Gnucash people to change > their software :-) from the GnuCash web page: How can you help? Testing: Test it and help us discover all bugs that might show up in there. Please enter each and every bug into bugzilla. Translating: The new release comes with some new translation strings. If you consider contributing a translation, we invite you to test this release already. A string freeze will be announced in one of the later 2.3.x releases. Please check http://wiki.gnucash.org/wiki/Translation_Status for updates on this. We would like to encourage people to test this and any further releases as much as possible and submit bug reports in order that we can polish GnuCash to be as stable as possible for the 2.4.0 release in a few weeks. Then post any bugs you find to bugzilla (http://bugzilla.gnome.org/enter_bug.cgi?product=GnuCash) -- http://mail.python.org/mailman/listinfo/python-list
Changing Hash Values Across Versions
How stable should the implementation of, for example, a string's hash across different Python versions? Is it defined that hash("foo") will return the same value for Python 2.5.1, 2.6.1 and 2.6.2? Thanks, Phil -- http://mail.python.org/mailman/listinfo/python-list
Issues download 2.4 python (for windows) msi
Hi I am having issues downloading any msi prior to the 3.X version. All of them appear to be truncated. My old scripts dont compile on the later versions. Can someone please fix this ASAP ? Thanks -Praveen -- http://mail.python.org/mailman/listinfo/python-list
Re: Convert integer to fixed length binary string
On Jun 11, 4:29 am, casebash wrote: > Sorry, I didn't quite make it clear. The issue isn't about limiting > the length (as I won't be using integers bigger than this). The > problem is that sometimes the output is shorter. This works also: >>> bin(15)[2:].zfill(32) '' -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing Hash Values Across Versions
Phil Thompson wrote: How stable should the implementation of, for example, a string's hash across different Python versions? Is it defined that hash("foo") will return the same value for Python 2.5.1, 2.6.1 and 2.6.2? Thanks, Phil Pretty certain that A.B.* will always hash the same (unacceptable change as a bugfix), and that there has been no recent need to change the string hash (it gets a _lot_ of use), so in practice I suspect that 2.3.* through 2.8.* all produce the same string hashes. However, the language doesn't guarantee a lot about the value, and I would not be surprised to see a non-CPython implementation use a different hash. Generally it is a good idea to keep the result of hash computations inside the program, and off the persistant store, so I'd want a good excuse to use the hash "outside." --Scott David Daniels scott.dani...@acm.org -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing Hash Values Across Versions
En Thu, 11 Jun 2009 13:56:17 -0300, Phil Thompson escribió: How stable should the implementation of, for example, a string's hash across different Python versions? I cannot find any such guarantee in the documentation: http://docs.python.org/reference/datamodel.html Is it defined that hash("foo") will return the same value for Python 2.5.1, 2.6.1 and 2.6.2? I've tested and it does, all versions since Python 1.5 up to 3.0.1. But you should not rely on that. What do you want to do exactly? -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: itertools.intersect?
Jack Diederich wrote: On Thu, Jun 11, 2009 at 12:03 AM, David M. Wilson wrote: [snip] I found my answer: Python 2.6 introduces heap.merge(), which is designed exactly for this. Thanks, I knew Raymond added something like that but I couldn't find it in itertools. That said .. it doesn't help. Aside, heapq.merge fits better in itertools (it uses heaps internally but doesn't require them to be passed in). The other function that almost helps is itertools.groupby() and it doesn't return an iterator so is an odd fit for itertools. More specifically (and less curmudgeonly) heap.merge doesn't help for this particular case because you can't tell where the merged values came from. You want all the iterators to yield the same thing at once but heapq.merge muddles them all together (but in an orderly way!). Unless I'm reading your tokenizer func wrong it can yield the same value many times in a row. If that happens you don't know if four "The"s are once each from four iterators or four times from one. David is looking to intersect sorted lists of document numbers with duplicates removed in order to find documents that contain worda and wordb and wordc ... . But you are right that duplicate are a possible fly in the ointment to be removed before merging. -- http://mail.python.org/mailman/listinfo/python-list
Alter list items within loop
Can someone please explain what is happening in the output below? The number 3 never gets printed. Does Python make a copy of a list before it iterates through it?: >>> e = range(1,5) >>> for i in e: print i if i == 2 : e.remove(i) 1 2 4 >>> e [1, 3, 4] -- http://mail.python.org/mailman/listinfo/python-list
Re: Restart the interactive python shell like in IDLE
Chris Rebert wrote: On Wed, Jun 10, 2009 at 12:01 PM, Matt Burson wrote: Is there a way to reproduce the behavior of IDLE's restart shell ability by using a function? I thought there would be since you can exit python by executing the simple quit() function I thought there would be an equally simple function name something like restart(). I'd prefer something like this as opposed to having to exit the shell and then start it up again to refresh it. I believe IDLE itself implements the "restart" capability by killing and re-launching its Python interpreter subprocess, so it's not like it's using some hidden capability of Python to accomplish this. Is doing Ctrl+D, up-arrow, Enter really that hard? It's even fewer keystrokes than "restart()"... This will do part of what you want: >>> a=1 >>> b=1 >>> globals().clear() >>> a Traceback (most recent call last): File "", line 1, in NameError: name 'a' is not defined That will not reset sys.modules, which is the only other thing I can imagine being worried about. The main reason IDLE has a restart is so that when you run a file after editing, you can be sure the behavior you see is what you get when running the file without IDLE, with a fresh interpreter. Another use of refresh is when creating example interactive sessions for doctest or book examples. Again, one wants to make sure that the example does not depend on previous entries not included in the example. For ordinary interactive exploration, refresh is seldom needed. tjr -- http://mail.python.org/mailman/listinfo/python-list
Re: zipfile doesn't compress very good, are there other solutions ?
Peter Otten wrote: Stef Mientki wrote: I packed all sources with zipfile, but the compression doesn't seem to be very good. If you don't specify the compression, the files are not compressed at all. Just in case you didn't know... .. and would you be willing to tell me how I could set the compression ( at maximum) ? thanks, Stef Mientki Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Alter list items within loop
On 6/11/2009 11:54 AM Brendan said... Can someone please explain what is happening in the output below? you delete e[2] before displaying it. The number 3 never gets printed. Does Python make a copy of a list before it iterates through it?: No. Mods to a list while passing it is generally not a good idea. Sometimes passing the list backwards works. Emile e = range(1,5) for i in e: print i if i == 2 : e.remove(i) 1 2 4 e [1, 3, 4] -- http://mail.python.org/mailman/listinfo/python-list
Re: zipfile doesn't compress very good, are there other solutions ?
On Thu, Jun 11, 2009 at 12:11 PM, Stef Mientki wrote: > Peter Otten wrote: >> Stef Mientki wrote: >>> I packed all sources with zipfile, >>> but the compression doesn't seem to be very good. >>> >> >> If you don't specify the compression, the files are not compressed at all. >> Just in case you didn't know... >> > > .. and would you be willing to tell me how I could set the compression ( at > maximum) ? If you had glanced @ the docs for 30 seconds: import zipfile z = zipfile.ZipFile(dest_file, "w", zipfile.ZIP_DEFLATED) Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Alter list items within loop
On 2009-06-11, Brendan wrote: > Can someone please explain what is happening in the output below? The > number 3 never gets printed. Does Python make a copy of a list before > it iterates through it?: You can see what is happening by printing the list as you work through the loop: >>> e = range(1,5) >>> for i in e: ... print e ... print i ... if i == 2 : ... e.remove(i) ... [1, 2, 3, 4] 1 [1, 2, 3, 4] 2 [1, 3, 4] 4 first loop: i = 0 e[i] = e[0] = 1 second loop i = 1 e[i] = e[1] = 2 third loop i = 2 e[i] = e[2] = 4 > number 3 never gets printed. Does Python make a copy of a list before > it iterates through it?: No, complex types are passed by reference unless explicity copied. You can do what you want by making an explicit copy before entering the loop: >>> e = range(1,5) >>> for i in e[:]: ... print e ... print i ... if i == 2 : ... e.remove(i) ... [1, 2, 3, 4] 1 [1, 2, 3, 4] 2 [1, 3, 4] 3 [1, 3, 4] 4 -- http://mail.python.org/mailman/listinfo/python-list
Re: zipfile doesn't compress very good, are there other solutions?
Stef Mientki wrote: > Peter Otten wrote: >> Stef Mientki wrote: >> >> >>> I packed all sources with zipfile, >>> but the compression doesn't seem to be very good. >>> >> >> If you don't specify the compression, the files are not compressed at >> all. Just in case you didn't know... >> > .. and would you be willing to tell me how I could set the compression ( > at maximum) ? According to the documentation (hint, hint) there is only on and off. zipfile.ZipFile(filename, "w", compression=zipfile.ZIP_DEFLATED) Peter -- http://mail.python.org/mailman/listinfo/python-list
Blogger Widget web app
Please give me directions on where to start researching for answers. I probably can do the javascript, but I don't know where to start on the Python. 1. Wife has a blogger blog and wants a widget to embed in the posts. 2. Widget will have a form that readers can enter their name and url. 3. Widget also lists in numeric order the name and url of all the readers who signed up in that post. 4. Son has an old PC he turned into a server, and installed Windows IIS on it. What I need to do is to write the cgi scripts to send a list of names and urls for each post on load, validate the input from the form and store the results per blog post. If you have seen Mr. Linky, that's what I'm tring to mimic. This will only be for her blog, so she can have a faster load time and less down time. I have NO intention to build this for more than her. So very simple and easy is the key. I've read the mailing list, I just got the books, Programming Python by Mark Lutz and and Python Web Programming by Steve Holden. There is so much great info I don't know where to start or what I need to start with. Thanks for any pointers. Steve Oldner -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing Hash Values Across Versions
Phil Thompson schrieb: > How stable should the implementation of, for example, a string's hash > across different Python versions? > > Is it defined that hash("foo") will return the same value for Python 2.5.1, > 2.6.1 and 2.6.2? The hash of an object is an internal implementation detail. The hash() value of an object isn't even stable for the one version of Python. It depends on the architecture (32 vs. 64bit OS, maybe even big vs. little endian). The hash sums of objects like classes may and most likely do change when you restart the interpreter. Python 2.5.4 (r254:67916, Feb 4 2009, 14:25:49) [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import struct; struct.calcsize("P") * 8 32 >>> hash("a") -468864544 Python 2.5.4 (r254:67916, Apr 4 2009, 17:56:17) [GCC 4.3.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import struct; struct.calcsize("P") * 8 64 >>> hash("a") 12416037344 $ python2.5 -c "import os; print hash(os)" 140519221134232 $ python2.5 -c "import os; print hash(os)" 139731515618200 $ python2.5 -c "import os; print hash(os)" 140460433757080 $ python2.5 -c "import os; print hash(os)" 140707220554648 -- http://mail.python.org/mailman/listinfo/python-list
EXTENDED: OSCON 2009 early bird (June 23)
Registration is now open for the O'Reilly Open Source Convention (OSCON). OSCON 2009 will be July 20-24 in San Jose, California. Early registration has been extended and now ends June 23. Use the special discount code 'os09pgm' for an extra 15% off. For more information: http://conferences.oreilly.com/oscon -- Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ "If you don't know what your program is supposed to do, you'd better not start writing it." --Dijkstra -- http://mail.python.org/mailman/listinfo/python-list
Unhandled exception in thread
(I'll ask you to bear with the somewhat vague description in advance - I'm a noob in all respects here, with regard to python and the mailing list) I'm getting some error reports sometimes when I quit a script, I've been working on. The reports are extremely uninformative, and I do not notice anything wrong in the script's behaviour, which makes the error very hard to pin down. I'm therefore looking for advice on a way that I can investigate the error. Here's the deal. The script in question is a screen scraping thing which downloads and parses the html in the background using a separate thread (the 'thread' module), while the main program serves up the data to the user, allowing some modicum of interaction. Sometimes, not always (though I cannot see a pattern), when I quit the script, the following error message is printed: Unhandled exception in thread started by Error in sys.excepthook: Original exception was: And that's it! Seems like there's some information missing? What is the error in sys.excepthook? What was the original exception? And where? Any suggestions on how to proceed? - Mads -- http://mail.python.org/mailman/listinfo/python-list
Voronoi diagram algorithm (Fortune’s sweepline)
Hi, I am implementing Voronoi diagram to find out the nearest location in a map visually. Right now I want to do this using integer coordinates (x,y) only in a canvas. Problem is- I am really confused about this algorithm. I read the Computational Geometry book, few more theory on Fortune's algorithm. And I am really confused now. It seems very complex to me when I am going for coding. Please advice me very simple implementation of voronoi diagram (given coordinates). Please advice me simple python code preferably without- hash, multi-threading, Delaunay Traingulation, fancy colors etc (which are confusing). Isn't it possible to implement Voronoi diagram using Fortune's algorithm without multithreading or hash map? Thanks in advance. -- http://mail.python.org/mailman/listinfo/python-list
Re: itertools.intersect?
David Wilson writes: > Hi, > > During a fun coding session yesterday, I came across a problem that I > thought was already solved by itertools, but on investigation it seems > it isn't. > > The problem is simple: given one or more ordered sequences, return > only the objects that appear in each sequence, without reading the > whole set into memory. This is basically an SQL many-many join. As it is a nice little problem I tried to find a solution today. FWIW, here it is (tested extensively only on the example below :): def intersect(iterables): nexts = [iter(iterable).next for iterable in iterables] v = [next() for next in nexts] while True: for i in xrange(1, len(v)): while v[0] > v[i]: v[i] = nexts[i]() if v[0] < v[i]: break else: yield v[0] v[0] = nexts[0]() >>> list(intersect([[1, 100, 142, 322, 12312], ... [2, 100, 101, 322, 1221], ... [100, 142, 322, 956, 1222]])) [100, 322] -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing Hash Values Across Versions
On Jun 11, 8:55 pm, Christian Heimes wrote: > Phil Thompson schrieb: > > > How stable should the implementation of, for example, a string's hash > > across different Python versions? > > > Is it defined that hash("foo") will return the same value for Python 2.5.1, > > 2.6.1 and 2.6.2? > > The hash of an object is an internal implementation detail. The hash() > value of an object isn't even stable for the one version of Python. It > depends on the architecture (32 vs. 64bit OS, maybe even big vs. little > endian). The hash sums of objects like classes may and most likely do > change when you restart the interpreter. What he said. I remember at least one recent hash change: the algorithm for computing the hash of a long is different in Python 2.5.x and Python 2.6.x. Mark -- http://mail.python.org/mailman/listinfo/python-list
reading from file
Hello all, In a text file aword.txt, there is a string: "\xea\xe0\xea+\xef\xee\xe7\xe2\xee\xed\xe8\xf2\xfc". There is a first script: f = open ("aword.txt", "r") for line in f: print chardet.detect(line) b = line.decode('cp1251') print b _RESULT_ {'confidence': 1.0, 'encoding': 'ascii'} \xea\xe0\xea+\xef\xee\xe7\xe2\xee\xed\xe8\xf2\xfc There is a second script: line = "\xea\xe0\xea+\xef\xee\xe7\xe2\xee\xed\xe8\xf2\xfc" print chardet.detect(line) b = line.decode('cp1251') print b _RESULT_ {'confidence': 0.98999, 'encoding': 'windows-1251'} как+позвонить Why is reading from a file into a string variable is defined as ascii, but when it is clearly defined in the script is defined as cp1251. How do I solve this problem. -- Only one 0_o -- http://mail.python.org/mailman/listinfo/python-list
Re: Voronoi diagram algorithm (Fortune’s sweepline )
Captain___nemo: > Isn't it possible to implement Voronoi diagram using Fortune's > algorithm without multithreading or hash map? Multi-threading isn't part of Fortune's algorithm. Multi-threading can be confusing, but it's better for you to learn to feel at home using hash maps (named dicts in Python), because they are both essential in computer science and in Python. Ask if you need some help regarding dicts and sets. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list
Re: zipfile doesn't compress very good, are there other solutions?
Peter Otten wrote: Stef Mientki wrote: Peter Otten wrote: Stef Mientki wrote: I packed all sources with zipfile, but the compression doesn't seem to be very good. If you don't specify the compression, the files are not compressed at all. Just in case you didn't know... .. and would you be willing to tell me how I could set the compression ( at maximum) ? According to the documentation (hint, hint) there is only on and off. zipfile.ZipFile(filename, "w", compression=zipfile.ZIP_DEFLATED) sorry guys I made a mistake, I did read the doc, but as there was no default value metioned, I did a few tests, and I made a mistake with these experiments. Indeed the compression is much better now, just 15% larger than 7zip, which is quit acceptable. thanks again, Stef Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: reading from file
On Jun 11, 4:24 pm, Sydoruk Yaroslav wrote: > Hello all, > > In a text file aword.txt, there is a string: > "\xea\xe0\xea+\xef\xee\xe7\xe2\xee\xed\xe8\xf2\xfc". > > There is a first script: > f = open ("aword.txt", "r") > for line in f: > print chardet.detect(line) > b = line.decode('cp1251') > print b > > _RESULT_ > {'confidence': 1.0, 'encoding': 'ascii'} > \xea\xe0\xea+\xef\xee\xe7\xe2\xee\xed\xe8\xf2\xfc > > There is a second script: > line = "\xea\xe0\xea+\xef\xee\xe7\xe2\xee\xed\xe8\xf2\xfc" > print chardet.detect(line) > b = line.decode('cp1251') > print b > > _RESULT_ > {'confidence': 0.98999, 'encoding': 'windows-1251'} > как+позвонить > > Why is reading from a file into a string variable is defined as ascii, > but when it is clearly defined in the script is defined as cp1251. > How do I solve this problem. > > -- > Only one 0_o Is the string in your text file literally "\xea\xe0\xea+\xef\xee \xe7\xe2\xee\xed\xe8\xf2\xfc" as "plain text?" My assumption is that when you're reading that in, Python is interpreting each byte as an ASCII value (and rightfully so) rather than the corresponding '\x' escapes. As an experiment: (t)j...@marvin:~/t$ cat test.py import chardet s = "\xea\xe0\xea+\xef\xee\xe7\xe2\xee\xed\xe8\xf2\xfc" with open('test.txt', 'w') as f: print >>f, s print chardet.detect(open('test.txt').read()) (t)j...@marvin:~/t$ python test.py {'confidence': 0.98999, 'encoding': 'windows-1251'} (t)j...@marvin:~/t$ HTH, Jeff mcjeff.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Alter list items within loop
Tim Harig wrote: >> number 3 never gets printed. Does Python make a copy of a list before >> it iterates through it?: > > No, complex types are passed by reference unless explicity copied. *All* types are passed by reference unless explicitly copied. Python does make special cases for simple and complex types. -- http://mail.python.org/mailman/listinfo/python-list
Re: zipfile doesn't compress very good, are there other solutions?
On Thu, Jun 11, 2009 at 1:41 PM, Stef Mientki wrote: > Peter Otten wrote: >> Stef Mientki wrote: >>> Peter Otten wrote: Stef Mientki wrote: > I packed all sources with zipfile, > but the compression doesn't seem to be very good. > If you don't specify the compression, the files are not compressed at all. Just in case you didn't know... >>> >>> .. and would you be willing to tell me how I could set the compression ( >>> at maximum) ? >>> >> >> According to the documentation (hint, hint) there is only on and off. >> >> zipfile.ZipFile(filename, "w", compression=zipfile.ZIP_DEFLATED) >> >> > > sorry guys I made a mistake, > I did read the doc, but as there was no default value metioned, Erm... class zipfile.ZipFile(file[, mode[, compression[, allowZip64]]]) Open a ZIP file [...] compression is the ZIP compression method to use when writing the archive, and should be ZIP_STORED or ZIP_DEFLATED; [...] The default is ZIP_STORED. [...] Though I admit the docs could definitely do with having "compression=ZIP_STORED" in the signature part of the doc. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Voronoi diagram algorithm (Fortune’s sweep line)
On 2009-06-11 14:56, Captain___nemo wrote: Hi, I am implementing Voronoi diagram to find out the nearest location in a map visually. Right now I want to do this using integer coordinates (x,y) only in a canvas. Problem is- I am really confused about this algorithm. I read the Computational Geometry book, few more theory on Fortune's algorithm. And I am really confused now. It seems very complex to me when I am going for coding. Yup. It is complex. Please advice me very simple implementation of voronoi diagram (given coordinates). Please advice me simple python code preferably without- hash, multi-threading, Delaunay Traingulation, You can't really do the Voronoi diagram without Delaunay Triangulation. They are two ways of looking at the same thing. fancy colors etc (which are confusing). You can see a mild modification of Fortune's original C code here: http://svn.scipy.org/svn/scikits/trunk/delaunay/scikits/delaunay/VoronoiDiagramGenerator.cpp Isn't it possible to implement Voronoi diagram using Fortune's algorithm without multithreading or hash map? It's possible to do it without multithreading, of course, but Fortune's algorithm does require some sophisticated data structures. Computational geometry is rarely a simple matter. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
ANN: WHIFF += Flash chart widget support
WHIFF.0.3 adds amChart Flash based Charts. The amChart chart widgets [ http://www.amcharts.com ] provide a sophisticated methodology for creating beautiful and interactive statistical charts using Adobe Flash plug-in technology. The WHIFF.0.3 release adds extensive support for embedding amCharts charts in web pages under the ./demo/amcharts demo directory area. Please look at chart examples explained in the documentation at http://aaron.oirt.rutgers.edu/myapp/amcharts/doc Also please try out the "disk usage analysis" pie chart drill down demo at http://aaron.oirt.rutgers.edu/myapp/amcharts/diskUsage Below is more information about WHIFF: === WHIFF -- WSGI/HTTP INTEGRATED FILESYSTEM FRAMES WHIFF is an infrastructure for easily building complex Python/WSGI Web applications by combining smaller and simpler WSGI components organized within file system trees. To DOWNLOAD WHIFF go to the WHIFF project information page at http://sourceforge.net/projects/whiff and follow the download instructions. To GET THE LATEST WHIFF clone the WHIFF Mercurial repository located at http://aaron.oirt.rutgers.edu/cgi-bin/whiffRepo.cgi. To READ ABOUT WHIFF view the WHIFF documentation at http://aaron.oirt.rutgers.edu/myapp/docs/W.intro. To PLAY WITH WHIFF try the demos listed in the demos page at http://aaron.oirt.rutgers.edu/myapp/docs/W1300.testAndDemo. Why WHIFF? == WHIFF (WSGI HTTP Integrated Filesystem Frames) is intended to make it easier to create, deploy, and maintain large and complex Python based WSGI Web applications. I created WHIFF to address complexity issues I encounter when creating and fixing sophisticated Web applications which include complex database interactions and dynamic features such as AJAX (Asynchronous JavaScript and XML). The primary tools which reduce complexity are an infrastructure for managing web application name spaces, a configuration template language for wiring named components into an application, and an applications programmer interface for accessing named components from Python and javascript modules. All supporting conventions and tools offered by WHIFF are optional. WHIFF is designed to work well with other modules conformant to the WSGI (Web Service Gateway Interface) standard. Developers and designers are free to use those WHIFF tools that work for them and ignore or replace the others. WHIFF does not provide a "packaged cake mix" for baking a web application. Instead WHIFF is designed to provide a set of ingredients which can be easily combined to make web applications (with no need to refine your own sugar or mill your own wheat). I hope you like it. -- Aaron Watters === less is more -- http://mail.python.org/mailman/listinfo/python-list
Re: reading from file
Jeff McNeil wrote: > Is the string in your text file literally "\xea\xe0\xea+\xef\xee > \xe7\xe2\xee\xed\xe8\xf2\xfc" as "plain text?" My assumption is that > when you're reading that in, Python is interpreting each byte as an > ASCII value (and rightfully so) rather than the corresponding '\x' > escapes. > > As an experiment: > > (t)j...@marvin:~/t$ cat test.py > import chardet > > s = "\xea\xe0\xea+\xef\xee\xe7\xe2\xee\xed\xe8\xf2\xfc" > with open('test.txt', 'w') as f: >print >>f, s > > print chardet.detect(open('test.txt').read()) > (t)j...@marvin:~/t$ python test.py > {'confidence': 0.98999, 'encoding': 'windows-1251'} > (t)j...@marvin:~/t$ > > HTH, > > Jeff > mcjeff.blogspot.com Thank you for your reply. You are right, Python reads data form the file in bytes and all data in this case is ASCII I solved the problem, just added line = line.decode('string_escape') f = open ("aword.txt", "r") for line in f: line = line.decode('string_escape') print chardet.detect(line) b = line.decode('cp1251') print b -- Only one 0_o -- http://mail.python.org/mailman/listinfo/python-list
Re: Alter list items within loop
On 2009-06-11, Duncan Booth wrote: > Tim Harig wrote: >>> number 3 never gets printed. Does Python make a copy of a list before >>> it iterates through it?: >> No, complex types are passed by reference unless explicity copied. > *All* types are passed by reference unless explicitly copied. Python does > make special cases for simple and complex types. That is technically true; but, you will not have this issue with simple singlular data types. Technically the difference as to whether you will have this problem depends on whether or not an object is mutable. Simple objects (numbers and strings) are all immutable. Since this issue revolves around changing objects in place, it cannot arise with immutable objects. I am not always conscous of whether I am working with objects that are mutable or immutable; but, I am generally concious of the general complexity of the object. Whenever I am working with objects that are complex, I am reminded to watch out for mutability issues. So, while it is not totally correct to think of it this way, I find it an easier guideline to follow. -- http://mail.python.org/mailman/listinfo/python-list
Re: Voronoi diagram algorithm (Fortune’s sweepline )
Robert Kern: > You can see a mild modification of Fortune's original C code here: > > http://svn.scipy.org/svn/scikits/trunk/delaunay/scikits/delaunay/Voro... That's C++; C++ makes simple things hard and hard things possible :-) In Python that code may become much shorter (and slower). Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list
Re: itertools.intersect?
On Thu, Jun 11, 2009 at 2:54 PM, Terry Reedy wrote: > Jack Diederich wrote: >> >> On Thu, Jun 11, 2009 at 12:03 AM, David M. Wilson wrote: >> [snip] >>> >>> I found my answer: Python 2.6 introduces heap.merge(), which is >>> designed exactly for this. >> >> Thanks, I knew Raymond added something like that but I couldn't find >> it in itertools. >> That said .. it doesn't help. Aside, heapq.merge fits better in >> itertools (it uses heaps internally but doesn't require them to be >> passed in). The other function that almost helps is >> itertools.groupby() and it doesn't return an iterator so is an odd fit >> for itertools. >> >> More specifically (and less curmudgeonly) heap.merge doesn't help for >> this particular case because you can't tell where the merged values >> came from. You want all the iterators to yield the same thing at once >> but heapq.merge muddles them all together (but in an orderly way!). >> Unless I'm reading your tokenizer func wrong it can yield the same >> value many times in a row. If that happens you don't know if four >> "The"s are once each from four iterators or four times from one. > > David is looking to intersect sorted lists of document numbers with > duplicates removed in order to find documents that contain worda and wordb > and wordc ... . But you are right that duplicate are a possible fly in the > ointment to be removed before merging. Ah, in that case the heap.merge solution is both useful and succinct: import heapq import itertools def intersect(its): source = heapq.merge(*its) while True: sames = [source.next()] sames.extend(itertools.takewhile(lambda v:v == sames[0], source)) if len(sames) == len(its): yield sames[0] return -Jack -- http://mail.python.org/mailman/listinfo/python-list
Re: Unhandled exception in thread
Mads Michelsen wrote: ...I'm getting some error reports ... looking for advice on a way that I can investigate the error The script ... downloads and... > in the background using a separate thread (the 'thread' module), > Sometimes, when I quit the script, the following error message is printed: Unhandled exception in thread started by Error in sys.excepthook: Original exception was: And that's it! Seems like there's some information missing? What is the error in sys.excepthook? What was the original exception? And where? Any suggestions on how to proceed? (1) Generally, use Threading rather than thread. It says so at the top of the thread docs. (2) Sounds like it might be a timing problem while shutting down. Do you tell the thread to stop, and then stop it? That would generally be the best architecture. (3) From above, what you might want is something like: import Threading import Queue ... def code(commands, replies): for element in iter(commands.get, None): # None: stop signal time.sleep(1) # just here to represent processing time. results = repr(element) # and simulating some results" replies.put(results) replies.put(None) # superfluous indication of end ... def main(): ... commands = queue.Queue() replies = queue.Queue() worker = threading.Thread(target=code, args=(commands, replies)) worker.daemon = True # or use .setDaemon(True) for older for n in range(5): commands.put(n) print 'first couple:', results.get(), results.get() commands.put(None) # tell the worker to stop. worker.join() # wait for the worker to shut down. Making the worker a daemon, telling it to stop with a command, and waiting for it to finish with a .join are all belts-and-suspenders related to the thread shutdown, but it sounds like you have a thread shutdown issue. --Scott David Daniels Scott.Daniels#Acm.Org -- http://mail.python.org/mailman/listinfo/python-list
Re: unladen swallow: python and llvm
Stefan Behnel wrote: > Nick Craig-Wood wrote: > > Luis M González wrote: > >> I am very excited by this project (as well as by pypy) and I read all > >> their plan, which looks quite practical and impressive. > >> But I must confess that I can't understand why LLVM is so great for > >> python and why it will make a difference. > > > > CPython uses a C compiler to compile the python code (written in C) > > into native machine code. > > That would be Cython: compile Python code to (optimised) C code and then > run a C compiler over that to get native machine code. Actually I meant "compile the python source code" - sorry if I wasn't clear! -- Nick Craig-Wood -- http://www.craig-wood.com/nick -- http://mail.python.org/mailman/listinfo/python-list
help with errror
could some one explain this error to me and possibly how to fix it? Traceback (most recent call last): File "C:\Users\brandon\workspace\tanner's workshop\tanner's workshop\src\WxGlade.py", line 458, in Edit = MyDialog2(None, -1, "") File "C:\Users\brandon\workspace\tanner's workshop\tanner's workshop\src\WxGlade.py", line 27, in __init__ self.Name = wx.StaticText(self.panel_41, -1, "Name") File "C:\Python26\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 8522, in SetName return _core_.Window_SetName(*args, **kwargs) TypeError: String or Unicode type required _ Hotmail® has ever-growing storage! Don’t worry about storage limits. http://windowslive.com/Tutorial/Hotmail/Storage?ocid=TXT_TAGLM_WL_HM_Tutorial_Storage_062009-- http://mail.python.org/mailman/listinfo/python-list
Re: Voronoi diagram algorithm (Fortune’s sweep line)
On 2009-06-11 16:16, Bearophile wrote: Robert Kern: You can see a mild modification of Fortune's original C code here: http://svn.scipy.org/svn/scikits/trunk/delaunay/scikits/delaunay/Voro... That's C++; C++ makes simple things hard and hard things possible :-) The actual algorithm implementation is just Fortune's original C code. The C++ is mostly just wrapped around it. In Python that code may become much shorter (and slower). Doubtful. The algorithm would mostly be the same. The data structure doesn't really translate to Python very well; it's very pointer-based. Yes, I've tried. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
what does "lost sys.stdin" error mean?
I have a very simple program from the first chapter of a book on python 3 (I'm a novice). I called the program tmp.py and the data input file is sum.dat (just a list of numbers, 1 per line). When I type into my command shell "tmp.py < sum.dat" I get an error from "line=input()" -- that's line 7 from the code below. The exact error message is: RuntimeError: input(): lost sys.stdin. print("Type integers, each followed by Enter; or ^D or ^Z to finish") total=0 count=0 while True: try: line=input() if line: number=int(line) total += number count += 1 print("number =",number) except ValueError as err: print(err) continue except EOFError: break if count: print("count =", count, "total =", total, "mean =", total/ count) -- http://mail.python.org/mailman/listinfo/python-list
Re: preferring [] or () in list of error codes?
Gunter Henriksen writes: > I think I would have difficulty holding a position that this should > not be a class (or equivalent via namedtuple()) or a dict. It seems to > me like a case could be made that there are far more situations where > it makes sense to use tuples as immutable sequences than as objects > whose attributes are named implicitly by an index. This dodge_city > definitely does not seem to me like a good candidate for a plain > tuple. It's a fair cop. (I only meant that for this example a tuple was superior to a list, but you're right that a dict would be better than either.) Try, then, this tuple: event_timestamp = (2009, 06, 04, 05, 02, 03) (year, month, day, hour, minute, second) = event_timestamp A list would be wrong for this value, because each position in the sequence has a specific meaning beyond its mere sequential position. Yet it also matters to the reader that these items are in a specific sequence, since that's a fairly standard ordering for those items. In this case, a tuple is superior to a list because it correctly conveys the semantic meaning of the overall value: the items must retain their sequential order to have the intended meaning, and to alter any one of them is conceptually to create a new timestamp value. -- \ “[The RIAA] have the patience to keep stomping. They're playing | `\ whack-a-mole with an infinite supply of tokens.” —kennon, | _o__) http://kuro5hin.org/ | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: Voronoi diagram algorithm (Fortune’s sweepline )
Found this python implementation: http://www.oxfish.com/python/voronoi.py >From what I understand, not my area of expertise, it would seem to be correct. -- http://mail.python.org/mailman/listinfo/python-list
Re: Voronoi diagram algorithm (Fortune’s sweepline )
Found this python implementation: http://www.oxfish.com/python/voronoi.py >From what I understand, not my area of expertise, it would seem to be correct. -- http://mail.python.org/mailman/listinfo/python-list
Re: Voronoi diagram algorithm (Fortune’s sweepline )
Found this python implementation: http://www.oxfish.com/python/voronoi.py >From what I understand, not my area of expertise, it would seem to be correct. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to escape # hash character in regex match strings
On Thu, 11 Jun 2009 15:22:44 +0100, Brian D wrote: I'm surprised it's been so difficult to find an example of the hash character in a RegEx string -- for exactly this type of situation, since it's so common in the real world that people want to put a pound symbol in front of a number. It's a character with no special meaning to the regex engine, so I'm not in the least surprised that there aren't many examples containing it. You could just as validly claim that there aren't many examples involving the letter 'q'. By the way, I don't know what you're doing but I'm seeing all of your posts twice, from two different addresses. This is a little confusing, to put it mildly, and doesn't half break the threading. -- Rhodri James *-* Wildebeest Herder to the Masses -- http://mail.python.org/mailman/listinfo/python-list
Re: install older Python version parallel
On Thu, 11 Jun 2009 15:50:06 +0100, S. Dornseifer wrote: Hi everybody, The situation: I wrote a GUI, based on Python, TkInter and Pmw. It runs perfectly fine with Python 2.4 (providing, TkInter and Pmw are installed). But it crashes with Python 2.6. I tried this on MacOSX11.4 and various Linux Distributions. Crashes occurs when I activate a Pmw.Diaog (I guess this is due to a bug in the installed blt package), also when I use setitems on Pmw.OptionMenu (I guess this is due to another package, associated with tcl/tk). You might get more helpful responses if you say more than just "it crashes". What's the traceback? -- Rhodri James *-* Wildebeest Herder to the Masses -- http://mail.python.org/mailman/listinfo/python-list
Re: How to escape # hash character in regex match strings
I'm surprised it's been so difficult to find an example of the hash character in a RegEx string -- for exactly this type of situation, since it's so common in the real world that people want to put a pound symbol in front of a number. It's a character with no special meaning to the regex engine, so I'm not in the least surprised that there aren't many examples containing it. You could just as validly claim that there aren't many examples involving the letter 'q'. It depends on whether the re.VERBOSE option is passed. If you're using a verbose regexp, you can use "#" to comment portions of it: r = re.compile(r""" \d+ # some digits [aeiou] # some vowels """, re.VERBOSE) -tkc -- http://mail.python.org/mailman/listinfo/python-list
command line of a process.exe on another host
HI , I have number of process run on different windows servers which run's with different command line parameters. for example "process.exe -input statusurl: http://sss.com "., These parameters can vary from host to host. using Psexec I know the PID and process name which are running on these machines, but how I can read the command line parameters of these process. Is there a way to read these command line of the proess via python pls? any feedback appreciated.. thanks Hari -- http://mail.python.org/mailman/listinfo/python-list
Re: xml.dom.minidom losing the XML document type attribute
Stefan Behnel schrieb: > Johannes Bauer wrote: >> Stefan Behnel schrieb: >> Can I somehow force Python to generate it anyways? >>> Did you try passing encoding='UTF-8' on serialisation? >> Uhm... nope - how can I do that? > > Well, depends on what your code currently does. > > Maybe you could use something like > > doc.xmlwrite(..., encoding='UTF-8') Yay! Does the trick, thanks a lot! Kind regards, Johannes -- "Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit, verlästerung von Gott, Bibel und mir und bewusster Blasphemie." -- Prophet und Visionär Hans Joss aka HJP in de.sci.physik <48d8bf1d$0$7510$54022...@news.sunrise.ch> -- http://mail.python.org/mailman/listinfo/python-list
Re: xml.dom.minidom losing the XML document type attribute
David Robinow schrieb: > On Thu, Jun 11, 2009 at 9:20 AM, Johannes Bauer wrote: >> Well, I'm not speaking about my software :-) Actually it's Gnucash which >> complains if the tag is not explicitly set. This is because they >> appearently had a ancient version which did not specify the charset, but >> used a different one than UTF-8. Kind of annoying, but fixing my XML >> output seems to be easier than convincing the Gnucash people to change >> their software :-) > > from the GnuCash web page: > How can you help? Well, it's not as if it's a bug of GnuCash. This is a deliberate decision used to ensure backwards compatibility with older versions of GnuCash. So a bug report wouldn't really do good anything at all ("Please remove your backwards compatibility feature, it annoys me and I only use recent versions anyways"). Kind regards, Johannes -- "Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit, verlästerung von Gott, Bibel und mir und bewusster Blasphemie." -- Prophet und Visionär Hans Joss aka HJP in de.sci.physik <48d8bf1d$0$7510$54022...@news.sunrise.ch> -- http://mail.python.org/mailman/listinfo/python-list
Re: itertools.intersect?
On Jun 11, 1:54 pm, Terry Reedy wrote: > Jack Diederich wrote: > > On Thu, Jun 11, 2009 at 12:03 AM, David M. Wilson wrote: > > [snip] > >> I found my answer: Python 2.6 introduces heap.merge(), which is > >> designed exactly for this. > > > Thanks, I knew Raymond added something like that but I couldn't find > > it in itertools. > > That said .. it doesn't help. Aside, heapq.merge fits better in > > itertools (it uses heaps internally but doesn't require them to be > > passed in). The other function that almost helps is > > itertools.groupby() and it doesn't return an iterator so is an odd fit > > for itertools. > > > More specifically (and less curmudgeonly) heap.merge doesn't help for > > this particular case because you can't tell where the merged values > > came from. You want all the iterators to yield the same thing at once > > but heapq.merge muddles them all together (but in an orderly way!). > > Unless I'm reading your tokenizer func wrong it can yield the same > > value many times in a row. If that happens you don't know if four > > "The"s are once each from four iterators or four times from one. > > David is looking to intersect sorted lists of document numbers with > duplicates removed in order to find documents that contain worda and > wordb and wordc ... . But you are right that duplicate are a possible > fly in the ointment to be removed before merging. Removing the duplicates could be a big problem. With SQL, the duplicates need not have to be removed. All I have to do is change "SELECT" to "SELECT DISTINCT" to change 100 100 100 322 322 322 322 322 322 322 322 into 100 322 -- http://mail.python.org/mailman/listinfo/python-list
Re: Voronoi diagram algorithm (Fortune’s sweepline )
On Jun 11, 2:01 pm, Robert Kern wrote: > On 2009-06-11 14:56, Captain___nemo wrote: > > Please advice me very simple implementation of voronoi diagram (given > > coordinates). Please advice me simple python code preferably without- > > hash, multi-threading, Delaunay Traingulation, > > You can't really do the Voronoi diagram without Delaunay Triangulation. They > are > two ways of looking at the same thing. You might not be able to calculate the exact points of a Voronoi without Delaunay triangulation but you can certainly draw one without it. For instance, this code does that: import numpy from PIL import Image def voronoi(points,shape=(500,500)): depthmap = numpy.ones(shape,numpy.float)*1e308 colormap = numpy.zeros(shape,numpy.int) def hypot(X,Y): return (X-x)**2 + (Y-y)**2 for i,(x,y) in enumerate(points): paraboloid = numpy.fromfunction(hypot,shape) colormap = numpy.where(paraboloid < depthmap,i+1,colormap) depthmap = numpy.where(paraboloid < depthmap,paraboloid,depthmap) for (x,y) in points: colormap[x-1:x+2,y-1:y+2] = 0 return colormap def draw_map(colormap): shape = colormap.shape palette = numpy.array([ 0x00FF, 0xFFFF, 0x00FF00FF, 0x00FF, 0x, 0xFF00, 0x00FF, 0x, ]) colormap = numpy.transpose(colormap) pixels = numpy.empty(colormap.shape+(4,),numpy.int8) pixels[:,:,3] = palette[colormap] & 0xFF pixels[:,:,2] = (palette[colormap]>>8) & 0xFF pixels[:,:,1] = (palette[colormap]>>16) & 0xFF pixels[:,:,0] = (palette[colormap]>>24) & 0xFF image = Image.fromstring("RGBA",shape,pixels) image.save('voronoi.png') if __name__ == '__main__': draw_map(voronoi(([100,100],[356,301],[400,65],[324,145], [200,399]))) Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: How *build* new elements and *replace* elements with xml.dom.minidom ?
Stefan Behnel schrieb: >> So I need to build hyperlinks (a elements) with href attribute and >> replace the text elements (numbers) somehow. > > Try lxml.html instead. It makes it really easy to do these things. For > example, you can use XPath to find all table cells that contain numbers: > > td_list = doc.xpath("//td[number() >= 0]") > > or maybe using regular expressions to make sure it's an int: > > td_list = doc.xpath("//td[re:match(., '^[0-9]+$')]", > namespaces={'re':'http://exslt.org/regular-expressions'}) > > and then replace them by a hyperlink: > > # assuming links = ['http://...', ...] > > from lxml.html.builder import A > for td in td_list: > index = int(td.text) > a = A("some text", href=links[index]) > td.getparent().replace(td, a) Oh no! I was looking for something like this for *ages* but always fought with minidom - where this is a real pain :-( Had I only known before that such a wonderful library exists. I'll definitely use lxml from now on. Does it compile with Python3? Kind regards, Johannes -- "Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit, verlästerung von Gott, Bibel und mir und bewusster Blasphemie." -- Prophet und Visionär Hans Joss aka HJP in de.sci.physik <48d8bf1d$0$7510$54022...@news.sunrise.ch> -- http://mail.python.org/mailman/listinfo/python-list
Re: what does "lost sys.stdin" error mean?
On 2009-06-11, lynvie wrote: > I have a very simple program from the first chapter of a book on > python 3 (I'm a novice). I called the program tmp.py and the data > input file is sum.dat (just a list of numbers, 1 per line). When I > type into my command shell "tmp.py < sum.dat" I get an error from What OS are you using? I know that pipes cause issues when working with interpreters on Windows. You must call the python interpreter directly or it will not be able to handle the pipe directly. I don't remember exacly how the issue manifests but I remember it has bitten me before on Win32. I don't currenlty have a Windows machine to test on. > "line=input()" -- that's line 7 from the code below. The exact error > message is: > RuntimeError: input(): lost sys.stdin. I am working on Linux and I have been unable to produce your error. My Python version identifies itself as: >>> import sys >>> sys.version '3.0.1 (r301:69556, Mar 17 2009, 11:42:03) \n[GCC 4.1.2]' This is what I have done using your same tmp.py file as pasted from a shell session: 17:52,505$ count=0 17:53,506$ while [ $count -lt 20 ]; do echo $RANDOM >> sum.dat; newcount=`echo "$count + 1" | bc`; count=$newcount; done 17:53,507$ python3.0 tmp.py < sum.dat Type integers, each followed by Enter; or ^D or ^Z to finish number = 22657 count = 1 total = 22657 mean = 22657.0 number = 12223 count = 2 total = 34880 mean = 17440.0 number = 10250 count = 3 total = 45130 mean = 15043.333 number = 20919 count = 4 total = 66049 mean = 16512.25 number = 20995 count = 5 total = 87044 mean = 17408.8 number = 28988 count = 6 total = 116032 mean = 19338.667 number = 13015 count = 7 total = 129047 mean = 18435.2857143 number = 25701 count = 8 total = 154748 mean = 19343.5 number = 6566 count = 9 total = 161314 mean = 17923.778 number = 19396 count = 10 total = 180710 mean = 18071.0 number = 16771 count = 11 total = 197481 mean = 17952.8181818 number = 2039 count = 12 total = 199520 mean = 16626.667 number = 655 count = 13 total = 200175 mean = 15398.0769231 number = 27417 count = 14 total = 227592 mean = 16256.5714286 number = 5000 count = 15 total = 232592 mean = 15506.133 number = 12015 count = 16 total = 244607 mean = 15287.9375 number = 8746 count = 17 total = 253353 mean = 14903.1176471 number = 29487 count = 18 total = 282840 mean = 15713.333 number = 3194 count = 19 total = 286034 mean = 15054.4210526 number = 8225 count = 20 total = 294259 mean = 14712.95 As you can see, it seems to be working as prescribed. -- http://mail.python.org/mailman/listinfo/python-list
Win32 stdin redirection
On 2009-06-11, Tim Harig wrote: > On 2009-06-11, lynvie wrote: >> I have a very simple program from the first chapter of a book on >> python 3 (I'm a novice). I called the program tmp.py and the data >> input file is sum.dat (just a list of numbers, 1 per line). When I >> type into my command shell "tmp.py < sum.dat" I get an error from > What OS are you using? I know that pipes cause issues when working with > interpreters on Windows. You must call the python interpreter directly or > it will not be able to handle the pipe directly. I don't remember exacly > how the issue manifests but I remember it has bitten me before on Win32. I > don't currenlty have a Windows machine to test on. More info: http://groups.google.co.uk/group/comp.lang.python/browse_frm/thread/10d3928277319bef -- http://mail.python.org/mailman/listinfo/python-list
Re: preferring [] or () in list of error codes?
> Try, then, this tuple: > >event_timestamp = (2009, 06, 04, 05, 02, 03) >(year, month, day, hour, minute, second) = event_timestamp > > A list would be wrong for this value, because each position in the > sequence has a specific meaning beyond its mere sequential position. Yet > it also matters to the reader that these items are in a specific > sequence, since that's a fairly standard ordering for those items. > > In this case, a tuple is superior to a list because it correctly conveys > the semantic meaning of the overall value: the items must retain their > sequential order to have the intended meaning, and to alter any one of > them is conceptually to create a new timestamp value. I totally agree about anything to do with immutability, I think the relative ordering of the elements in this example may be orthogonal to the concept of a tuple as an object whose elements have a semantic meaning implicitly defined by location in the sequence... in other words knowing that element i+1 is in some sense ordinally smaller than element i does not give me much information about what element i+1 actually is. To me a timestamp could be (date, time), or (days, seconds, microseconds) (as in datetime.timedelta()), so it is not clear to me that using a tuple as something where the semantic meaning of the element at position i should readily apparent would be the best approach for timestamps, or enough to distinguish list and tuple (in other words I am not suggesting a dict or class). In the case of something like (x, y) or (real, imag), or (longitude, latitude), or any case where there is common agreement and understanding, such that using names is arguably superfluous... I think in those cases the concept makes sense of a tuple as a sequence of attributes whose elements have a semantic meaning implicitly defined by position in the sequence. My feeling is the number of cases where tuples are better than lists for that is small relative to the number of cases where tuple adds value as an immutable list. I do not mean to be suggesting that a tuple should only ever be used or thought of as a "frozenlist" though. -- http://mail.python.org/mailman/listinfo/python-list
Re: preferring [] or () in list of error codes?
Gunter Henriksen writes: > > Try, then, this tuple: > > > >event_timestamp = (2009, 06, 04, 05, 02, 03) > >(year, month, day, hour, minute, second) = event_timestamp > > I totally agree about anything to do with immutability, I think the > relative ordering of the elements in this example may be orthogonal to > the concept of a tuple as an object whose elements have a semantic > meaning implicitly defined by location in the sequence... in other > words knowing that element i+1 is in some sense ordinally smaller than > element i does not give me much information about what element i+1 > actually is. The point of each position having a different semantic meaning is that tuple unpacking works as above. You need to know the meaning of each position in order to unpack it to separate names, as above. So two tuples that differ only in the sequence of their items are different in meaning. This is unlike a list, where the sequence of items does *not* affect the semantic meaning of each item. Note that I'm well aware that the language doesn't impose this as a hard restriction; but that says more about Python's “consenting adults” philosophy than anything else. -- \ “I went to a general store. They wouldn't let me buy anything | `\ specifically.” —Steven Wright | _o__) | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: help with errror
On Thu, Jun 11, 2009 at 2:33 PM, tanner barnes wrote: > could some one explain this error to me and possibly how to fix it? > > Traceback (most recent call last): > File "C:\Users\brandon\workspace\tanner's workshop\tanner's > workshop\src\WxGlade.py", line 458, in > Edit = MyDialog2(None, -1, "") > File "C:\Users\brandon\workspace\tanner's workshop\tanner's > workshop\src\WxGlade.py", line 27, in __init__ > self.Name = wx.StaticText(self.panel_41, -1, "Name") > File "C:\Python26\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line > 8522, in SetName > return _core_.Window_SetName(*args, **kwargs) > TypeError: String or Unicode type required Might I suggest you inquire on the WxPython mailinglist: http://www.wxpython.org/maillist.php Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: preferring [] or () in list of error codes?
> > >event_timestamp = (2009, 06, 04, 05, 02, 03) > > >(year, month, day, hour, minute, second) = event_timestamp > > > > [...] > > The point of each position having a different semantic meaning is that > tuple unpacking works as above. You need to know the meaning of each > position in order to unpack it to separate names, as above. > > So two tuples that differ only in the sequence of their items are > different in meaning. This is unlike a list, where the sequence of items > does *not* affect the semantic meaning of each item. I do not feel the above is significantly different enough from event_timestamp = [2009, 06, 04, 05, 02, 03] (year, month, day, hour, minute, second) = event_timestamp event_timestamp = (2009, 06, 04, 05, 02, 03) (year, month, day, hour, minute, second) = event_timestamp event_timestamp = [2009, 06, 04, 05, 02, 03] [year, month, day, hour, minute, second] = event_timestamp to suggest tuples are really adding significant value in this case, especially when I can do something like event_timestamp = (2009, 06, 04, 05, 02, 03) (year, month, day, hour, second, minute) = event_timestamp and not have any indication I have done the wrong thing. I guess to me, fundamentally, the interpretation of tuple as a sequence whose elements have semantic meaning implicitly defined by position is a relatively abstract intrepretation whose value is dubious relative to the value of immutability, since it seems like a shortcut which sacrifices explicitness for the sake of brevity. I would feel differently if seemed unusual to find good Python code which iterates through the elements of a tuple as a variable length homogenous ordered collection. But then I would be wishing for immutable lists... -- http://mail.python.org/mailman/listinfo/python-list
Specify the sorting direction for the various columns/
Managed to get a dictionary to sort on multiple columns using a tuple to set the sort order (see below). However how can I control that column "date" orders descending and the column "name" orders ascending. Thanks import datetime import pprint import operator faUserFormInput = {'DDPageSortOrder': 'PageAge'} mypages = ["PageName","PageAge","PageAge"] gaValidSortOrder = [ {'OrderType': 'PageName', 'SortOrder': ('name')}, {'OrderType': 'PageAge','SortOrder': ('date','name')}, {'OrderType': 'PageAuthor', 'SortOrder': ('username','date')} ] entries = [{'name': 'ZZ2', 'username': 'ZZ3', 'date': datetime.datetime (2008, 9, 30, 16, 43, 54)},{'name': 'ZZ2', 'username': 'ZZ5','date': datetime.datetime(2008, 9, 30, 16, 43, 54)},{'name': 'ZZ2', 'username': 'ZZ1', 'date': datetime.datetime(2007, 9, 30, 16, 43, 54)}, {'name': 'AA2', 'username': 'AA2','date': datetime.datetime(2007, 9, 30, 16, 43, 54)}] sortorderarr = ('name','date') #if ("DDPageSortOrder" in faUserFormInput): for item in gaValidSortOrder: print "1=%s" % (item["OrderType"]) print "2=%s" % (faUserFormInput["DDPageSortOrder"]) if (item["OrderType"] == faUserFormInput["DDPageSortOrder"]): sortorderarr = item["SortOrder"] #sortorderarr = '\','.join(sortorder) print sortorderarr pp = pprint.PrettyPrinter(depth=2) pp.pprint(entries) bob = entries bob.sort(key=operator.itemgetter(*sortorderarr),reverse=True) pp.pprint(bob) -- http://mail.python.org/mailman/listinfo/python-list
Re: Convert integer to fixed length binary string
Thanks, this is what I needed On Jun 11, 9:40 pm, Ulrich Eckhardt wrote: > casebash wrote: > > I know the bin function converts an int into a binary string. > > Binary string sounds ambiguous. Firstly, everything is binary. Secondly, > strings are byte strings or Unicode strings. In any case, I'm not 100% sure > what you mean - giving an example of input and output would help! > > > Unfortunately, I need to know the length of the binary string when it > > is being read in and len(bin(x)) depends on x. Is there any way to > > limit it to 4 bytes? > > If you need a piece of four bytes which contain a number in a packed format > similar to the one used in memory, using bin(x) is the wrong way. Instead, > take a look at the struct module: > > import struct > struct.pack('=L', 255) > > Alternatively, also the array module might help. > > Uli > > -- > Sator Laser GmbH > Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932 -- http://mail.python.org/mailman/listinfo/python-list