Popular recipes by Giampaolo Rodolà http://code.activestate.com/recipes/users/4178764/popular/2017-03-05T11:00:27-08:00ActiveState Code RecipesRegister exit function (Python)
2016-05-31T00:42:47-07:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/580672-register-exit-function/
<p style="color: grey">
Python
recipe 580672
by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a>
(<a href="/recipes/tags/exit/">exit</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/signal/">signal</a>).
Revision 3.
</p>
<p>This is a function / decorator which registers a function which will be executed on "normal" interpreter exit or in case one of the <code>signals</code> is received by this process (differently from atexit.register()). Also, it makes sure to execute any other function which was previously registered via signal.signal(). If any, it will be executed after our own <code>fun</code>. The full blogpost explaining why you should use this instead of atexit module is here: <a href="http://grodola.blogspot.com/2016/02/how-to-always-execute-exit-functions-in-py.html" rel="nofollow">http://grodola.blogspot.com/2016/02/how-to-always-execute-exit-functions-in-py.html</a></p>
Bytes-to-human / human-to-bytes converter (Python)
2012-02-02T16:09:52-08:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/578019-bytes-to-human-human-to-bytes-converter/
<p style="color: grey">
Python
recipe 578019
by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a>
(<a href="/recipes/tags/bytes/">bytes</a>, <a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/converter/">converter</a>, <a href="/recipes/tags/human/">human</a>, <a href="/recipes/tags/humanize/">humanize</a>, <a href="/recipes/tags/string/">string</a>).
Revision 15.
</p>
<p>Here goes.</p>
socket.sendfile() (Python)
2014-06-13T13:41:25-07:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/578889-socketsendfile/
<p style="color: grey">
Python
recipe 578889
by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a>
(<a href="/recipes/tags/backport/">backport</a>, <a href="/recipes/tags/performance/">performance</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sendfile/">sendfile</a>).
Revision 2.
</p>
<p>This is a backport of socket.sendfile() for Python 2.6 and 2.7.
socket.sendfile() will be included in Python 3.5:
<a href="http://bugs.python.org/issue17552" rel="nofollow">http://bugs.python.org/issue17552</a></p>
Server supporting IPv4 and IPv6 (Python)
2017-03-05T11:00:27-08:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/578504-server-supporting-ipv4-and-ipv6/
<p style="color: grey">
Python
recipe 578504
by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a>
(<a href="/recipes/tags/dualstack/">dualstack</a>, <a href="/recipes/tags/ipv4/">ipv4</a>, <a href="/recipes/tags/ipv6/">ipv6</a>, <a href="/recipes/tags/ipv6_v6only/">ipv6_v6only</a>, <a href="/recipes/tags/networking/">networking</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/socket/">socket</a>).
Revision 13.
</p>
<p>Utility functions to create a single server socket which able to listen on both IPv4 and IPv6. Inspired by:
<a href="http://bugs.python.org/issue17561" rel="nofollow">http://bugs.python.org/issue17561</a></p>
<p>Expected usage:</p>
<pre class="prettyprint"><code>>>> sock = create_server_sock(("", 8000))
>>> if not has_dual_stack(sock):
... sock.close()
... sock = MultipleSocketsListener([("0.0.0.0", 8000), ("::", 8000)])
>>>
</code></pre>
<p>From here on you have a socket which listens on port 8000, all interfaces, serving both IPv4 and IPv6. You can start accepting new connections as usual:</p>
<pre class="prettyprint"><code>>>> while True:
... conn, addr = sock.accept()
... # handle new connection
</code></pre>
<p>Supports UNIX, Windows, non-blocking sockets and socket timeouts.
Works with Python >= 2.6 and 3.X.</p>
Log watcher (tail -F *.log) (Python)
2014-04-04T15:54:03-07:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/577968-log-watcher-tail-f-log/
<p style="color: grey">
Python
recipe 577968
by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a>
(<a href="/recipes/tags/color/">color</a>, <a href="/recipes/tags/log/">log</a>, <a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/monitor/">monitor</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/rotate/">rotate</a>, <a href="/recipes/tags/rotations/">rotations</a>, <a href="/recipes/tags/tail/">tail</a>).
Revision 10.
</p>
<p>A python class which "watches" a directory and calls a callback(filename, lines) function every time one of the files being watched gets written, in real time.</p>
<p>Practically speaking, this can be compared to <em>"tail -F *.log"</em> UNIX command, but instead of having lines printed to stdout a python function gets called.</p>
<p>Similarly to tail, it takes care of "watching" new files which are created after initialization and "unwatching" those ones which are removed in the meantime. This means you'll be able to "follow" and support also rotating log files.</p>
<p><strong>History</strong></p>
<ul>
<li>rev5 (2013-04-05):
<ul>
<li>sizehint parameter</li>
</ul></li>
<li>rev4 (2013-03-16):
<ul>
<li>python 3 support (also dropped support for python <= 2.5)</li>
<li>windows support</li>
<li>unit tests</li>
<li>main class can also be used as a context manager</li>
</ul></li>
<li>rev3 (2012-01-13): initial release</li>
</ul>
@unittest.skip_others decorator (Python)
2012-08-06T02:20:19-07:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/578234-unittestskip_others-decorator/
<p style="color: grey">
Python
recipe 578234
by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a>
(<a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/skip/">skip</a>, <a href="/recipes/tags/unittest/">unittest</a>).
Revision 3.
</p>
<p>Decorator which skips all tests except the one decorated with it.
Based on original proposal on python-ideas:
<a href="http://mail.python.org/pipermail/python-ideas/2010-August/007992.html" rel="nofollow">http://mail.python.org/pipermail/python-ideas/2010-August/007992.html</a></p>
Disk usage (Python)
2012-10-06T15:33:40-07:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/577972-disk-usage/
<p style="color: grey">
Python
recipe 577972
by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a>
(<a href="/recipes/tags/disk/">disk</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/space/">space</a>, <a href="/recipes/tags/statistics/">statistics</a>, <a href="/recipes/tags/usage/">usage</a>).
Revision 6.
</p>
<p>Provides disk usage statistics (total, used and free disk space) about a given path.</p>
<p>This recipe was initially developed for psutil:</p>
<ul>
<li><a href="http://code.google.com/p/psutil/issues/detail?id=172" rel="nofollow">http://code.google.com/p/psutil/issues/detail?id=172</a></li>
</ul>
<p>...and then included into shutil module starting from Python 3.3:</p>
<ul>
<li><a href="http://mail.python.org/pipermail/python-ideas/2011-June/010480.html" rel="nofollow">http://mail.python.org/pipermail/python-ideas/2011-June/010480.html</a></li>
<li><a href="http://bugs.python.org/issue12442" rel="nofollow">http://bugs.python.org/issue12442</a></li>
<li><a href="http://docs.python.org/dev/library/shutil.html#shutil.disk_usage" rel="nofollow">http://docs.python.org/dev/library/shutil.html#shutil.disk_usage</a></li>
</ul>
<p>The recipe you see here is a modified version of the latter one in that the Windows implementation uses ctypes instead of a C extension module. As such it can be used with python >= 2.5.</p>
Handle exit context manager (Python)
2014-08-01T08:28:07-07:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/577997-handle-exit-context-manager/
<p style="color: grey">
Python
recipe 577997
by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a>
(<a href="/recipes/tags/atexit/">atexit</a>, <a href="/recipes/tags/contextlib/">contextlib</a>, <a href="/recipes/tags/contextmanager/">contextmanager</a>, <a href="/recipes/tags/exit/">exit</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/shutdown/">shutdown</a>, <a href="/recipes/tags/sigint/">sigint</a>, <a href="/recipes/tags/signal/">signal</a>, <a href="/recipes/tags/sigterm/">sigterm</a>).
Revision 23.
</p>
<p>A context manager which properly handles SIGTERM (SystemExit) and SIGINT (KeyboardInterrupt) signals, registering a function which is always guaranteed to be called on interpreter exit.
Also, it makes sure to execute previously registered functions as well (if any).</p>
Python interpreter auto-completion and history (Python)
2014-01-09T10:04:47-08:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/578098-python-interpreter-auto-completion-and-history/
<p style="color: grey">
Python
recipe 578098
by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a>
(<a href="/recipes/tags/bashrc/">bashrc</a>, <a href="/recipes/tags/completion/">completion</a>, <a href="/recipes/tags/history/">history</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/pythonstartup/">pythonstartup</a>).
Revision 5.
</p>
<p>Useful in case you don't want to install iPython.</p>
Wait for PID and check for PID existance (POSIX) (Python)
2012-04-15T14:13:02-07:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/578022-wait-for-pid-and-check-for-pid-existance-posix/
<p style="color: grey">
Python
recipe 578022
by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a>
(<a href="/recipes/tags/exists/">exists</a>, <a href="/recipes/tags/exit/">exit</a>, <a href="/recipes/tags/pid/">pid</a>, <a href="/recipes/tags/process/">process</a>, <a href="/recipes/tags/status/">status</a>, <a href="/recipes/tags/timeout/">timeout</a>, <a href="/recipes/tags/wait/">wait</a>).
Revision 7.
</p>
<p>Two functions: one which waits for a certain PID to terminate (with optional timeout), another one which checks whether a process with a given PID is running.</p>
<p>This was extracted from <a href="http://code.google.com/p/psutil/">psutil</a> project which also provides a Windows implementation.</p>
<p>Timeout functionality is implemented as a busy loop and it was inspired by <a href="http://bugs.python.org/issue5673." rel="nofollow">http://bugs.python.org/issue5673.</a></p>
Named constant type (Python)
2012-06-13T13:43:47-07:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/577984-named-constant-type/
<p style="color: grey">
Python
recipe 577984
by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a>
(<a href="/recipes/tags/constant/">constant</a>, <a href="/recipes/tags/named/">named</a>, <a href="/recipes/tags/python/">python</a>).
Revision 7.
</p>
<p>A simple constant type which overrides the base integer type to provide a useful name on str().</p>
Deprecated decorator (Python)
2013-12-14T01:24:31-08:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/577819-deprecated-decorator/
<p style="color: grey">
Python
recipe 577819
by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a>
(<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/deprecated/">deprecated</a>, <a href="/recipes/tags/python/">python</a>).
Revision 3.
</p>
<p>A decorator to deprecate a function and provide a new one as replacement.</p>
Profile decorator (Python)
2011-08-02T19:08:58-07:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/577817-profile-decorator/
<p style="color: grey">
Python
recipe 577817
by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a>
(<a href="/recipes/tags/profile/">profile</a>, <a href="/recipes/tags/profiling/">profiling</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>A decorator for profiling a function which prints profiling results to stdout.
This was originally proposed as a patch for inclusion into python stdlib:</p>
<p><a href="http://bugs.python.org/issue9285" rel="nofollow">http://bugs.python.org/issue9285</a></p>
<p>Note that on certain Linux systems pstats module isn't available, despite it is supposed to be part of Python stdlib. On my Ubuntu box I had to run "sudo apt-get install python-profile" first.</p>
Coloured / highlighted version of string (Python)
2014-06-03T17:54:33-07:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/577818-coloured-highlighted-version-of-string/
<p style="color: grey">
Python
recipe 577818
by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a>
(<a href="/recipes/tags/color/">color</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/highlighting/">highlighting</a>, <a href="/recipes/tags/shell/">shell</a>).
Revision 3.
</p>
<p>Ok, this is really simple but I think it worths a recipe as it's one of those things I always use in shell scripts and for debugging in general.</p>
asyncore scheduler (Python)
2011-07-25T23:42:21-07:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/577808-asyncore-scheduler/
<p style="color: grey">
Python
recipe 577808
by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a>
(<a href="/recipes/tags/asynchronous/">asynchronous</a>, <a href="/recipes/tags/asyncore/">asyncore</a>, <a href="/recipes/tags/heapq/">heapq</a>, <a href="/recipes/tags/nonblocking/">nonblocking</a>, <a href="/recipes/tags/scheduler/">scheduler</a>, <a href="/recipes/tags/twisted/">twisted</a>).
Revision 5.
</p>
<p>The thing I miss mostly in asyncore is a system for calling a function after a certain amount of time without blocking. This is crucial for simple tasks such as disconnecting a peer after a certain time of inactivity or more advanced use cases such as <a href="http://code.google.com/p/pyftpdlib/source/browse/tags/release-0.6.0/pyftpdlib/ftpserver.py#1048">bandwidth throttling</a>.</p>
<p>This recipe was initially inspired by Twisted's internet.base.DelayedCall class:</p>
<p><a href="http://twistedmatrix.com/trac/browser/tags/last_vfs_and_web2/twisted/internet/base.py#L34" rel="nofollow">http://twistedmatrix.com/trac/browser/tags/last_vfs_and_web2/twisted/internet/base.py#L34</a></p>
<p>...then included into pyftpdlib:</p>
<p><a href="http://code.google.com/p/pyftpdlib/issues/detail?id=72" rel="nofollow">http://code.google.com/p/pyftpdlib/issues/detail?id=72</a></p>
<p>...and finally proposed for inclusion into asyncore:</p>
<p><a href="http://bugs.python.org/issue1641" rel="nofollow">http://bugs.python.org/issue1641</a></p>