Most viewed recipes by Jack Trainor http://code.activestate.com/recipes/users/4076953/views/2015-12-28T17:56:08-08:00ActiveState Code RecipesSimple breadth-first, depth-first tree traversal (Python) 2015-12-18T18:15:18-08:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/579138-simple-breadth-first-depth-first-tree-traversal/ <p style="color: grey"> Python recipe 579138 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/tree/">tree</a>). </p> <p>When you want to avoid recursion with a tree, you read the tree nodes into a stack, which is organized either breadth-first or depth-first.</p> <p>Here are two dead simple routines for doing so. Most of the recipe is just a test bed for those functions.</p> "More Sex is Safer Sex" Paradox (Python) 2010-09-22T02:56:37-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/577403-more-sex-is-safer-sex-paradox/ <p style="color: grey"> Python recipe 577403 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/educational/">educational</a>, <a href="/recipes/tags/simulation/">simulation</a>). </p> <p>In his book, "More Sex Is Safer Sex: The Unconventional Wisdom of Economics, " Steven Landsburg argues that if sexual conservatives took more sexual partners it would improve everyone's chances of finding low-risk partners, thereby reducing the spread of STDs for all. </p> <p>See <a href="http://www.nytimes.com/2007/07/08/books/chapters/0708-1st-land.html?_r=1&amp;pagewanted=print" rel="nofollow">http://www.nytimes.com/2007/07/08/books/chapters/0708-1st-land.html?_r=1&amp;pagewanted=print</a> .</p> <p>This recipe attempts to simulate the scenario which Landsburg describes and test his claim that "More sex is safer sex."</p> Simple calls to Python functions from command line or shortcut (Python) 2008-07-22T02:17:06-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/528891-simple-calls-to-python-functions-from-command-line/ <p style="color: grey"> Python recipe 528891 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 3. </p> <p>This module allows one to create a command to call a Python function from the command line, and to call that function with arguments from the command line without using getopt or optparse</p> Python 2.7 Linked List vs List (Python) 2010-08-13T04:05:42-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/577355-python-27-linked-list-vs-list/ <p style="color: grey"> Python recipe 577355 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/educational/">educational</a>, <a href="/recipes/tags/list/">list</a>). </p> <p>Linked list implementation based on Python 2.7 collections.MutableSequence with a few benchmarks comparing the linked list with the built-in list type.</p> Unicode String Hex Dump (Python) 2008-05-03T01:12:27-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/572181-unicode-string-hex-dump/ <p style="color: grey"> Python recipe 572181 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/debugging/">debugging</a>). Revision 3. </p> <p>Simple routine for dumping any kind of string, ascii, encoded, or unicode, to a standard hex dump. Plus read/write of unicode and encoded strings.</p> PathCatcher - Windows utility for right-click capture of file or folder path (Python) 2008-04-19T17:14:18-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/528893-pathcatcher-windows-utility-for-right-click-captur/ <p style="color: grey"> Python recipe 528893 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 3. </p> <p>PathCatcher is a Windows utility that allows one to right-click on a file or a folder or a group of files and folders in Explorer and save its path to the clipboard.</p> <p>To install, save the code as PathCatcher.py, then double-click the PathCatcher.py file. Afterwards, PathCatcher will appear in the Explorer right-click menu.</p> Multimap: Associating multiple values to a key. (Python) 2009-07-08T13:52:49-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/576835-multimap-associating-multiple-values-to-a-key/ <p style="color: grey"> Python recipe 576835 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/data_structures/">data_structures</a>, <a href="/recipes/tags/defaultdictionary/">defaultdictionary</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/multimap/">multimap</a>). </p> <p>Implementation of multimap based on lists, dicts, or sets using Python 2.5 defaultdict.</p> Convert text/enriched MIME to text/html (Python) 2009-06-09T15:08:40-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/576800-convert-textenriched-mime-to-texthtml/ <p style="color: grey"> Python recipe 576800 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/email/">email</a>, <a href="/recipes/tags/html/">html</a>, <a href="/recipes/tags/mime/">mime</a>, <a href="/recipes/tags/text_enriched/">text_enriched</a>). </p> <p>Converts text stream in text/enriched MIME format from file or stdin to text/html output to file or stdout.</p> Mandelbrot Set made simple (Python) 2015-12-28T17:56:08-08:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/579143-mandelbrot-set-made-simple/ <p style="color: grey"> Python recipe 579143 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/educational/">educational</a>, <a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/mandelbrot/">mandelbrot</a>, <a href="/recipes/tags/mathematics/">mathematics</a>). Revision 2. </p> <p>This is a mini-framework for exploring the Mandelbrot Set.</p> <p>It outputs to a Tkinter window or to a PNG file (Pillow package required). It includes a disk caching mechanism for saving and later retrieving Mandelbrot calculations to avoid recalculating the Set on each run.</p> <p>See module documentation for further information. Compatible with Python 2 and 3.</p> Walker encapsulates os.walk for easy reuse (Python) 2007-08-29T15:07:29-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/528904-walker-encapsulates-oswalk-for-easy-reuse/ <p style="color: grey"> Python recipe 528904 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/files/">files</a>). Revision 2. </p> <p>Walker encapsulates os.walk's directory traversal as an object with the added features of excluded directories and a hook for calling an outside function to act on each file.</p> <p>Walker can easily be subclassed for more functionality, as with ReWalker which filters filenames in traversal by a regular expression.</p> Roll your own Postscript code from scratch (Python) 2015-12-09T23:30:13-08:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/579136-roll-your-own-postscript-code-from-scratch/ <p style="color: grey"> Python recipe 579136 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/ghostscript/">ghostscript</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/postscript/">postscript</a>, <a href="/recipes/tags/ps/">ps</a>). </p> <p>This recipe provides a mini-framework for creating custom Postscript PS and PDF files from scratch. It includes sample code for a personalized business index card.</p> <p>Recipe does not use any Python PDF libraries. However, Ghostscript and a PDF viewer are useful for displaying/debugging output.</p> <p>It's easier than you might think to roll your own Postscript code!</p> Line Oriented State Machine (Python) 2008-06-30T05:13:27-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/574430-line-oriented-state-machine/ <p style="color: grey"> Python recipe 574430 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/text/">text</a>). Revision 4. </p> <p>State machine for processing text files on a line-by-line basis using regular expressions to determine transition to next state.</p> <p>Developed from sample code from "Text Processing in Python" by David Mertz,pp. 274-280.</p> Simplest Registry I/O (Python) 2007-08-24T12:38:48-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/528896-simplest-registry-io/ <p style="color: grey"> Python recipe 528896 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <p>Most of the time I just want to either read or write one value to the registry. Here are two functions: ReadRegistryValue and WriteRegistryValue plus a few helpers.</p> Martin Gardner's "The Monkey and the Coconuts" Problem (Python) 2010-07-26T16:32:08-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/577335-martin-gardners-the-monkey-and-the-coconuts-proble/ <p style="color: grey"> Python recipe 577335 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/martingardner/">martingardner</a>, <a href="/recipes/tags/mathematical/">mathematical</a>, <a href="/recipes/tags/puzzle/">puzzle</a>). </p> <p>Five sailors arrive at a deserted island that has only coconuts and one monkey. The sailors collect all the coconuts into one big pile and agree to divide up the coconuts into equal shares the next morning. However during the night each sailor wakes up one at a time afraid to trust the others and decides to take his share secretly. So each sailor takes 1/5 of the coconuts and hides it. Each time there is one coconut left over and the sailor gives that to the monkey. In the morning they divide what is left of the pile into equal shares and there is still one coconut left for the monkey.</p> <p>How many coconuts were in the original pile?</p> Automate creation of new Subversion project (Python) 2008-07-09T20:57:15-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/574453-automate-creation-of-new-subversion-project/ <p style="color: grey"> Python recipe 574453 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 2. </p> <p>NewSvnProject creates a new Subversion repository, imports new files to it, then checks out those files back to original directory, with a single command or drag'n'drop.</p> TRAC Interpreter -- Sixties programming language (Python) 2010-08-22T19:05:09-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/577366-trac-interpreter-sixties-programming-language/ <p style="color: grey"> Python recipe 577366 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/educational/">educational</a>, <a href="/recipes/tags/interpreter/">interpreter</a>, <a href="/recipes/tags/language/">language</a>, <a href="/recipes/tags/trac/">trac</a>). </p> <p>A Python implementation of an interpreter for the TRAC programming language developed by Calvin Mooers in the early 1960s. This implementation reconstructs Mooers' original algorithm in Python and supports only a limited number of TRAC primitives for demonstration purposes.</p> State Machine for Text Processing (Python) 2009-01-21T14:01:23-08:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/576624-state-machine-for-text-processing/ <p style="color: grey"> Python recipe 576624 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/state_machine/">state_machine</a>, <a href="/recipes/tags/text_processing/">text_processing</a>). </p> <p>General state machine mechanism plus a specialized version, LineStateMachine, for processing text files based by using regular expressions to determine state transitions.</p> Dragon Lexical Analyzer (Python) 2010-09-01T14:49:37-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/577380-dragon-lexical-analyzer/ <p style="color: grey"> Python recipe 577380 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/educational/">educational</a>, <a href="/recipes/tags/lexical_analyzer/">lexical_analyzer</a>, <a href="/recipes/tags/parsing/">parsing</a>). Revision 2. </p> <p>The lexical analyzer from "Compliers: Principles, Techniques and Tools," Chapter 2, by Aho, Sethi, Ullman (1986) implemented in Python.</p> CommentEditor: HTML editor for online comments (Python) 2009-06-17T12:10:55-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/576814-commenteditor-html-editor-for-online-comments/ <p style="color: grey"> Python recipe 576814 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/editor/">editor</a>, <a href="/recipes/tags/html/">html</a>, <a href="/recipes/tags/wxwidgets/">wxwidgets</a>). Revision 3. </p> <p>Edit online comments with easy addition of HTML tags for bold, italics, underlining, blockquote and anchored links. Then check your work with the preview feature.</p> <p>Requires wxWidgets.</p> WxFileRenamer - GUI app for renaming files by regular expressions (Python) 2008-07-07T12:41:48-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/574448-wxfilerenamer-gui-app-for-renaming-files-by-regula/ <p style="color: grey"> Python recipe 574448 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/files/">files</a>). </p> <p>WxFileRenamer is a small GUI utility for renaming files in a directory with regular expressions, which allows the user to preview the changes before committing them to disk.</p>