From: Chuck E. <ec...@mi...> - 2000-05-30 12:03:39
|
A reply just came in to my earlier ASP question, but was not CCed to the list. Here it is: >From: "Tom Funk" <td...@td...> >To: "Chuck Esterbrook" <ec...@mi...> >Subject: Re: ASP & Python indentation >Date: Tue, 30 May 2000 07:02:45 -0400 >X-Mailer: Microsoft Outlook Express 5.00.2919.6600 > >Hi,Chuck, > >First, please let me apologize for taking so long to respond -- I've been >installing a new machine at home and it's been fighting me. I'm finally >back online. > >Now on to the meat of your question. > >My experience with indentation when using Python as the ASP scripting >language has come from using Win32. Also, all of the following is from >memory, as I've been away from Python and ASP for a couple of months now. > >Under Win32, ASP Python follows the same rules as for "regular" Python. >Iow, *all* indentation is significant. > > >> The following cases are especially interesting: > >> > * When the contents of the loop go back and forth between HTML and >Python statements wrapped in <% %> > >I've found it best not to use inline constructs like this: > >... > <td>The value is <% string.lower("ABC") %></td> >... > >The interpreter remembers the indentation from any previous Python code and >complains if the new code is indented incorrectly. In a "<%=" construct, I >think the interpreter starts counting spaces after the "=", so, if you have >the wrong number of spaces between your Python expression and the "=", the >interpreter will get sick. Also, when you follow "<%" with one or more >Python expressions, I belive the interpreter starts counting spaces after >the "%". So, the first character is at the left margin, the next is the >second character, etc. I suppose you could keep the indentaion consistent >by adding the correct number of spaces or tabs afterthe "%". I'd think this >would be tedious, though. > >For most of my Python ASP I inserted the "<%" at the left margin, added a >line break and then started the Python code on the left margin, like this: > >... > <td>The value is ><% >string.lower("ABC") >%> > </td> >... > >This sometimes shoots your careful HTML indentation out of the water, but it >keeps the Python interpreter happy. > >If you're using a loop construct, you can do that like this: > >... ><% >for i in range(10): > if i % 2 == 0: > Response.Write("<br>%d is even\n" % (i)) > else: > Response.Write("<br>%d is odd\n" % (i)) >%> > >At first I was tempted to use "<%=" and mix it with HTML text, but that got >confusing. After the two previous indentations (first for the "for" and >next for the "if"), when the interpreter encounters something like: > ><% >for i in range(10): > if i % 2 == 0: >%> > <br><%=i%> is even ><% > else: >%> > <br><%=i%> is odd > >The interpreter gets upset. In this case, the "i" after the "<%=" is not >indented (it's right up against the <%=), so it looks to the interpreter as >though the "i" is sitting at the left margin. Since the interpreter was >expecting to see some code after the "if" statement, you'll get a syntax >error for invalid indentation. If you insert a space after the "<%=" for >readability, as I often do, this looks like you've indented by a single >space. If this is inconsistent with the expected indentation, the >interpreter will complain. > > >> > * When loops are nested > >Nested loops would be the same way: >... ><table> ><th colspan="10">Numbers from 0 to 99, in a 10x10 matrix</th> ><% ># start on the left margin >for i in range(10): > Response.Write("<tr>\n) > # notice the indentation is continued from above > for j in range(10): > Response.Write("<td>%d</td>" % ((i * 10) + j) > Response.Write("\n</tr>\n) >%> ></table> >... > >If you tried to use the "<%=" construct, you'd again get an error because >the indentation is inconsistent. You *can* use the "<%=" construct, but I >think you'd have to follow the equals sign with the approprate number of >spaces/tabs for the indentation to be consistent. Count wrong and you'll >get an error. > >I also tend to use spaces (either two or three spaces per indentation) >instead of tabs and I count them very deliberately. Also, as much as >possible I use function calls whereever I can and define my functions and >classes at the top of the page. The previous example could be rewritten as: > ><%@Language=Python%> ><% ># again, start on the left margin >def printMatrix(rows, cols): > Response.Write("<table>\n") > Response.Write("""<th colspan="%d">Numbers from 0 to %d, in a %dx%d >matrix</th>\n""" % \ > (cols, (rows * cols) - 1, rows, cols) > for i in range(rows): > Response.Write("<tr>\n) > for j in range(cols): > Response.Write("<td>%d</td>" % ((i * cols) + j) > Response.Write("\n</tr>\n) >%> >... ... <% # start back at he margin printMatrix(10,10) %> Of course, you >can use the entire Python language, including creating classes and invoke >methods on those classes to execute your logic. The sky's the limit. I >know this was long, but I hope it helps. -=< tom >=- ----- Original >Message ----- From: "Chuck Esterbrook" To: ; Cc: Sent: Wednesday, May 24, >2000 12:30 PM Subject: ASP & Python indentation > Hi, > > After searching >usenet on deja.com, I found that both of you are successfully running >Python in Microsoft's ASP. > > My group is struggling with indentation >issues/policies for our PSP (Python Server Pages) subproject which could >be considered an alternative to ASP. > > Could you point us to a resource >that explains more about using Python in MS ASP, and/or show us examples >of how indentation works? The following cases are especially >interesting: > * When the contents of the loop go back and forth between >HTML and Python statements wrapped in <% %> > * When loops are nested > > >If you like, you can learn more about our project at: >http://webware.sourceforge.net > > Any help is appreciated in >advance! > > > -Chuck > > |