quick search:
 

Print debugging message without print

Submitted by: peterbe
Last Edited: 2001-08-01

Category: Python(Script)

Average rating is: 0.0 out of 5 (0 ratings)

Description:
When working with External Methods and developing Python code you can sometimes use 'print variable' to print to stdout the value of things without disturbing the workflow of the method or function that in the end only will "act" on proper returns or response redirects.

That's very nifty and can't be done in Python Scripts unless you use my little simple trick.


Save and change the code in a file called w2f.py in [zopehome]/Extensions and create an External Method instance in the root of your zope.

Now you can use it like any other print statement and don't worry about what you pass it. Everything is converted to a string.

If you'd like to reset the loggings, pass the parameter "reset=1" to it.

Example usage:

goto=context.REQUEST.URL1+'?page=here'
context.w2f(goto)
return goto

Then you can open the text file and see what it wrote.



Source (Text):
def w2f(self,s,reset=0):
    print "Using Write2File module"

    fileplace='c:\\peter\\file.txt'

    s=str(s)
    content=""
    if not reset:
        try:
            r=open(fileplace,'r')
            content=r.read()
            r.close()
            del r
        except:pass
    f=open(fileplace,'w')
    f.write('From %s:\n%s\n\n'%(self.title_or_id(),s))
    f.write(content+'\n')

    f.close()
    del f
    return "done"

Explanation:
Unclear?

Comments:

arguments and keywords by peterbe - 2001-08-01
one could do this perhaps to enhance it a litte bit::

 def w2f(self,*args,**kws):
    if 'reset' in args or 'reset' in kws.keys():
        reset=0

That way, one could call the w2f() like this:

 context.w2f('The script','reset',firstname='Peter')

My intention was just to hint you of how to be able to print from Python Scripts that use 'return'