Wednesday, April 24, 2013

Program Online Without any Tools on Your Computer

If you've never programmed before and you want to get an idea of how the process works I suggest you go try the tools at : http://ideone.com/.

First you select which language you're going to program in from the list box that's on the left. Select Python.

Move your cursor to the right side and enter some code (programming instructions)  and hit the submit key.

The ideone website will execute your code and send you back the results. !!

I suggest the following little program:

for i in range(1,11):
    print i

The above code snippet will display the numbers 1 to 10 each on a line l

A screen shot of the output of ideone



Comming Post - How to Write a Directory Walker in Python

Python has the ability to walk directories as a built in library function somewhere. But I wanted to write my own implementation just for fun and to improve my understanding of recursion, so here's what I managed to whip up:


'''
Created     15th April 2013
@author: Yasser Al - Jammal
Directory traversal test
'''
import os

''
#--------------------------------------------------------------
#prefixes must end with a \\
#returns a list of subfolders each appended to its parent folder  and with a \ in the end
def BuildSubFoldersList(path):
    folders = []
    #build subfolders list
    for f in os.listdir(path):
        if(os.path.isdir(path + f) == True):
            #print str(f) + "\\"
            folders.append(path + f )
    return folders
          
#----------------------------------------------------------------
#folder must end wit h \\       
def ListFilesInFolder(path):
    n = 0
    #Add slash if missing
    if path[-1] <> "\\":
        path = path + "\\"
       
    for f in os.listdir(path):
        if os.path.isfile(path + f):
            n = n + 1
            print path + f
    print str(n) + " Files in folder " + path        
#################################################################
def main():
    DirWalk("d:\\")
################################################################# 
      
#Here is where the magic happens   
def DirWalk(path):
    if path[-1] <> "\\":
        path = path + "\\"
   
    ListFilesInFolder(path)
    subfs = BuildSubFoldersList(path)
   
    if len(subfs)>0:
        while(len(subfs) > 0):
            DirWalk(subfs[0])
            subfs.pop(0)
    else:
        return

if __name__ == "__main__":
    main()  

Why I LOVE Python

Python is such an amzaing language. I'm sure it will have a huge impact on how programming is done in the future. The brevity of expression is astounding. Python is "a higher level" language.

  • Lists can be indexed backwards. E.g. list1[-1] points to the lists last item, list1[-2] , points to the second last item and so on.
  • Dictionaries are built in the language itself.
  • Threading is EASY
And a million other reasons that make Python such an amazing programming language. Everything is straightforward, everything works straight out the box. Everything is clean and tidy and easy to use. 

Some macho type dude will come up and tell me no , Python is a toy language, real programmers program in C. Python is the future I say, C has done its job and done it very well in my opinion. If you want to get any real work done, you'll have to do it in Python..

I think Python has been limited in its growth due to the following reasons: There is no way to guarantee the safety of source code. Byte code can be easily de-compiled with easily available tools.No commercial software company will want to expose its trade secrets to the whole world.
Weak type checking
When you have a huge project made up of a couple hundred modules , each sending parameters back and forth you're destined to make a mistake passing a variable here or there. 

It would be nice if we could add some sort of voluntary type checking that would catch such errors.

I feel Python is a very powerful language and a very beautiful one for that matter.

 

Monday, December 10, 2012

Installing PyDev in Eclipse (Python IDE)




Programming Python is so much fun in Eclipse using the PyDev Plugin. If you've never programmed in an IDE (Integrated Development Environment) then your going to be pleasantly surprised. Eclipse is a good and powerful IDE that is meant to be a Java IDE. But it can be extended with plugins to support several other languages. Eclipse requires the Java Runtime Environment in order to run. The following screen shot is an example of the sort of error you get when you try to run Eclipse without first installing the JDK.


A Missing Java Runtime Engine (JRE) will prevent Eclipse running
Running Eclipse is simple. You just run the eclipse.exe executalbe and choose a workspace. 
You Then :
  • Choose Help
  • Click "Install New Software"
  • Click Add and in the location box type : http://pydev.org/updates
  • In the "Work With" text box at the top, type : PyDev and then select PyDev in the checkbox below.



Choose Help - > Install New Software -> Click Add, In the location box type http://pydev.org/updates. Type Pydev in the "Work with" box and select PyDev by clicking the checkbox

Agree to the license





After PyDev installs successfully you will be able to create PyDev Projects. Select 
Starting an new Python Project in PyDev

Your code won't run  before you configure an interpreter to run it.Auto config is an easy way to do so.
PyDev must be told where the python interpreter is on your computer. Fortunately PyDev can easily detect Python Interpreters using the Auto Config option 







More tutorials on the way. To show how PyDev works and how to use the debugger.


PyDev + Eclipse : A good Python IDE for beginners

Installing Pydev in Eclipse

Installing PyDev is straight forward. When I installed it I went through the following steps:
  1. Go to www.eclipse.org/downloads/ and you;ll find several different flavors of the Eclipse IDE each targeted to different users. 
  2. Download the Eclipse Classic 4.2.1 in the version that suits the architecture of your machine(i.e. either 64-bit or 32-bit.)
  3. Run Eclipse and choose 'Help' 
  4. Choose  "Install new software"
  5. Click Add
  6. In the location box type "http://pydev.org/updates"
  7. Typed "Pydev" in the "Work with" text box
  8. Select the PyDev for eclipse checkbox
  9. Uncheck the "contact all update sites during install to find required software". Leaving it checked stalls the installation process for some unknown reason.

Python 3.0

Python 3.0 was released in December , 2008 and is referred to sometimes as Python 3000 or Py3k. It is a new version of the language and is incompatible with the 2.x line of releases. The language is mostly the same but many details  how built in objects like dictionaries  and strings have been changed considerably and a lot of deprecated features have been removed. Also, the standard library has been reorganized in a few key places.

Saturday, December 8, 2012

Some String Operations in Python

Finding the length of  a string 

 >>> message = "new string"
>>> len(message)
10
 

 So to find the length of string x you simply use len(x) . The function len() gets called with x as a parameter and returns the length as a return value. 10 in this case.



Accessing string elements by Index

 >>> message = "new string"
 >>> print(message[0])
n
>>> print(message[1])
e
>>> print (message[2])
w
 

All stuff in the string (i.e. couple of letters,numbers, characters strung to each other) can be accessed individually using the [] operator . The first character(letter) is at index 0 or numbered by 0. Hence print(message[0]) gave us the first letter in the string message which is 'n'. Moving to the next index(number) moves us to the next character(letter). This is really simple stuff.


Going through all letters of a string 

>>> for letter in message:
    print(letter)
n
e
w

s
t
r
i
n
g


This is a for loop and goes through all letters in  the string message.


Testing if two strings match( are the same)

>>> message = "new string"
>>> m = "new string"
>>> if  m == message:
    print("They match")

   
They match