Capture real time data and update in text control : example signal catured to be displayed one by one in wxPython

There’s a little utility I tripped across that may help (or confuse)
capturing output to a file.

YMMV !

···

http://groups.google.com/group/comp.lang.python/browse_thread/thread/d25a9f5608e473af/?hide_quotes=no#msg_a3aff8e250179e30

Thanx Ray .... i ll check it out and try to implemet it ...thanx , any
more problem i ll have to ask you guys back.

···

On Jul 10, 1:35 am, Ray Pasco <pas...@verizon.net> wrote:

There's a little utility I tripped across that may help (or confuse) capturing output to a file.
YMMV !

'''
PrintTee.py
Based on Mike Muller's "Writer"
Modified by Ray Pasco
It is a true Python "tee-display-output-also-to-a-file" print utility.
I suppose the 2 functions could be incorporated into the PrintTee class,
but, don't destroy or allow to the destroyed the PrintTee instantiation object too soon !
'''
import sys, os
# ---------------------------
class PrintTee :
def __init__( self, *writers ) :
self.writers = writers

def write\( self, text \) :
    for w in self\.writers :
        w\.write\( text \)

#end class PrintTee
# ---------------------------
def PrintTeeOn( printTeeFilename ):
sysStdoutSaved = sys.stdout
outFile = file( printTeeFilename, 'a' )
sys.stdout = PrintTee( sys.stdout, outFile )
return ( sysStdoutSaved, outFile )
#end def
# ---------------------------
def PrintTeeOff( sysStdoutSaved, printTeeFile ):

sys\.stdout = sysStdoutSaved
printTeeFile\.close\(\)

#end def
# ---------------------------
if __name__ == '__main__' :

print &#39;\\n\-\-\-\-  1\. This line SHOULD NOT be in the PrintTee output file\.\\n&#39;

printTeeFilename = &#39;PRINTTEE\.LOG&#39;
sysStdoutSaved, printTeeFile = PrintTeeOn\( printTeeFilename \)


print &quot;There you go 1\.&quot;
print &quot;There you go 2\.&quot;
print &quot;There you go 3\., This line \# is&quot;, 2\+1
print &quot;There you go 4\.&quot;
print &quot;There you go 5\.&quot;


PrintTeeOff\( sysStdoutSaved, printTeeFile \)

print &#39;\\n\-\-\-\-  2\. This line SHOULD NOT end up in the PrintTee output file\.\\n&#39;

sysCmdStr = &#39;START Notepad\.exe &#39; \+ printTeeFilename

os\.system\( sysCmdStr \)

#end if __name__

Thanks everyone for the help and guides , i search out that with
proper use of a worker function for carrying out the transfer using
thread and by use of wx.CallAfter() we can resolve the problem..Thanx
all.

···

On Jul 10, 2:52 pm, anil ph <ph.anilsha...@gmail.com> wrote:

Thanx Ray .... i ll check it out and try to implemet it ...thanx , any
more problem i ll have to ask you guys back.

On Jul 10, 1:35 am, Ray Pasco <pas...@verizon.net> wrote:

