12 Reusable Code Snippet Topics
Remove Filter Most commonly a dictionary of key:value pairs is searched by the unique key. Sometimes it becomes necessary to search for the key, or keys given a value. Special consideration has to be given to this case, because the value does not have to be unique and may return several keys … | |
More or less a templet database that allows you to load, save, add, list, search, edit and delete data items. I used movies as an example, but you can easily change that to make a database of many other things. | |
Just a simple console blackjack game. The bread and butter code was written with lots of comments, so you can improve on it. Really, the most important part is figuring out when an ace is 11 in value and when it is a 1 in value, so you don't bust. | |
You can download the matplotlib free from: [url]http://matplotlib.sourceforge.net/[/url] 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 … | |
This simple Python code uses the Tkinter GUI toolkit and the Python Image Library (PIL) to create a program that allows you to grab an image of the display screen (or portion of it) and save it to an image file. Unfortunately, the ImageGrab.grab() method only works on a Windows … | |
The small images you put on buttons and such are nice, but a real pain if you have to attach all those image files to your program. Python's base64 encoding module makes it easy to convert the binary image code to a string that you can simply make part of … | |
Nothing fancy. just shows a simple monthly calendar in a Tkinter label. The trick is to use a fixed font so spaces take up the same area as numbers. | |
Let's say you had a list of full names, where the name would consist of a title, followed by first, middle and last name, or maybe just title and last name. You can sort this list by last name only, by creating a temporary list of sublists of type [last … | |
The snippet shows the use of a list of class instances to form a sortable and searchable database for solvents, or for that matter other chemicals. The list can be expanded and all the needed data added at the same time. | |
Just a short little C program to send beeps to the PC internal speaker. Beep(frequency_hrz, duration_ms) is a WinAPI function as is Sleep(ms). | |
A list of tuples is easy to sort for items in the tuples. This snippet shows you how to sort a (character, frequency) tuple either by character or by frequency. One useful example would be the sorting and display of a list of (player, score) tuples, you might find in … | |
Powers of 2 are closly linked to the position values of binary numbers. The most right position is 2 to the power 0 = 1 then goes to the left with 2 to the power 1 = 2, 2 to the power 2 = 4 and such. If you want … |
The End.