Most viewed recipes by Stephen Chappell http://code.activestate.com/recipes/users/2608421/views/2014-11-07T17:51:32-08:00ActiveState Code RecipesSimple Average Calculation (Python) 2012-07-11T12:27:59-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/440625-simple-average-calculation/ <p style="color: grey"> Python recipe 440625 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/programs/">programs</a>). Revision 2. </p> <p>Here is a simple program that can average a list of numbers. I wrote this to introduce someone to the language that we all use and love. :D The program may be simple, but it shows how easily a simple but useful program can turn out to be.</p> Port 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> 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> Bouncing Ball Simulation (Python) 2007-02-20T19:04:03-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/502241-bouncing-ball-simulation/ <p style="color: grey"> Python recipe 502241 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>The physics system used in this simulation is both incomplete and incorrect. This recipe evolved from the Boids Simulation and was an early attempt to simulate bouncing balls in an area contained with a force field on the sides and a floor on the bottom. The spheres will respond to movement of the window and will bounce if accelerated upward quickly. This is primarily a recipe that I later built off of to create better software later on.</p> Schedule Maker (Python) 2012-07-03T05:09:03-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/475112-schedule-maker/ <p style="color: grey"> Python recipe 475112 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/cgi/">cgi</a>). Revision 5. </p> <p>This CGI program is designed to create schedules once given valid information on a form. It is not designed to look pretty but tests the ability to combine custom modules in different sized projects. It has minimal functionality but gets its job done in a timely manner and in a format that can be understood.</p> Proxy Example (Python) 2007-02-01T21:55:34-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/502205-proxy-example/ <p style="color: grey"> Python recipe 502205 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/programs/">programs</a>). </p> <p>This is just a simple example usage example of the proxy module presented here: <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/502204" rel="nofollow">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/502204</a></p> Timeout Any Function (Python) 2010-02-04T17:36:31-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577028-timeout-any-function/ <p style="color: grey"> Python recipe 577028 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/asynchronous/">asynchronous</a>, <a href="/recipes/tags/generic/">generic</a>, <a href="/recipes/tags/process/">process</a>, <a href="/recipes/tags/timeout/">timeout</a>, <a href="/recipes/tags/universal_code/">universal_code</a>). Revision 6. </p> <p>This recipe uses the <code>multiprocessing</code> module to kill functions that have run longer than intended. Unlike other recipes that use threading, this code is designed to actually kill execution that has continued for too long. This implementation does not rely on signals or anything that is OS specific, so it should work on any system on which you might need generic timeouts.</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> War Card Game Simulation (Python) 2012-07-01T01:23:03-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578179-war-card-game-simulation/ <p style="color: grey"> Python recipe 578179 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/automatically/">automatically</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/simulation/">simulation</a>). </p> <p>After playing the card game with people for the first time, the game turned out to be rather boring. The way we played took absolutely no thought whatsoever and turned out to be very easy to automate. The game below is configured for four different players to complete the simple game until it ends. Each player's hand in printed, and the cards played in each round is shown till someone finishes. Whoever wins is shown at the end of the simulation, and the program quickly exits when complete.</p> Example For winreg (2) (Python) 2011-05-20T13:22:37-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/498151-example-for-winreg-2/ <p style="color: grey"> Python recipe 498151 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 2. </p> <p>This recipe is another example of how to use the winreg module. The code had the first purpose of demonstrating the concept of a graphical shell built in Python. The shell was easily modified to make use of the Window's registry but retains traces of its original method of operation (all of which has been commented out).</p> <p>The program down below originally used the pickle module to save all of its settings when closing down. Now once the escape key is pressed, this Python 2.5 program can use the module presented in <a href="http://code.activestate.com/recipes/510392/">recipe 510392</a> to access Microsoft Window's Registry and store its settings there. Check <code>do_exit</code> and <code>do_config</code> functions for usage.</p> Disk Dumper (Python) 2006-03-20T20:33:47-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/475157-disk-dumper/ <p style="color: grey"> Python recipe 475157 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/programs/">programs</a>). </p> <p>This recipe will display a hex dump of the disk specified on the command line. As the last two arguments, the program takes the first sector and last sector that should be displayed by this utility. The size of the sectors is stored in a variable created right after the imports executed by this script. The main feature of this recipe is its cross OS capabilities as demonstrated in get_data.</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> Discreet Simulation of Round Robin Scheduling (Python) 2012-01-10T15:33:42-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/475215-discreet-simulation-of-round-robin-scheduling/ <p style="color: grey"> Python recipe 475215 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/programs/">programs</a>). </p> <p>This recipe demonstrates DS of RRS (Discreet Simulation of Round Robin Scheduling). The purpose of this exercise is twofold.</p> <ol> <li>It demonstrates discreet simulation in a practical way.</li> <li>It demonstrates how different factors affect round robin scheduling.</li> </ol> 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> Text Editor in Python 3.3 (Python) 2013-06-19T15:58:17-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578569-text-editor-in-python-33/ <p style="color: grey"> Python recipe 578569 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/editor/">editor</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/text_processing/">text_processing</a>). </p> <p>This is a simple text editor written in Python using <code>tkinter</code> for graphics.</p> <p>Check out Captain DeadBones' <a href="http://thelivingpearl.com/">Chronicles</a> blog.</p> Copying and Pasting Directories (and Files) (Python) 2005-10-02T18:54:25-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/440626-copying-and-pasting-directories-and-files/ <p style="color: grey"> Python recipe 440626 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/files/">files</a>). </p> <p>Here are a few basic fuctions that allow someone to copy a directory (along with its files and sub-directories) and paste it somewhere else. They ignore links and anything else that is not a directory or a file. The data returned from copy_dir() is in a format that paste_dir() can understand. A design note: what you get back from copy_dir() may be different whether or not you have a path separator at the end of the path; paste_dir() makes use of that feature (whether or not you want the first folder to be created). The fuctions can easily be modified to not include sub-directories in the returned package, and errors caused by accessing to files or directories can also be ignored with some simple editting. Also, note the asserts in the functions; these functions are designed to crash hard so that the calling code has to take care of the errors.</p> War Game (Version 1) (Python) 2005-10-02T17:33:38-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/440621-war-game-version-1/ <p style="color: grey"> Python recipe 440621 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/programs/">programs</a>). </p> <p>This is the first version of a game that I wrote. If you have ever played a card game called "WAR," then you already should be familiar with one version of this game. Well, this is my first interpretation of the game. One little note about winning the game -- control the "nines." That is how you will win. :)</p> MineSweep (Python) 2010-09-16T14:45:36-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577365-minesweep/ <p style="color: grey"> Python recipe 577365 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/minesweep/">minesweep</a>, <a href="/recipes/tags/python/">python</a>). Revision 2. </p> <p>A friend was wondering how to implement MineSweep and decided to try making the game in Excel. Unfortunately, the game was not quite as interactive as he wanted; and he is unable to get around certain limitations at the moment since he is still new to programming. To show off the power of Python and demonstrate how easy it can be to develop a simple demonstration program in the language, the following program (and its iterations) were put together between the hours of 1 and 6 in the morning. It could certainly be much better in its implementation, but any serious work were to be put into MineSweep, starting over from scratch would be the best choice (especially with interface design).</p> Module For Running Simple Proxies (Python) 2007-02-01T21:51:53-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/502204-module-for-running-simple-proxies/ <p style="color: grey"> Python recipe 502204 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/network/">network</a>). </p> <p>Proxies can be useful at times but may not be simple to create and run. This recipe provides a single class that can build proxy objects capable of being both started and stopped.</p> timer.py (Python) 2012-07-02T19:10:03-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/492231-timerpy/ <p style="color: grey"> Python recipe 492231 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/threads/">threads</a>). </p> <p>The Timer class was originally written to be used in conjuction with a Progress Bar and a custom driver function. Three example functions are given in discussion to help better illustrate how this can be used.</p>