Re: read text file byte by byte
On 13 דצמבר, 22:39, Dennis Lee Bieber wlfr...@ix.netcom.com wrote: On Sat, 12 Dec 2009 22:15:50 -0800 (PST), daved170 daved...@gmail.com declaimed the following in gmane.comp.python.general: Thank you all. Dennis I really liked you solution for the issue but I have two question about it: 1) My origin file is Text file and not binary Do you need to process the bytes in the file as they are? Or do you accept changes in line-endings (M$ Windows text files use crlf as line ending, but if you read it in Python as text crlf is converted to a single lf. 2) I need to read each time 1 byte. I didn't see that on your example code. You've never explained why you need to READ 1 byte at a time, vs reading a block (I chose 1KB) and processing each byte IN THE BLOCK. After all, if you do use 1 byte I/O, your program is going to be very slow, as each read is blocking (suspends) while asking the O/S for the next character in the file (this depends upon the underlying I/O library implementation -- I suspect any modern I/O system is still reading some block size [256 to 4K] and then returning parts of that block as needed). OTOH, reading a block at a time makes for one suspension and then a lot of data to be processed however you want. You originally stated that you want to scramble the bytes -- if you mean to implement some sort of encryption algorithm you should know that most of them work in blocks as the key is longer than one byte. My sample reads in chunks, then the scramble function XORs each byte with the corresponding byte in the supplied key string, finally rejoining all the now individual bytes into a single chunk for subsequent output. -- Wulfraed Dennis Lee Bieber KD6MOG wlfr...@ix.netcom.com HTTP://wlfraed.home.netcom.com/ Hi All, As I read again your comments and the codes you posted I realize that I was mistaken. I don't need to read the file byte by byte. you all right. I do need to scramble each byte. So I'll do as you said - I'll read blocks and scramble each byte in the block. And now for my last question in this subject. Lets say that my file contains the following line: Hello World. I read it using the read(1024) as you suggested in your sample. Now, how can I XOR it with 0xFF for example? Thanks again Dave -- http://mail.python.org/mailman/listinfo/python-list
Re: read text file byte by byte
On Dec 13, 2:34 am, Dennis Lee Bieber wlfr...@ix.netcom.com wrote: On Sat, 12 Dec 2009 10:46:01 +0100, census cen...@no-email.de declaimed the following in gmane.comp.python.general: def scramble (a): return (a + 13) % 256 I'll see your modulo rot 13 and raise with a exclusive or... -=-=-=-=- import sys def scramble(block, key=don't look): copies = int(len(block) / len(key)) + 1 keystring = key * copies return .join([ chr( ord(block[i]) ^ ord(keystring[i])) for i in range(len(block))]) def process(fin, fout, key=None): din = open(fin, rb) dout = open(fout, wb) while True: block = din.read(1024) if not block: break if key is None: block = scramble(block) else: block = scramble(block, key) dout.write(block) dout.close() din.close() if __name__ == __main__: fin = sys.argv[1] fout = sys.argv[2] if len(sys.argv) 3: key = sys.argv[3] else: key = None process(fin, fout, key) -- Wulfraed Dennis Lee Bieber KD6MOG wlfr...@ix.netcom.com HTTP://wlfraed.home.netcom.com/ Thank you all. Dennis I really liked you solution for the issue but I have two question about it: 1) My origin file is Text file and not binary 2) I need to read each time 1 byte. I didn't see that on your example code. Thanks again All of you Dave -- http://mail.python.org/mailman/listinfo/python-list
read text file byte by byte
Hello everybody, I need to read a text file byte after byte. Eache byte is sent to a function that scramble it and I need to write the result to binary file. I've got some questions - 1) How do I read the file byte by byte 2) Should I use streams? If so and I get my entire scrambled text in stream can I just write it to the binary file? Thanks Dave -- http://mail.python.org/mailman/listinfo/python-list
run exe and create exe
Hi everybody, I have 2 questions: 1) I created my python application. It has QT Gui. How can I make exe of it? I don't want everytime I run the file it'll open the command line window which does nothing. 2) My Application suppose to be a client server app. Anyhow, for now It's running only on local host. I added a button that run the server file. my server file located at c:\temp\server.py. It takes no arguments. I tried the following codes at the push button function: os.system(c:\temp\server.py) - It stuck my GUI. I guess that this function doesn't open a new proccess. I also tried : os.spawnv(os.P_NOWAIT,c:\temp\server.py); It raised the following error: OSError: [Errno 8] Exec format error. Any Idea what to do? Thanks DaveD -- http://mail.python.org/mailman/listinfo/python-list
Looger object only prints ERRORs
hi everybody, I took your adviced and used the logging object. I copied the example in 16.6.15.2 - using logging in multiple modules from http://docs.activestate.com/activepython/3.1/python/library/logging.html. unfortunattly it only prints to file the ERROR level's messages and ignore the others. I tried to change the level with SETLEVEL but it didn't help. Any idea? thanks, DaveD -- http://mail.python.org/mailman/listinfo/python-list
raise errors
Hi everybody, I need help with exceptions raising. My goal is to print at the outer functions all the errors including the most inner one. For example: def foo1(self): try: foo2() except ? : print outer Err at foo1 + ?? def foo2(self): try: error occured except ? : raise inner Err at foo2 the ? remarks that I have no idea what to use. I would like the print to be : outer Err at foo1 , inner Err at foo1 thanks daved -- http://mail.python.org/mailman/listinfo/python-list
passing object between classes
Hi everybody, I built my owen log obj as a class. I'm passing it to another object (actually to a thread). When I run my app it raise error at the line when I'm using that log obj. is there any problem with the concept of passing object as I do it? How can I do that? class A: def foo1: myLog = cLog() myYhread = cThread(myLog) myThread.start() class cThread: def __init__(self,in_myLog): sel.LogObj = in_myLog def run(): sel.LogObj.writeLine(HI) thanks Dave -- http://mail.python.org/mailman/listinfo/python-list
Re: passing object between classes
On Sep 21, 1:44 pm, Duncan Booth duncan.bo...@invalid.invalid wrote: daved170 daved...@gmail.com wrote: Hi everybody, I built my owen log obj as a class. I'm passing it to another object (actually to a thread). When I run my app it raise error at the line when I'm using that log obj. is there any problem with the concept of passing object as I do it? How can I do that? class A: def foo1: myLog = cLog() myYhread = cThread(myLog) myThread.start() class cThread: def __init__(self,in_myLog): sel.LogObj = in_myLog def run(): sel.LogObj.writeLine(HI) thanks Dave Please always post real code and state the exact error you get. That will maximise the chance that you get a useful answer. The code you posted will fail for numerous reasons and I can't guess whether you have a problem with indentation, a problem because you misspelled 'self' as 'sel', or because you missed out the 'self' parameter altogether on a method, or perhaps your LogObj simply doesn't have a writeLine method. -- Duncan Boothhttp://kupuguy.blogspot.com- Hide quoted text - - Show quoted text - Hi Duncan, You are right, I should have put the entire code but my question was more theroretical and I know that the code that I posted won't work. Let me simplified my question, I need to be able to write to the same log file from different classes. and even more, from different threads. Is there any existing python Log object that do so? I no, I created my own Log object that only open file and write a line to it, how can I make it be global? Should I use it as a static object? will it work? (offcurse in the case of the threads I'll use utex) Thanks again, DaveD -- http://mail.python.org/mailman/listinfo/python-list
Re: weird str error
On Sep 15, 6:29 pm, Peter Otten __pete...@web.de wrote: daved170 wrote: Hi everybody, I'm using SPE 0.8.3.c as my python editor. I'm using thestr() function and i got a very odd error. I'm trying to do this: printstr(HI) When i'm writing this line in the shell it prints: HI When it's in my code (it's the only line) i'm getting the following error: file c:\Python25\lib\local.py line 242, instr return format(%.12g,val) file c:\Python25\lib\local.py line 145, in format formatted = percent % value TypeError, float argument required any idea? It's worked for the entire day and unfortunately when i started my testing it raised this erroe/ local.py or locale.py? If the latter you are probably doing a star import: from locale import * This will replace the builtinstr() with locale.str() which indeed requires a float: str(HI) 'HI' from locale import * str(HI) Traceback (most recent call last): File stdin, line 1, in module File /usr/lib/python2.5/locale.py, line 244, instr return format(%.12g, val) File /usr/lib/python2.5/locale.py, line 147, in format formatted = percent % value TypeError: float argument required As a general rule use the standard import import locale and then invoke the module's functions with an explicit prefix: locale.setlocale(...) It's a bit more to type, but it will save you a lot of trouble. Peter PS: Whenstr() is the builtinstr(HI) converts a string into a string which does not make much sense.- Hide quoted text - - Show quoted text - Hi Peter, Thanks for your answer. I'll clearify myself. I'm using QString cause I have a GUI app. I want to convert it to python string. It works well for several month and suddenly two days ago it didn't. I'm not using localE at all (i'm not importing it). After your answer I tried it but it still didn't work. Any other ideas? Thanks DaveD -- http://mail.python.org/mailman/listinfo/python-list
PyQT Qthread stuck main app
Hi everybody, I've got a simple GUI app written in python and pyqt. I'm having 2 buttons - start and stop start calls a function that start a thread and stop stops it. my problem is that when start is pusshed the entire window stuck and it's impossible to push the STOP button and even when it looks like it's been pushed it actually don't do anything. any idea how to fix it? Thanks Dave class myThread(QtCore.QThread): def__init__(self): self.alive = 1 def run(self): while self.alive: print Alive print Not Alive def stop(self): print stop pushed self.alive = 0 class GUI(QtGui.QMainWindow): def __init__(self): # all kind of initialization @QtCore.pyqtSignature(start) def on_start_clicked(self): self.cThread = myThread() self.cThread.start() @QtCore.pyqtSignature(stop) def on_stop_clicked(self): self.cThread.stop() -- http://mail.python.org/mailman/listinfo/python-list
run exe on different computer
Hi everybody, I'm building a small python program that run a service (exe file) on my servers. I don't want to use remote desktop and it's siblings. I would like to have some information on how to run an exe on a different computer and if there a way to check if that exe is still alive. Thanks Dave -- http://mail.python.org/mailman/listinfo/python-list
Re: run exe on different computer
On Sep 13, 2:17 pm, Dave Angel da...@ieee.org wrote: daved170 wrote: Hi everybody, I'm building a small python program that run a service (exe file) on my servers. I don't want to use remote desktop and it's siblings. I would like to have some information on how to run an exe on a different computer and if there a way to check if that exe is still alive. Thanks Dave On a question like this, you really need to supply much more information on your constraints. You could start by saying these servers are running Windows Server 2003. And that they're on a domain (rather than a workgroup). And that you're trying to access them from another machine within the same local domain, not over the internet. And that your local machine is on the same domain, and has an account with admin privileges for all the desired servers. And that you are allowed to do a one-time install (of something) on each server prior to this particular need. And that each server already has Python version 2.5 installed, and the IT department won't allow you to install any later version. Then once you have an environment, you need to specify just what kind of program you want to run on those servers. Is it an EXE program? Or is it Python, with a particular script? Does it really need to be a *service*, which has a particular set of constraints, and should be installed, and started/stopped using the service manager. Do you want this program to restart whenever the servers are restarted? One solution that should work for nearly every Windows topology might be to go to each server, run the scheduler task, and specify a new batch file to be run upon boot. This batch file can check a specified (shared) directory for a python script, and if found, run it. If not found, sleep for 60 seconds or so, then repeat. Note that it's a good idea to put a five minute delay at the very beginning, in case the script needs to be deleted at the next boot. Sometimes a bug requires surgery, and it's good to have enough time to do it. Now, to control those servers from another machine, copy an appropriate script into the prearranged directory. Within a minute, it'll be running, and it can post whatever results it likes in another accessible directory. Whether this is a safe thing to do is a separate question. Generally an IT department likes to have some control over just what programs run on their servers, and for good reason. DaveA Hi DaveA Thanks for your answer. I'll try to clearify myself. For now I'm trying to do that on client server that are win XP. They both on the same domain (maybe in the future they'll be runinig on the web). I have admin user on both my computers. I have both an exe and a python app that I'd like to control from my client. Insted of logging to my Server I would like to write a python app at my client that allows me to control both that exe and my Server-python- app. I don't want to use the schedualer because I would like to control it from my client. I can install whatever I'll like on both of the computers. they are mine and I have full access for them. I hope I clearify myself and if there are more solutions I'll be happy to be noted. Thans DaveD :) -- http://mail.python.org/mailman/listinfo/python-list
PyQT child forms
Hi everybody, I'm building my GUI app with PyQT and i'm quite new with it. I looked for example for managing a form and a child form. My goal is to run the main form and a certain button will open a child form. Whenever the child form will be open the main form will be disabled until the child form is closed. Moreover I would like to know how to pass data from the child form to the main form. Thank you very much DaveD -- http://mail.python.org/mailman/listinfo/python-list
Re: PythonCard - My app stuck when button clicked
On May 13, 7:42 pm, Dave Angel da...@ieee.org wrote: daved170 wrote: Hi there, I'm newbie in pythonCard. I have an application with 2 buttons : START , STOP Start execute a while(1) loop that execute my calculations. Stop suppose to raise a flag that will end that loop. Whenever I pish the START button my GUI is stuck. the calculation executes but I can't push the STOP button. I added thread that START start a thread that execute my calculations. I also added a Global variable that will hold the indication if the loop should continue. The problem now is that the thread ignore that variable and loop forever. Is there a simple way to make sure that the GUI won't stuck (without threads)? And if there isn't such way I would appriciet it very much if anyone could post an example of how to make my thread read that variable Thanks Dave I don't know PythonCard, but most GUI's are similar enough that the concepts will work, even though the details differ. I'll assume that PythonCard has a traditional event loop, from which all events are dispatched. If your loop is fairly small, then you should keep it to one thread. Debugging it will usually be much easier. The trick is to break the task into pieces (each piece might be once around what is now a loop), and invoke one piece each time the event loop empties. I can't tell you how to do that without seeing your loop, but it's not usually very hard. Now, there is some way of POSTing an event to the event loop. That puts the event *after* all the events that are already there, but returns control immediately. So create a custom event, and POST it from the START button's button-pressed event. That will fire off one loop of the special task, in other words, make one function call to your new function. Then at the end of the function, POST it again, unless the STOP button has been pressed in the meantime. An optimization for this is to use coroutines, which are usually done with a generator. It's much trickier to describe, but much easier to accomplish. Roughly, you'd take your existing loop, and put a yield statement in it at appropriate place(s). Then the custom event is simply a call to the .next() function of that generator. Now, threading isn't that tough either, depending on how much data is being shared between the thread and the main program. You say that sharing a global flag isn't working, but it should. So how about if you show us some code, and somebody'll spot the trouble. For example, is the thread defined in the same module as the App? Global only shares between a single module. Another reason globals might seem to fail is if you tried to do mutual imports between two or more modules. (A imports B, which imports A). Sometimes that fails in mysterious ways. Make a simple (stripped) example of what you're trying, and we'll try to find the problem. Without concrete code, we end up with ambiguities like the above usage of two different meanings for the loop.- Hide quoted text - - Show quoted text - Thank's Dave, Here my code, It's a very simple app. the Start button starts a TCP/IP communication and the Stop should dtop it and kill the client. I'll be thankful if you'll be able to spot my mistake. Thanks again Dave #Global Variable bStopLoop = False #Global Function def execute(sockObj): while(!bStopLoop): str = sockObj.recv(1024) tmpStr = Hello + str sockObj.send(tmpStr) #Thread handle class class myThread(threading.Thread): def __init__(self,sockObj): threading.Thread.__init__(self) bStopLoop = False self.sockObj = sockObj def run(self): execute(self.SockObj) # GUI class GUI(model.Background) def on_Start_mouseclick(self,event): try: event.target.enable = False event.target.redraw() self.components.Start.enable = False self.currThread = myThread(self.sockObj) self.currThread.Start() wx.SafeYield(self) self.components.Start.enable = True except: . def on_Stop_mouseclick(self,event): bStopLoop = True -- http://mail.python.org/mailman/listinfo/python-list
Re: PythonCard - My app stuck when button clicked
On May 14, 2:37 pm, Dave Angel da...@ieee.org wrote: daved170 wrote: On May 13, 7:42 pm, Dave Angel da...@ieee.org wrote: daved170 wrote: Hi there, I'm newbie in pythonCard. I have an application with 2 buttons : START , STOP Start execute a while(1) loop that execute my calculations. Stop suppose to raise a flag that will end that loop. Whenever I pish the START button my GUI is stuck. the calculation executes but I can't push the STOP button. I added thread that START start a thread that execute my calculations. I also added a Global variable that will hold the indication if the loop should continue. The problem now is that the thread ignore that variable and loop forever. Is there a simple way to make sure that the GUI won't stuck (without threads)? And if there isn't such way I would appriciet it very much if anyone could post an example of how to make my thread read that variable Thanks Dave I don't know PythonCard, but most GUI's are similar enough that the concepts will work, even though the details differ. I'll assume that PythonCard has a traditional event loop, from which all events are dispatched. If your loop is fairly small, then you should keep it to one thread. Debugging it will usually be much easier. The trick is to break the task into pieces (each piece might be once around what is now a loop), and invoke one piece each time the event loop empties. I can't tell you how to do that without seeing your loop, but it's not usually very hard. Now, there is some way of POSTing an event to the event loop. That puts the event *after* all the events that are already there, but returns control immediately. So create a custom event, and POST it from the START button's button-pressed event. That will fire off one loop of the special task, in other words, make one function call to your new function. Then at the end of the function, POST it again, unless the STOP button has been pressed in the meantime. An optimization for this is to use coroutines, which are usually done with a generator. It's much trickier to describe, but much easier to accomplish. Roughly, you'd take your existing loop, and put a yield statement in it at appropriate place(s). Then the custom event is simply a call to the .next() function of that generator. Now, threading isn't that tough either, depending on how much data is being shared between the thread and the main program. You say that sharing a global flag isn't working, but it should. So how about if you show us some code, and somebody'll spot the trouble. For example, is the thread defined in the same module as the App? Global only shares between a single module. Another reason globals might seem to fail is if you tried to do mutual imports between two or more modules. (A imports B, which imports A). Sometimes that fails in mysterious ways. Make a simple (stripped) example of what you're trying, and we'll try to find the problem. Without concrete code, we end up with ambiguities like the above usage of two different meanings for the loop.- Hide quoted text - - Show quoted text - Thank's Dave, Here my code, It's a very simple app. the Start button starts a TCP/IP communication and the Stop should dtop it and kill the client. I'll be thankful if you'll be able to spot my mistake. Thanks again Dave #Global Variable bStopLoop =alse #Global Function def execute(sockObj): while(!bStopLoop): str =ockObj.recv(1024) tmpStr =Hello + str sockObj.send(tmpStr) #Thread handle class class myThread(threading.Thread): def __init__(self,sockObj): threading.Thread.__init__(self) bStopLoop =alse self.sockObj =ockObj def run(self): execute(self.SockObj) # GUI class GUI(model.Background) def on_Start_mouseclick(self,event): try: event.target.enable =alse event.target.redraw() self.components.Start.enable =alse self.currThread =yThread(self.sockObj) self.currThread.Start() wx.SafeYield(self) self.components.Start.enable =rue except: . def on_Stop_mouseclick(self,event): bStopLoop =rue In the two methods that try to change bStopLoop, you don't declare it global. Add the line global bStopLoop to beginning of both on_Start_mouseclick() and on_Stop_mouseclick(), and (my preference) to function execute() The semantics of global versus local variables for non-nested functions/methods is roughly: if a function or method assigns to a name, it's taken to be a local, unless it's explicitly declared as global.- Hide quoted text - - Show quoted text - Thanks Dave. It works! It solved that problem. Now all i need is to stop the socket in a way that whenever I push the Start button again my app won't raise network connectio, but I think I'll handle
PythonCard - My app stuck when button clicked
Hi there, I'm newbie in pythonCard. I have an application with 2 buttons : START , STOP Start execute a while(1) loop that execute my calculations. Stop suppose to raise a flag that will end that loop. Whenever I pish the START button my GUI is stuck. the calculation executes but I can't push the STOP button. I added thread that START start a thread that execute my calculations. I also added a Global variable that will hold the indication if the loop should continue. The problem now is that the thread ignore that variable and loop forever. Is there a simple way to make sure that the GUI won't stuck (without threads)? And if there isn't such way I would appriciet it very much if anyone could post an example of how to make my thread read that variable Thanks Dave -- http://mail.python.org/mailman/listinfo/python-list