ElementTree ; change namespace
Hello the list, I have question about ElementTree module, I didn't find a specific list so I post here. I hope I'm not wrong. I would like to know how to change the namespace URI of all the Element of my XML tree without changing anything else. I want that because I have to compare 2 XML trees, so I walk trought the first tree, and each time I found an element, I delete it (with the Element.remove() function) on the second tree. This way, I can see if there is remaining or lacking elements in my second tree. The problem is that my two trees may not have the same namespace, so the Element.remove() function doesn't work. That's why I want to change the namespace. Also, The Element.remove() documentation says it compares element with their identity ; what is exactly an element identity ? Thank you, Ben -- http://mail.python.org/mailman/listinfo/python-list
Re: Files application architecture
Bruno Desthuilliers a écrit : Benjamin Watine a écrit : Hi, I'm about to develop a small python application and I wonder how to organize files in this application. I'm familar to java, so I'm tempted to use the same convention http://dirtsimple.org/2004/12/python-is-not-java.html : 1 file per class and 1 folders per package. Don't. This is a waste of time and a pain to maintain, and more over it doesn't make any sense since Python doesn't force you to put everything in classes. I know that packages doesn't exists in python, Did you actually read the doc ? While Python's packages are not the same thing as Java's, they do exist. http://docs.python.org/tut/node8.html#SECTION00840 they are modules instead. May I create specific module for each "group of class" ? The usual way to get cohesive modules is indeed to group closely related objects (classes, functions, etc) in a same module. My application follow the MVC paradigm, so basically, I've a package Model, a package View, and a package Controller. If your app is small, having _modules_ models, views and controllers should be enough. So, what are best practices for organizing files and folders in a small python project ? The best practice is to keep things simple, as usual. Thank you all for your good advices and links. I'm new to python and I have yet a lot of things to learn ! Now, I would like to take a look to a well coded wxPython application. Could anybody indicate a project that I could take as reference for standard python coding style ? Regards, Ben -- http://mail.python.org/mailman/listinfo/python-list
Files application architecture
Hi, I'm about to develop a small python application and I wonder how to organize files in this application. I'm familar to java, so I'm tempted to use the same convention : 1 file per class and 1 folders per package. I know that packages doesn't exists in python, they are modules instead. May I create specific module for each "group of class" ? My application follow the MVC paradigm, so basically, I've a package Model, a package View, and a package Controller. So, what are best practices for organizing files and folders in a small python project ? I've found PEP8 (http://www.python.org/dev/peps/pep-0008/) that gives a lot of good hints on coding convention, but nothing about files organization. Thanks in advance ! Ben -- http://mail.python.org/mailman/listinfo/python-list
Re: My python interpreter became mad !
John Machin a écrit : > On Mar 25, 10:05 pm, Benjamin Watine <[EMAIL PROTECTED]> wrote: >> Yes, my python interpreter seems to became mad ; or may be it's me ! :) >> >> I'm trying to use re module to match text with regular expression. In a >> first time, all works right. But since yesterday, I have a very strange >> behaviour : >> >> $ python2.4 >> Python 2.4.4 (#2, Apr 5 2007, 20:11:18) >> [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2 >> Type "help", "copyright", "credits" or "license" for more information. >> >>> import re >> X-Spam-Flag: YES >> X-Spam-Checker-Version: SpamAssassin 3.1.7-deb (2006-10-05) on w3hosting.org >> X-Spam-Level: ** >> X-Spam-Status: Yes, score=22.2 required=5.0 tests=MISSING_HB_SEP, >> MISSING_HEADERS,MISSING_SUBJECT,RAZOR2_CF_RANGE_51_100, >> >> RAZOR2_CF_RANGE_E8_51_100,RAZOR2_CHECK,TO_CC_NONE,UNPARSEABLE_RELAY, >> >> URIBL_AB_SURBL,URIBL_JP_SURBL,URIBL_OB_SURBL,URIBL_SBL,URIBL_SC_SURBL, >> URIBL_WS_SURBL autolearn=failed version=3.1.7-deb >> >> Traceback (most recent call last): >>File "", line 1, in ? >>File "/etc/postfix/re.py", line 19, in ? >> m = re.match('(Spam)', mail) >> AttributeError: 'module' object has no attribute 'match' >> >>> >> >> What's the hell ?? I'm just importing the re module. > > No you're not importing *the* re module. You're importing *an* re > module, the first one that is found. In this case: your own re.py. > Rename it. > Oh... yes, that's it ; I have a file named re.py ! Ouch, sorry for this stupid question, and thank you for this evident answer. I didn't knew that it was possible to import a module this way... Thank you for the fast answer ! Ben -- http://mail.python.org/mailman/listinfo/python-list
My python interpreter became mad !
Yes, my python interpreter seems to became mad ; or may be it's me ! :) I'm trying to use re module to match text with regular expression. In a first time, all works right. But since yesterday, I have a very strange behaviour : $ python2.4 Python 2.4.4 (#2, Apr 5 2007, 20:11:18) [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import re X-Spam-Flag: YES X-Spam-Checker-Version: SpamAssassin 3.1.7-deb (2006-10-05) on w3hosting.org X-Spam-Level: ** X-Spam-Status: Yes, score=22.2 required=5.0 tests=MISSING_HB_SEP, MISSING_HEADERS,MISSING_SUBJECT,RAZOR2_CF_RANGE_51_100, RAZOR2_CF_RANGE_E8_51_100,RAZOR2_CHECK,TO_CC_NONE,UNPARSEABLE_RELAY, URIBL_AB_SURBL,URIBL_JP_SURBL,URIBL_OB_SURBL,URIBL_SBL,URIBL_SC_SURBL, URIBL_WS_SURBL autolearn=failed version=3.1.7-deb Traceback (most recent call last): File "", line 1, in ? File "/etc/postfix/re.py", line 19, in ? m = re.match('(Spam)', mail) AttributeError: 'module' object has no attribute 'match' >>> What's the hell ?? I'm just importing the re module. The code showed, is a previous test code, that seems to be buffered and that make an error. Each call to re module generate that error. How can I workaround ? Is there is a way to flush or restart python interpreter. Is it a bug in python ?? Thanks ! Ben -- http://mail.python.org/mailman/listinfo/python-list
Re: How to send a var to stdin of an external software
Bryan Olson a écrit : > Benjamin Watine wrote: >> [EMAIL PROTECTED] a écrit : >>> I wrote: >>>> And here's a thread example, based on Benjamin's code: >>> [...] >>> >>> Doh! Race condition. Make that: >>> >>> import subprocess >>> import thread >>> import Queue >>> >>> def readtoq(pipe, q): >>> q.put(pipe.read()) >>> >>> cat = subprocess.Popen('cat', shell=True, stdin=subprocess.PIPE, >>> stdout=subprocess.PIPE) >>> >>> myVar = str(range(100)) # arbitrary test data. >>> >>> q = Queue.Queue() >>> thread.start_new_thread(readtoq, (cat.stdout, q)) >>> cat.stdin.write(myVar) >>> cat.stdin.close() >>> cat.wait() >>> myNewVar = q.get() >>> >>> assert myNewVar == myVar >>> print len(myNewVar), "bytes piped around." > >> Great, it works, thank you Bryan ! >> >> Could you explain me why you use a queue instead of a simple array for >> getting the piped var ? > > The call to q.get() will block until an item is in the queue. > At that point in the program, we had already waited for cat to > terminate: > > cat.wait() > myNewVar = q.get() > > But passing cat.wait() does not imply that our own thread has > already read all of cat's output and put it in some destination > object. Data could still be in transit. > > My first version, subsequently titled, "Doh! Race condition," > worked in all of several runs of its built-in test. Doesn't > make it right. > OK, so if I understand well what you said, using queue allow to be sure that the data is passed in totality before coninuing with next instruction. That make sense. Using thread and queue seems to be very more slow than using files redirection with bash. I'll see if it make a great load average and/or I/O time. Thanks again for your help Bryan. Ben -- http://mail.python.org/mailman/listinfo/python-list
Re: How to send a var to stdin of an external software
[EMAIL PROTECTED] a écrit : > I wrote: >> And here's a thread example, based on Benjamin's code: > [...] > > Doh! Race condition. Make that: > > import subprocess > import thread > import Queue > > def readtoq(pipe, q): > q.put(pipe.read()) > > cat = subprocess.Popen('cat', shell=True, stdin=subprocess.PIPE, > stdout=subprocess.PIPE) > > myVar = str(range(100)) # arbitrary test data. > > q = Queue.Queue() > thread.start_new_thread(readtoq, (cat.stdout, q)) > cat.stdin.write(myVar) > cat.stdin.close() > cat.wait() > myNewVar = q.get() > > assert myNewVar == myVar > print len(myNewVar), "bytes piped around." > > > -- > --Bryan > Great, it works, thank you Bryan ! Could you explain me why you use a queue instead of a simple array for getting the piped var ? Regards, Ben -- http://mail.python.org/mailman/listinfo/python-list
Re: How to send a var to stdin of an external software
Bryan Olson a écrit : > I wrote: >> [...] Pipe loops are tricky business. >> >> Popular solutions are to make either the input or output stream >> a disk file, or to create another thread (or process) to be an >> active reader or writer. > > Or asynchronous I/O. On Unix-like systems, you can select() on > the underlying file descriptors. (MS-Windows async mechanisms are > not as well exposed by the Python standard library.) > Hi Bryan Thank you so much for your advice. You're right, I just made a test with a 10 MB input stream, and it hangs exactly like you said (on cat.stdin.write(myStdin))... I don't want to use disk files. In reality, this script was previously done in bash using disk files, but I had problems with that solution (the files wasn't always cleared, and sometimes, I've found a part of previous input at the end of the next input.) That's why I want to use python, just to not use disk files. Could you give me more information / examples about the two solutions you've proposed (thread or asynchronous I/O) ? Thank you ! Ben -- http://mail.python.org/mailman/listinfo/python-list
Re: How to send a var to stdin of an external software
Marko Rauhamaa a écrit : > Benjamin Watine <[EMAIL PROTECTED]>: > >> How can I do this ? I would like a function like that : >> >> theFunction ('cat -', stdin=myVar) >> >> Another related question : Is there's a limitation of var size ? I >> would have var up to 10 MB. > > import subprocess > myVar = '*' * 1000 > cat = subprocess.Popen('cat',shell = True,stdin = subprocess.PIPE) > cat.stdin.write(myVar) > cat.stdin.close() > cat.wait() > > > Marko > Thank you Marko, it's exactly what I need. And if somebody need it : to get the stdout in a var (myNewVar), not in the shell : cat = subprocess.Popen('cat', shell = True, stdin = subprocess.PIPE, stdout=subprocess.PIPE) cat.stdin.write(myVar) cat.stdin.close() cat.wait() myNewVar = cat.stdout.read() Is it correct ? Ben -- http://mail.python.org/mailman/listinfo/python-list
How to send a var to stdin of an external software
Hi the list, I need to send a var to stdin of an external soft ("cat" command for example). How can I do this ? I would like a function like that : theFunction ('cat -', stdin=myVar) I don't need to get any return value. Another related question : Is there's a limitation of var size ? I would have var up to 10 MB. Thanks ! Ben -- http://mail.python.org/mailman/listinfo/python-list