Re: Reading output from a child process non-blockingly
Yuan HOng ha scritto: > In my program I have to call an external program and parse its output. > For that I use the os.popen2 function, and then read the output > stream. > > But the complexity is that the external program gives back its output > in a piecemeal manner, with long delays between the outputs. In the > main program I want to therefore read the output in a non-blocking > manner, to read as many bytes as the child process is spitting out. > > The question is, how can I achieve that? > What about using a thread to control the child process? Licia -- http://mail.python.org/mailman/listinfo/python-list
Implementing a circular counter using property / descriptors?
I'd like to implement an object that represents a circular counter, i.e. an integer that returns to zero when it goes over it's maxVal. This counter has a particular behavior in comparison: if I compare two of them an they differ less than half of maxVal I want that, for example, 0 > maxVal gives true. This is because my different counters grew together and they never differ a lot between them and so a value of 0 compared with an other of maxVal means that the previous one just made its increment before the other one. The python problem that I give you it's about style. I'd like to write in my code something that looks almost like using an integer object. I mean, I'd like to write: cnt = CircularConter(maxVal=100, initialVal=10) cnt += 100 # cnt value is 9 print cnt # prints 9 100 > cnt # is false cnt = 100 # cnt new value is 100 [NOT rebind cnt with 100] Until now I used this class: class CircularConter: def __init__(self, maxVal, initialVal=0): self.maxVal = maxVal self.val = None self.set( initialVal ) def __add__(self, increment): self.set( self.val + increment ) return self def __cmp__(self, operand): return cmp(self.maxVal/2, abs(operand - self.val)) * cmp(self.val, operand) def __repr__(self): return str(self.val) def __str__(self): return str(self.val) def set(self, val): if val > self.maxVal: self.val = val-self.maxVal-1 else: self.val = val ... and so my real writing was: cnt = CircularConter(maxVal=100, initialVal=10) cnt += 100 print cnt 100 > cnt# is false cnt.set(100) The fact is that I don't like to write cnt.set(100) or cnt = CircularConter(100, 100) instead of cnt = 100. So I thought that property or descriptors could be useful. I was even glad to write: cnt = CircularConterWithProperty(maxVal=100, initialVal=10) cnt.val += 100 print cnt.val 100 > cnt.val # is false cnt.val = 100 just to give uniformity to counter accessing syntax. But I wasn't able to implement nothing working with my __cmp__ method. I'll post one of mine NOT WORKING implementation. class pro(object): def __init__(self, maxVal, val): self._maxVal = maxVal self._val = val def getval(self): return self._val def setval(self, val): if val > self._maxVal: self._val = val-self._maxVal-1 else:self._val = val val = property(getval, setval) class CircularConterWithProperty(pro): def __init__(self, maxVal, val=0): super(CircularConterWithProperty, self).__init__( maxVal, val) def __cmp__(self, operand): return cmp(self.maxVal/2, abs(operand - self.val)) * cmp(self.val, operand) __ I know why this doesn't work. __ __ What I don't know __ is if there is a way to write a class that allows my desire of uniform syntax or if IT IS JUST A NON SENSE. I'll thank in advance for any answer. Saluti a tutti Licia -- http://mail.python.org/mailman/listinfo/python-list
Really strange behavior
Sorry I wasn't able to be more specific on my topic but I really do not know how to classify my problem, I mean that I can't understand if it's a python or a twisted or a Qt4 problem I'm trying to run a simple application with Twisted and Qt4. To do this I downloaded this: http://twistedmatrix.com/trac/attachment/ticket/1770/qt4reactor.py Now, if I run this: # > import qt4reactor import sys from PyQt4 import QtGui from winIum import Window def main(): app = QtGui.QApplication(sys.argv) qt4reactor.install(app) MainWindow = QtGui.QMainWindow() win = Window(MainWindow) MainWindow.show() from twisted.internet import reactor reactor.run() # my window shows and run correctly. If I run this: # > import qt4reactor import sys from PyQt4 import QtGui from winIum import Window def creApp(): app = QtGui.QApplication(sys.argv) qt4reactor.install(app) retrun app def creWin(): MainWindow = QtGui.QMainWindow() win = Window(MainWindow) MainWindow.show() def main(): app = creApp() creWin() from twisted.internet import reactor reactor.run() # < my window doesn't show and the script doesn't stop but remains trapped in some gui loop. What's the problem I can't see?? Thank you in advance for any help. Licia -- http://mail.python.org/mailman/listinfo/python-list
Re: Really strange behavior
Il Sun, 05 Nov 2006 04:19:36 +1100, Steven D'Aprano ha scritto: > Why don't you add some temporary print statements into your code to try > to narrow it down? > > I did it and I followed the script with the dubugger and what I saw is that it gets lost in the simulate method of QTReactor instead if executing QTapplication. So I thought about some wrong rebinding of 'app' in the multiple functions call. I was guessing about some python error that I could not see. But the fact that neither you can't see anything wrong, let me guess about some problem with the reactor. Thank for your time. Licia -- http://mail.python.org/mailman/listinfo/python-list
Py2exe with PyQt4 and sqlite
I wrote an application that uses PyQt4 to access a sqlite DB.
Now I'm trying to convert it using py2exe and I found some problems.
The last one, that I'm not able to avoid, is that when I launch the
application's binary on a PC (that contains only a Python 2.5
installation and no QT4) I get an error trying to open the DB withi
this code:
db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName(defines.DB_FILE)
if not db.open():
# displays:
Driver not loaded.
I made many attempts changing options in my setup file but none of
them successfull.
The last one uses this options:
options={"py2exe": {"includes":["sip", 'PyQt4.QtSql' ],
"packages": ["sqlite3",]}}
but I verified that neither QtSql, neither sqlite3 are usefull to
avoid the error.
My development configuration is
Python 2.5
Qt4 4.2.2
PyQt4.1.1
Any suggestion to solve this problem?
Thanks in advance.
Ciao.
Licia.
--
http://mail.python.org/mailman/listinfo/python-list
How to import a module that must override a standard one
I'm writing a python package that will contain a logging service for twisted in python style. I'm using some modules I downloaded from a twisted trunk, that are not released with twisted but have the same names. One of them is log.py that is usually imported from twisted modules like: from twisted.python import log But I need to import __my__ log, and I need that all the other modules that will use my package would import __my__ log module too. So I see 2 options to do so, but neither of them satisfies me: 1) I put __my__ log in my package tree and I import it from there, but in this way any other module must do the same (and this is bad for previous twisted application that want to use my package) 2) I oblige any potential user to patch the standard twisted installation with my modules, but this is even worse. I'm quite sure there is some pythonic way to override the original twisted's log module after its first import with my one: I just don't know how to do it. So I'll patiently wait for an answer from this group. Ciao! Licia -- http://mail.python.org/mailman/listinfo/python-list
Is it possible to reload a module from a different file?
I mean ... The main module makes this import: import moduleX later other modules make different import to an other module named moduleX like this: from pack.subdir import moduleX What I want is that the second import of moduleX in some way must be redirected onto the first one. Is there any way to do so? Would there be any way to do so (moduleX overrides pack.subdir.moduleX) if they were imported on reverse order? Any time I discover that Python can do amazing things, but perhaps this one is just a silly one, isn't it? ciao Licia -- http://mail.python.org/mailman/listinfo/python-list
