Popular recipes by Trent Mick http://code.activestate.com/recipes/users/4173505/2011-07-12T17:46:49-07:00ActiveState Code Recipessubtract or add a month to a datetime.date or datetime.datetime (Python) 2010-06-25T18:41:19-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577274-subtract-or-add-a-month-to-a-datetimedate-or-datet/ <p style="color: grey"> Python recipe 577274 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/date/">date</a>, <a href="/recipes/tags/datetime/">datetime</a>, <a href="/recipes/tags/month/">month</a>). </p> <p>Adding or subtracting a month to a Python <code>datetime.date</code> or <code>datetime.datetime</code> is a little bit of a pain. Here is the code I use for that. These functions return the same datetime type as given. They preserve time of day data (if that is at all important to you).</p> <p>See also: </p> <ul> <li><a href="http://code.activestate.com/recipes/476197/">Recipe 476197</a>: First / Last Day of the Month.</li> <li><a href="http://packages.python.org/MonthDelta/">monthdelta module</a></li> </ul> slugify: make a string usable in a URL or filename (JavaScript) 2011-07-12T17:46:49-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577787-slugify-make-a-string-usable-in-a-url-or-filename/ <p style="color: grey"> JavaScript recipe 577787 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/filename/">filename</a>, <a href="/recipes/tags/nodejs/">nodejs</a>, <a href="/recipes/tags/slug/">slug</a>, <a href="/recipes/tags/slugify/">slugify</a>, <a href="/recipes/tags/url/">url</a>). Revision 2. </p> <p>"Slugify" a string so it has only alphanumeric and hyphen characters. Useful for URLs and filenames. This is a JavaScript (node.js too) version of <a href="http://code.activestate.com/recipes/577257/">Recipe 577257</a>.</p> <p>Note: It is missing the guarantee that only ascii characters are passed through. I don't know an NFKD equivalent in JavaScript-land.</p> Perl URL encode and decode (Perl) 2010-11-02T17:52:46-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577450-perl-url-encode-and-decode/ <p style="color: grey"> Perl recipe 577450 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/decode/">decode</a>, <a href="/recipes/tags/encode/">encode</a>, <a href="/recipes/tags/url/">url</a>). </p> <p>URL encode and decode functions for Perl.</p> nicerest: pretty-print JSON output (JavaScript) 2011-01-18T22:59:12-08:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577549-nicerest-pretty-print-json-output/ <p style="color: grey"> JavaScript recipe 577549 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/curl/">curl</a>, <a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/node/">node</a>, <a href="/recipes/tags/nodejs/">nodejs</a>, <a href="/recipes/tags/rest/">rest</a>). Revision 3. </p> <p>Save this to "nicerest", <code>chmod +x</code>-it, and pipe your REST API <code>curl</code> calls through this for nicer output. It will notice HTTP headers (curl's <code>-i</code> option) and skips those before attempting to pretty-print the following JSON.</p> <p>Note: This is currently using node 0.2. I should update for 0.3 changes (I think <code>process.openStdin</code> changed).</p> query yes/no (Python) 2010-03-09T17:57:28-08:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577058-query-yesno/ <p style="color: grey"> Python recipe 577058 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/ask/">ask</a>, <a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/no/">no</a>, <a href="/recipes/tags/query/">query</a>, <a href="/recipes/tags/raw_input/">raw_input</a>, <a href="/recipes/tags/yes/">yes</a>). Revision 2. </p> <p>Ask the user a question using raw_input() and looking something like this:</p> <pre class="prettyprint"><code>QUESTION [Y/n] ...validate... </code></pre> <p>See also: <a href="http://code.activestate.com/recipes/577096/">Recipe 577096</a> (query custom answers), <a href="http://code.activestate.com/recipes/577097/">Recipe 577097</a> (query yes/no/quit), <a href="http://code.activestate.com/recipes/577098/">Recipe 577098</a> (query long), <a href="http://code.activestate.com/recipes/577099/">Recipe 577099</a> (query)</p> transform command line arguments to args and kwargs for a method call (Python) 2010-03-21T22:41:42-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577122-transform-command-line-arguments-to-args-and-kwarg/ <p style="color: grey"> Python recipe 577122 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/argv/">argv</a>, <a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/json/">json</a>). Revision 2. </p> <p>For many of my Python modules I find it convenient to provide a small <code>if __name__ == "__main__": ...</code> block to be able to call individual methods from the command line. This requires some kind of translation of command-line string arguments to <code>args</code> and <code>kwargs</code> for the method call. This recipe uses a few conventions to do that:</p> <ul> <li>the first argument is the method name</li> <li>positional args are positional (duh)</li> <li>"key=value" is a keyword argument</li> <li>an attempt is made to interpret arguments as JSON to allow specifying types other than string</li> </ul> slugify: make a string usable in a URL or filename (Python) 2010-06-07T04:11:55-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577257-slugify-make-a-string-usable-in-a-url-or-filename/ <p style="color: grey"> Python recipe 577257 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/ascii/">ascii</a>, <a href="/recipes/tags/django/">django</a>, <a href="/recipes/tags/filename/">filename</a>, <a href="/recipes/tags/slug/">slug</a>, <a href="/recipes/tags/slugify/">slugify</a>, <a href="/recipes/tags/url/">url</a>). Revision 2. </p> <p>"Slugify" a string so it is ascii, has only alphanumeric and hyphen characters. Useful for URLs and filenames. This is heavily based on the slugify in Django.</p> <p>Note: presumes that you've <code>import re</code>d higher up in your module.</p> quick 3-column grid CSS (CSS) 2010-09-15T17:13:26-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577391-quick-3-column-grid-css/ <p style="color: grey"> CSS recipe 577391 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/column/">column</a>, <a href="/recipes/tags/grid/">grid</a>, <a href="/recipes/tags/layout/">layout</a>). </p> <p>CSS snippet for a 3-column grid. Based on that used for some Apple product pages.</p> <p>HTML:</p> <pre class="prettyprint"><code>&lt;div class="grid3col"&gt; &lt;div class="column first"&gt; ... &lt;/div&gt; &lt;div class="column"&gt; ... &lt;/div&gt; &lt;div class="column last"&gt; ... &lt;/div&gt; &lt;/div&gt; </code></pre> <p>In general, you have to recalculate the widths based on (a) the available full width in your pages and (b) the amount a spacing you want between columns.</p> node.js quicklog method to log to a file (JavaScript) 2010-08-11T05:08:57-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577351-nodejs-quicklog-method-to-log-to-a-file/ <p style="color: grey"> JavaScript recipe 577351 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/nodejs/">nodejs</a>). </p> <p>I tend to have a "quicklog" method for a few of the languages I'm working in to do logging when stdout/stderr isn't necessarily available (GUI app) or convenient (lots of other output on stdout, etc.). My usage with <a href="http://nodejs.org">nodejs</a> was while working on the node REPL. Log output to stdout interfered with the REPL.</p> Using a getter for a one-time calculation of a JavaScript object attribute (JavaScript) 2010-07-16T18:10:51-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577310-using-a-getter-for-a-one-time-calculation-of-a-jav/ <p style="color: grey"> JavaScript recipe 577310 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/getter/">getter</a>, <a href="/recipes/tags/performance/">performance</a>). </p> <p>This is a technique for using a JavaScript getter to calculate the value of an attribute <strong>just the first time</strong>. Rather than caching the value in some private variable and returning it, you just delete the getter and put the calculated value in its place.</p> <p>Note: I'd read about this technique ages ago, but forgot the details and had to look-up "delete" for removing the getter. :)</p> file path generator from path patterns (Python) 2010-07-09T19:10:59-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577230-file-path-generator-from-path-patterns/ <p style="color: grey"> Python recipe 577230 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/path/">path</a>, <a href="/recipes/tags/paths/">paths</a>, <a href="/recipes/tags/walk/">walk</a>). Revision 4. </p> <p>Provides a <code>_paths_from_path_patterns</code> that will generate a list of paths from a list of path patterns. A "path pattern" can include glob chars. By default it generates a recursive listing of file paths, but: recursion can be turned off, file and/or dir paths can be listed. It supports a list of glob exclusions or inclusions.</p> <p>This function makes it easy to implement typical "-r|--recursive" and "-x|--exclude" options for command-line scripts that work on given file paths. See <a href="#block-1">example usages below</a>.</p> <p>Note: I use a leading <code>_</code> on function names because my typical usage of my recipes is as re-usable <em>internal</em> functions in Python modules.</p> Python script main line for graceful exception handling and logging (Python) 2010-09-11T05:46:59-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577258-python-script-main-line-for-graceful-exception-han/ <p style="color: grey"> Python recipe 577258 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/error/">error</a>, <a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/main/">main</a>, <a href="/recipes/tags/mainline/">mainline</a>, <a href="/recipes/tags/script/">script</a>). Revision 5. </p> <p>This is a recipe is often use for the mainline of my Python scripts. With this recipe your Python script will:</p> <ul> <li>gracefully handle <code>Ctrl+C</code> (i.e. <code>KeyboardInterrupt</code>)</li> <li>log an error (using the <code>logging</code> module) for uncaught exceptions, importantly <strong>with the file and line number</strong> in your Python code where the exception was raised</li> <li>gracefully ignore a closed output pipe (common when the user pipes your script through <code>less</code> and terminates that)</li> <li>if your script logger is enabled for <code>DEBUG</code> level logging, a full traceback will be shown for an uncaught exception</li> </ul> <p>Presumptions:</p> <ul> <li><p>you have a global <code>log</code> variable a la:</p> <pre class="prettyprint"><code>import logging log = logging.setLevel("scriptname") </code></pre></li> <li><p>your script's entry point is a function <code>def main(argv): ...</code></p></li> </ul> parse a date/time string to a `datetime` instance (Python) 2010-04-02T07:32:17-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577135-parse-a-datetime-string-to-a-datetime-instance/ <p style="color: grey"> Python recipe 577135 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/date/">date</a>, <a href="/recipes/tags/datetime/">datetime</a>, <a href="/recipes/tags/parse/">parse</a>, <a href="/recipes/tags/time/">time</a>). Revision 2. </p> <pre class="prettyprint"><code>&gt;&gt;&gt; import datetime &gt;&gt;&gt; str(datetime.datetime.now()) '2010-03-21 21:33:32.750246' &gt;&gt;&gt; str(datetime.date.today()) '2010-03-21' </code></pre> <p>This function goes the other way for date and datetime strings of this format.</p> query yes/no/quit (Python) 2010-03-09T17:57:18-08:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577097-query-yesnoquit/ <p style="color: grey"> Python recipe 577097 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/ask/">ask</a>, <a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/no/">no</a>, <a href="/recipes/tags/query/">query</a>, <a href="/recipes/tags/quit/">quit</a>, <a href="/recipes/tags/raw_input/">raw_input</a>, <a href="/recipes/tags/yes/">yes</a>). </p> <p>Ask the user a question using raw_input() and looking something like this:</p> <pre class="prettyprint"><code>QUESTION [Y/n/q] ...validate... </code></pre> <p>See also: <a href="http://code.activestate.com/recipes/577058/">Recipe 577058</a> (query yes/no), <a href="http://code.activestate.com/recipes/577096/">Recipe 577096</a> (query custom answers), <a href="http://code.activestate.com/recipes/577098/">Recipe 577098</a> (query long), <a href="http://code.activestate.com/recipes/577099/">Recipe 577099</a> (query)</p> command line query (Python) 2010-03-09T18:37:52-08:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577098-command-line-query/ <p style="color: grey"> Python recipe 577098 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/ask/">ask</a>, <a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/query/">query</a>, <a href="/recipes/tags/raw_input/">raw_input</a>). Revision 3. </p> <p>Ask the user a question using raw_input() and looking something like this (<code>style=="compact"</code>):</p> <pre class="prettyprint"><code>QUESTION [DEFAULT]: _ ...validation... </code></pre> <p>or this (<code>style=="verbose"</code>):</p> <pre class="prettyprint"><code>QUESTION Hit &lt;Enter&gt; to use the default, DEFAULT. &gt; _ ...validate... </code></pre> <p>It supports some basic validation/normalization of the given answer.</p> <p>See also: <a href="http://code.activestate.com/recipes/577058/">Recipe 577058</a> (query yes/no), <a href="http://code.activestate.com/recipes/577097/">Recipe 577097</a> (query yes/no/quit), <a href="http://code.activestate.com/recipes/577096/">Recipe 577096</a> (query custom answers)</p> query custom answers (Python) 2010-03-09T17:57:22-08:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577096-query-custom-answers/ <p style="color: grey"> Python recipe 577096 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/query/">query</a>). </p> <p>Ask the user a question using raw_input() and looking something like this:</p> <pre class="prettyprint"><code>Frob nots the zids? [Yes (default), No, quit] _ ...validate... </code></pre> <p>See also: <a href="http://code.activestate.com/recipes/577058/">Recipe 577058</a> (query yes/no), <a href="http://code.activestate.com/recipes/577097/">Recipe 577097</a> (query yes/no/quit), <a href="http://code.activestate.com/recipes/577098/">Recipe 577098</a> (query long), <a href="http://code.activestate.com/recipes/577099/">Recipe 577099</a> (query)</p> query (Python) 2010-03-09T18:34:05-08:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577099-query/ <p style="color: grey"> Python recipe 577099 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/ask/">ask</a>, <a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/query/">query</a>, <a href="/recipes/tags/raw_input/">raw_input</a>). </p> <p>Ask/prompt the user for a short piece of data. <a href="http://code.activestate.com/recipes/577098/">Recipe 577098</a> is a much more generic/functional (but longer) take on this.</p> write_path and load_path methods for file generation scripts (Python) 2010-07-27T06:45:51-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577172-write_path-and-load_path-methods-for-file-generati/ <p style="color: grey"> Python recipe 577172 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/files/">files</a>). Revision 7. </p> <p>I have a lot of scripts that end up writing files (often build system stuff). Everytime I either end up writing the obvious quick <code>content = open(path).read()</code> or I re-implement a function that handles things like: making a backup, some typical logging, encoding support, trying to make it no-op if no changes, etc.</p> <p>In this recipe I'll try to add a number of these features so I don't have to keep re-writing this. :) So far this is just a start.</p> <p>Current features:</p> <ul> <li>rudimentary <code>encoding</code> support</li> <li>logging on a given <code>log</code> argument</li> <li><code>create_backup</code> argument to create a backup file</li> <li>writes to a temporary file and uses atomic <code>os.rename</code> to avoid destroying the existing file if writing fails</li> </ul> send a multipart email (Python) 2009-10-15T13:56:10-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/576931-send-a-multipart-email/ <p style="color: grey"> Python recipe 576931 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/email/">email</a>, <a href="/recipes/tags/multipart/">multipart</a>, <a href="/recipes/tags/smtp/">smtp</a>). Revision 2. </p> <p>A simple function to send an email (can specify text and html parts).</p> <p>See also: <a href="http://code.activestate.com/recipes/576824/">Recipe 576824</a> (send via Gmail's SMTP server), <a href="http://code.activestate.com/recipes/576858/">Recipe 576858</a> (more complex, but supports attachments).</p> touch (Python) 2009-09-28T16:19:23-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/576915-touch/ <p style="color: grey"> Python recipe 576915 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/shell/">shell</a>, <a href="/recipes/tags/touch/">touch</a>, <a href="/recipes/tags/unix/">unix</a>). </p> <p>Python function a la the Unix <code>touch</code> program (<a href="http://www.manpagez.com/man/1/touch/">man touch</a>).</p>