Popular recipes by Zack Weinberg http://code.activestate.com/recipes/users/4190298/2015-01-29T20:54:38-08:00ActiveState Code RecipesBuild extension modules inplace with a Makefile (Python)
2015-01-29T20:54:38-08:00Zack Weinberghttp://code.activestate.com/recipes/users/4190298/http://code.activestate.com/recipes/579016-build-extension-modules-inplace-with-a-makefile/
<p style="color: grey">
Python
recipe 579016
by <a href="/recipes/users/4190298/">Zack Weinberg</a>
(<a href="/recipes/tags/compiler/">compiler</a>, <a href="/recipes/tags/module/">module</a>).
</p>
<p>Do you find distutils to be poorly documented, overdesigned yet still inadequate, and far too difficult to do anything out of the ordinary with? Do you find yourself wishing that you could just write a Makefile for your extension modules, if only you knew how to form the compile commands?</p>
<p>Then this tool is for you. An example (GNU) makefile to use it with is embedded in the code; it assumes you save this program as <code>get-module-compile-cmds.py</code> in the same directory as the makefile. Tested with 2.7 and 3.4; may work with older versions as well.</p>
<p>Installation is not currently supported; patches welcome.</p>
Non-blocking readlines() (Python)
2014-06-30T20:30:57-07:00Zack Weinberghttp://code.activestate.com/recipes/users/4190298/http://code.activestate.com/recipes/578900-non-blocking-readlines/
<p style="color: grey">
Python
recipe 578900
by <a href="/recipes/users/4190298/">Zack Weinberg</a>
(<a href="/recipes/tags/concurrency/">concurrency</a>, <a href="/recipes/tags/input/">input</a>, <a href="/recipes/tags/line/">line</a>, <a href="/recipes/tags/nonblocking/">nonblocking</a>).
</p>
<p>A generator function which takes a file object (assumed to be some sort of pipe or socket, open for reading), and yields lines from it without blocking. If there is no input available, it will yield an endless stream of empty strings until input becomes available again; caller is responsible for not going into a busy loop. (Newlines are normalized but not stripped, so if there is actually a blank line in the input, the value yielded will be <code>'\n'</code>.) The intended use case is a thread which must respond promptly to input from a pipe, and also something else which cannot be fed to <code>select</code> (e.g. a <code>queue.Queue</code>). Note that the file object is ignored except for its <code>fileno</code>.</p>
<p>Only tested on Unix. Only tested on 3.4; ought to work with any python that has <code>bytearray</code>, <code>locale.getpreferredencoding</code>, and <code>fcntl</code>.</p>
strsignal (Python)
2014-06-29T15:47:24-07:00Zack Weinberghttp://code.activestate.com/recipes/users/4190298/http://code.activestate.com/recipes/578899-strsignal/
<p style="color: grey">
Python
recipe 578899
by <a href="/recipes/users/4190298/">Zack Weinberg</a>
(<a href="/recipes/tags/signals/">signals</a>).
</p>
<p>This is an emulation of the standard (POSIX.1-2008) C library function <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/strsignal.html"><code>strsignal()</code></a>, which should be in the <a href="https://docs.python.org/3/library/signal.html"><code>signal</code> module</a>, but isn't.</p>
<p>If possible, it uses <a href="https://docs.python.org/3/library/ctypes.html"><code>ctypes</code></a> to call the C library function; otherwise, it tries to build a reverse mapping of the <code>SIG*</code> constants in the signal module; if that doesn't work either, it just produces <code>"signal %d"</code> where <code>%d</code> is the decimal signal number.</p>
<p>If invoked as a script, will test all of the strategies and print the results.</p>
<p>Tested in 2.7 and 3.4; <em>should</em> work with 2.6 and 3.3 as well.</p>