2,322 Reusable Code Snippet Topics

Remove Filter
Member Avatar for
Member Avatar for vegaseat

If you want a window with a menubar, toolbar, statusbar, dock and central widgets, you can use the PySide GUI toolkit's QMainWindow. To use the more traditional box and grid layouts, you simply add a QWidget instance as the central widget. Here is an example.

2
3K
Member Avatar for TrustyTony

Here is code, which you can play with before installing and using the [superior blist package from pypi](http://pypi.python.org/pypi/blist/). I did it as therapy after doing programming test answer using zillion of setters and getters. Motivation is basically to show why you should use properties after you need them and attribute …

0
314
Member Avatar for vegaseat

A colorful look at the PySide GUI toolkit and its QPainter class and methods. Well commented and demonstrated with the drawing of a number of rectangles and the different ways to apply colors.

1
1K
Member Avatar for imti321

'the code is working properly only problem with this code is when the first line ends in excel and data is inserted in next Row "instead of inserting another row it copies the same content which is inseted last time in form "

Member Avatar for tinstaafl
0
382
Member Avatar for nanodano

[B]Introduction[/B] Hello everyone. This little code snippet shows you how to read in scan codes from the keyboard. It is slightly different than reading in a regular character. When you use the [INLINECODE]getch()[/INLINECODE] function, it is normally returning an ASCII value. Some keyboards have extra keys though. These include the …

Member Avatar for thendrluca
2
6K
Member Avatar for tinstaafl

I'm working on a project that contains 3 different lists. 2 lists of 2 different controls, and 1 list of a custom class. I found that I needed each of these lists sorted. The problem with sorting a list of complex types is there's no default comparer for them. That …

Member Avatar for Ketsuekiame
0
301
Member Avatar for vegaseat
Member Avatar for dashing.adamhughes
2
7K
Member Avatar for vegaseat
3
345
Member Avatar for vegaseat

This snippet shows you how to create a list of class instances. The class itself cannot be pickled, but the list of instances can. To use the pickled data file in another program, you have to recode the class in that program.

3
2K
Member Avatar for vibhaJ

I was struggling long for proper image resize function for maintaining image ratio. Finally i end end up with merging and changing all codes and it works ! function resizeImage($sourceFile,$destFile,$width,$height) sourceFile => Full path along with filename destinationFile => Full path along with filename I hope this will help coders..

Member Avatar for diafol
1
338
Member Avatar for vegaseat

Jython is a version of Python that can use the extensive Java library. Jython uses Python syntax and avoids the rather ugly syntax (at least for Pythonions) of Java. Here we apply it to bring up an image from a file.

4
722
Member Avatar for nitin1

#include<iostream> #include<algorithm> using namespace std; #define MAX 10000000 #include<stdlib.h> #include<string.h> char s[MAX]; int p[MAX]; int bucket[MAX],nbucket[MAX]; struct suffix { int idx; }pos[MAX]; int H=0; bool cmp(struct suffix i,struct suffix j) { if(H==0) { return s[i.idx]<s[j.idx]; } else { if(bucket[i.idx]==bucket[j.idx]) { return bucket[i.idx+H]<bucket[j.idx+H]; } else { return bucket[i.idx]<bucket[j.idx]; } } } …

Member Avatar for deceptikon
0
618
Member Avatar for Gribouillis

This small python script invokes the [Treeline](http://treeline.bellz.org/index.html) tree visualizer to display xml files in a pleasant way. ![viewxml](/attachments/large/1/viewxml.png "viewxml")

0
2K
Member Avatar for sid78669

I was trying to set a UniqueIdentifier based on Long. The objective was to give a certain custom Object a unique identifier in order to allow me to store them in a hashmap. I saw someone trying to use the most significant bits. So I realized that the addition of …

Member Avatar for ~s.o.s~
1
469
Member Avatar for Gribouillis

This snippet is easy: it defines an immutable wrapper around a sequence type (list, deque, etc) which allows a class, function or module to expose a read only version of a sequence to the outer world. A constant wrapper around mapping types could also be defined in a similar manner.

Member Avatar for Gribouillis
2
578
Member Avatar for Killer_Typo

I got bored and started some scripting the other day in PHP, this is just a sample that i wrote, just uncomment the part of script that you would like to see. IE remove the /* and */ around a certain area. i know its not great, but some beginners …

Member Avatar for LastMitch
0
149
Member Avatar for vegaseat

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.

Member Avatar for Ene Uran
3
804
Member Avatar for jrcagle

Generators are essentially dynamic sequences, making their official debut in Python 2.5. That got me to thinking: can we make a circular list with them? Why yes, we can...

Member Avatar for vegaseat
2
191
Member Avatar for vegaseat

You can alternately show one of two lines drawn on the Tkinter canvas by simply moving them on and off the canvas screen. This beats a cumbersome create and delete cycle.

3
535
Member Avatar for tinstaafl

In a recent discussion these code snippets were developed to replace Convert.ToInt32 and Convert.ToDouble. The person I was helping had a very large number of conversions to do. In our tests, using these 2 routines we were able to do over 1 million conversions in less than half the time. …

1
147
Member Avatar for john.knapp

# Have you ever tried using the String.Compare method for a case-insensitive string comparison? # The [documentation](http://msdn.microsoft.com/en-us/library/zkcaxw5y(v=vs.100).aspx) on MSDN for the overloaded method: 'Declaration Public Shared Function Compare ( _ strA As String, _ strB As String, _ ignoreCase As Boolean _ ) As Integer states `Compares two specified String …

Member Avatar for jeffreyk16
1
419
Member Avatar for vegaseat

A closer look at the Tkinter GUI toolkit Toplevel Window and how to lift and lower it respective to other windows.

0
6K
Member Avatar for vegaseat

Comparing a number of different approaches to finding the closest pair of numeric elements in a list. The timing is done with the mpmath module, but you can also use Python module timeit.

Member Avatar for Lardmeister
5
757
Member Avatar for vegaseat

Python is entirely object oriented and using classes is made relatively simple. Beginners have a certain angst when it comes to using classes. There is a hump in the learning curve, which I like to overcome with this example. Inheritance really makes sense and saves you a lot of extra …

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

An approach to create multiple GUI buttons (or other widgets) using list comprehension. In this case I used the Tkinter GUI toolkit that comes with the Python installation.

Member Avatar for Ene Uran
2
484
Member Avatar for pritaeas

Below is a documented code example showing how to use the SQLite3 class to connect and query your database (and check for errors). IMHO using PDO is a better alternative, as this extension uses mixed technology in a non-standard way (objects, arrays and exceptions). If there are any questions let …

2
4K
Member Avatar for pritaeas

Below is a documented code example showing how to use the SQLite functions to connect and query your database (and check for errors). Please note that using the SQLite extension is NOT RECOMMENDED. This extension is no longer available in PHP 5.4.0. If you want your scripts to be future …

2
589
Member Avatar for pritaeas

This demo will show a PHP script using a Smarty template. It assumes basic installation. In the folder where you copy the script, there should be a smarty folder containing the Smarty files. You should create a writable `cache` and `templates_c` folder, and a folder called `templates` where the `.tpl` …

Member Avatar for pritaeas
2
532
Member Avatar for NicAx64

Circuit, [Circuit Diagram](https://picasaweb.google.com/116591322380497799116/December52012#) Video: [Video](http://www.youtube.com/watch?v=qGoM_ydAtBc)

0
489
Member Avatar for jstfsklh211

A simple way for using templates in php new pages can run server side code or pretty much do anything you would normally want to do on a web page First create your template. cleary there is way more that can be done with this, I've kept it simple for …

Member Avatar for jstfsklh211
0
433
Member Avatar for G_Waddell

Hi All, In my opinion, this is the best way to read in text from a csv file. Take for example if you are importing addresses: "1 Main Street","SmallTown","SmallCounty" The above is fine and easy to parse in BUT what if an address like this was entered: "App 1A, Tall …

Member Avatar for Nutster
1
5K
Member Avatar for ddanbe

LINQ is a great extension to the C# language! But it can be terrifying at first. So here is a little snippet just to get your feet wet. You would normally use some sort of looping here. See how easy it is to get some values from an array. Enjoy

Member Avatar for claventure
0
182
Member Avatar for vegaseat

Just a small example showing how to draw circles with PySide (public PyQT). I used LightShot to capture the display, which gives you a link to the picture. http://prntscr.com/kw6b6

2
10K
Member Avatar for Cudmore

Often we're faced with the need to represent primitive data types as readable/writable bytes that can be sent through a data stream. In Java, there are classes such as DataOutputStream/DataInputStream that can do the work for us, but often a programmer wants to manage the conversions himself (or herself, to …

Member Avatar for subramanya.vl
1
5K
Member Avatar for zeroliken

I bet all Computer Science related courses would learn low level or machine level languanges in due time and in our university we used the portable 80x86 assembler NASM for our Assembly code So here's my experimentation on how to use "functions" and recursion using a Fibonacci Solver as a …

0
1K
Member Avatar for vegaseat

Let's say you have a whole bunch of nicely named lists and want to save them all and their names in a pickle file. One solution can be to store these lists and their names in a Bag container class and pickle the bag instance object. Now you can use …

Member Avatar for Lardmeister
4
253
Member Avatar for ben1996123

Input the minutes, then the seconds, and it will do the rest. Pretty simple but I just thought I'd post it. Made as a quick test for my game.

Member Avatar for PalashBansal96
0
300
Member Avatar for skiboy209

I thought i would share this with the community wrote most of it in school but the ai i wrote at home enjoy!

Member Avatar for PalashBansal96
2
256
Member Avatar for jstfsklh211

to create a new comboBox when you post your form checkbox_name will contain the selected value $(document).ready(function() { var cb = "checkbox_name"; comboBox(document.getElementById(cb+"CB"), cb); var sel = myForm.elements[cb+"_hold"]; sel.length = 0; //add options sel.options[sel.options.length] = new Option("text", "value"); }); <div id="checkbox_name"></div>

0
369
Member Avatar for vegaseat

A real useful piece of code, hence I put it under Z. It does nothing but open and close the door of the CD-ROM player, and pushes the tray in and out. Should your coffee be too hot, you can put it on the moving tray and cool it off! …

Member Avatar for RonalBertogi
1
653
Member Avatar for Gribouillis

This python 2.7 snippet adds a thin layer of sugar on the itertools module's api, allowing many of its functions to be used as decorators and adding some new functions. Enjoy !

Member Avatar for Gribouillis
3
331
Member Avatar for vegaseat

If you have a smaller image, you can include it in your program as a base64 encoded string. This way you don't have to worry about an image file to go with your code. In your program you can then decode it back to the familiar bytes of an image …

Member Avatar for HiHe
5
8K
Member Avatar for Echo89

Well, I have just made this funtion that will split an array using a delimiter. I'm new to C++ so I will take any suggestions you have. Anyhow, good luck! Enjoy!

Member Avatar for Echo89
0
991
Member Avatar for aramil daern

These are codes that do absolutley nothing, except to show whart you can do with to much time on your hands. Oh the silliness

Member Avatar for Echo89
1
485
Member Avatar for Reverend Jim

The code in this snippet (and attached project zip) demonstrates how to use a background worker thread to monitor a network address (name or IP). It shows how to start and stop the thread and how to update controls in the main thread using delegates. It should be simple to …

Member Avatar for Reverend Jim
1
2K
Member Avatar for xixi.li.7

I have a project to calculate simple moving average of a data, and the data contains over 4000 values. And requires me to skip the first 14 days and calculate the simple moving average on rest of data. I never experience use moving average and ask me to use LINQ …

0
227
Member Avatar for BevoX

This is my solution for generating prime numbers. With this code hopefully you can generate prime numbers with incredible speed. The generated numbers will be stored in a text file titled as "Primes.txt". I have a dual core machine, but this program does not support dual core architecture, so it …

Member Avatar for Microno
1
678
Member Avatar for TrustyTony

Now that MITx first test is over, I got feeling that this one task requested for generate and test solution is begging also for less efficient recursive solution as it is so simple. Here it is! Single liner aproved by the released grader! If you wander if you are able …

0
314
Member Avatar for ddanbe

For anyone who likes to do something with sets in C#, this can be a starting point. The full project (VS 2010) is also included. Have fun with programming!

0
307
Member Avatar for vegaseat

Python experiment to get an image from a web page and save it to an image file. Updated code to work with both Python versions ...

Member Avatar for snippsat
5
2K

The End.