Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #11576

Re: Idea for pure-python templates using AST.

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <anand.ibmgsi@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'debug': 0.03; 'python.': 0.04; 'context': 0.04; 'subject:using': 0.04; 'python:': 0.05; 'think,': 0.05; ':-)': 0.06; 'attribute': 0.07; 'statements,': 0.07; 'transform': 0.07; 'python': 0.08; 'contexts': 0.09; 'expressions.': 0.09; 'libraries.': 0.09; 'namespace': 0.09; 'preparation,': 0.09; 'syntax.': 0.09; 'template.': 0.09; 'subject:python': 0.11; 'def': 0.15; '16,': 0.15; "'a'": 0.16; '60,': 0.16; 'approach,': 0.16; 'ast,': 0.16; 'genshi': 0.16; 'idea:': 0.16; 'ive': 0.16; 'renders': 0.16; 'skip:\xc2 30': 0.16; 'templating': 0.16; 'wouldnt': 0.16; '\xc2\xa0if': 0.16; 'looked': 0.16; 'syntax': 0.16; 'wrote:': 0.16; 'perl': 0.18; 'insert': 0.19; 'template': 0.19; 'received:74.125.82.174': 0.19; 'received :mail-wy0-f174.google.com': 0.19; 'appropriate': 0.20; 'posting': 0.20; '(most': 0.21; 'maybe': 0.21; 'header:In-Reply-To:1': 0.22; 'tue,': 0.23; '(or': 0.23; 'calls.': 0.23; 'literal': 0.23; 'translated': 0.23; 'testing': 0.24; 'aug': 0.24; 'traceback': 0.24; 'code': 0.25; 'creating': 0.25; 'statement': 0.25; 'tests': 0.25; 'code.': 0.26; 'skip:b 20': 0.26; "i'm": 0.27; 'function': 0.27; 'paul': 0.28; 'all,': 0.28; 'classes': 0.28; 'separate': 0.28; 'done.': 0.28; 'effect': 0.28; 'lee': 0.28; 'url:mailman': 0.28; 'correct': 0.28; 'message-id:@mail.gmail.com': 0.29; 'environment.': 0.30; 'anand': 0.30; 'bare': 0.30; 'context,': 0.30; 'filters,': 0.30; 'parent': 0.30; 'recursively': 0.30; 'skip:\xc2 20': 0.30; 'strings,': 0.30; 'worlds': 0.30; 'thanks': 0.30; "skip:' 10": 0.30; 'class': 0.30; 'error': 0.32; 'values': 0.32; 'pure': 0.32; 'this.': 0.32; 'comment': 0.32; 'it.': 0.33; 'too': 0.33; 'there': 0.33; 'to:addr:python-list': 0.33; 'url:listinfo': 0.33; 'idea': 0.34; 'all.': 0.34; 'improvements': 0.34; 'last):': 0.34; 'object': 0.35; 'skip:" 20': 0.35; 'url:python': 0.36; 'doing': 0.36; 'aspect': 0.37; 'using': 0.37; 'but': 0.37; 'could': 0.38; 'think': 0.38; 'received:74.125.82': 0.38; 'allows': 0.38; 'received:google.com': 0.38; '8bit%:8': 0.38; 'url:org': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'feed': 0.39; 'php': 0.39; 'help': 0.39; 'either': 0.39; 'love': 0.62; 'below': 0.62; 'manager,': 0.62; 'lives': 0.63; 'power': 0.63; 'truth': 0.64; 'bottom': 0.64; 'here': 0.65; 'man': 0.65; 'plus': 0.65; 'ever': 0.65; 'share': 0.65; 'here.': 0.66; '\xc2\xa0\xc2\xa0': 0.67; 'kept': 0.68; 'legal': 0.70; 'concept': 0.72; 'design.': 0.73; 'skip:\xc2 10': 0.74; 'bear': 0.80; 'balanced': 0.84; 'blow': 0.84; 'face,': 0.84; 'pain': 0.84; 'subject:Idea': 0.84; 'technique.': 0.84; 'templates)': 0.84; 'render': 0.91; 'templates,': 0.91; 'technique': 0.93; 'url:site': 0.95
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=XdV2MDaUy8LeXeSgRU8TITU7OLRzBHmKhUBjgtD5Xis=; b=o8btopOPYJoFy6PDKgCzc2ls392bU6hFinzAQXjlbYLvHoXjKD03H02KCILXnUJ3ha pL4HVnrZ09xvckd9Ba0G64uoJWndTrL+OQ321RHTVsfyg47pHK4jPg2+nfoVz18M5/Vg 6/C2z4oFm6QyqbB/e+xALJ9Zou2yuD6vU4ha4=
MIME-Version 1.0
In-Reply-To <5bed8db1-a4d7-4788-a389-69c10b47fbd1@a15g2000yqb.googlegroups.com>
References <2As2q.636$7r4.389@viwinnwfe02.internal.bigpond.com> <5bed8db1-a4d7-4788-a389-69c10b47fbd1@a15g2000yqb.googlegroups.com>
From anand jeyahar <anand.ibmgsi@gmail.com>
Date Tue, 16 Aug 2011 21:46:07 +0530
Subject Re: Idea for pure-python templates using AST.
To python-list@python.org
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.80.1313511388.27778.python-list@python.org> (permalink)
Lines 162
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1313511388 news.xs4all.nl 23835 [2001:888:2000:d::a6]:58551
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:11576