> There's a little utility I tripped across that may help (or confuse) capturing output to a file.
> YMMV !
> =======================================
> '''
> PrintTee.py
> Based on Mike Muller's "Writer"
> Modified by Ray Pasco
> It is a true Python "tee-display-output-also-to-a-file" print utility.
> I suppose the 2 functions could be incorporated into the PrintTee class,
> but, don't destroy or allow to the destroyed the PrintTee instantiation object too soon !
> '''
> import sys, os
> # ---------------------------
> class PrintTee :
> def __init__( self, *writers ) :
> self.writers = writers
>
> def write( self, text ) :
> for w in self.writers :
> w.write( text )
> #end class PrintTee
> # ---------------------------
> def PrintTeeOn( printTeeFilename ):
> sysStdoutSaved = sys.stdout
> outFile = file( printTeeFilename, 'a' )
> sys.stdout = PrintTee( sys.stdout, outFile )
> return ( sysStdoutSaved, outFile )
> #end def
> # ---------------------------
> def PrintTeeOff( sysStdoutSaved, printTeeFile ):
>
> sys.stdout = sysStdoutSaved
> printTeeFile.close()
>
> #end def
> # ---------------------------
> if __name__ == '__main__' :
>
> print '\n---- 1. This line SHOULD NOT be in the PrintTee output file.\n'
>
> printTeeFilename = 'PRINTTEE.LOG'
> sysStdoutSaved, printTeeFile = PrintTeeOn( printTeeFilename )
>
>
> print "There you go 1."
> print "There you go 2."
> print "There you go 3., This line # is", 2+1
> print "There you go 4."
> print "There you go 5."
>
>
> PrintTeeOff( sysStdoutSaved, printTeeFile )
>
> print '\n---- 2. This line SHOULD NOT end up in the PrintTee output file.\n'
>
> sysCmdStr = 'START Notepad.exe ' + printTeeFilename
>
> os.system( sysCmdStr )
>
> #end if __name__
> =======================================

There is the possibility to intercept exceptions and

show as done to stdout?


Fabio Spadaro
www.fabiospadaro.com

You can use the traceback module to get that info. Just use it in the except portion of your try/except block to grab the text in the traceback and then send it to stdout.

···

Mike Driscoll

Blog: http://blog.pythonlibrary.org

Hi,

Hi.

Sometimes make mistakes in programming. Absurd to suppose that I
put a statement like "1 / 0" and I don't see it, I
would intercept unhandled exceptions.
For example, if put at the beginning of the script above:
sys.stderr = open ("err.txt", "w")
in theory it should work but do not.
Is there any other way to write to a file unhandled exceptions?

Yes,

During the startup of your program replace 'sys.excepthook' with your
own exception hook handler.

def ExceptionHook(exctype, value, trace):
    exc = traceback.format_exception(exctype, value, trace)
    ...
    logfile.write(exc)

sys.excepthook = ExceptionHook

When an unhandled exception is thrown your exception hook handler will
be called.

Cody

···

On Fri, Jul 16, 2010 at 3:38 PM, Fabio Spadaro <fabiolinospad@gmail.com> wrote:

Hi.

Hi,

Hi.

Sometimes make mistakes in programming. Absurd to suppose that I

put a statement like “1 / 0” and I don’t see it, I

would intercept unhandled exceptions.

For example, if put at the beginning of the script above:

sys.stderr = open (“err.txt”, “w”)

in theory it should work but do not.

Is there any other way to write to a file unhandled exceptions?

Yes,

During the startup of your program replace ‘sys.excepthook’ with your

own exception hook handler.

def ExceptionHook(exctype, value, trace):

exc = traceback.format_exception(exctype, value, trace)

...

logfile.write(exc)

sys.excepthook = ExceptionHook

When an unhandled exception is thrown your exception hook handler will

be called.

Cody

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

I tried with this simple script, but no go. Logfile file is empty.
import sys, os

import traceback

logfile = open(‘error.txt’,‘w’)

def ExceptionHook(exctype, value, trace):

print ‘entro qui’

exc = traceback.format_exception(exctype, value, trace)

logfile.write(exc)

logfile.close()

sys.excepthook = ExceptionHook

class PrintTee :

def init(self ) :

1/0

if name == ‘main’ :

a = PrintTee()

···

2010/7/16 Cody Precord codyprecord@gmail.com

On Fri, Jul 16, 2010 at 3:38 PM, Fabio Spadaro fabiolinospad@gmail.com wrote:


Fabio Spadaro
www.fabiospadaro.com

Hi,

···

On Fri, Jul 16, 2010 at 4:04 PM, Fabio Spadaro <fabiolinospad@gmail.com> wrote:

Hi.

2010/7/16 Cody Precord <codyprecord@gmail.com>

Hi,

