This snippet defines a context to execute a block of statements in a different working directory (with the help of the with statement). After the block is exited, the initial working directory is restored, even if an exception has occurred in the with block.
(Read More) Hi Folks
the game runs. jippie, but i have a small problem
i cant get the board to get drawn from left to right, its drawn from upwards to downwards
hope anyone can help me
Sincerly
elomanias
(Read More) !USING PYTHON 3.1!
USING WINDOWS
I never thought it could be so simple.
However I should have clarified that it's a 1 character input. But the general idea is the same. Someone suggested that I explain what is going on better, so I'm going to do that. The function 'getch()' waits for a...
(Read More) PlugIns in their simplest form can be just python modules that are dropped into some designated directory and dynamically loaded by the main application. This snippet can be used to do just that.
Usage
To load plugin modules:
>>> plugins = importPluginModulesIn('mypackage')
>>> plugins...
(Read More) #! /usr/bin/env python
#filename:pwd.py
# development environment:python2.51
import getpass
usr=getpass.getuser()
while True:
(Read More) A simple program to count the words, lines and sentences contained in a text file. The assumptions are made that words are separated by whitespaces, and sentences end with a period, question mark or exclamation mark.
(Read More) This is one simple way to emulate a singleton class on behalf of a normal class. (A singleton class is a class with at most one instance.)
The implementation does not emulate a class 100% (for example it lacks the special attributes lile __dict__ or __bases__), but it should be absolutely...
(Read More) You can download the matplotlib free from: http://matplotlib.sourceforge.net/
It has, amongst many other things, the module pylab that allows for high quality plotting of data. All you have to do is to feed it a list of x values and the corresponding list of y values calculated from the x data...
(Read More) This code snippet shows you how to sort more complicated objects like a list of lists (or tuples) by selecting an index of the item to sort by.
(Read More) Python can use its COM support and the OCX of the WindowsMediaPlayer to play mp3 music files. Make sure that your Python installation has the win32com folder. Check http://starship.python.net/crew/mhammond/win32/Downloads.html for the latest win32com support.
This code relies on file...
(Read More) If a word or sentence reads the same way forward and backward, then it is a palindrome. A small admonition is in place, whitespaces and punctuation marks can be ignored. Also, all the letters should be in one case, lower or upper, your choice. Ideal for Python to show off its prowess with string...
(Read More) This program uses Python module re for splitting a text file into words and removing some common punctuation marks. The word:frequency dictionary is then formed using try/except. In honor of 4th of July the text analyzed is National Anthem of USA (found via Google).
(Read More) from Tkinter import *
root = Tk()
tipwindow = None
# Creates a tooptip box for a widget.
def createToolTip( widget, text ):
def enter( event ):
global tipwindow
x = y = 0
(Read More) Some time ago, I was writing a small command line interpreter, with the help of the standard module cmd which offers minimal support for such tasks, and I had the problem that this module doesn't have a function to parse a command line to produce a list argv which can be passed to optparse for...
(Read More) The syntax of the str.format() method described in the python 2.6 documentation looks both powerful and complex. The idea of this thread is to start a collection of nice formatting examples which will ease the task of mastering this function. Please post useful examples, and document them :)
(Read More) Printing a convenient ansi escape sequence clears the terminal (if you're running python from a terminal). Similar techniques could be used to print in colors.
(Read More) This snippet generates the best rational approximations of a floating point number, using the method of continued fractions (tested with python 2.6 and 3.1).
(Read More) Converting a decimal integer (denary, base 10) to a binary string (base 2) is amazingly simple. An exception is raised for negative numbers and zero is a special case. To test the result, the binary string is converted back to the decimal value, easily done with the int(bStr, 2) function.
(Read More) A simple function to return the number of occurances of a letter in a text. An easier way to implement this would be by using a dictionary :)
P.S sorry if there already is a code snipped like that somewhere.
(Read More) Simple functions for binary-to-decimal and decimal-to-binary conversion :)
(Read More) This snippet shows a fast and efficient way to cut strings in substrings with a fixed length (note that the last substring may be smaller).
(Read More) This Python code allows you to get selected statistics of a story text. It will count lines, sentences, words, list words and characters by frequency, and give the average word length. It should be easy to add more statistics using the dictionaries created.
(Read More) This is a simple game of pong with made using pygame. You will need to make a picture of the pong for it to run properly. Simply make a cirlce in paint, 25x25.
Joe
(Read More) This snippet defines a function mousepos() which gets the mouse position on the screen. The package python-xlib is required.
(Read More) this is my version of the popular snake game. The snake is controlled by the mouse so be careful when you start :) it grows infinitely long...you should download the apple.jpg file for the game, or graw one of your own just name it apple.jpg and place it in the same directory as the code itself :)...
(Read More) Let Python do the work for you and figure out the roman numerals for a given integer. I have to ask you to keep the integer values somewhat reasonably low (from 1 to 4999), since roman numerals from 5000 on use characters with an overline and most PCs don't have those in the character set.
(Read More) Tired of the Tk icon in the corner of the form. It's easy to replace with any fancy icon you have. Here is the code ...
(Read More) Hi,
The code below will check if a particular exe is running in windows, return the Process ID and force kill the process. If anybody has an easier way to do it please feel free to comment :)
(Read More) An example of a range generator that handles floating point numbers, appropriately called frange(). The function range() or the generator xrange() can only be used for integers. Note that xrange() now is range() in Python3.
(Read More) I've created a few functions to be used with pygame which are based on anti-aliased curves. At the moment and likely forever, it has two useful functions:
aacircle(s,x,y,r,colour):
Draws an anti-aliased circle on the surface s at the starting point x and y with the radius r. The circle will...
(Read More) If you own a stopwatch, here is your chance to benchmark the various versions of Python on recursion performance. The Ackermann function gives the old computer quite a workout.
(Read More) This code shows an example of using recursion to simply solve a problem. Note though, it can take a long time to do larger numbers such as the 50th fibonacci numbers this way.
Hope this helps! :)
(Read More) When several producer threads put items in a queue.Queue, and a consumer thread gets items from the queue, there is no way for the consumer thread to tell the producers that they should stop feeding the queue. This snippet defines a subclass of Queue with a close method. After the queue has been...
(Read More) This snippet defines a function a2bits which converts a characters string to a string of 0's and 1's showing its bitwise representation.
(Read More) A while ago I created a code snippet in C for the same question. Solving this question with Python is a lot simpler, and on top of that Python takes care of impossible dates with the appropriate error message.
(Read More) This is a test to get the Python snippets going! For those of you who are scared of snakes, the language is named after the TV program, not the snake. Python is an interpreted language, but programs to compile/combine the code to an exe file are available (Py2Exe). The latest version of Python...
(Read More) See I've made a short little login that works with a dictionary, but I don't know how to add something to the dictionary while the program is running, so if you know how to fix this can you please help. Much apperciated.
(Read More) The user enters the full location of a file and the program determines whether the file is a sound file or an application and opens it after a user specified time.
Example:
Location: C:\Where ever\file.mp3
OR
C:\Another Where Ever\prog.exe
TIme(in mins): 10
(Read More) A program which accepts a sentence from a user, and uses a 'for loop' and determines the longest word in the sentence entered.
(Read More) This code creates a sound file in Sun's simple AU audio format of a sine wave of given frequency, duration and volume.
(Read More)