Python Scripts
(January 2006)

Python is an interpreted, interactive, object-oriented programming language.
This articles presents some python scripts.
Table of contents

Implementing a chain list

Multiple Inheritance

XML parsing and hierarchy display

Implementing a chain list

chain_list.py
class node:
	def __init__(self, name):
		self.name = name
		self.previous = None
		self.next = None
	def __str__(self):
		return self.name

	def show_next(self):
		print self, '->',
		if self.next != None: self.next.show_next()
		else: print

	def show_previous(self):
		print self, '<-',
		if self.previous != None: self.previous.show_previous()
		else: print

	def insert(self, node):
		node.next = self
		node.previous = self.previous
		if self.previous != None: self.previous.next = node
		self.previous = node

x = node('N1')
z = node('N3')
head = x
head.next = z
z.previous = head

x.show_next()
z.show_previous()

print "-- adding a node --"
y = node('N2')
z.insert(y)
x.show_next()
z.show_previous()

print "-- modifying a node --"
x.name="ZOZO"

x.show_next()
z.show_previous()
The command line:
shell> python chain_list.py
And there is the output:
N1 -> N3 ->
N3 <- N1 <-
-- adding a node --
N1 -> N2 -> N3 ->
N3 <- N2 <- N1 <-
-- modifying a node --
ZOZO -> N2 -> N3 ->
N3 <- N2 <- ZOZO <-

Multiple Inheritance

The following example shows how multiple inheritance may be used:

class A:
        def printA(self):
                print 'A: Class', self.__class__.__name__

        def X(self):
                print 'X of A'

class B:
        def printB(self):
                print 'B: Class', self.__class__.__name__

        def X(self):
                print 'X of B'

class C(A, B):
        pass

a = A()
b = B()
c = C()

a.printA()
b.printB()
c.printA()
c.printB()
c.X()
This scripts outputs:
A: Class A
B: Class B
A: Class C
B: Class C
X of A

XML parsing and hierarchy display

This script shows the hierarchy of an XML document ("t.xml").
import xml.parsers.expat
import sys

def str_hierarchy() :
    global current_hierarchy
    result=""
    for i in current_hierarchy :
        if i==0 : return result
        result = result + str(i) + "."

def start_element(name, attrs):
    global level, current_hierarchy
    level = level + 1
    if level > len(current_hierarchy) :
        print "error: too many levels (" + str(level) + ")"
        sys.exit(1)

    # zero all sub-levels
    for i in range(level, len(current_hierarchy)) :
        current_hierarchy[i] = 0

    # increase current level
    current_hierarchy[level-1] = current_hierarchy[level-1] + 1

    print str_hierarchy(), name

def end_element(name):
    global level
    level = level - 1

def char_data(data):
    None

p = xml.parsers.expat.ParserCreate()

p.StartElementHandler = start_element
p.EndElementHandler = end_element
p.CharacterDataHandler = char_data

f=open("t.xml")
level = 0
current_hierarchy = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] # 16 levels

p.ParseFile(f)
Example with a sample XML document (t.xml) :
<?xml version="1.0"?>
<document>
  <header>this is ...
  <header>
  <body>
    <chapter>First Part<chapter>
    <chapter>Second Part
                <note>Note that ...<note>
    <chapter>
    <chapter>Third Part<chapter>
  <body>
<document>
And the result is :
1. document
1.1. header
1.2. body
1.2.1. chapter
1.2.2. chapter
1.2.2.1. note
1.2.3. chapter