marked-up Python code

2007-11-20 Thread Luc Goossens
Hi,

I would like to experiment with marked-up Python source code. A more  
elaborate explanation of the use-case is at the end of this mail. The  
short story is that I would like to do things like assign colors to  
pieces of text in my editor and have this information saved _in my  
source  code_
smth like

else :
  print "ERROR"
  return -1


"all" the Python parser has to do is skip the mark-up.

Has something like this been done before? Is there a way  to do this  
without changing the Python executable? If not, where in the source  
code should I start looking?

cheers,
Luc

PS1
I know I can put the mark-up after a # and the problem is solved  
trivially, but this will not work for all cases (e.g. mark-up of  
single identifiers) and to be honest I was thinking of recycling some  
mark-up capable editor and an existing mark-up language

PS2
here's the real use case
I have a small application in Python. The code shares a recurring  
feature: within methods 20% of the code lines is about the actual  
handling of the correct case, 40% is about the handling of the  
incorrect cases, 40% is instrumenting (logging and timing).
A case for aspect oriented programming? I would certainly think so,  
but unfortunately  there is no obvious way to map this to the  
prevailing aspect-advice-joint point-point cut model. Moreover, I  
really do not want this code to become fragmented over multiple  
source code files, I just want the aspects to be readily visible in  
my editor, and be able to selectively show/hide some of them (like  
expanding/collapsing code blocks).


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: marked-up Python code

2007-11-20 Thread Luc Goossens
Hi Tim,

thanks for your suggestions

I have two questions.
1. can I color the background of the text keeping the normal syntax  
coloring for actual text? can you give some hints on how to do that  
in vim?
2. will the # mark-up lines show in the editor? is there some visual  
clue that something has been hidden?

I will gladly settle for some pointer into the vim documentation,  
which I found already.

many thanks,
Luc

On Nov 20, 2007, at 1:03 PM, Tim Chase wrote:

>> "all" the Python parser has to do is skip the mark-up.
> [snip]
>> I know I can put the mark-up after a # and the problem is
>> solved trivially, but this will not work for all cases (e.g.
>> mark-up of single identifiers) and to be honest I was thinking
>>  of recycling some mark-up capable editor and an existing
>> mark-up language
>
> Comments *are* the way in which you tell Python parser to "skip
> the mark-up".
>
> With a good editor, this is fairly easy to do.  And
> folding/coloring is the job of the editor, not of Python.
>
> I'll use Vim as an example, as it's my preferred tool and I know
> it well enough to offer a solution using it, though surely any
> other good editor can do similarly.
>
> Vim offers two ways to do what you're describing.  First, you can
> color sections by defining your own highlighting/syntax
> augmentation.  Second, you can use folding to hide away bits of
> your code that you don't want to see.
>
> This combo can be used to do something like define several
> markers such as
>
>   # begin instrumenting
>   # end instrumenting
>   # one-line instrumentation
>
> You can then do something like
>
>   :set foldmethod=marker
>   :set foldmarker=#\ begin,#\ end
>
> This will create folds across all your blocks.  You can
> selectively open/close these blocks with
>
>   zR
>
> to open all the folds and
>
>   :g/#begin instrumenting/norm zC
>
> to close just those folds tagged with "begin instrumenting".  The
> two can be mapped into a single keypress, so you can do something
> like
>
>   :nnoremap  zR:g/# begin instrumenting/norm zC
>
> which will open all the folds and then just close the ones that
> involve instrumenting merely by pressing 
>
> As for the single-line ones, you can use Vim's ":match"
> functionality:
>
>   :match Folded /.*# one-line instrumentation
>
> The "Folded" is a highlighting group (it can be an existing one
> such as "Folded" or "Error", or one you create to give you the
> coloring you want).
>
> Thus, in Vim, the whole thing could be done with a fairly simple
> setup:
>
>   :set foldmethod=marker foldmarker=#\ begin,#\ end
>   :nnoremap  zR:g/# begin instrumentation/norm zC:match
> Folded /.*# one-line instrumentation/
>   :nnoremap  zR:g/# begin debugging/norm zC:match Folded
> /.*# one-line debugging/
>
> You would then mark up your code with things like
>
> import pdb; pdb.set_trace() # one-line debugging
> start = now() # one-line instrumentation
> do_stuff()
> # begin instrumentation
> end = now()
> delta = end - start
> # end instrumentation
>
> and then use  to hide your instrumentation code, and  to
> hide your debugging code.  it's a little more complex to invert
> the behavior, but doable.
>
> This can even be augmented to create these blocks for you:
>
>   :vnoremap  :':'>put
> ='# end instrumentation'
>   :nnoremap  A# one-line instrumentation
>
> which should define two mappings for shift+F4:  the first, in
> visual-mode (with text selected) wraps those lines in the
> begin/end pair.  In normal mode, shift+F4 just appends the
> one-line tag to the end of the line.  Do similarly for shift+F5
> for the debugging.
>
> Hope this gives you some ideas to work with.  And perhaps
> advocates of other editors can chime in with how it would be done
> there.
>
> -tkc
>
>
>
>
>
>
>
>

-- 
http://mail.python.org/mailman/listinfo/python-list