2,322 Reusable Code Snippet Topics
Remove Filter ![]() | |
Squaring a number is a rather common task in programming, but if you want to square an integer. Easy you think! Int MySquare = AnInt *.AnInt; This will happily overflow without giving any exception if AnInt is bigger than 46340, unless you put your code in a checked block. Now … | |
Similar to echoserver, but this one handles multiple clients by forking the process. | |
This is a simple echo server that I've written while learning sockets programming. Compile the server and run it. You can connect to it using telnet like this: telnet localhost 1337 Disconnect the client by typing "/quit" without quotes. | |
This is a program I wrote for my x86 assembly class which generates matrices, multiplies them, and computes how long the arithmetic took. It uses Irvine32.inc which came with the textbook. | |
This is a function that generates a string of random characters to the length that you specify. | |
This snippet prints a python list of strings in multicolumn format (similarly to linux `ls` command). It uses module prettytable, available in [pypi](https://pypi.python.org/pypi/PrettyTable). | |
This snippet is reserved for users of the KDE linux desktop. It adds a *service menu* to kde applications such as dolphin and konqueror, to launch an ipython dashboard in a directory in one click. More specifically, save the text of this snippet as a file `~/.kde/share/kde4/services/ServiceMenus/opennotebook.desktop` (create the directory … | |
A modal dialog is a window that you must respond to. The MessageBox for example is such a window. It is a handy tool for displaying a warning or other kind of message to the user. But, you can configure it only in a limited way. Xcelled194 did a good … | |
you need to give the input text files in the command line... | |
THIS IS DANGEROOUS CODE, if it fails, it may prevent yu frome editing it. also, you can use the functions to do crazy stuff. Look at [this refrence](http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx) and change the `0x12` to what you want. (This one hold alt, so typing r will actibvate the menu that starts with … | |
Shows you how to create multiple Tkinter buttons in a loop, each with its own name, label and command response. | |
The code sample shows how to get an image from the internet and show it in a Tkinter label widget. | |
The test.txt file I'm using is: 1,2,3,Name1,Pizza,Pizza Hut,7,8,9 1,2,3,Name2,goose,my home,7,8,9 1,2,3,Name3,chicken,airplane,7,8,9 Most of the explanation can be found in the snippet. Some CSV(Comma Separated Value) files often contain as first line, a header. You could skip this, or threat it a bit differently. I had no header, so I leave … | |
Just a simple image file slide show using PySide (PyQT). | |
Here is a simple implementation of implementing a simple Left trim (ltrim) and Right trim(rtim) of unwanted characters from a C style string. (null terminated strings). Doesn't suffer the overheads of the memmove implementation in which the worst case scenario (a string containing all junk characters) is approx. N^2 / … | |
Here little debugged version of my calculator posted earlier in GUI calculator thread. I prepared it after listening that their teacher did more limited calculator in around 150 lines of code with their graphics module. This is 115 lines without empty lines and comment lines [CODE]>>> li=[i for i in … | |
This example calculates timed x, y points of a projectile motion that can be used for plotting or other calculations. | |
This code snippet can be used to get the current time in 12 hour format using JS | |
A Python sort with a helper function to allow for mixed order sort like age descending and weight ascending. | |
This simulator works for: 1. First Come First Serve (FCFS) 2. Round Robin (RROB) 3. Priority (PRIO) 4. Longest Job First (LJF) 5. Shortest Job First (SJF) algorithms. The program is written for gcc linux compiler. In input file a sequence of processes coming to the systems included, where also … | |
This has by far no practical use. But as I was used to VS, handling most of the code housekeeping "behind the back", I wanted to try to make a Forms program as minimalistic as possible. Start an Empty project, include a reference to System.Windows.Forms and fill in the code. … | |
Here's a simple class for what it's worth that will do simple base85 encoding and decoding. This only works with the required blocks of data and not large data structures or streams, and does no validation of the data. What it does do is encode a block of 4 8-bit … | |
The server only does the basics. TODOs: The header method needs expanding. The file send method can't handle too large files. Maybe write custom buffer class since some buffering is done. Keep cache of recent files. | |
Just another loan calculation, this time using a handy function in Python module numpy. | |
Starting with Python 3.3 you now have access to a high resolution time counter (nanoseconds and better). | |
You can teach your linux system that executable python files must be executed with /usr/bin/python. This allows you to write python scripts without the shebang line [icode]#!/usr/bin/env python[/icode]. This snippet shows you how to do it ! | |
Python module turtle makes it simple to draw graphics art similar to art done by the Logo language. | |
Using Python module turtle to draw a Koch snow fractal. | |
I was being really lazy and didn't want to write one up myself, couldn't find one, and ended writing one up myself anyways. Hope this helps other lazy people. | |
The most efficient static sieve generator in python I have seen so far. Returns the list of primes, that are not greater than n. | |
Yes, I know Fibonacci(=sun of a good man) again. Most famous for his series, but who among you all, know that he was the man who introduced to the Western world, the Arabic numeral system(including zero) in 1202 A.D.? The Italian merchands of those days adored it. It was far … | |
The luxuries we now have in the amount of pixels on a screen to draw some amazing graphs with, were lacking in the early days of computing. Most of the time you had to resort to rude printed output. It wasn’t that bad always. Sometimes it was and still is … | |
I wanted to draw a 5 pointed star in C# and here is how I finally did it. I leave it as an exercise (some hints are given in the code) to work out how I did this. It was fun to do (but a bit hard, my trig is … | |
A simple way to find duplicate words in a text. In this case the text is preprocessed to eliminate punctuation marks and set all words to lower case. | |
**NOTE** This code won't work at the application level. It directly addresses memory at 0xB8000 which will result in a segfault at the application level. This code must be part of an OS or an application in a not-so-secure operating system like DOS, where applications can directly address any part … | |
Here is simple Digital Clock coded in C++ and compiler used is TurboC. A class DIGIT is designed to draw digits. Follwoing are the details for this class: Consructor: DIDGT(); for default settings DIGITE(int s,int c,int st); for user define settings s -> is size of the digit c -> … | |
Lets say you have lots of data your listview. Now you want to Group This data According to a Perticular Subitems. For Example: Suppose i have some books data in my ListView. this listview items contains Author name and Books Title. And there are 2000 Books in list view. Now … | |
Hi all, First tutorial submission. The standardised strtotime() function really grinds my gears. I hate messing about with dates in different formats... so I mullered this little function up. It works perfectly - probably easier ways to do it, I don't know... I just include this function in my functions … ![]() | |
The snippet provided will cover the basics of an on screen keyboard; the only things it doesn't have are numbers, symbols, and extra keys. This will give you the fundementals to build off of. It's written using Microsoft's XNA Framework, however it should be fairly simple to port over to … | |
I have add the two functions the add and subtract I have to now add the multiply and divsion functions can someone help | |
In Las Vegas random numbers move billions of Dollars in and out of gamblers' pockets. For the mildly inquisitive here is a test of three simple generators. | |
Something that we normally have to do at one point is right click on a richtextbox etc to copy/paste. Now, we all know there are keyboard shortcuts but for the end user it is better to have this feature. Here is the basic code to allow you to add this … | |
Ever feel the default session handling is insecure? Or do you just want some originality to show off? Well, here you are. A custom, MySQL-based session handler. It supports base64_encoding and MySQL storage. Note: I haven't tested this thing in months so I don't know how well it'll work : … | |
An example how to plot the function y = sin(x) using the WinApi function SetPixel(). The plot is centered along a line at pixel y = 200. Add an x-axis and a couple of tickmarks and it could look impressive. | |
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). | |
For some reasone everytime I enter either correct mine or someone else's valid email and password, I get message: Sorry, your credentials are not valid, Please try again. What is wrong with this form? | |
Just a little fun with Python's named tuple, behaves like a class but is much leaner. | |
Applying the famous Gauss algorithm to determine the date of Easter for a given year. | |
Factorials get large very rapidly. The factorial of 13 is !13 = 1*2*3*4*5*6*7*8*9*10*11*12*13 = 6227020800. This number already exceeds the unsigned long integer, and gets into the real money as the politicians say! This program uses an array of characters to store the factorial as a numeric string. Go ahead, … | |
Here's I wrote some code to insert\retrieve images from SQL Server database 1- Create table to hold Image ID, Name and itself [CODE=SQL] USE [ImagesDatabase] GO /****** Object: Table [dbo].[Image] Script Date: 07/10/2009 23:46:46 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Image]( … |
The End.