Handy Python Notes

How to initialize a list in python and assign values to the fields in the list dynamically 

>>> print (a)
['', '']

>>> print (a[0])
>>> print (a[1])

>>> a[0] = "asdfasdf"
>>> a[1] = "asdfasdffsadf"
>>> print (a)
['asdfasdf', 'asdfasdffsadf']

Structure in C 'vs' Class in Python 
A C struct as a way to organize data, while a Python (or C++ or Objective-C) "class" is a way to organize not only your data, but the operations for that data.
The big difference is that in Python (and other object oriented languages such as C++, Java, or C#), a class can also have functions associated with it that operate only on the instance of the class, whereas in C, a function that wishes to operate on a struct must accept the structure as a parameter in some way, usually by pointer.

Basic python program to understand attributes, classes, objects, methods and constructors, in python.
self as it suggests, refers to itself- the object which has called the method.
That is, if you have N objects calling the method, then self.a will refer to a
separate instance of the variable for each of the N objects. Imagine N copies
of the variable a for each object

__init__ is what is called as a constructor in other OOP languages such as
C++/Java. The basic idea is that it is a special method which is automatically
called when an object of that Class is created

###############################################################
# A CLASS IS A TEMPLETE FOR CREATING OBJECTS.
###############################################################
class player:
    name = "default"      # Attribute of a class.
    grand_slams = 0       # Attribute of a class.
    l_r_hander='default'  # Attribute of a class.

    #########################################################################
    # Once, there are attributes that “belong” to the class,                #
    # you can define functions/Method that will access the class attribute. #
    #########################################################################
    def player_nickname(self, nick_name):
        self.name = nick_name  # 'self' is the keyword that is used by a method
                               # to access the attribute of the class in which
                               # the method is defined.

    ##############################################################################
    # The name of a constructor is always the same, __init__().                  #
    # The constructor can accept arguments when necessary to create the object.  #
    # When you create a class without a constructor, Python automatically        #
    # creates a default constructor for you that doesn’t do anything.            #
    # Every class must have a constructor, even if it simply relies on the       #
    # default constructor.                                                       #
    # USING THIS, THE VALUES OF ATTRIBUTES CAN BE PROVIDED AT RUNTIME.           #
    ##############################################################################
    def __init__(self, nick_name):
        self.name = nick_name
        print ("contructor in class %s" %(self.name))

print (player.name, ' ', player.grand_slams)
print (player)

###############################################################
# THE BELOW ARE INSTANCES OF A CLASS. THEY ARE CALLED OBJECTS.
###############################################################

print ("##################################################################")
player1 = player("Fedex")
player2 = player("Rafa")
player3 = player("Djoker")
print ("##################################################################")

##############################################################
# this will print address of the instance of 'player' object
##############################################################
print (player1)
print (player2)
print (player3)


##############################################################
# NOW UPDATE THE VARIABLE VALUES IN RESPECTIVE CLASSES.
##############################################################


##################################################

# Object 1
##################################################
print ("##############################################################")
player1.name = "Roger Federer"          # Attribute
player1.grand_slams = 20                # Attribute
player1.l_r_hander  = 'Right'           # Attribute
print ("%s %s %s" %(player1.name, player1.grand_slams, player1.l_r_hander))
player1.player_nickname("Fedex")        # Method
print ("%s %s %s" %(player1.name, player1.grand_slams, player1.l_r_hander))
print ('##############################################################')

#################################################
# Object 2
#################################################
player2.name = "Rafael Nadal"
player2.grand_slams = 17
player2.l_r_hander  = 'left'
print ("%s %s %s" %(player2.name, player2.grand_slams, player2.l_r_hander))
player2.player_nickname("Rafa")        # Method
print ("%s %s %s" %(player2.name, player2.grand_slams, player2.l_r_hander))
print ('##############################################################')

#################################################

# Object 3
#################################################
player3.name = "Novak Djokovic"
player3.grand_slams = 13
player3.l_r_hander  = 'Right'
print ("%s %s %s" %(player3.name, player3.grand_slams, player3.l_r_hander))
player3.player_nickname("Djoker")        # Method
print ("%s %s %s" %(player3.name, player3.grand_slams, player3.l_r_hander))
print ('##############################################################')



Search for a file in directories in python recursively.

import os

file_list = []

def search_files(directory='.', extension=''):
    extension = extension.lower()
    for dirpath, dirnames, files in os.walk(directory):
        for name in files:
            if extension and name.lower().endswith(extension):
                file_list.append(name)
                print(os.path.join(dirpath, name))
            elif not extension:
                print(os.path.join(dirpath, name))


if __name__ == '__main__':
   # syntax Directory, filename or extension or substring 
   search_files("D:", "expect")                
   print ("##############################################")
          
   print (file_list)




switch case concept [by using dictionaries]  in python
def one(arg):
    print str(arg) + "January"

def two(arg):
    print str(arg) + "February"

def three(arg):
    print str(arg) + "March"

def four(arg):
    print str(arg) + "April"

def five(arg):
    print str(arg) + "May"

def six(arg):
    print str(arg) + "June"

def seven(arg):
    print str(arg) + "July"

def eight(arg):
    print str(arg) + "August"

def nine(arg):
    print str(arg) + "September"

def ten(arg):
    print str(arg) + "October"

def eleven(arg):
    print str(arg) + "November"

def twelve(arg):
    print str(arg) + "December"


def numbers_to_months(argument, arg):
    switcher = {
        1: one,
        2: two,
        3: three,
        4: four,
        5: five,
        6: six,
        7: seven,
        8: eight,
        9: nine,
        10: ten,
        11: eleven,
        12: twelve
    }
    # Get the function from switcher dictionary
    func = switcher.get(argument, lambda: "Invalid month")
    # Execute the function
    func(arg)

'''
###########################################################################
# Main code starts Here
###########################################################################
'''
numbers_to_months(1, 1234)

Validate whether a string is subset of another string or not
def validate_substring_in_string(substring, text):
    if text.find(substring) != -1:
        print "substring is subset of main string"
        return 0
    else:
        print "No substring found"
        return 1

validate_substring_in_string("robot dance", "show me robot dance")