Tuesday 28 September 2010

Object Orientation Example

I knocked this example up today for a friend on IRC. Thought I would post it here.
#! /usr/bin/env python
################################################################################
##  Python Object Orientated example by rowinggolfer                          ##
##  the same object... but extra functionality added in layers                ##
##                                                                            ##
##  A noteable "Gotcha" for newbs is the __init__ function.                   ##
##  the base class in this example sets some important attributes in this     ##
##  function. Therefore classes which inherit from it, but be careful if      ##
##  re-implementing this function                                             ##
##  NOTE - the teenager class does NOT overwrite this function... hence when  ##
##  a teenager is initiated, python looks up the class hierarchy and executes ##
##  the first __init__ method it finds.. in this case the Toddler's __init__  ##
##                                                                            ##
##                                                                            ##
##  Run this script, and study the output.                                    ##
##                                                                            ##
##  Pay special attention to the attribute "description, and see how it is    ##
##  altered mid method in the teenage years.                                  ##
##                                                                            ##
################################################################################

from datetime import date
from base64 import b64decode

class TimritBaby(object):
    '''
    a baby has a date of birth, a name, and limited functionality
    '''
    def __init__(self, date_of_birth):
        print "TimritBaby initiated"
        self.name = 'Scott Murtz'
        self.dob = date_of_birth
        self.description = "(a baby)"

    def poop(self):
        print "    %s %s can Poop!"% (self.name, self.description)


class TimritToddler(TimritBaby):
    '''
    a toddler has a new method
    '''
    def __init__(self, dob):
        # call the init method of the base class..
        # otherwise the name attribute won't work!!
        print "\nTimritToddler initiated"
        TimritBaby.__init__(self, dob)
        self.description = "(a toddler)"
    
    def walk(self):
        print "    %s %s can Walk!"% (self.name, self.description)

class TimritTeenager(TimritToddler):
    '''
    a teenager with a secret habit
    '''
    def private_time(self):
        self.description = "(now a teeneager)"
        message = b64decode('aXMgbWFzdHVyYmF0aW5nIGFnYWlu')
        print "    %s %s %s"% (self.name, self.description, message)


if __name__ == "__main__":
    baby = TimritBaby(date(1968,1,1))
    baby.poop()
    try:
        baby.walk()
    except AttributeError as e:
        print "ERROR - WHOOPS!", e
    
    teenager = TimritTeenager(date(1968,1,1))
    teenager.poop()
    teenager.walk()
    teenager.private_time()
    teenager.walk()
 

Sunday 12 September 2010

fuzzymatching in postgres on ubuntu

I had a battle getting the soundex function installed into my database on postgres.
but here's how I succeeded.

One - Install the postgres-contrib package
sudo apt-get install  postgresql-contrib

Two - change user to postgres (a database superuser created on install of postgresql
neil@slim-maroon:~$ sudo su - postgres
postgres@slim-maroon:~$ 

Three - pipe the supplied script into the database (my database is called "openmolar_demo")
postgres@slim-maroon:~$ psql -d openmolar_demo -f /usr/share/postgresql/8.4/contrib/fuzzystrmatch.sql
SET
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
postgres@slim-maroon:~$

Four - test it!
openmolar_demo=> SELECT soundex('Neil');
 soundex 
---------
 N400
(1 row)

Tuesday 7 September 2010

Postgres for openmolar2

I've been playing with postgres all morning, and have decided that the next version of openmolar will use it in preference to MySQL.

Main Reasons.
    1. it's a clean break with the past
    2. GNUmed use postgres
    3. active development
    4. standards compliance
    5. not owned by oracle
A huge thankyou to all postgres devs for a wonderful open-source back-end.

Thursday 2 September 2010

Pynotify style notifications

The notifcation system for openmolar2 is nearly complete.

Why not use pynotify? Cross-platform is a goal here.