Popular recipes by Jack Trainor http://code.activestate.com/recipes/users/4076953/2016-03-11T03:39:32-08:00ActiveState Code RecipesCatalog multiple drives (Python) 2016-03-11T03:39:32-08:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/580619-catalog-multiple-drives/ <p style="color: grey"> Python recipe 580619 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/drives/">drives</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/utility/">utility</a>). </p> <p>As one accumulates multiple drives, hard and flash, containing, thousands even millions of files, it becomes useful to have a text file containing an alphabetized catalog list of all files and their locations by drive and directory.</p> <p>The list can be searched by eye or by an editor to locate particular files.</p> <p>The list can also be loaded into a script to be filtered programmatically as desired.</p> FileSpec: Set it, forget it, reuse it (Python) 2016-03-08T05:49:16-08:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/580618-filespec-set-it-forget-it-reuse-it/ <p style="color: grey"> Python recipe 580618 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/filename/">filename</a>, <a href="/recipes/tags/utilities/">utilities</a>). </p> <p>Python provides good utilities for transforming filenames, but they are tedious to use and clutter up the source code.</p> <p>FileSpec offers one-stop shopping to convert a file path to every component you might want to know, reuse, or transform.</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> 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> MicroXml: Stand-alone library for basic XML features (C++) 2016-02-18T19:33:07-08:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/580612-microxml-stand-alone-library-for-basic-xml-feature/ <p style="color: grey"> C++ recipe 580612 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/parsing/">parsing</a>, <a href="/recipes/tags/xml/">xml</a>). </p> <p>MicroXml provides stand-alone support for the basic, most-used features of XML -- tags, attributes, and element values. It produces a DOM tree of XML nodes. MicroXml does not support DTDs, CDATAs and other advanced XML features.</p> <p>MicroXml is easy to use and provides easy access to view/navigate its nodes in a debugger.</p> Simple 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> MicroXml: Stand-alone library for basic XML features (Python) 2015-12-04T22:36:56-08:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/579133-microxml-stand-alone-library-for-basic-xml-feature/ <p style="color: grey"> Python recipe 579133 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/parsing/">parsing</a>, <a href="/recipes/tags/xml/">xml</a>). </p> <p>MicroXml provides stand-alone support for the basic, most-used features of XML -- tags, attributes, and element values. It produces a DOM tree of XML nodes. It's compatible with Python 2.7 and Python 3. MicroXml does not support DTDs, CDATAs and other advanced XML features.</p> <p>MicroXml is easy to use and easy to view/navigate its nodes in a debugger. It also includes a minimal XPath-like implementation.</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> 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> TRAC Interpreter - Dragon style (Python) 2010-09-20T00:42:21-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/577396-trac-interpreter-dragon-style/ <p style="color: grey"> Python recipe 577396 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/lexical_analyzer/">lexical_analyzer</a>, <a href="/recipes/tags/trac/">trac</a>). </p> <p>I've rewritten the TRAC interpreter from <a href="http://code.activestate.com/recipes/577366/">Recipe 577366</a> using a modern recursive descent style based on the Dragon book's lexer/parser model at the end of "Compliers: Principles, Techniques and Tools," Chapter 2, by Aho, Sethi, Ullman (1986).</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> 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> TRAC Interpreter - Class and Stack version (Python) 2010-09-20T01:16:07-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/577397-trac-interpreter-class-and-stack-version/ <p style="color: grey"> Python recipe 577397 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/lexical_analyzer/">lexical_analyzer</a>, <a href="/recipes/tags/trac/">trac</a>). </p> <p>This is my third and final version of the Trac Interpreter from <a href="http://code.activestate.com/recipes/577366/">Recipe 577366</a>. It processes the character stream into appropriate class objects and stores these objects on a stack.</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> Attribute-based Framework 1: Basics (Python) 2010-07-21T21:40:54-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/577327-attribute-based-framework-1-basics/ <p style="color: grey"> Python recipe 577327 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/educational/">educational</a>, <a href="/recipes/tags/framework/">framework</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/simulation/">simulation</a>). Revision 2. </p> <p>I am adapting Prof. David Cheriton's OO software methodology to Python. It's an approach for building industrial-strength code with a disciplined architecture, consistent naming conventions, and a rigorous division of interface from implementation. I'll be adding more of his techniques in further recipes. These recipes are based on Cheriton's CS249a course at Stanford.</p> Consolidate group of modules into one recipe (Python) 2010-07-21T21:22:46-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/577297-consolidate-group-of-modules-into-one-recipe/ <p style="color: grey"> Python recipe 577297 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/programs/">programs</a>, <a href="/recipes/tags/recipe/">recipe</a>, <a href="/recipes/tags/shortcut/">shortcut</a>). Revision 4. </p> <p>Packs all python source files from current directory into a single recipe file that can be later run to recreate packed files. Skips files whose names begin with underscore or contain "recipe".</p> Attribute-based Framework 2: Notifications (Python) 2010-07-23T20:12:10-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/577330-attribute-based-framework-2-notifications/ <p style="color: grey"> Python recipe 577330 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/educational/">educational</a>, <a href="/recipes/tags/framework/">framework</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/simulation/">simulation</a>). Revision 2. </p> <p>I am adapting Prof. David Cheriton's OO software methodology to Python. It's an approach for building industrial-strength code with a disciplined architecture, consistent naming conventions, and a rigorous division of interface from implementation. I'll be adding more of his techniques in further recipes. These recipes are based on Cheriton's CS249a course at Stanford.</p> BigDirs - Where's my disk space gone? (Python) 2010-07-18T02:52:18-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/577301-bigdirs-wheres-my-disk-space-gone/ <p style="color: grey"> Python recipe 577301 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/administration/">administration</a>, <a href="/recipes/tags/shortcut/">shortcut</a>). Revision 2. </p> <p>BigDirs locates directories taking up the most disk space in files at that directory level, not in the nested directories below it. In my experience this is the easiest way to find where my disk space got chewed up.</p> <p>Set DEFAULT_DIR for the root directory you wish to check. Set DEFAULT_THRESHOLD for the minimum size of the directories you wish to see in the output.</p> One-click Copy of CD/DVD for Windows (Python) 2010-03-16T11:35:23-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/577118-one-click-copy-of-cddvd-for-windows/ <p style="color: grey"> Python recipe 577118 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/shortcut/">shortcut</a>, <a href="/recipes/tags/utility/">utility</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>CdCopy: Windows utility for copying contents of a CD/DVD to local hard disk with most free space to a directory named for full name of CD/DVD.</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>