Show key headers only | View raw


Hi all,
   I did it. Finally managed to port mysqltuner.pl to python. Was a
real pain in the butt doing it from bottom up manually, without ever
really learing perl syntax. But i finally got it done. Now i need help
testing it. find it here.
 git@github.com:anandjeyahar/mysqlDbAdmin-python.git.

Also i never really thought about design. Just blindly/mechanically,
translated from perl to python. So criticize and let me know how i can
improve this.

Thanks and Regards,
==============================================
Anand Jeyahar
https://sites.google.com/site/anandjeyahar
==============================================
The man who is really serious,
with the urge to find out what truth is,
has no style at all. He lives only in what is.
                  ~Bruce Lee

Love is a trade with lousy accounting policies.
                 ~Aang Jie




On Tue, Aug 16, 2011 at 18:02, aspineux <aspineux@gmail.com> wrote:
> On Aug 16, 1:33 pm, "Paul Wray" <paul.w...@det.nsw.edu.au> wrote:
>> Hello all
>>
>> Ive had what I think is a great idea for pure-python templates (I can almost
>> hear the groans, bear with me...)
>>
>> For the impatient, proof of concept is athttp://pastie.org/2379978
>> demonstrating simple substitution, balanced tags using context manager,
>> subtemplates,  and template inheritance.
>
> You code fail, see below for other comment
>
> Traceback (most recent call last):
>  File "Download/pastie-2379978.rb", line 108, in <module>
>    make_template(template1)
>  File "Download/pastie-2379978.rb", line 60, in make_template
>    ast.fix_missing_locations(astFromSrc)
>  File "/usr/lib/python2.6/ast.py", line 133, in fix_missing_locations
>    _fix(node, 1, 0)
>  File "/usr/lib/python2.6/ast.py", line 132, in _fix
>    _fix(child, lineno, col_offset)
>  File "/usr/lib/python2.6/ast.py", line 132, in _fix
>    _fix(child, lineno, col_offset)
>  File "/usr/lib/python2.6/ast.py", line 121, in _fix
>    if 'lineno' in node._attributes:
> AttributeError: 'arguments' object has no attribute '_attributes'
>
>
>
>
>>
>> I'm posting here to get opinions on:
>> * the merits of the idea, (or otherwise!)
>> * whether there are any established/mature templating systems that use this
>> approach, or whether its come up before,
>> * ideas for improvements and advice on other aspects such as sandboxing
>> * of course, to share the idea in case others want to use it
>
> This is very original ! First time I see it. I like it.
> But how to debug large template ?
> How to find/detect a missing </TAG> ?
> This is very important. This is one big advantage of Genshi over Kid
> How to report the correct error at the correct line ?
> How to find/get nice editor to edit large template ?
>
>
>>
>> Background: Ive been working on an application that recursively renders
>> html/xml documents from heterogenoeus trees, with separate classes for each
>> document component. First I built my own renderer but was dissatisfied with
>> the repetitive code. Then looked at Mako and Jinja, and used Jinja but was
>> still disatisfied, because I still needed a render method in each class to
>> do preparation, and also the template which was centrally managed by the
>> Jinja loader and environment. I found a way to call templates recursively
>> via Jinja filters, but was not sure that it wouldnt blow up in my face, so I
>> also had separate calls to render the children of each node, and then feed
>> the value to the parent template. I kept thinking that there must be a way
>> to get the brevity and clarity of declarative templates, plus the simplicity
>> of pure python loops, tests and function calls.
>>
>> The idea:
>> Python syntax allows a statement to be a bare literal or identifier. These
>> have no effect on the program.
>>
>> So the function below is legal python:
>>
>> def myFunc():
>>     'a'
>>     x = 45
>>     'b'; 'c'; x
>>
>> So is this (within the appropriate class context of course):
>>
>> def body(self, r):
>>         '<h1>'; self.heading; '</h1>'
>>         '<ul>'
>>         for itm in self.items:
>>             '<li>'; itm; '</li>'
>>         '</ul>'
>>
>> The idea is simply to use python ASTs to transform this code so that it
>> accumulates the values of the bare expressions.
>
> You could call it PHP :-)
>
>>
>> I think this give the best of both worlds - declarative syntax for the
>> template literals, but in a pure python context, giving you all the power of
>> python control statements, classes etc.
>>
>> For application contexts outside pure python code (ie user-created
>> templates) , it would be simple to extend the technique to creating template
>> functions from strings, and insert the resulting methods into a namespace
>> for execution.)
>>
>> I think, given the technique is already working with the AST, that
>> sandboxing should not be too hard either - advice on this aspect would be
>> appreciated.
>
>
> Maybe a good idea.
> But still a lot of work to get the level of existing libraries.
>
> Maybe better if mixed with other tools like Genshi to create widget.
> Maybe next toscawidget could use such a technique.
>
>
> Thanks for sharing
>
>>
>> Paul Wray
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Idea for pure-python templates using AST. "Paul Wray" <paul.wray@det.nsw.edu.au> - 2011-08-16 21:33 +1000
  Re: Idea for pure-python templates using AST. aspineux <aspineux@gmail.com> - 2011-08-16 05:32 -0700
    Re: Idea for pure-python templates using AST. anand jeyahar <anand.ibmgsi@gmail.com> - 2011-08-16 21:46 +0530
    Re: Idea for pure-python templates using AST. Paul Wray <paulwray111111@gmail.com> - 2011-08-16 18:10 -0700
  Re: Idea for pure-python templates using AST. Terry Reedy <tjreedy@udel.edu> - 2011-08-16 11:47 -0400
  Re: Idea for pure-python templates using AST. Chris Angelico <rosuav@gmail.com> - 2011-08-16 17:14 +0100
    Re: Idea for pure-python templates using AST. Paul Wray <paulwray111111@gmail.com> - 2011-08-16 16:57 -0700
      Re: Idea for pure-python templates using AST. Chris Angelico <rosuav@gmail.com> - 2011-08-17 01:03 +0100
  Re: Idea for pure-python templates using AST. Irmen de Jong <irmen@-NOSPAM-xs4all.nl> - 2011-08-16 21:23 +0200
    Re: Idea for pure-python templates using AST. Paul Wray <paulwray111111@gmail.com> - 2011-08-16 18:02 -0700
  Re: Idea for pure-python templates using AST. Tim Roberts <timr@probo.com> - 2011-08-16 17:39 -0700

csiph-web