Popular Python recipes tagged "meta:requires=cstringio"http://code.activestate.com/recipes/langs/python/tags/meta:requires=cstringio/2014-05-25T16:23:55-07:00ActiveState Code RecipesThreaded communication with subprocess (Python)
2013-03-13T06:06:16-07:00Jan Müllerhttp://code.activestate.com/recipes/users/4183984/http://code.activestate.com/recipes/578488-threaded-communication-with-subprocess/
<p style="color: grey">
Python
recipe 578488
by <a href="/recipes/users/4183984/">Jan Müller</a>
(<a href="/recipes/tags/async/">async</a>, <a href="/recipes/tags/ipc/">ipc</a>, <a href="/recipes/tags/messaging/">messaging</a>, <a href="/recipes/tags/multithreading/">multithreading</a>, <a href="/recipes/tags/pipe/">pipe</a>, <a href="/recipes/tags/queue/">queue</a>, <a href="/recipes/tags/subprocess/">subprocess</a>).
Revision 3.
</p>
<p>This recipe shows how to domesticate another executable as a service in a subprocess.</p>
Profile Manager (Cave Story) (Python)
2012-12-07T01:21:04-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578368-profile-manager-cave-story/
<p style="color: grey">
Python
recipe 578368
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/manager/">manager</a>, <a href="/recipes/tags/profile/">profile</a>).
</p>
<p>If you have ever played a game that only had one save slot and wanted to be able to manage profiles, the following code written for Cave Story may be of use to you. The recipe provides a starting point for how one might go about writing a profile manager for such a program that runs through a command interface.</p>
Table indentation (Python)
2011-03-18T14:39:59-07:00Jonathan Blakeshttp://code.activestate.com/recipes/users/4177368/http://code.activestate.com/recipes/577615-table-indentation/
<p style="color: grey">
Python
recipe 577615
by <a href="/recipes/users/4177368/">Jonathan Blakes</a>
(<a href="/recipes/tags/text/">text</a>).
</p>
<p>A function for pretty-printing a table.</p>
Improved ReportLab recipe for "page x of y" (Python)
2009-07-06T10:03:28-07:00Vinay Sajiphttp://code.activestate.com/recipes/users/4034162/http://code.activestate.com/recipes/576832-improved-reportlab-recipe-for-page-x-of-y/
<p style="color: grey">
Python
recipe 576832
by <a href="/recipes/users/4034162/">Vinay Sajip</a>
(<a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/reportlab/">reportlab</a>).
Revision 2.
</p>
<p>This recipe is based on <a href="http://code.activestate.com/recipes/546511/"><a href="http://code.activestate.com/recipes/546511/">Recipe 546511</a></a> which does not work reliably if there are images in the content.</p>
Profile Manager 2.1 (Python)
2010-01-15T18:11:28-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577008-profile-manager-21/
<p style="color: grey">
Python
recipe 577008
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/model_view_controller/">model_view_controller</a>, <a href="/recipes/tags/python_2_5/">python_2_5</a>).
</p>
<p>This is a demonstration of the "cmd.Cmd" class in Python. The recipe demonstrates limited model-view-controller design. The "Profile_Manager" class acts as the model, the "Interface" class is the view, and the basic "cmd.Cmd" class takes the role of controller. It is the "Interface" that handles the boundary between the application's user and the logic of the program. On the other hand, the "Profile_Manager" sits in the place between the interface and the file system (the database holding the data). It is the part that takes care of all the implemented program logic.</p>
Debugging a running python process by interrupting and providing an interactive prompt (Python)
2008-09-25T11:23:29-07:00Brian McErleanhttp://code.activestate.com/recipes/users/111980/http://code.activestate.com/recipes/576515-debugging-a-running-python-process-by-interrupting/
<p style="color: grey">
Python
recipe 576515
by <a href="/recipes/users/111980/">Brian McErlean</a>
(<a href="/recipes/tags/debug/">debug</a>, <a href="/recipes/tags/interactive/">interactive</a>, <a href="/recipes/tags/remote/">remote</a>, <a href="/recipes/tags/signal/">signal</a>).
Revision 2.
</p>
<p>This provides code to allow any python program which uses it to be interrupted at the current point, and communicated with via a normal python interactive console. This allows the locals, globals and associated program state to be investigated, as well as calling arbitrary functions and classes.</p>
<p>To use, a process should import the module, and call listen() at any point during startup.
To interrupt this process, the script can be run directly, giving the process Id of the process to debug as the parameter.</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>
'with' statement for StringIO (Python)
2009-02-24T00:01:16-08:00sridhttp://code.activestate.com/recipes/users/4053089/http://code.activestate.com/recipes/576650-with-statement-for-stringio/
<p style="color: grey">
Python
recipe 576650
by <a href="/recipes/users/4053089/">srid</a>
(<a href="/recipes/tags/stringio/">stringio</a>, <a href="/recipes/tags/with_statement/">with_statement</a>).
Revision 3.
</p>
<p>NOTE: Consider this recipe obsolete. Instead use <code>contextlib.closing</code> (see comment below).</p>
<p>This contextmanager adds 'with' statement support for StringIO. Peruse the following simple example:</p>
<pre class="prettyprint"><code>with StringIO() as sio:
function_accepting_file_handle(sio)
print sio.getvalue()
</code></pre>
Web browser emulator (Python)
2008-05-15T01:09:08-07:00Laszlo Nagyhttp://code.activestate.com/recipes/users/4150221/http://code.activestate.com/recipes/572202-web-browser-emulator/
<p style="color: grey">
Python
recipe 572202
by <a href="/recipes/users/4150221/">Laszlo Nagy</a>
(<a href="/recipes/tags/web/">web</a>).
</p>
<p>Objects of the MozillaEmulator class can emulate a browser that is capable of:</p>
<ul>
<li>cookie management</li>
<li>caching</li>
<li>configurable user agent string</li>
<li>GET and POST</li>
<li>multipart POST (send files)</li>
<li>receive content into file</li>
<li>progress indicator</li>
</ul>
Listing the package/patches dependencies of a binary on Solaris (Python)
2008-07-30T04:30:44-07:00Benjamin Sergeanthttp://code.activestate.com/recipes/users/4039626/http://code.activestate.com/recipes/576397-listing-the-packagepatches-dependencies-of-a-binar/
<p style="color: grey">
Python
recipe 576397
by <a href="/recipes/users/4039626/">Benjamin Sergeant</a>
(<a href="/recipes/tags/solaris/">solaris</a>, <a href="/recipes/tags/system/">system</a>, <a href="/recipes/tags/unix/">unix</a>).
Revision 2.
</p>
<p>Print (1) packages used by a binary, and (2) the list of installed patches
related to these packages. If you have a binary that works with Solaris 10 update N, but doesn't with Solaris 10 update N-2, run this script on both platform and it will help you to find the patches you're looking for.</p>
<p>(1) is retrieved:</p>
<ul>
<li>By using pldd(pid) on the process you want to trace to get a list of loaded
shared library </li>
<li>By retrieving in the main /var/sadm/install/contents database
the list of package related to these shared libraries</li>
</ul>
<p>(2) is retrieved by parsing the output of the showrev -p command, given as
input of this script</p>
HTML/CSS to PDF converter (Python)
2008-04-20T06:06:52-07:00Dirk Holtwickhttp://code.activestate.com/recipes/users/636691/http://code.activestate.com/recipes/572160-htmlcss-to-pdf-converter/
<p style="color: grey">
Python
recipe 572160
by <a href="/recipes/users/636691/">Dirk Holtwick</a>
(<a href="/recipes/tags/web/">web</a>).
</p>
<p>Most people know how to write a page with HTML and CSS. Why not using these skills to dynamically generate PDF documents using it? The open source project "pisa" <a href="http://www.htmltopdf.org" rel="nofollow">http://www.htmltopdf.org</a> enables you to to this quite simple in a pythonic way.</p>
Generate a PDF Greeting Card (Python)
2007-12-06T04:06:04-08:00greg phttp://code.activestate.com/recipes/users/4047849/http://code.activestate.com/recipes/535155-generate-a-pdf-greeting-card/
<p style="color: grey">
Python
recipe 535155
by <a href="/recipes/users/4047849/">greg p</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 2.
</p>
<p>Code to generate a PDF greeting card using a user specified image, and text.</p>
<p>You can try it out online here: <a href="http://utilitymill.com/utility/Greeting_Card_Generator" rel="nofollow">http://utilitymill.com/utility/Greeting_Card_Generator</a></p>
An Erlang Port in Python (Python)
2010-02-18T06:47:09-08:00thanos vassilakishttp://code.activestate.com/recipes/users/4048784/http://code.activestate.com/recipes/534162-an-erlang-port-in-python/
<p style="color: grey">
Python
recipe 534162
by <a href="/recipes/users/4048784/">thanos vassilakis</a>
(<a href="/recipes/tags/distributed/">distributed</a>, <a href="/recipes/tags/erlang/">erlang</a>, <a href="/recipes/tags/integration/">integration</a>, <a href="/recipes/tags/process/">process</a>).
Revision 3.
</p>
<p>Erlang has two built-in interoperability mechanisms. One is distributed Erlang nodes and the other one is ports.
Ports provide the basic Erlang mechanism for communication with the external world. They provide a byte-oriented interface to an external program. When a port has been created, Erlang can communicate with it by sending and receiving lists of bytes. This recipe cooks an Erlang port in python. Making it easy for Erlang to instantiate and use python objects. Like most simple port implementations it uses an external python program and lets Erlang communicate via standard input and write to standard output. Theoretically, the external program could be written in any programming language. This recipe is pretty abstract and you will have to implement your own encode and decoding scheme.</p>
Converting XML to dict (for a XPATH-like access syntax) and back to XML (Python)
2007-06-28T03:16:28-07:00Rodrigo Strausshttp://code.activestate.com/recipes/users/2678660/http://code.activestate.com/recipes/522991-converting-xml-to-dict-for-a-xpath-like-access-syn/
<p style="color: grey">
Python
recipe 522991
by <a href="/recipes/users/2678660/">Rodrigo Strauss</a>
(<a href="/recipes/tags/xml/">xml</a>).
Revision 4.
</p>
<p>I really like config files done in Python language itself, using a dictionary declaration. It's cool for programmers, but not so cool for system administrators not used to Python (it's so easy to forget a comma...).
To keep using dictionaries internally providing something more admin friendly, I've done some functions to convert a XML file to Python dictionary (and the reverse as well):</p>
An asynchronous HTTP server with CGI support (Python)
2007-04-05T05:46:26-07:00Pierre Quentelhttp://code.activestate.com/recipes/users/1552957/http://code.activestate.com/recipes/511454-an-asynchronous-http-server-with-cgi-support/
<p style="color: grey">
Python
recipe 511454
by <a href="/recipes/users/1552957/">Pierre Quentel</a>
(<a href="/recipes/tags/cgi/">cgi</a>).
</p>
<p>This recipes implements the HTTP protocol with persistent connection and CGI support on top of the generic asynchronous server provided in <a href="http://code.activestate.com/recipes/511453/">recipe 511453</a></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 >= 2.5</p>
A generic, multi protocol asynchronous server (Python)
2007-04-05T05:35:23-07:00Pierre Quentelhttp://code.activestate.com/recipes/users/1552957/http://code.activestate.com/recipes/511453-a-generic-multi-protocol-asynchronous-server/
<p style="color: grey">
Python
recipe 511453
by <a href="/recipes/users/1552957/">Pierre Quentel</a>
(<a href="/recipes/tags/web/">web</a>).
</p>
<p>This recipe implements the concept of asynchronous socket programming in a
more straighforward way than in the modules asyncore and asynchat of the
standard distribution, which I find difficult to understand</p>
twvalidator: a Markup Validator Gateway for Twisted Web (Python)
2007-01-17T04:00:20-08:00Manlio Perillohttp://code.activestate.com/recipes/users/2425438/http://code.activestate.com/recipes/500263-twvalidator-a-markup-validator-gateway-for-twisted/
<p style="color: grey">
Python
recipe 500263
by <a href="/recipes/users/2425438/">Manlio Perillo</a>
(<a href="/recipes/tags/web/">web</a>).
</p>
<p>twvalidator is a simple markup validator gateway you can embed in your
Twisted Web application for validating all your pages.</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>
simple readlines in reverse w/deque (Python)
2006-08-04T19:09:16-07:00John Nielsenhttp://code.activestate.com/recipes/users/135654/http://code.activestate.com/recipes/496941-simple-readlines-in-reverse-wdeque/
<p style="color: grey">
Python
recipe 496941
by <a href="/recipes/users/135654/">John Nielsen</a>
(<a href="/recipes/tags/text/">text</a>).
Revision 5.
</p>
<p>This a very simple implementation for how to do a readlines in reverse. It is not optimized for performance (which often doesn't matter). I have a 2nd version that is faster by loading blocks of data into memory instead of character by character. Of course, the code then almost doubles in size. And finally a third version that is the fastest, using split.</p>