Posts Tagged With: Code

Object Or Array Check in JavaScirpt

Tagged with: , , , ,

Test whether an object is an object or an array


if(Object.prototype.toString.apply(arr) === '[object Array]') alert("Array")

Original Article

1 Comment »

Start a new Django Project

Tagged with: , , , ,

Command to start a new Django project…


django-admin.py startproject [PROJECT_NAME]
cd [PROJECT_NAME]
python manage.py startapp [PROJECT_NAME]
No Comments »

Random Password in PHP

Tagged with: , , , , ,

A simple way to create a random password in PHP


$password = substr(md5(time()), 0, rand(4,8));
4 Comments »

Finding the Factorial using Recursion

Tagged with: , , , ,

Finding the Factorial using Recursion in javascript.


function factorial(x) {
	if(x <= 1) return 1;
	return x*factorial(x-1);
}
factorial(5);
No Comments »

Remove www From URL using .htaccess in Apache

Tagged with: , , , , ,

This htaccess code will remove the ‘www’ part from the url. It uses the mod_rewrite module of apache to re-write the url – so make sure that’s installed.


RewriteEngine On 
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
5 Comments »

Find if a Variable is Defined in Ruby

Tagged with: , , , , , , ,

This will let you find whether a variable is defined in ruby.


if(defined? var_name) then
    print var_name + " is set"
end
No Comments »

Using Ant to Concatenate files

Tagged with: , , , , , ,

The build.xml file to concatenate files using ant. This can be executed using the command ‘ant “ConcatAll”‘


<?xml version="1.0" encoding="UTF-8"?>
<project name = "MyProj" basedir = "." >

	<target name = "ConcatAll" >
		<concat destfile = "final.txt" fixlastline = "yes" >
			<filelist dir = "."
						files = "first_file.txt
								second_file.txt
								third_file.txt"/>
		</concat>
	</target>

</project>
1 Comment »

Creating a Toolbar in GTK Python(PyGTK)

Tagged with: , , , , ,

Creating a toolbar using GTK Python. There is an old method – but this example shows a better method of creating a toolbar using pygtk.


toolbar = gtk.Toolbar()
toolbar.set_style(gtk.TOOLBAR_BOTH)

toolbar_item = gtk.ToolButton("Quit")
toolbar_item.set_stock_id(gtk.STOCK_QUIT)
toolbar_item.connect("clicked", exit)
toolbar_item.show()
toolbar.insert(toolbar_item, -1)

toolbar.show()
No Comments »

Python Function for Space Units

Tagged with: , , , ,

A Python function to return readable size using the given size(KB)


# Returns a more readable format of the given data. For example, getReadableSize(1024) returns "1 MB"
	def getReadableSize(size):
		unit = "KB"
		size = float(size)
		final = size

		if(size >= 1024):
			unit = "MB"
			final= "%.02f" % (size / 1024)
			size = (size / 1024)

			if(size >= 1024):
				unit = "GB"
				final= "%.02f" % (size / 1024)
				size = (size / 1024)

		return str(final) + " " + unit

[tags][/tags]

No Comments »

Get Result of Command using Python in Linux

Tagged with: , , , , , ,

Execute a command and get its results using python in Linux


import commands

result = commands.getoutput("ls")
1 Comment »