On Fri, Jul 16, 2010 at 3:38 PM, Fabio Spadaro <fabiolinospad@gmail.com> >> wrote:
> Hi.
>
>
> Sometimes make mistakes in programming. Absurd to suppose that I
> put a statement like "1 / 0" and I don't see it, I
> would intercept unhandled exceptions.
> For example, if put at the beginning of the script above:
> sys.stderr = open ("err.txt", "w")
> in theory it should work but do not.
> Is there any other way to write to a file unhandled exceptions?

Yes,

During the startup of your program replace 'sys.excepthook' with your
own exception hook handler.

def ExceptionHook(exctype, value, trace):
exc = traceback.format_exception(exctype, value, trace)
...
logfile.write(exc)

sys.excepthook = ExceptionHook

When an unhandled exception is thrown your exception hook handler will
be called.

Cody

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

I tried with this simple script, but no go. Logfile file is empty.
import sys, os
import traceback
logfile = open('error.txt','w')
def ExceptionHook(exctype, value, trace):
print 'entro qui'
exc = traceback.format_exception(exctype, value, trace)
logfile.write(exc)
logfile.close()
sys.excepthook = ExceptionHook

class PrintTee :
def __init__(self ) :
1/0
if __name__ == '__main__' :
a = PrintTee()

Did you look at the console output after running this?

If you did you would have caught my typo.

format_exception returns a list need to convert to string before
writing to the file.

i.e) write( "".join(exc))

Cody

Hi.

Hi,

Hi.

Hi,

Hi.

Sometimes make mistakes in programming. Absurd to suppose that I

put a statement like “1 / 0” and I don’t see it, I

would intercept unhandled exceptions.

For example, if put at the beginning of the script above:

sys.stderr = open (“err.txt”, “w”)

in theory it should work but do not.

Is there any other way to write to a file unhandled exceptions?

Yes,

During the startup of your program replace ‘sys.excepthook’ with your

own exception hook handler.

def ExceptionHook(exctype, value, trace):

exc = traceback.format_exception(exctype, value, trace)

logfile.write(exc)

sys.excepthook = ExceptionHook

When an unhandled exception is thrown your exception hook handler will

be called.

Cody

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

I tried with this simple script, but no go. Logfile file is empty.

import sys, os

import traceback

logfile = open(‘error.txt’,‘w’)

def ExceptionHook(exctype, value, trace):

print 'entro qui'
exc = traceback.format_exception(exctype, value, trace)
logfile.write(exc)
logfile.close()

sys.excepthook = ExceptionHook

class PrintTee :

def __init__(self ) :
    1/0

if name == ‘main’ :

a = PrintTee()

Did you look at the console output after running this?

If you did you would have caught my typo.

format_exception returns a list need to convert to string before

writing to the file.

i.e) write( “”.join(exc))

Cody

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

Did you look at the console output after running this?

Yes.

Traceback (most recent call last):

File “/home/fabio/Scrivania/python/Archivia/print.py”, line 19, in

a = PrintTee()

File “/home/fabio/Scrivania/python/Archivia/print.py”, line 16, in init

1/0

ZeroDivisionError: integer division or modulo by zero

If you did you would have caught my typo.

format_exception returns a list need to convert to string before
writing to the file.
i.e) write( “”.join(exc))

Done but does not work. logfile always empty.

def ExceptionHook(exctype, value, trace):

print ‘entro qui’

exc = traceback.format_exception(exctype, value, trace)

**logfile.write( “”.join(exc))

logfile.close()

···

2010/7/16 Cody Precord codyprecord@gmail.com

On Fri, Jul 16, 2010 at 4:04 PM, Fabio Spadaro fabiolinospad@gmail.com wrote:

2010/7/16 Cody Precord codyprecord@gmail.com

On Fri, Jul 16, 2010 at 3:38 PM, Fabio Spadaro fabiolinospad@gmail.com > > >> wrote:


Fabio Spadaro
www.fabiospadaro.com

Hi,

Hi.

Hi,

