Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You might not need to re-generate source code. That's a bit dangerous for me to say, of course, since you have not actually explained why you think you need to generate a .py file full of code; but:</p> <ul> <li><p>If you want to generate a .py file that people will actually use, maybe so that they can fill out a form and get a useful .py file to insert into their project, then you don't want to change it into an AST and back because you'll lose <strike>all formatting (think of the blank lines that make Python so readable by grouping related sets of lines together)</strike> (<a href="http://docs.python.org/py3k/library/ast.html#ast.AST.lineno" rel="noreferrer">ast nodes have <code>lineno</code> and <code>col_offset</code> attributes</a>) comments. Instead, you'll probably want to use a templating engine (the <a href="http://docs.djangoproject.com/en/dev/topics/templates/#topics-templates" rel="noreferrer">Django template language</a>, for example, is designed to make templating even text files easy) to customize the .py file, or else use Rick Copeland's <a href="http://code.google.com/p/metapython/" rel="noreferrer">MetaPython</a> extension.</p></li> <li><p>If you are trying to make a change during compilation of a module, note that you don't have to go all the way back to text; you can just compile the AST directly instead of turning it back into a .py file.</p></li> <li><p>But in almost any and every case, you are probably trying to do something dynamic that a language like Python actually makes very easy, without writing new .py files! If you expand your question to let us know what you actually want to accomplish, new .py files will probably not be involved in the answer at all; I have seen hundreds of Python projects doing hundreds of real-world things, and not a single one of them needed to ever writer a .py file. So, I must admit, I'm a bit of a skeptic that you've found the first good use-case. :-)</p></li> </ul> <p><strong>Update:</strong> now that you've explained what you're trying to do, I'd be tempted to just operate on the AST anyway. You will want to mutate by removing, not lines of a file (which could result in half-statements that simply die with a SyntaxError), but whole statements — and what better place to do that than in the AST?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload