[Python-ideas] An Alternate Suite Delineation Syntax For Python? (was Re: [Python-Dev] [PATCH] Adding braces to __future__)
Bruce Leban
bruce at leapyear.org
Mon Dec 12 07:09:06 CET 2011
More information about the Python-ideas mailing list
Mon Dec 12 07:09:06 CET 2011
- Previous message: [Python-ideas] An Alternate Suite Delineation Syntax For Python? (was Re: [Python-Dev] [PATCH] Adding braces to __future__)
- Next message: [Python-ideas] An Alternate Suite Delineation Syntax For Python? (was Re: [Python-Dev] [PATCH] Adding braces to __future__)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Sun, Dec 11, 2011 at 9:21 PM, Greg Ewing <greg.ewing at canterbury.ac.nz>wrote: > Mike Meyer wrote: >> >> With significant whitespace, you have to keep explicit track of the >>> current nesting level in order to generate the appropriate >>> indent. With real delimiters, you can drop that. >>> >> > Yes, but with an appropriate design you can arrange for that to > be done in only *one* place. There's no need for every method > that generates code to know about indentation levels. > I've written this code (for HTML) and it's not only straightforward, it makes the code easier to write and more robust. And you can make the closing automatic with a context manager. Here's how you might write some HTML: with html_tag('table'): with html_tag('tr'): for x in data: with html_tag('td'): # do stuff with x On the other hand, this might not help with template languages as Nick points out: On Sun, Dec 11, 2011 at 2:30 AM, Nick Coghlan <ncoghlan at gmail.com> wrote: > <% if danger_level > 3 {: %> > <div class="alert"><% if danger_level == 5 {: %>EXTREME <% :} > %>DANGER ALERT!</div> > > <% elif danger_level > 0 {: %> > <div>Some chance of danger</div> > > <% else {: %> > <div>No danger</div> > > <% :} %> > > <% for a in ['cat', 'dog', 'rabbit'] {: %> > <h2><%= a %></h2> > <p><%= describe_animal(a) %></p> > > <% :} %> While I thought his example was compelling at first, after thinking it through I realized that any existing template language that generates Python has to be aware of indentation and it's going to have to modify the indentation of the included Python code. So it would need to either read the indentation or read the {: and :} tokens. So what if we think in terms of modifying the template language instead of Python? In the code below, I've added two extra tokens to the template language: %{> which increases the indentation and <}% which decreases it. All other leading spaces would be stripped. Here's that example rewritten: <% if danger_level > 3: %{> <div class="alert"><% if danger_level == 5: %{>EXTREME <}% %>DANGER ALERT!</div> <% elif danger_level > 0: %{> <div>Some chance of danger</div> <% else: %{> <div>No danger</div> <}%> <% for a in ['cat', 'dog', 'rabbit']: %{> <h2><%= a %></h2> <p><%= describe_animal(a) %></p> <}%> This isn't a perfect design. In particular %{> wouldn't be necessary if the template language parses the embedded code, but it's sufficient to illustrate the point. --- Bruce Follow me: http://www.twitter.com/Vroo http://www.vroospeak.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-ideas/attachments/20111211/0350525e/attachment.html>
- Previous message: [Python-ideas] An Alternate Suite Delineation Syntax For Python? (was Re: [Python-Dev] [PATCH] Adding braces to __future__)
- Next message: [Python-ideas] An Alternate Suite Delineation Syntax For Python? (was Re: [Python-Dev] [PATCH] Adding braces to __future__)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-ideas mailing list