Are there any libraries for generating Python source? - python

I'd like to write a code generation tool that will allow me to create sourcefiles for dynamically generated classes. I can create the class and use it in code, but it would be nice to have a sourcefile both for documentation and to allow something to import.
Does such a thing exist? I've seen sourcecodegen, but I'd rather avoid messing with the ast trees as they're not portable.

I'm not aware of any off-the-shelf library, but have a look at the Python templating engines Mako and Jinja2. They can both generate Python source behind the scenes (they convert text templates to Python code and then to Python bytecode).

Related

Analyze Python source code using Python and Inspector

I'm developing a static analyzer tool in Python that analyzes Python code for a given .py file and extract the body of classes, methods, etc in order to analyze code metrics (methods size, inheritance, and so on).
I identified the library "Inspect" that seems to suit me. However, I noticed that all the examples provided in the official documentation and external website only use this library by passing concrete instances of objects or functions that they want to analyze; however, I would like to analyze the source code without creating concrete instances of them, to avoid possible compiler errors due to missing library or other factors.
There is possible alternative methods or libraries that I can try to do it?

Use jinja for generating python class

I have a project that needs to automatically generate a python class according to some configuration files using python.
During my searches, I became familiar with Jinja2 which seems to be very popular for generating web pages, but I couldn't really find a similar case which uses Jinja to generate some python codes using Jinja (I know that it is definitely possible to do it, just the lack of examples made me hesitated).
Is it make sense to use Jinja2 for my case or is there any easier solution for generating python from python?!
You can use jinja to generate any text. I use jinja myself to generate python and there is at least one previous stack overflow post.

How can I use both Sphinx and Pycco for Python documentation?

I'd like to be able to use Sphinx for the main project documentation, so the docstrings must be in reStructuredText, but I'd also like to generate HTML for reading the inline comments in the style of Pycco, which uses Markdown.
Is there a tool or combination of tools that will allow me to do convert only the docstrings from reStructuredText to Markdown?
The pyment tool can convert docstrings to different formats. You could start with that, and then write a subclass of DocToolsBase to format docstrings the way you like.
See this question What is the standard Python docstring format? for more about python docstring conventions and tooling.
A software called Pandoc may be the right tool. You can see the detail in the page through the hyperlink. I ever wanted to have try of it, but it needs Haskell runtime environment which is a little big, so I gave up.

Why does Sphinx generate json?

I notice that Sphinx has the ability to generate documentation in JSON. What are these files used for?
As the docs say, it's
for use of a web application (or
custom postprocessing tool) that
doesn’t use the standard HTML
templates.
json's a good simple way for language-agnostic data interchange, so, why not?-)
I assume you're talking about the SerializingHTMLBuilder, in which case I think the answer might be that there isn't necessarily a specific purpose in mind. Rather many things provide conversion routines of various kinds with a "loads/dumps" API convention, and the json module (known as simplejson before it was brought standard library in 2.6) is but one of many such packages.
Presumably some people would prefer to work with data in JSON format for their own purposes. If I were trying to build some sort of dynamic Javascripty documentation system, I could well imagine choosing to use JSON as the way to get documentation from the backend out to the client in a manageable format, if for some reason HTML or XML didn't seem like the better option.

How To: View MFC Doc File in Python

I want to use Python to access MFC document files generically? Can CArchive be used to query a file and view the structure, or does Python, in opening the document, need to know more about the document structure in order to view the contents?
I think that the Python code needs to know the document structure.
Maybe you should make a python wrapper of your c++ code.
In this case, I would recommend to use http://sourceforge.net/projects/pycpp/>pycpp which is my opinion a great library for making python extensions in c++.

Categories

Resources