Top-rated recipes by Stephen Chappell http://code.activestate.com/recipes/users/2608421/top/2015-06-25T17:55:33-07:00ActiveState Code RecipesPort Forwarding (Python) 2012-05-15T18:45:34-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/483730-port-forwarding/ <p style="color: grey"> Python recipe 483730 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/network/">network</a>). </p> <p>The title of this recipe contains the two words that gave the inspiration for the writing of what appears below. Port forwarding -- think about it.</p> Sliding Block Puzzle Solver (Python) 2009-03-16T13:35:05-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/576692-sliding-block-puzzle-solver/ <p style="color: grey"> Python recipe 576692 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/breadth_first_search/">breadth_first_search</a>, <a href="/recipes/tags/puzzle_solver/">puzzle_solver</a>). </p> <p>Recently, I was playing a game called "An Untitled Story" and was frustrated at some of the puzzles it contained. One such puzzle type was a sliding block puzzle game that presented the player with walls, movable blocks, and targets that you had to place the blocks on. The blocks could be moved an infinite amount of times, but once put into motion, they would continue until they hit either a wall or another block. To help solve these puzzle, I wrote the following program to figure out the solutions for me. It can find valid answers far faster than the human mind. The program is somewhat messy but looks much better than its first version.</p> Reading Guide Generator (Python) 2009-01-01T10:38:15-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/576600-reading-guide-generator/ <p style="color: grey"> Python recipe 576600 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/bible/">bible</a>, <a href="/recipes/tags/program/">program</a>). </p> <p>For those of you who have not found a Bible reading schedule or are not satisfied with what you have and want a change, the following recipe is provided as the result of a programming project today for the New Year. Begin with the first reference and read up to but not including the next reference. Continue from day to day, and you will find that by the end of the year, the entire Bible has been read with equally spaced portions of reading and time. The schedule was developed with the intention to read about the same number of words per day. The Bible used was "bible13.txt" (King James Version) from Project Gutenberg.</p> Standard Error Redirector (Python) 2006-03-20T01:53:49-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/475149-standard-error-redirector/ <p style="color: grey"> Python recipe 475149 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/network/">network</a>). Revision 2. </p> <p>This recipe was designed for remotely receiving bug reports. It was written after participating in a programming contest where feedback was not helpful. The concept presented here is a step towards working with Python remotely. As sys.stderr is replaced in this recipe, so sys.stdin and sys.stdout can also be redirect to an alternate source (such as sockets connected to another computer).</p> Extracting *.tar.gz files in Windows ... (Python) 2005-11-01T10:36:05-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/442503-extracting-targz-files-in-windows/ <p style="color: grey"> Python recipe 442503 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/programs/">programs</a>). </p> <p>This recipe is meant to be used as a commmand line *.tar.gz file extractor. If it fails, then a usage note is given. It may be small, but it can be very useful for some people.</p> Examples for random float between 0 and 1 (Python) 2015-06-25T17:55:33-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/579072-examples-for-random-float-between-0-and-1/ <p style="color: grey"> Python recipe 579072 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/example/">example</a>, <a href="/recipes/tags/random/">random</a>). </p> <p>I was researching how floats are stored in a computer and was trying to think up a way to generate random values between 0 and 1. Python already provides an implementation allowing this already, and two of the functions below are directly inspired by that code, but the third is a slightly different way of doing the same thing. A similar version of this code has been used to implement similar functionality in C# at one time. Others might also find it useful if they want to create equivalent code in a separate language while having access to random bytes but not random floats. It should be noted that the various implementations get slower as you go down the list.</p> Pendulum Simulation with Turtle (Python) 2011-01-27T22:59:54-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577553-pendulum-simulation-with-turtle/ <p style="color: grey"> Python recipe 577553 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/demonstration/">demonstration</a>, <a href="/recipes/tags/simulation/">simulation</a>, <a href="/recipes/tags/turtle/">turtle</a>). </p> <p>This program was written per R.T.Giles's second assignment in 10COF180 (November 25, 2010). A student under his instruction wanted help writing a program, and while not required, a GUI was placed on the program's output to visually demonstrate what was taking place within the program. The recipe below provides a simple simulation of a undamped pendulum and produces a visual representation for about thirty seconds. The code could be greatly improved but gives a demonstration of what a few turtle commands can do in Python. Very little code must be written to produce graphics on the computer screen.</p> Boids 2.0 for Python 3.1 (Python) 2009-11-28T10:42:51-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/576959-boids-20-for-python-31/ <p style="color: grey"> Python recipe 576959 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/boids/">boids</a>). Revision 8. </p> <p>This is a revisiting of <a href="http://code.activestate.com/recipes/502216/">Recipe 502216</a>. All the code is new in this implementation, and the concept has been advanced into something akin to a screensaver. Development has ended on this version until work can be done on a third version that takes the to-do lists into account in a more structured way. Feel free to modify this code as you wish (as someone apparently did in the original recipe). If nothing else, it can serve as a short diversion from the predictability of life. Enjoy!</p> Hex Dump 2 (Python) 2009-11-03T05:25:21-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/576945-hex-dump-2/ <p style="color: grey"> Python recipe 576945 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/dump/">dump</a>, <a href="/recipes/tags/hex/">hex</a>). </p> <p>This is a fast hex-dumping utility written for Python 3.1 and later.</p> <p>Written for the command line with usage information printed as needed.</p> Tkinter Splash Screen (Python) 2009-11-09T04:26:52-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/576936-tkinter-splash-screen/ <p style="color: grey"> Python recipe 576936 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/splash/">splash</a>, <a href="/recipes/tags/tix/">tix</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 4. </p> <p>This program is <a href="http://code.activestate.com/recipes/534124/">recipe 534124</a> re-envisioned. The code is mostly the same but was written with a slightly different style. It has also been tested on Python 3.1.1 and appears to work well with GIF images. Usage is the same as can be found in the original recipe. Any further improvements are welcome, and all are encouraged to use as needed.</p> Convert Byte Size to String Representation (Python) 2012-04-13T19:55:07-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/576924-convert-byte-size-to-string-representation/ <p style="color: grey"> Python recipe 576924 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/representation/">representation</a>). Revision 4. </p> <p>In anticipation of creating large data structure, it appeared to be helpful if the users could get an idea of how much memory (RAM in particular) would be used while attempting to create a large, multidimensional array. In order to convert the calculated size into a precise, human-readable format, the follow code was devised. In order to convert a number into an equivalent representation of bytes, just call the "convert" function while providing the number as its argument. The other functions are currently public in case anyone else finds them useful.</p> Expression Evaluator (Python) 2009-06-02T23:57:44-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/576790-expression-evaluator/ <p style="color: grey"> Python recipe 576790 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/evaluation/">evaluation</a>, <a href="/recipes/tags/expressions/">expressions</a>, <a href="/recipes/tags/parsing/">parsing</a>). Revision 4. </p> <p>After reading a portion of the book "The C# Programming Language: Third Edition," I found a section in the introduction that introduced abstract classes and methods that involved an example that included the concept of expression trees. The code was easy to implement since it just had to be copied out of the book. After playing around with the program a little and extending it, I thought that it would be fun to write a program in C# that could (interactively) evaluate expressions and display the results. Not knowing C# quite as well as Python led to the following program written and tested in Python 3.0 (not sure about previous languages).</p> <p>The first section of the code includes port of the program from the aforementioned book along with extra code that allows for further features not originally included in the C# version. Those sections are clearly marked as being new code written by yours truly. The second area of the program has six functions that are profusely documented so as to explain how they go about parsing and processing expressions entered for evaluation. For those wishing to use the code, the "run" function should be all that you need. The final part of the module contains a test program that can be used to check the validity of the how well the program works.</p> <p>The parser is not very complicated and will except expressions that are both normal to Python and completely illegal in Python. The main features are its ability to (1) identify simple assignment and mathematical operations, (2) identify constant floating point numbers, and (3) identify variables that would otherwise have no other meaning to the program. A limited number of error messages are given when appropriate but may leave one guessing what the problem really is. Mathematical operations are evaluated from left to right without regards to precedence, and assignment statements are evaluated from right to left.</p> Utility Mill Test (Python) 2008-12-28T21:24:11-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/576598-utility-mill-test/ <p style="color: grey"> Python recipe 576598 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/programs/">programs</a>). </p> <p>After recently discovering the Utility Mill, I decided to create an automated tester that would check that the latest revision was still operating according to specifications. It checks the web site once a minute and displays status information as it runs. It is included here because it comes in three sections. The first section has several utility functions that you may find useful in your own applications. The actual test suite is located in the second section. The last section includes code that makes getting random text easy and fast (after an initial loading session). Hopefully, the first portion of the code may be of use to you.</p> _winreg Wrapper (Python) 2007-03-20T18:35:30-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/510392-_winreg-wrapper/ <p style="color: grey"> Python recipe 510392 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/database/">database</a>). </p> <p>This module proves itself to be a useful _winreg wrapper. Examples following this recipe include install.py, uninstall.py, and opt.py [all part of the "Kaos Rain (MKv3)" hand-eye coordination testing program].</p> Boids Demonstration (Python) 2007-02-12T21:34:08-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/502216-boids-demonstration/ <p style="color: grey"> Python recipe 502216 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>This recipe demonstrates a 2D boids simulation. The code is configurable based on some constants defined near the top. The idea for the code shown here came from the following URL: <a href="http://www.vergenet.net/%7Econrad/boids/pseudocode.html" rel="nofollow">http://www.vergenet.net/~conrad/boids/pseudocode.html</a></p> "Smart" Links (Python) 2012-07-03T05:05:39-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/440647-smart-links/ <p style="color: grey"> Python recipe 440647 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/cgi/">cgi</a>). </p> <p>The following allows someone to create "smart" links on their web site. This is just a simple system and is not very advanced, but it works quite well (given its purpose). If a link is not active (because a server is down), the link is shown as such.</p> Draw a diamond with asterisks using recursion (Python) 2014-11-07T17:51:32-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578959-draw-a-diamond-with-asterisks-using-recursion/ <p style="color: grey"> Python recipe 578959 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/drawing/">drawing</a>). </p> <p>Given the following function header:</p> <pre class="prettyprint"><code>def triangles(n): </code></pre> <p>If n for example is 5, then the drawing would be like this:</p> <pre class="prettyprint"><code> * * * * * * * * * * * * * </code></pre> Custom Windows Alarm Clock (Python) 2012-07-10T17:27:48-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578204-custom-windows-alarm-clock/ <p style="color: grey"> Python recipe 578204 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/alarm/">alarm</a>, <a href="/recipes/tags/archive/">archive</a>, <a href="/recipes/tags/clock/">clock</a>, <a href="/recipes/tags/old/">old</a>, <a href="/recipes/tags/utility/">utility</a>). </p> <p>After writing a few alarms using <code>winsound.Beep</code>, desire to expand into other sound possibilities led to the writing of this program using <code>winsound.PlaySound</code>. The sounds are stored in a custom format (some experimentation in archiving files). This is committed for archival to be run under Python 2.5 or later versions.</p> Markov Encryption Demonstration 1 (Python) 2012-03-16T18:59:06-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578062-markov-encryption-demonstration-1/ <p style="color: grey"> Python recipe 578062 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/demo/">demo</a>, <a href="/recipes/tags/demonstration/">demonstration</a>, <a href="/recipes/tags/encode/">encode</a>, <a href="/recipes/tags/encoding/">encoding</a>, <a href="/recipes/tags/encrypt/">encrypt</a>, <a href="/recipes/tags/encryption/">encryption</a>). </p> <p>This is a simple GUI program that shows Markov Encryption at work. It is built to be interactive and has all needed code embedded within itself. This version of ME library is not very efficient and represents an early attempt at developing and easily testing the code. Certain limits are built into the program, and the code is not meant to be robust at this stage. This program is a proof-of-concept design meant to ensure that the work being done was viable for future use and that the encryption process could be carried out both ways, both in encoding plaintext and decoding ciphertext.</p> Easily Write Nested Loops (Python) 2012-02-18T01:34:55-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578046-easily-write-nested-loops/ <p style="color: grey"> Python recipe 578046 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/iteration/">iteration</a>, <a href="/recipes/tags/nested/">nested</a>). </p> <p>The "nest" generator function in this module is provided to make writing nested loops easier to accomplish. Instead of writing a for loop at each level, one may call "nest" with each sequence as an argument and receive items from the sequences correctly yielded back to the caller. A test is included at the bottom of this module to demonstrate how to use the code.</p>