Thursday, February 25, 2010

Python - a simple polymorphism example

# A quick example of polymorphism at work in python 
 
class Food(object):
    def __init__(self, name, calories):
        self.name = name
        self.calories = calories
    def tastesLike(self):
        raise NotImplementedException("Subclasses are responsible for creating this method")
 
class HotDog(Food):
    def tastesLike(self):
        return "Extremely processed meat" 
 
class Hamburger(Food):
    def tastesLike(self):
        return "grilled goodness" 
 
class ChickenPatty(Food):
    def tastesLike(self):
        return "tastes like chicken" 
 
dinner = []
dinner.append(HotDog('Beef/Turkey BallPark', 230))
dinner.append(Hamburger('Lowfat Beef Patty', 260))
dinner.append(ChickenPatty('Micky Mouse shaped Chicken Tenders', 170))
 
# even though each course of the dinner is a differnet type 
# we can process them all in the same loop 
for course in dinner:
    print course.name + " is type " + str(type(course))
    print "  has " + str(course.calories) + " calories " 
    print "  and tastes like " + course.tastesLike()
 
 
# my output: 
# 
#Beef/Turkey BallPark is type <class '__main__.HotDog'> 
#  has 230 calories 
#  and tastes like Extremely processed meat 
#Lowfat Beef Patty is type <class '__main__.Hamburger'> 
#  has 260 calories 
#  and tastes like grilled goodness 
#Micky Mouse shaped Chicken Tenders is type <class '__main__.ChickenPatty'> 
#  has 170 calories 
#  and tastes like tastes like chicken 
 
 

Python - the final countdown

# There is more than one way to accomplish a task. 
# I needed a method to countdown from a positive 
# number down to 0.  Here are three ways to solve 
# the issue.  Of course there are many more possible 
# solutions. 
# 
# I'd like to title this post "The Final Count Down" 
# Listen to this tune whilst perusing the script: 
# www.youtube.com/watch?v=tt_ro2aerQg 
 
 
# Using recursion always makes the girls swoon. 
def recursiveCountdown(number):
    print number
    if number > 0:
        recursiveCountdown(number-1)
 
# Old school while loop.  This solution gets points 
# for being the most obvious. 
def whileLoopCountdown(number):
    while number > -1:
        print number
        number -= 1
 
# Using python's xrange function is the most concise. 
def xrangeCountdown(number):
    for i in xrange(number, -1, -1):
        print i
 
print "It's the final countdown" 
number = input("Enter number: ")
 
print "Recursive..." 
recursiveCountdown(number)
print "While Loop..." 
whileLoopCountdown(number)
print "xrange..." 
xrangeCountdown(number)
 
 
# my output: 
# 
#It's the final countdown 
#Enter number: 5 
#Recursive... 
#5 
#4 
#3 
#2 
#1 
#0 
#While Loop... 
#5 
#4 
#3 
#2 
#1 
#0 
#xrange... 
#5 
#4 
#3 
#2 
#1 
#0 
 
 

Wednesday, February 24, 2010

Python - Bulk rename a directory of files

 
# I found myself with the need to rename my video 
# collection.  For some reason I decided that using 
# spaces in a file name is lame....underscores should 
# always be used.  After two or three files of manually 
# renaming I decided that python could do all the 
# work for me. 
 
import os
import glob
 
# My video collection is all matroska files.  So 
# the extension of them all is *.mkv format. 
files_to_change = '*.mkv' 
 
# new and old versions of a space 
lame_space = ' ';
cool_space = '_';
 
# use glob to gather a list of matching files 
for f in glob.glob(files_to_change):
        f2 = f
        f2 = f2.replace(lame_space, cool_space)
        # add a little status for instant gratification 
        print 'renaming: ', f, ' -> ', f2
        os.rename(f, f2)
 
print 'All Done' 
 
 
## my output: 
# 
# renaming:  Me at home.mkv  ->  Me_at_home.mkv 
# renaming:  Max in kid pool.mkv  ->  Max_in_kid_pool.mkv 
# ........ < and so on > 
# All Done