Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since Python is an interpreted language, it can't jump around in its own code. But you don't need a "special tool" to strip off parts of your code -- use Python for it!</p> <p>Here is a minimal example. You'll probably want to put the <code>strip_debug()</code> function in your <code>__init__.py</code> and let it process a list of modules. Also, you'd probably want to add some additional check that the user really wants to <em>modify</em> the code, not just <em>run</em> it. Probably, using a command line option like <code>--purge</code> would be nice. You can then either make a copy of your library and once run</p> <pre><code>python __init__.py --purge </code></pre> <p>before you publish the library or leave it up to your users to do so.</p> <pre><code>#!/usr/bin/env python3.2 # BEGIN DEBUG def _strip_debug(): """ Generates an optimized version of its own code stripping off all debugging code. """ import os import re import shutil import sys import tempfile begin_debug = re.compile("^\s*#+\s*BEGIN\s+DEBUG\s*$") end_debug = re.compile("^\s*#+\s*END\s+DEBUG\s*$") tmp = None debug = False try: tmp = tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) with open(sys.argv[0]) as my_code: for line in my_code: if begin_debug.match(line): debug = True continue elif end_debug.match(line): debug = False continue else: if not debug: tmp.write(line) tmp.close() shutil.copy(tmp.name, sys.argv[0]) finally: os.unlink(tmp.name) # END DEBUG def foo(bar, baz): """ Do something weired with bar and baz. """ # BEGIN DEBUG if DEBUG: print("bar = {}".format(bar)) print("baz = {}".format(baz)) # END DEBUG return bar + baz # BEGIN DEBUG if __name__ == "__main__": _strip_debug() # END DEBUG </code></pre> <p>After executed, this file will only contain the functional code of the <code>foo()</code> function. I have used the special comments</p> <pre><code># BEGIN DEBUG </code></pre> <p>and</p> <pre><code># END DEBUG </code></pre> <p>in this example which allows to strip off arbitrary code but if it's just for removing</p> <pre><code>if DEBUG: # stuff </code></pre> <p>sections, it would also be pretty easy to detect those without any additional comments.</p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. 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