Popular recipes by Nick Coghlan http://code.activestate.com/recipes/users/2035254/2011-12-18T07:20:46-08:00ActiveState Code RecipesCleanupManager for with statements (Python)
2011-12-18T07:20:46-08:00Nick Coghlanhttp://code.activestate.com/recipes/users/2035254/http://code.activestate.com/recipes/577981-cleanupmanager-for-with-statements/
<p style="color: grey">
Python
recipe 577981
by <a href="/recipes/users/2035254/">Nick Coghlan</a>
(<a href="/recipes/tags/contextmanager/">contextmanager</a>, <a href="/recipes/tags/context_manager/">context_manager</a>).
Revision 5.
</p>
<p>Inspired by unittest.TestCase.addCleanup(), CleanupManager provides a means to programmatically add resources to be cleaned up when leaving a with statement. This makes it easy to use with optional resources, and those derived from sequences of inputs.</p>
<p>An more powerful version of this recipe with a few additional features is published under the name <code>ContextStack</code> as part of the contextlib2 module: <a href="http://contextlib2.readthedocs.org" rel="nofollow">http://contextlib2.readthedocs.org</a></p>
<p>This recipe is based on a suggestion originally posted by Nikolaus Rath at <a href="http://bugs.python.org/issue13585" rel="nofollow">http://bugs.python.org/issue13585</a></p>
Selective directory walking (Python)
2011-10-20T05:05:39-07:00Nick Coghlanhttp://code.activestate.com/recipes/users/2035254/http://code.activestate.com/recipes/577913-selective-directory-walking/
<p style="color: grey">
Python
recipe 577913
by <a href="/recipes/users/2035254/">Nick Coghlan</a>
(<a href="/recipes/tags/directories/">directories</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/globbing/">globbing</a>).
Revision 2.
</p>
<p>Python's os.walk() standard library iterator is useful if you want to walk an entire directory tree, but you're on your own when it comes to implementing name filtering and recursive depth limiting on top of it.</p>
<p>This recipe supports these features with an interface that is just as convenient as the underlying os.walk() API, while being significantly more powerful.</p>
Simple invocation of shell commands (Python)
2011-10-21T06:44:35-07:00Nick Coghlanhttp://code.activestate.com/recipes/users/2035254/http://code.activestate.com/recipes/577891-simple-invocation-of-shell-commands/
<p style="color: grey">
Python
recipe 577891
by <a href="/recipes/users/2035254/">Nick Coghlan</a>
(<a href="/recipes/tags/processes/">processes</a>, <a href="/recipes/tags/shell/">shell</a>).
Revision 3.
</p>
<p>Some simple wrappers around the subprocess functions for use in system administration utilities that frequently need to interpolate trusted data into shell commands (e.g. filenames from directory listings, etc):</p>
<pre class="prettyprint"><code>import shellcmd
return_code = shellcmd.shell_call('ls -l {}', dirname)
listing = shellcmd.check_shell_output('ls -l {}', dirname)
</code></pre>
<p>Each function invokes the subprocess function of the same name with <code>shell=True</code> and the supplied command string. Any positional and keyword arguments provided to the call are interpolated into the command string with the <code>str.format</code> method.</p>
format_iter: easy formatting of arbitrary iterables (Python)
2011-08-16T11:44:59-07:00Nick Coghlanhttp://code.activestate.com/recipes/users/2035254/http://code.activestate.com/recipes/577845-format_iter-easy-formatting-of-arbitrary-iterables/
<p style="color: grey">
Python
recipe 577845
by <a href="/recipes/users/2035254/">Nick Coghlan</a>
(<a href="/recipes/tags/formatting/">formatting</a>, <a href="/recipes/tags/strings/">strings</a>).
</p>
<p>The <code>format_iter</code> recipe defines a simple wrapper around <code>str.join</code> and <code>str.format</code> that makes it easy to format an arbitrary iterable with a specified format string and item separator.</p>
Named Values (Python)
2011-07-28T00:23:34-07:00Nick Coghlanhttp://code.activestate.com/recipes/users/2035254/http://code.activestate.com/recipes/577810-named-values/
<p style="color: grey">
Python
recipe 577810
by <a href="/recipes/users/2035254/">Nick Coghlan</a>
.
Revision 2.
</p>
<p>NamedValue is a mixin class that modifies the signature of the class constructor to accept a name as the first positional argument and modifies repr() to display the name without affecting the result of str() (even for peer classes that rely on the str->repr fallback for their str implementation).</p>
Create module directly from a filesystem path (Python)
2011-07-15T04:13:59-07:00Nick Coghlanhttp://code.activestate.com/recipes/users/2035254/http://code.activestate.com/recipes/577792-create-module-directly-from-a-filesystem-path/
<p style="color: grey">
Python
recipe 577792
by <a href="/recipes/users/2035254/">Nick Coghlan</a>
.
</p>
<p>Given a filesystem path, use it to create a valid Python module. Based on <code>runpy.run_path()</code>, so accepts Python source files, compiled Python files and directories and zipfiles containing __main__.py files as valid targets.</p>
Simple creation, configuration and installation of logging handlers (Python)
2011-06-01T12:13:21-07:00Nick Coghlanhttp://code.activestate.com/recipes/users/2035254/http://code.activestate.com/recipes/577496-simple-creation-configuration-and-installation-of-/
<p style="color: grey">
Python
recipe 577496
by <a href="/recipes/users/2035254/">Nick Coghlan</a>
(<a href="/recipes/tags/logging/">logging</a>).
</p>
<p>Creation of log message handler for the logging module is often a multi-step process, involving creation of the handler object, configuration of the message levels and formats, installation of any filters and then actual connection of the handler to the relevant logger object.</p>
<p>This helper function allows all of these things to be specified up front in a single function call, which then takes care of configuring the handler object appropriately.</p>
Interpret Unicode escape escape sequences written to stdout and stderr (Python)
2008-04-16T14:47:48-07:00Nick Coghlanhttp://code.activestate.com/recipes/users/2035254/http://code.activestate.com/recipes/570089-interpret-unicode-escape-escape-sequences-written-/
<p style="color: grey">
Python
recipe 570089
by <a href="/recipes/users/2035254/">Nick Coghlan</a>
(<a href="/recipes/tags/text/">text</a>).
Revision 3.
</p>
<p>Python 2.6 and 3.0 make it practical to implicitly convert hexadecimal Unicode escape sequences sent to stdout or stderr (or other text files) back to the original Unicode characters.</p>
Number to string in arbitrary base (Python)
2005-02-02T04:56:21-08:00Nick Coghlanhttp://code.activestate.com/recipes/users/2035254/http://code.activestate.com/recipes/365468-number-to-string-in-arbitrary-base/
<p style="color: grey">
Python
recipe 365468
by <a href="/recipes/users/2035254/">Nick Coghlan</a>
.
</p>
<p>The function <code>num_in_base</code> can be used to print a number using an arbitrary base. It allows numbers to be padded to a minimum field width, and can display negative numbers in a complemented format instead of with a leading negative sign.</p>
<p>The digits used can be overriden with an arbitrary sequence.</p>
Executing modules inside packages with '-m' (Python)
2004-10-10T19:03:49-07:00Nick Coghlanhttp://code.activestate.com/recipes/users/2035254/http://code.activestate.com/recipes/307772-executing-modules-inside-packages-with-m/
<p style="color: grey">
Python
recipe 307772
by <a href="/recipes/users/2035254/">Nick Coghlan</a>
.
Revision 3.
</p>
<p>Python 2.4 provides the '-m' option to run a module as a script. However, "python -m <script>" will report an error if the specified script is inside a package.</p>
<p>Putting the following code in a module called "execmodule.py" and placing it in a directory on sys.path allows scripts inside packages to be executed using "python -m execmodule <script>".</p>
Sorting dictionaries by value in Python 2.4 (Python)
2004-09-13T04:19:24-07:00Nick Coghlanhttp://code.activestate.com/recipes/users/2035254/http://code.activestate.com/recipes/304440-sorting-dictionaries-by-value-in-python-24/
<p style="color: grey">
Python
recipe 304440
by <a href="/recipes/users/2035254/">Nick Coghlan</a>
(<a href="/recipes/tags/search/">search</a>).
</p>
<p>Python 2.4 adds a new builtin function sorted(), which can make obtaining the items of a dictionary sorted by key or value a single line operation.</p>