Python MySQLdb equivalent for PHP’s mysql_insert_id()

What you’re looking for is lastrowid property of a cursor object.

Code:

import MySQLdb # MySQLdb module must be installed on the system
connection = MySQLdb.connect(...) # Details skipped
cursor = connection.cursor()
query = "INSERT INTO ... " # put query here
cursor.execute(query)
print cursor.lastrowid # BINGO! This will print the id (auto-increment column) of the last inserted row
# Other codes here

Thanks to this article on the Internet

C style For loop in Python

A for loop in C/C++/PHP/Java Or many well-known programming language will appear as:

int loopStartVariable = 0;
int loopEndVariable = 5;
for(i=loopStartVariable; i < loopEndVariable; i++){
	// do something with i
}

Python has no C-style for loop, which is has is for in loop like foreach loop available in PHP.

To achive the same goal as the code snippet written in C above, we may write following code in python:

loopStartVariable = 0;
loopEndVariable = 5;
for i in range(loopStartVariable, loopEndVariable):
	# Do something with i
print "loop ends here"

Python Tutorial: Dictionaries (Key-value pair Maps) Basics

Dictionary in Python is a dat type also known as map. You may be already familiar with them, if using Associative Arrays in PHP or Hash-tables in C++ or Java. You may imagine a dictionary as an array, where instead of numerical indexes (the first element of array is indexed as 0, the second element indexed by 1 and so on) you can use string indexes. Each element of a map is accessed (or, indexed) by an unique key, and so they are known as key-value pairs. Dictionaries are not sequences, hence maintains no left-to-right order.

Dictionaries are not sequences, hence maintains no left-to-right order.

Operations

You can create a map by the declaration: var_name = {key:value}

Code after “>>>” in following examples means: you need to type the code in Python command line. Any line not beginning with “>>>” indicates an output.

Code:
>>> D = {'food': 'Spam', 'quantity': 4, 'color': 'pink'}

>>> D['food'] # Fetch value of key 'food'
'Spam'

>>> D['quantity'] += 1 # Add 1 to 'quantity' value

>>> D
{'food': 'Spam', 'color': 'pink', 'quantity': 5}

Alternately, you can declare an empty map, and insert key-value pairs later. Unlike Lists, you can perform out-of-bound assignments:

Code:
>>> D = {}

>>> D['name'] = 'Bob' # Create keys by assignment

>>> D['job'] = 'dev'

>>> D['age'] = 40

>>> D
{'age': 40, 'job': 'dev', 'name': 'Bob'}

>>> print D['name']
Bob

Nesting

Like lists, nesting is also possible in maps. Consider the following code pattern:

Code:
>>> rec = {'name': {'first': 'Bob', 'last': 'Smith'},
                  'job': ['dev', 'mgr'],
                  'age': 40.5}

>>> rec['name'] # 'Name' is a nested dictionary
{'last': 'Smith', 'first': 'Bob'}

>>> rec['name']['last'] # Index the nested dictionary
'Smith'

>>> rec['job'] # 'Job' is a nested list
['dev', 'mgr']

>>> rec['job'][-1] # Index the nested list
'mgr'

>>> rec['job'].append('janitor') # Expand Bob's job description in-place

>>> rec
{'age': 40.5, 'job': ['dev', 'mgr', 'janitor'], 'name': {'last': 'Smith', 'first':
'Bob'}}

Free Java Game Code: HangMan Code by Java!

I’ve coded a simple Game by Java – A HangMan Game! To make it interesting, The game is called HangCoder which means, it is played by a programmer! Don’t worry, it only means you will be given words related to programming to guess.

The source code is open and allowed to modify. It is helpful as a quick demonstration of java Swing and AWT components like textboxex, buttons, images and other stuffs. Study the code, modify as you need and make your own Java HangMan!

Download Source From here.

Images: Can be obtained from here (no need to download if you get the zip file above)

Difference between ./ and / (relative path and Absolute path)

In web or desktop programming/command-prompt use, we often come through such situations where we have to use relative/absolute paths. Is there are any differences between the notation “./” and “/” ?

./

Yes, there are! When you use “./” we mean an relative path. Suppose, you are in the location http://www.somedomain.com/some_directory/index.html

Now, if you want to insert an Image file which is located at this directory “some_directory”, you can write following code in your index.html page:

<img src = "./myimage.jpg">

If you want to use an image from a directory “another_dir” on level up, you should use “../”

<img src = "../another_dir/another_image.jpg">

Both some_dir and another_dir are located under the home of your domain (www.somedomain.com).

/

Now, let’s talk about using “/”

“/” means beginning from the Root level of your file system. Suppose, in Linux, you want to Change your current working directory to the directory “etc”. Since “etc” is placed just in the Root directory of your file system (see the image below), you should type something like this in your command-prompt:

cd /etc

If you tried with the command “cd ./etc”, it wouldn’t work! Why? Because “./” means beginning from the Current Working directory. Normally, when you use a command-prompt it selects the user’s Home folder as current working directory. So if you want to go to a directory based on root level, you should use “/” instead of “./”

Image: Unix/Linux File System. See, the folder “etc” stays just below the “root” or “/”

Python Basics: Indent

Previous Article: Beginning Python Prgramming! How to Download, install, compile & run program in Python

In this post I’ll point out some details about the Python language. Important concepts to be clarified.

Python maintains indention strictly:

In python, you should maintain indention strictly! This is necessary to tell python which portion of code is under a block. For example, consider the loop segment below:

 

As you can see, there is no Curly-braces to identify where a loop starts and where ends! Thus, you must maintain indention strictly.

 

 

This entry will be updated eventually!

Automatic Mouse click, Key Press Script in Java

By Java, you can perform automatic mouse clicks, keyboard press etc. 🙂

I’ve created a short application by which you can make automatic mouse click in a certain co-ordinate! 😉

Download Source/Executable

Download from GoogleCode (source/executable)

Please note:  This is not any professional application, if you want to use professional mouse-recording or macro-recorder for presentation or other tasks, use other software.

If you want only to run the executable file, run the .jar file. You must have JRE installed in your computer 😉

How it works?

Java has a class Robot which dedicated for performing these activities. I simply used the class. More documentation here, and analysis the source code uploaded.