[BangPypers] Code Extractor in Python

Anand Chitipothu anandology at gmail.com
Thu Jun 13 04:48:19 CEST 2013


On Thu, Jun 13, 2013 at 6:50 AM, Gopalakrishnan Subramani <
gopalakrishnan.subramani at gmail.com> wrote:

> All,
>
> I am looking for possibility to pull out the code from source file without
> specifying the line numbers explicitly. I am writing a  blog and book on
> Java script AngularJS using Markdown, I wanted that to be driven as
> code-text blog.
>
> Small example,
> ------------------------------------------
> File: math.js
>
> function sum(a, b) {
>    return a + b;
> }
>
> function mul(a, b) {
>    return a * b;
> }
> -----------------------------------------
>
> On Markdown, the explanation shall be in Function.txt,
>
>
> ---------------------------------------------------------------------------------
> File: Function.txt
>
> Basic Functions
> ============
>
> Here is the simple *function* example that adds two numbers and return the
> result.
>       {{{insertCode math.js, function:sum}}}
>
>
> ---------------------------------------------------------------
>
> {{{insertCode math.js, function:sum}}} is very abstract, I am ok with any
> syntax, something that can be put on Jinja2 templates, in Python. But I
> would like to pull the code from a file, function or specific class (for
> other languages).
>
> The idea is, store the source code in Github/separate file, the markdown
> text shall be stored in Dropbox, I can display the code, and execute the
> output, display the result underneath. Any changes to Github will be
> reflected automatically, right now, I copy paste the code into markdown.
>
> Alternative ideas or tools are welcome. Python based tools are preferred,
>
> I use Flask, Markdown, Jinja2. Sometimes, I put Jinja2 templates inside
> Markdown, so I can extend the template.
>
> Python inspect is doing similar for Python but that is purely on run time,
> I am looking for options to pull out code from Python, HTML. I tried, HTML,
> it is possible with BS4.
>

I think the best way to do it is by separating extracting code from
including code. You can write a script to preprocess the source files and
extracts each function and writes into a different files. Once you have
solved that problem, then all you have to do just include one file in your
markdown.

Now for extracting, the dumb way is to insert markers in the source file
and let the preprocessor look for those markers and extract them.

// BEGIN sum
function sum(a, b) {
   return a + b;
}
// END

This adds little more work for you as you have to manually do the markings,
but this is very flexible. If you want to include multiple functions or a
class or something completely different, it'll just work.

If you know you are always going to extract a function or a class, then you
can be a bit more smarter and write a parser to extract them.

Hope this helps!

-- 
Anand
http://anandology.com/


More information about the BangPypers mailing list