> Hi.
>
>>
>> Hi,
>>
>> > Hi.
>> >
>> >
>> > Sometimes make mistakes in programming. Absurd to suppose that I
>> > put a statement like "1 / 0" and I don't see it, I
>> > would intercept unhandled exceptions.
>> > For example, if put at the beginning of the script above:
>> > sys.stderr = open ("err.txt", "w")
>> > in theory it should work but do not.
>> > Is there any other way to write to a file unhandled exceptions?
>>
>> Yes,
>>
>> During the startup of your program replace 'sys.excepthook' with your
>> own exception hook handler.
>>
>> def ExceptionHook(exctype, value, trace):
>> exc = traceback.format_exception(exctype, value, trace)
>> ...
>> logfile.write(exc)
>>
>> sys.excepthook = ExceptionHook
>>
>> When an unhandled exception is thrown your exception hook handler will
>> be called.
>>
>>
>> Cody
>>
>> --
>> To unsubscribe, send email to
>> wxPython-users+unsubscribe@googlegroups.com
>> or visit http://groups.google.com/group/wxPython-users?hl=en
>
> I tried with this simple script, but no go. Logfile file is empty.
> import sys, os
> import traceback
> logfile = open('error.txt','w')
> def ExceptionHook(exctype, value, trace):
> print 'entro qui'
> exc = traceback.format_exception(exctype, value, trace)
> logfile.write(exc)
> logfile.close()
> sys.excepthook = ExceptionHook
>
> class PrintTee :
> def __init__(self ) :
> 1/0
> if __name__ == '__main__' :
> a = PrintTee()

Did you look at the console output after running this?

If you did you would have caught my typo.

format_exception returns a list need to convert to string before
writing to the file.

i.e) write( "".join(exc))

Cody

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

Did you look at the console output after running this?

Yes.
Traceback (most recent call last):
File "/home/fabio/Scrivania/python/Archivia/print.py", line 19, in
<module>
a = PrintTee()
File "/home/fabio/Scrivania/python/Archivia/print.py", line 16, in
__init__
1/0
ZeroDivisionError: integer division or modulo by zero

If you did you would have caught my typo.
format_exception returns a list need to convert to string before
writing to the file.
i.e) write( "".join(exc))

Done but does not work. logfile always empty.
def ExceptionHook(exctype, value, trace):
print 'entro qui'
exc = traceback.format_exception(exctype, value, trace)
logfile.write( "".join(exc))
logfile.close()

Before changing the "".join(exc) you should have seen the following in
the console

"""
entro qui
Error in sys.excepthook:
Traceback (most recent call last):
  File "junk.py", line 7, in ExceptionHook
    logfile.write(exc)
TypeError: argument 1 must be string or read-only character buffer, not list

Original exception was:
Traceback (most recent call last):
  File "junk.py", line 19, in <module>
    a = PrintTee()
  File "junk.py", line 16, in __init__
    1/0
ZeroDivisionError: integer division or modulo by zero
"""

I copied and pasted you code (plus the "".join(exc) change) and ran
it and it writes the ZeroDivision error to the the error.txt file just
fine.

What version of python are you using?

Cody

···

On Fri, Jul 16, 2010 at 4:30 PM, Fabio Spadaro <fabiolinospad@gmail.com> wrote:

2010/7/16 Cody Precord <codyprecord@gmail.com>

On Fri, Jul 16, 2010 at 4:04 PM, Fabio Spadaro <fabiolinospad@gmail.com> >> wrote:
> 2010/7/16 Cody Precord <codyprecord@gmail.com>
>> On Fri, Jul 16, 2010 at 3:38 PM, Fabio Spadaro >> >> <fabiolinospad@gmail.com> >> >> wrote:

Python 2.6.5 on Ubuntu 10.4
Strange, I see only the exception ZeroDivision with
logfile.write(exc).

···

2010/7/16 Cody Precord codyprecord@gmail.com

Hi,

