strange 'print' behavior in Python Scripts
I'm running into problems because Zope is printing a space (" ") at the beginning of each line when 'print'ing from a Python Script. I'm trying to print out XML to a Java Applet and the Applet isn't smart enough to take this into account. Bad parser, but Zope shouldn't be doing this anyway. Any fix that I can do? -Chris -- -------------------------------------------------------------------- Christopher N. Deckard | Lead Web Systems Developer cnd@ecn.purdue.edu | Engineering Computer Network http://www.ecn.purdue.edu/ | Purdue University ---- zlib.decompress('x\234K\316Kq((-J)M\325KM)\005\000)"\005w') ---
Clarification, this happens when using triple quotes print ''' foo bar ''' Actually prints: foo bar -Chris "Christopher N. Deckard" wrote:
I'm running into problems because Zope is printing a space (" ") at the beginning of each line when 'print'ing from a Python Script. I'm trying to print out XML to a Java Applet and the Applet isn't smart enough to take this into account. Bad parser, but Zope shouldn't be doing this anyway. Any fix that I can do?
-Chris
-- -------------------------------------------------------------------- Christopher N. Deckard | Lead Web Systems Developer cnd@ecn.purdue.edu | Engineering Computer Network http://www.ecn.purdue.edu/ | Purdue University ---- zlib.decompress('x\234K\316Kq((-J)M\325KM)\005\000)"\005w') ---
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
From: "Christopher N. Deckard" <cnd@ecn.purdue.edu>
Clarification, this happens when using triple quotes
print ''' foo bar '''
Actually prints: foo bar
Ah, I knew this would bite somebody someday, but you're the first to mention it. When a Script is compiled, it is actually converted into a function by prefixing it with a "def f(params..." line. If the body isn't indented, it won't compile, so it indents it one space. You can get around this by manually indenting. In other words: ## Script (Python) "indented" print ''' foo bar ''' return printed Cheers, Evan @ digicool
From: "Christopher N. Deckard" <cnd@ecn.purdue.edu>
Clarification, this happens when using triple quotes
print ''' foo bar '''
Actually prints: foo bar
Ah, I knew this would bite somebody someday, but you're the first to mention it. When a Script is compiled, it is actually converted into a function by prefixing it with a "def f(params..." line. If the body isn't indented, it won't compile, so it indents it one space. You can get around this by manually indenting. In other words:
## Script (Python) "indented" print ''' foo bar ''' return printed
Hmm, this was not the answer I was looking for. Are there any thoughts on fixing this in the future? I've fun into it many times now and couldn't figure out what was going on. It is especially bad when printing a textarea for a form. Everything comes out indented a space. I'll just go line by line for now. Thanks for the answer. -Chris
I suppose this has to do with the way PythonScripts are interpreted. As you may have noticed, Zope prepends a function definition line like "def your_id(args...):" to the text of your PythonScript before evaluating it (that is why errors in PythonScripts are reported with the line count off by one). In order to make the whole block syntatically valid, the body of the function must be indented relative to the function definition. So before evaluating your function, Zope must prepend a space in each line. It should be smart enough to treat triple-quotes differently, but it isn't. Now, I think the need to use triple-quoted strings can imply a design problem: it means you are embedding the template in your code, which is often a bad idea. Without knowing what you are doing, I would try and separate the embedded template from the Python logic. Regards, Luciano "Christopher N. Deckard" wrote:
From: "Christopher N. Deckard" <cnd@ecn.purdue.edu>
Clarification, this happens when using triple quotes
print ''' foo bar '''
Actually prints: foo bar
Ah, I knew this would bite somebody someday, but you're the first to mention it. When a Script is compiled, it is actually converted into a function by prefixing it with a "def f(params..." line. If the body isn't indented, it won't compile, so it indents it one space. You can get around this by manually indenting. In other words:
## Script (Python) "indented" print ''' foo bar ''' return printed
Hmm, this was not the answer I was looking for. Are there any thoughts on fixing this in the future? I've fun into it many times now and couldn't figure out what was going on. It is especially bad when printing a textarea for a form. Everything comes out indented a space.
I'll just go line by line for now. Thanks for the answer.
-Chris
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
I suppose this has to do with the way PythonScripts are interpreted. As you may have noticed, Zope prepends a function definition line like "def your_id(args...):" to the text of your PythonScript before evaluating it (that is why errors in PythonScripts are reported with the line count off by one).
In order to make the whole block syntatically valid, the body of the function must be indented relative to the function definition. So before evaluating your function, Zope must prepend a space in each line. It should be smart enough to treat triple-quotes differently, but it isn't.
Now, I think the need to use triple-quoted strings can imply a design problem: it means you are embedding the template in your code, which is often a bad idea. Without knowing what you are doing, I would try and separate the embedded template from the Python logic.
Just started getting into the nitty and gritty of Python Scripts ( I've been using 2.2.5 for a while previously ), I was wondering if we couldn't just put the whole def: statement in the data box. It would make porting that much easier, if I could just stick a whole python script in there, def statement and all. and if it was all already indented, so much the better. me = { 'name' : 'Zachery Bir', 'email' : 'zbir@urbanape.com', 'voice' : '(804) 353-3742', 'url' : 'http://www.urbanape.com/' }
Hmmm, ok. I see your point there. :-) For rapidly throwing something together though, it is easy to triple quote things and get it to work and then do it right. Guess I could have just used DTML and change the content type. Again, thanks for the advice. I'll try and do better about seperating the templates out from the logic. -Chris Luciano Ramalho wrote:
I suppose this has to do with the way PythonScripts are interpreted. As you may have noticed, Zope prepends a function definition line like "def your_id(args...):" to the text of your PythonScript before evaluating it (that is why errors in PythonScripts are reported with the line count off by one).
In order to make the whole block syntatically valid, the body of the function must be indented relative to the function definition. So before evaluating your function, Zope must prepend a space in each line. It should be smart enough to treat triple-quotes differently, but it isn't.
Now, I think the need to use triple-quoted strings can imply a design problem: it means you are embedding the template in your code, which is often a bad idea. Without knowing what you are doing, I would try and separate the embedded template from the Python logic.
Regards,
Luciano
"Christopher N. Deckard" wrote:
From: "Christopher N. Deckard" <cnd@ecn.purdue.edu>
Clarification, this happens when using triple quotes
print ''' foo bar '''
Actually prints: foo bar
Ah, I knew this would bite somebody someday, but you're the first to mention it. When a Script is compiled, it is actually converted into a function by prefixing it with a "def f(params..." line. If the body isn't indented, it won't compile, so it indents it one space. You can get around this by manually indenting. In other words:
## Script (Python) "indented" print ''' foo bar ''' return printed
Hmm, this was not the answer I was looking for. Are there any thoughts on fixing this in the future? I've fun into it many times now and couldn't figure out what was going on. It is especially bad when printing a textarea for a form. Everything comes out indented a space.
I'll just go line by line for now. Thanks for the answer.
-Chris
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
On Fri, 29 Jun 2001, Christopher N. Deckard wrote:
From: "Christopher N. Deckard" <cnd@ecn.purdue.edu>
Clarification, this happens when using triple quotes
print ''' foo bar '''
Actually prints: foo bar
Ah, I knew this would bite somebody someday, but you're the first to mention it. When a Script is compiled, it is actually converted into a function by prefixing it with a "def f(params..." line. If the body isn't indented, it won't compile, so it indents it one space. You can get around this by manually indenting. In other words:
## Script (Python) "indented" print ''' foo bar ''' return printed
Hmm, this was not the answer I was looking for. Are there any thoughts on fixing this in the future? I've fun into it many times now and couldn't figure out what was going on. It is especially bad when printing a textarea for a form. Everything comes out indented a space.
I'll just go line by line for now. Thanks for the answer.
You can use DTML to format your XML: <dtml-call "RESPONSE.setHeader('Content-Type', 'text/xml')"> <xml> <name><dtml-var name></name> </xml> You can do any logic necessary in python scripts, and call them from DTML. DTML is meant for this task. -Michel
participants (5)
-
Christopher N. Deckard -
Evan Simpson -
Luciano Ramalho -
Michel Pelletier -
zbir@urbanape.com