Wednesday, March 31, 2010

Python - insult generator

# negative reinforcement through insults

#
#
# Sometimes negative reinforcement is the way to go.
# For the times when you need a kick in the pants
# rather than a pat on the back I whipped up this

# insult generator.
# It will churn out more insults then you can shake
# a stick at.

import random

class insultGenerator(object):
    def __init__(self):
        # setup the lists of insult fodder

        self.nounList = ['loser',
                         'jerk',
                         'nerd',
                         'doodie head',
                         'butthead',
                         'bonehead',
                         'dunce',
                         'moron',
                         'nerf herder']
        self.adjectiveList = ['smelly',
                              'ugly',
                              'gimpy',
                              'slimy',
                              'crabby',
                              'scabby',
                              'scratchy']
        self.connectorList = ['are one',
                              'are the biggest',
                              'are becoming a']
    def getInsult(self):
        insult = "you"

        # connector phrase
        connector = random.randint(1, len(self.connectorList))
        insult += " " + self.connectorList[connector-1]

        # adjectives
        adjCount = random.randint(2,4)
        random.shuffle(self.adjectiveList)
        for i in xrange(0,adjCount):
            if i != 0:
                insult += ", "

            else:
                insult += " "
            insult += self.adjectiveList[i]

        # ending noun
        noun = random.randint(1,len(self.nounList))
        insult += " " + self.nounList[noun-1]
        return insult


# a little example to get some insults flowing
if __name__ == '__main__':
    ig = insultGenerator()
    print ig.getInsult()
    print ig.getInsult()
    print ig.getInsult()
    print ig.getInsult()



# my output:
#   you are one ugly, slimy, scabby loser
#   you are the biggest scabby, slimy nerd
#   you are becoming a scabby, ugly, gimpy butthead

#   you are one slimy, smelly, crabby bonehead



Saturday, March 27, 2010

Python - unit tests with the unittest module

# python has a built in unit test module "unittest"
#

# Using python's unittest module is similar to NUnit
# or JUnit (if you are familiar with those).
#


import unittest


# my example function
# Normally you would have the class in its own
# file and then just import ExampleFunction into

# the unit test file.
class ExampleFunction(object):
    def __init__(self):
        self.example = 0
        self.example2 = 1
        self.example3 = 2
    def processVariable(self, item):
        item *= self.example2 * self.example3
        return item
    def setExampleVariables(self, example):
        self.example = example
        self.example2 = self.example + 1
        self.example3 = self.example2 + 1


# The test class inherits from unittest's TestCase.
# Inheriting ensures that the tests will run when you
# call unittest.Main()
class TestExampleFunction(unittest.TestCase):
    def setUp(self):
        self.ef = ExampleFunction()
    def test_defaults(self):
        # There are a lot types of asserts available

        # I only demo the assertEqual here.
        self.assertEqual(self.ef.example, 0, "example is not set to 0")
        self.assertEqual(self.ef.example2, 1, "example2 is not set to 1")
        self.assertEqual(self.ef.example3, 2, "example3 is not set to 2")
    def test_processVariable(self):
        self.assertEqual(self.ef.processVariable(1), 2, "example error message")
    def test_setExampleVariables(self):
        self.ef.setExampleVariables(5)
        self.assertEqual(self.ef.example, 5, "example is not set to 0")
        self.assertEqual(self.ef.example2, 6, "example2 is not set to 1")
        self.assertEqual(self.ef.example3, 7, "example3 is not set to 2")


if __name__ == '__main__':
    unittest.main()


# My Output:
#    ...
#    -------------------------------------
#    Ran 3 tests in 0.000s
#
#    OK


Tuesday, March 23, 2010

Python - calling super class methods from derived classes

# An inheritance, abstract classes, and
# calling super class methods from derived

# classes example

class AbstractBot(object):
    def __init__(self):
        self.size = 0
        self.speed = 1
        self.pos = [0,0]
    def applyPos(self, xy):
        self.pos[0] += xy[0] * self.speed
        self.pos[1] += xy[1] * self.speed
    def getCurrentPos(self):
        return self.pos


class WalkingBot(AbstractBot):
    def applyPos(self, xy):
        super(WalkingBot, self).applyPos(xy)
        print "step, step"

class DrivingBot(AbstractBot):
    def applyPos(self, xy):
        super(DrivingBot, self).applyPos(xy)
        print "vroom, vroom"

class RunningBot(AbstractBot):
    def __init__(self):
        super(RunningBot, self).__init__()
        self.speed = 5
    def applyPos(self, xy):
        super(RunningBot, self).applyPos(xy)
        print "trot, trot"

bot1 = WalkingBot()
bot2 = DrivingBot()
bot3 = RunningBot()
bots = [bot1, bot2, bot3]

distance = (1,1)

for step in xrange(3):
    for bot in bots:
        bot.applyPos(distance)
        print 'position: ' + str(bot.getCurrentPos())
        


Monday, March 8, 2010

Python - creating an interface with python

# python doesn't formally support an interface.
# You can work around this and support the spirit

# of an interface by creating abstract classes that
# have empty methods that just raise exceptions if
# haven't been implemented.

class Vehicle:
    def __init__(self, model):
        self.model = model
    def returnModel(self):
        raise NotImplementedError("Subclass must implement")
    def calculateTaxes(self):
        raise NotImplementedError("Subclass must implement")


class Toyota(Vehicle):
    pass


t = Toyota("Carolla")
t.returnModel()

#output:
# NotImplementedError: Subclass must implement

# You are required to implement the defined methods ( just

# like when using an interface