On Fri, Jul 16, 2010 at 4:30 PM, Fabio Spadaro fabiolinospad@gmail.com wrote:

Hi.

2010/7/16 Cody Precord codyprecord@gmail.com

Hi,

On Fri, Jul 16, 2010 at 4:04 PM, Fabio Spadaro fabiolinospad@gmail.com > > >> wrote:

Hi.

2010/7/16 Cody Precord codyprecord@gmail.com

Hi,

On Fri, Jul 16, 2010 at 3:38 PM, Fabio Spadaro > > >> >> fabiolinospad@gmail.com > > >> >> wrote:

Hi.

Sometimes make mistakes in programming. Absurd to suppose that I

put a statement like “1 / 0” and I don’t see it, I

would intercept unhandled exceptions.

For example, if put at the beginning of the script above:

sys.stderr = open (“err.txt”, “w”)

in theory it should work but do not.

Is there any other way to write to a file unhandled exceptions?

Yes,

During the startup of your program replace ‘sys.excepthook’ with your

own exception hook handler.

def ExceptionHook(exctype, value, trace):

exc = traceback.format_exception(exctype, value, trace)

logfile.write(exc)

sys.excepthook = ExceptionHook

When an unhandled exception is thrown your exception hook handler will

be called.

Cody

To unsubscribe, send email to

wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

I tried with this simple script, but no go. Logfile file is empty.

import sys, os

import traceback

logfile = open(‘error.txt’,‘w’)

def ExceptionHook(exctype, value, trace):

print 'entro qui'
exc = traceback.format_exception(exctype, value, trace)
logfile.write(exc)
logfile.close()

sys.excepthook = ExceptionHook

class PrintTee :

def __init__(self ) :
    1/0

if name == ‘main’ :

a = PrintTee()

Did you look at the console output after running this?

If you did you would have caught my typo.

format_exception returns a list need to convert to string before

writing to the file.

i.e) write( “”.join(exc))

Cody

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

Did you look at the console output after running this?

Yes.

Traceback (most recent call last):

File “/home/fabio/Scrivania/python/Archivia/print.py”, line 19, in

a = PrintTee()

File “/home/fabio/Scrivania/python/Archivia/print.py”, line 16, in

init

1/0

ZeroDivisionError: integer division or modulo by zero

If you did you would have caught my typo.

format_exception returns a list need to convert to string before

writing to the file.

i.e) write( “”.join(exc))

Done but does not work. logfile always empty.

def ExceptionHook(exctype, value, trace):

print 'entro qui'
exc = traceback.format_exception(exctype, value, trace)
logfile.write( "".join(exc))
logfile.close()

Before changing the “”.join(exc) you should have seen the following in

the console

“”"

entro qui

Error in sys.excepthook:
Traceback (most recent call last):

File “junk.py”, line 7, in ExceptionHook

logfile.write(exc)

TypeError: argument 1 must be string or read-only character buffer, not list

Original exception was:
Traceback (most recent call last):

File “junk.py”, line 19, in

a = PrintTee()

File “junk.py”, line 16, in init
1/0

ZeroDivisionError: integer division or modulo by zero

“”"

I copied and pasted you code (plus the “”.join(exc) change) and ran

it and it writes the ZeroDivision error to the the error.txt file just

fine.

What version of python are you using?

Cody

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en


Fabio Spadaro
www.fabiospadaro.com

Hi

Python 2.6.5 on Ubuntu 10.4
Strange, I see only the exception ZeroDivision with logfile.write(exc).

I am using the same version of Python but on Windows at the moment. I
have used this same approach for a number of years in different
applications without issue.

So I don't see any reason why it shouldn't work on your setup. If you
are not even seeing your print statement being executed then it would
mean that the exceptionhook is not getting called. Any different
results with different exception types (i.e raise OSError)?

http://docs.python.org/release/2.6/library/sys.html

Cody

···

On Fri, Jul 16, 2010 at 5:07 PM, Fabio Spadaro <fabiolinospad@gmail.com> wrote: