possible to preserve subprocess.Popen objects for later?
Hi all, I have a python gui app that launches multiple applications using subprocess.Popen class and prints their output in the gui (using PIPEs, threads and wxPython). Everything works great but the problem is that some applications should run in the background (ie they don't close when the gui closes) so next time when you start the gui it will not have a handle on those processes that are still running and therefore won't be able to print their output. So, I was wondering if there is any way to "preserve" the Popen object of those background processes and reload them when the gui is restarted? Ideally I would like to have the whole Popen object preserved but I could also get by with just the Popen.stdout file object. I can save the Popen.stdout.fileno() integer because that's what I use in os.read() to read the output anyway but that doesn't work. I have doubts that this could even conceptually work but I thought I'd try asking anyway. I don't have a full understanding of how processes and pipes work on the system level... Thanks, Ratko -- http://mail.python.org/mailman/listinfo/python-list
Re: possible to preserve subprocess.Popen objects for later?
> Sounds like you might want to consider running the backround task as > either a service or daemon and communicate via a TCP/IP connection or > a named pipe rather than using STDIN/OUT. Actually you could use > either idea without actually setting up a daemon. Unfortunately I don't have control over the processes that I am running (they are components of a much larger system that I am trying to bring under one roof). However, it seems that a named pipe might work. Is it possible to set up a named pipe for a process that I start and tell that process to direct all its output to it? Thanks, Ratko -- http://mail.python.org/mailman/listinfo/python-list
Re: popen and a long running process in a wx.python application
On Jun 26, 10:16 am, Doru Moisa <[EMAIL PROTECTED]> wrote: > Hello, > > How can I capture the output of a long runnning process which I open > with popen() ? > I tried reading line by line, char by char, but the result always > comes when the process finishes. > (I am trying to make a wx.python program that opens some "make ..." > with popen). How can I receive the output of the program immediatly, > so that I can show a progressbar in my application ? > I always get the program's output after it finished executing. > Is this the right place, or should I post this to wx.python ? > > Thank you in advance. I think this is the right list to post this in since it's independent of wxPython. I just recently went through this and got it working properly. Your problem is that the output is buffered and is only flushed upon exit (and that's when you read() it) Here's a piece of code: doRead = True p = subprocess.Popen(["ls"], stdout=sp.PIPE, stderr=sp.STDOUT, bufsize=1) while doRead: txt = os.read(p.stdout.fileno(), 2048) time.sleep(0.5) # read at most x times / sec A few things to note: (1) you must use os.read and not the builtin read() for unbuffered read. (2) you probably want to run this while loop in a thread because read will block until there's some text available (3) this is a polling method where I read at most 2 a second in the above example (4) this works on Mac, Windows and Linux the same (besides the "ls" command of course) (5) after you read this text you can send it to a TextCtrl or something Hope that helps. Ratko -- http://mail.python.org/mailman/listinfo/python-list
Can a base class know if a method has been overridden?
Hi all, I was wondering if something like this is possible. Can a base class somehow know if a certain method has been overridden by the subclass? I appreciate any ideas. Thanks, Ratko -- http://mail.python.org/mailman/listinfo/python-list
Re: Can a base class know if a method has been overridden?
> If your use case is to make sure a given ('abstract') method has been > overriden, the canonical solution is to raise NotImplementedError in the > base class's implementation I am not really interested in forcing the subclass to implement a method. I am interested in knowing *whether* it did implement it or not. > Else, this may be possible using a custom metaclass (or possibly just > specializing the __new__ method), but there may be better solutions > (depending on what you're really trying to do).. I have a base class EvtHandler that has methods defined to handle certain events. You then subclass from EvtHandler and override the methods for the events you want to receive. If a method has been overridden, the base class will automatically register for those events to make sure that they are even delivered to this handler (which is why I would need to know whether a method has been overridden or not). Of course, there are other ways of doing this which would require a bit more work from the subclass... I just thought this would be a neat "automatic" way of registering for events. For example: class EvtHandler: def __init__(self): if onKey is overridden: register_for_key_events() def onKey(self): pass class MyHandler(EvtHandler): def onKey(self): # do something here -- http://mail.python.org/mailman/listinfo/python-list
Re: Can a base class know if a method has been overridden?
> Ok. The simplest solution, then, is simply to not implement the method > in the base class, ie: > > class EvtHandler: > def __init__(self): > if hasattr(self, 'onKey'): > register_for_key_events() > > #def onKey(self): > #pass > > class MyHandler(EvtHandler): > def onKey(self): > # do something here > > Another solution is to compare the functions wrapped by the methods: > > class EvtHandler: > def __init__(self): > onKey = getattr(self, 'onKey') > if onKey.im_func is EvtHandler.onKey.im_func: > register_for_key_events() > > def onKey(self): > pass > > class MyHandler(EvtHandler): > def onKey(self): > # do something here > > HTH The second solution works beautifully! Thank you very much. I was aware that not implementing the onKey method in the first place is the simplest solution but it's much cleaner to offer the methods in advance so that the user can see what is possible. Ratko -- http://mail.python.org/mailman/listinfo/python-list
Re: Can a base class know if a method has been overridden?
On Sep 24, 12:56 pm, Stéphane Larouche <[EMAIL PROTECTED]> wrote: > What about something like: > > class A(object): > def my_method(self): > print "A.my_method" > def call_my_method(self): > if type(self).my_method == A.my_method: > print "Calling base class method." > else: > print "Calling derived class method." > self.my_method() > > class B(A): > pass > > class C(A): > def my_method(self): > print "C.my_method" > > a = A() > b = B() > c = C() > > a.call_my_method() > b.call_my_method() > c.call_my_method() > > Stéphane I think it would confuse the user to have to call "call_my_method" as opposed to calling "my_method" directly. The only reason why I wanted to do this is simplicity, clarity and transparency. Thanks though. Bruno's solution does exactly what I was looking for. Ratko -- http://mail.python.org/mailman/listinfo/python-list
properly delete item during "for item in..."
Say you have something like this: for item in myList: del item Would this actually delete the item from the list or just decrement the reference counter because the item in myList is not associated with name "item" anymore (but still is with myList[itemIndex])? In other words, is "item" a temporary reference to myList[itemIndex] or is it actually that reference that myList has stored? I am not sure if this question even makes any sense anymore. I've been using python for years and never had any problems (and I don't now either) but now that I had to revisit c++/STL, I had to deal about these issues and was wondering how python does it. Thanks, Ratko -- http://mail.python.org/mailman/listinfo/python-list
Re: properly delete item during "for item in..."
On Jul 17, 9:57 am, mk <[EMAIL PROTECTED]> wrote: > Gary Herron wrote: > > You could remove the object from the list with > > del myList[i] > > if you knew i. HOWEVER, don't do that while looping through the list! > > Changing a list's length will interact badly with the for loop's > > indexing through the list, causing the loop to mis the element following > > the deleted item. > > Jumping into a thread, I know how not to do it, but not how to do it > properly? > > Iterating over a copy may _probably_ work: > > >>> t=['a', 'c', 'b', 'd'] > >>> > >>> for el in t[:]: > del t[t.index(el)] > > >>> t > [] > > However, is it really safe? Defining safe as "works reliably in every > corner case for every indexable data type"? > > Con: suppose the data structure t is really, really big. Just deleting > some items from t temporarily doubles the memory consumption. Would this work (safely) then? It does in my test cases but that of course doesn't prove it works in a general case... for item in myList: myList.remove(item) For dictionaries we can just iterate over values() or items() as opposed to itervalues() or iteritems() since that's technically a copy of values or items in the dict, right? R -- http://mail.python.org/mailman/listinfo/python-list
Re: properly delete item during "for item in..."
> > For dictionaries we can just iterate over values() or items() as > > opposed to itervalues() or iteritems() since that's technically a copy > > of values or items in the dict, right? > > No! In fact the whole point of iteritems and itervalues and iterkeys is > that they *DO NOT* make copies, so changing the dictionary out from > under them is a programming error. > > If you use dict.items(), dict.keys() or dict.values(), then you're OK, > because these methods *do* create new lists for both. That's what I meant, it just didn't come across correctly I guess. Thanks for clarifying these issues. I think I have a better understanding now. R -- http://mail.python.org/mailman/listinfo/python-list
comparing two IP addresses and the underlying machine
Hi all, I've been trying to figure this one out for some time but with no success. I have a machine with two network interfaces, each with their own IP address and it's own domain, for example: - ipA on machineA.domainA - ipB on machineB.domainB Given any pair of IPs or hostnames (or a mix of them), how can I figure whether they belong to the same physical machine or not? Of course, this is trivial if my python program is running the given machine but what if a remote machine is trying to figure this out (but that machine has access to both domains/IPs). Appreciate and ideas. Ratko -- http://mail.python.org/mailman/listinfo/python-list
Re: comparing two IP addresses and the underlying machine
By the same physical machine I meant one OS using two interfaces and multiple IP addresses mapped to different interfaces. I figured there wouldn't be a direct solution to this so each time I will send all IP addresses to the "match maker" so I can make comparisons and that should work. Thanks for the help. Ratko On 12/12/06, Tim Chase <[EMAIL PROTECTED]> wrote: > I've been trying to figure this one out for some time but > with no success. I have a machine with two network > interfaces, each with their own IP address and it's own > domain, for example: > - ipA on machineA.domainA > - ipB on machineB.domainB > > Given any pair of IPs or hostnames (or a mix of them), how > can I figure whether they belong to the same physical > machine or not? Of course, this is trivial if my python > program is running the given machine but what if a remote > machine is trying to figure this out (but that machine has > access to both domains/IPs). > Appreciate and ideas. I have a feeling that you're trying to attempt the impossible. What do you mean by "the same physical machine"? The same case? What happens if there are two virtual OSes on the machine, each using its own NIC? Unless you have a client application running on "the physical machine" that can respond to queries on each NIC, you're pretty much out of luck. You could write something like a "same_ping" program that would listen on both NICs, and respond with a "yeah, I'm the same machine" confirmation. There are all sorts of ways to do this, depending on how sure you need to be that nobody can spoof it...ranging from simple "here's a random number on one IP address followed by asking the other IP if it's seen that random number before" to having the physical machine sign one request on each port and confirm that their signatures are the same. -tkc -- http://mail.python.org/mailman/listinfo/python-list
figuring out how much data was sent so far via XML-RPC?
I am currently using XML-RPC for a very convenient quick-and-dirty way of sending some files (base64 encoded). The files can be bigger sometimes (10-20mb) and I was wondering if there is a way to see how much data was sent already and how much still needs to be sent (like a progress bar). I would of course need that only on the client side. I know how I would do it with pure sockets but I am not sure how could I get to that lower level with xmlrpclib... I am using xmlrpclib.ServerProxy for the client and a SimpleXMLRPCServer. It would also be great if I could keep the solution (if it exists) within the standard python library (no extra modules) because I am distributing this to other people. Thanks in advance! Ratko -- http://mail.python.org/mailman/listinfo/python-list
Re: What version of python is running a script
import syssys.version yields something like:2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)]On 3/7/06, Fernando Rodríguez <[EMAIL PROTECTED]> wrote:Hi, How can my script tell which version of python is running it?Thanks--http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: inserting into a list
from the Library Reference:s.insert(i, x) same as s[i:i] = [x] (5)On 3/7/06, John Salerno <[EMAIL PROTECTED] > wrote:Let me apologize in advance for what I'm sure is an achingly simple question, but I just can't find the answer in either of my Python books.I've tried a few tests with the interactive prompt, but they don't workeither.All I'm trying to do is insert an item into a list, like so: L = [1, 2, 4]and I want to insert the integer 3 into the position L[2], so that thelist reads [1, 2, 3, 4]I've tried all kinds of combinations of slicing assignment, but I alwaysget: TypeError: can only assign an iterableCan someone please embarrass me with the simple answer? :)--http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list