Popular Python recipes tagged "meta:requires=tokenize"http://code.activestate.com/recipes/langs/python/tags/meta:requires=tokenize/2014-05-25T16:23:55-07:00ActiveState Code RecipesColorize Python -- Sourcecode Syntax Highlighting (Python) 2012-07-21T02:46:37-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/578178-colorize-python-sourcecode-syntax-highlighting/ <p style="color: grey"> Python recipe 578178 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/highlighting/">highlighting</a>, <a href="/recipes/tags/syntax/">syntax</a>). Revision 20. </p> <p>Generates colorized HTML, ANSI escaped text, or a LaTeX document from Python source code. Useful for publishing or viewing your code in a more readable way.</p> Python code minifier (Python) 2014-05-25T16:23:55-07:00Dan McDougallhttp://code.activestate.com/recipes/users/4169722/http://code.activestate.com/recipes/576704-python-code-minifier/ <p style="color: grey"> Python recipe 576704 by <a href="/recipes/users/4169722/">Dan McDougall</a> (<a href="/recipes/tags/bz2/">bz2</a>, <a href="/recipes/tags/bzip2/">bzip2</a>, <a href="/recipes/tags/comments/">comments</a>, <a href="/recipes/tags/compression/">compression</a>, <a href="/recipes/tags/docstring/">docstring</a>, <a href="/recipes/tags/embedded/">embedded</a>, <a href="/recipes/tags/gzip/">gzip</a>, <a href="/recipes/tags/minify/">minify</a>, <a href="/recipes/tags/pack/">pack</a>, <a href="/recipes/tags/regex/">regex</a>, <a href="/recipes/tags/zlib/">zlib</a>). Revision 16. </p> <p><strong>Update 05/25/2014:</strong> Pyminifier 2.0 has been released and now lives on Github: <a href="https://github.com/liftoff/pyminifier" rel="nofollow">https://github.com/liftoff/pyminifier</a> (docs are here: <a href="http://liftoff.github.io/pyminifier/" rel="nofollow">http://liftoff.github.io/pyminifier/</a>). The code below is very out-of-date but will be left alone for historical purposes.</p> <p>Python Minifier: Reduces the size of Python code for use on embedded platforms. Performs the following:</p> <ol> <li>Removes docstrings.</li> <li>Removes comments.</li> <li>Removes blank lines.</li> <li>Minimizes code indentation.</li> <li>Joins multiline pairs of parentheses, braces, and brackets (and removes extraneous whitespace within).</li> <li>Preserves shebangs and encoding info (e.g. "# -<em>- coding: utf-8 -</em>-")</li> <li><strong>NEW:</strong> Optionally, produces a bzip2 or gzip-compressed self-extracting python script containing the minified source for ultimate minification.</li> </ol> <p><strong>Update 09/23/2010:</strong> Version 1.4.1: Fixed an indentation bug when operators such as @ and open parens started a line.</p> <p><strong>Update 09/18/2010:</strong> Version 1.4:</p> <ul> <li>Added some command line options to save the result to an output file.</li> <li>Added the ability to save the result as a bzip2 or gzip-compressed self-extracting python script (which is kinda neat--try it!).</li> <li>Updated some of the docstrings to provide more examples of what each function does.</li> </ul> <p><strong>Update 06/02/2010:</strong> Version 1.3: Rewrote several functions to use Python's built-in tokenizer module (which I just discovered despite being in Python since version 2.2). This negated the requirement for pyparsing and improved performance by an order of magnitude. It also fixed some pretty serious bugs with dedent() and reduce_operators().</p> <p>PLEASE POST A COMMENT IF YOU ENCOUNTER A BUG!</p> typeparser (Python) 2007-04-15T04:24:53-07:00Florian Leitnerhttp://code.activestate.com/recipes/users/4049249/http://code.activestate.com/recipes/511473-typeparser/ <p style="color: grey"> Python recipe 511473 by <a href="/recipes/users/4049249/">Florian Leitner</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 2. </p> <p>Python type-string parser. The code evolved from a post in python-list on 11/22/05 by Fredrik Lundh on a dictionary parser. It parses a type-string to their type objects for all basic types. Raises SyntaxError and SemanticError on failures.</p> <p>Supported types: * containers: defaultdict, deque, dict, list, tuple, set * basic types: Decimal, bool, float, int, long, str * None type</p> <p>REQUIRES PYTHON &gt;= 2.5</p> Convert a formula string with implied multiplication to proper form (Python) 2006-09-16T06:50:04-07:00Ernesto Adoriohttp://code.activestate.com/recipes/users/2765793/http://code.activestate.com/recipes/498106-convert-a-formula-string-with-implied-multiplicati/ <p style="color: grey"> Python recipe 498106 by <a href="/recipes/users/2765793/">Ernesto Adorio</a> . </p> <p>This recipe will insert multiplication symbols and convert all grouping symbols to equivalent parentheses. Uses the tokenize module for compact coding.</p> Tabify (Python) 2006-07-19T04:25:01-07:00Yuce Tekolhttp://code.activestate.com/recipes/users/2933766/http://code.activestate.com/recipes/496893-tabify/ <p style="color: grey"> Python recipe 496893 by <a href="/recipes/users/2933766/">Yuce Tekol</a> (<a href="/recipes/tags/programs/">programs</a>). Revision 3. </p> <p>A little script for those of us who prefer tabs over spaces.</p> Generator expressions for database requests (Python) 2005-10-18T12:21:33-07:00Pierre Quentelhttp://code.activestate.com/recipes/users/1552957/http://code.activestate.com/recipes/442447-generator-expressions-for-database-requests/ <p style="color: grey"> Python recipe 442447 by <a href="/recipes/users/1552957/">Pierre Quentel</a> (<a href="/recipes/tags/database/">database</a>). </p> <p>This recipe is a follow-up to #440653, which was easy to implement but very slow because the iteration required to read all the rows of a table</p> <p>As suggested by Matteo Dell'Amico in a comment, it would be much better if we could write something like</p> <pre class="prettyprint"><code>query(r.name for r in plane_tbl if r.country == "France") </code></pre> <p>where the generator expression is first translated into an SQL select, so that the iteration on the instance of query only reads the rows selected by the SQL statement</p> <p>The present recipe is an attempt to achieve this. The first problem is to get the source code of the generator expression. I use information from the stack frame to get the file name and the line number, then the tokenize module to read the elements of the generator expression in the source code</p> <p>Then, to build the SQL statement, the source code must be parsed : this is done using the compiler package and "visitors" that walk the AST tree returned by compiler.parse and do operations on the nodes, depending on their type</p> <p>Finally, once the SQL statement is built, the iteration on the query instance can start : for the first one, the SQL statement is executed ; then the iteration yields the selected rows one by one.</p> <p>The items can be : - objects, with attribute names matching those in the generator expression, except that qualified names (table.name) are converted to table_name - dictionaries : the keys are the same as the attribute names above - lists</p> <p>For instance : - iterating on query(name for r in plane_tbl) returns objects with an attribute name - iterating on query(r.name for r in plane_tbl) returns objects with an attribute r_name</p> <p>This is because of iteration on tables which have the same field names</p> <p>query((r.name,c.name) for r in plane_tbl for c in country_tbl if r.speed &gt; 500 )</p> <p>The type of the items is set by query.return_type = object, dict or list</p> Python source to XHTML colorizer (Python) 2005-10-26T00:36:06-07:00Peter Krantzhttp://code.activestate.com/recipes/users/2641582/http://code.activestate.com/recipes/442482-python-source-to-xhtml-colorizer/ <p style="color: grey"> Python recipe 442482 by <a href="/recipes/users/2641582/">Peter Krantz</a> . </p> <p>Converts Python source to a portable XHTML 1.0 strict document that includes a basic set of Dublin Core metadata. Based on the MoinMoin source colorizer.</p> Colorize Python source using the built-in tokenizer (Python) 2001-04-06T23:05:53-07:00Jürgen Hermannhttp://code.activestate.com/recipes/users/98061/http://code.activestate.com/recipes/52298-colorize-python-source-using-the-built-in-tokenize/ <p style="color: grey"> Python recipe 52298 by <a href="/recipes/users/98061/">Jürgen Hermann</a> (<a href="/recipes/tags/programs/">programs</a>). Revision 3. </p> <p>This code is part of MoinMoin (<a href="http://moin.sourceforge.net/" rel="nofollow">http://moin.sourceforge.net/</a>) and converts Python source code to HTML markup, rendering comments, keywords, operators, numeric and string literals in different colors.</p> <p>It shows how to use the built-in keyword, token and tokenize modules to scan Python source code and re-emit it with no changes to its original formatting (which is the hard part).</p> <p>The test code at the bottom of the module formats itself and launches a browser with the result.</p>