aa_macro syntax 
 aa_macro source 
 HTML source 
 HTML render 
Key

§ 4.66 - [pythparse] Built-In

Mnemonic aid: pythparse for PYTHon PARSE pretty-print Python source to variable

See also:  [postparse] 

This built-in allows you to generate a pretty-printed representation of Python source code to a local variable, loc_pyth. In the example, you'll see I wrapped a  <div>  with a black background around the retrieval of the loc_pyth variable, as the default pretty-printing colors were selected with a black background in mind.

There are some details to consider here; please refer to the notes below the examples on this page.


[pythparse content]

[pythparse
def mygen(n):
names = ['Ben','Deb']
for i in range(0,n):
print "hello" + names[0] and names[1]
]
<div style="background: #000000;">
[v loc_pyth]
</div>

<div style="background: #000000; padding: 1em;"><span style="color: #00ff00"><span style="color: #ff00ff">def</span> mygen<span style="color:#ff8844;">(</span>n<span style="color:#ff8844;">)</span><span style="color:#00ffff;">:</span></span>
<span style="color: #00ff00"> names <span style="color:#00ffff;">=</span> <span style="color:#ff8844;">[lb]</span></span><span style="color: #ffffff">'</span><span style="color: #ff0000">Ben</span><span style="color: #ffffff">'</span><span style="color: #00ff00"><span style="color:#00ffff;">,</span></span><span style="color: #ffffff">'</span><span style="color: #ff0000">Deb</span><span style="color: #ffffff">'</span><span style="color: #00ff00"><span style="color:#ff8844;">[rb]</span></span>
<span style="color: #00ff00"> <span style="color: #ff00ff">for</span> i <span style="color: #ff00ff">in</span> range<span style="color:#ff8844;">(</span>0<span style="color:#00ffff;">,</span>n<span style="color:#ff8844;">)</span><span style="color:#00ffff;">:</span></span>
<span style="color: #00ff00"> <span style="color: #ff00ff">print</span> </span><span style="color: #ffffff">"</span><span style="color: #ff0000">hello</span><span style="color: #ffffff">"</span><span style="color: #00ff00"> <span style="color:#00ffff;">+</span> names<span style="color:#ff8844;">[lb]</span>0<span style="color:#ff8844;">[rb]</span> <span style="color: #ff00ff">and</span> names<span style="color:#ff8844;">[lb]</span>1<span style="color:#ff8844;">[rb]</span></span></div>
def mygen(n): names = ['Ben','Deb'] for i in range(0,n): print "hello" + names[0] and names[1]
Warning:

 [pythparse]  must be used at the top level; you can't embed it in a style or another built-in. It can handle Python code without any changes, but again, only in the top level context.

Caution:

Although  [pythparse]  takes raw Python, the aa_macro parser still has to wade through it to find the closing ] for the  [pythparse]  built-in itself. So if there are unbalanced [] or {} anywhere in the Python source, that will break the parsing. An easy fix for this is to balance to a pair in the comments before (if ] or }) or after (if [ or {) the line of Python. Sorry about that, but it's inherent in how all this works.

Because of this, it may be more convenient to edit all the occurrences of [, ], { and } to  [lb] ,  [rb] ,  [ls]  and  [rs]  and then use  [postparse]  instead. It's basically a tradeoff between getting a perfect code representation and having to do some manipulating of comments as described here.

One last thing: If you are displaying Python that contains aa_macro source, you should definitely use  [pythparse] ; this is because  [postparse]  will actually process anything like that that it finds.

You can control what the pretty printing does in terms of styling by setting the following variables (default values are shown):

CSS style wrap for Python keywords: import, while, etc.
tx_prekey
'<span style="color: #ff00ff">'
tx_poskey
'</span>'

CSS style wrap for single and double quotes
tx_prequo
'<span style="color: #ffffff">'
tx_posquo
'</span>'

CSS style wrap for Python code
tx_precod
'<span style="color: #00FF00">'
tx_poscod
'</span>'

CSS style wrap for text inside quotes
tx_pretxt
'<span style="color: #ff0000">'
tx_postxt
'</span>'

CSS style wrap for comment content
tx_precom
'<span style="color: #ffff00">'
tx_poscom
'</span>'

CSS style wrap for {}, [], and ()
tx_pybrace
'<span style="color: #ff8844">'
tx_epybrace
'</span>'

CSS style wrap for symbols such as ==, +, < and so on
tx_pysym
'<span style="color: #00ffff">'
tx_epysym
'</span>'
Keyboard Navigation
, Previous Page . Next Page t TOC i Index

Valid HTML 4.01 Loose
 

This manual was generated with wtfm
wtfm uses aa_macro and SqLite
aa_macro uses Python 2.7