12 Reusable Code Snippet Topics

Remove Filter
Member Avatar for Ene Uran

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 …

Member Avatar for Lucaci Andrew
0
4K
Member Avatar for Ene Uran

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.

Member Avatar for james.lu.75491856
1
998
Member Avatar for Ene Uran

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.

Member Avatar for s4bb4t
1
8K
Member Avatar for Ene Uran

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 …

Member Avatar for Flintstone_
2
2K
Member Avatar for Ene Uran

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 …

Member Avatar for AndyGman
1
3K
Member Avatar for Ene Uran

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 …

Member Avatar for vegaseat
0
2K
Member Avatar for Ene Uran

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.

Member Avatar for ndhdo
0
288
Member Avatar for Ene Uran

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 …

0
486
Member Avatar for Ene Uran

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.

0
269
Member Avatar for Ene Uran

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).

Member Avatar for Ene Uran
1
327
Member Avatar for Ene Uran

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 …

0
252
Member Avatar for Ene Uran

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 …

1
149

The End.