cls-ex1.py

#!/usr/bin/env python
#
# Python classes  Example
#
# Author:  Yotam Medini  [email protected] -- Created: 2006/March/20
#

import sys

# Gender numbers
Male   = 0  # since Adam was first
Female = 1  # since they live longer

# Python-classes

class Person:
    def __init__(self, name, age, gender, hc):
        self.name = name
        self.age = age
        self.gender = gender
        self.hair_color = hc


class Class:
    def __init__(self, name, teacher, students):
        self.name = name
        self.teacher = teacher
        self.students = students

class School:
    def __init__(self, name, principal, classes):
        self.name = name
        self.principal = principal
        self.classes = classes


# old fashion functions
def person_str(p):
    if p.gender == Male:
        mf = "Male"
    elif p.gender == Female:
        mf = "Female"
    else:
        mf = "Unknown?"
    s = "Name: %s, Age: %d, Gender: %s, Hair: %s" % (
        p.name, p.age, mf, p.hair_color)
    return s


def show_class(cls):
    sys.stdout.write("""
  Class: %s,
  Teacher: %s
  %d students:
""" %
                     (cls.name, person_str(cls.teacher), len(cls.students)))
    for s in cls.students:
        sys.stdout.write("    %s\n" % person_str(s))


def show_school(school):
    sys.stdout.write("""
School: %s
Principal: %s
%d classes
""" %
                     (school.name,
                      person_str(school.principal),
                      len(school.classes)))
    for cls in school.classes:
        show_class(cls)


#############
# All Persons
#

amalia = Person("Amalia Medini", 13, Female, "Brown")
raoul  = Person("Raoul Harel", 13, Male, "Black")
tomerg  = Person("Tomer Grossman", 13, Male, "Brown")
tomers  = Person("Tomer Solomon", 13, Male, "Black")
idan  = Person("Idan Armoni", 25, Male, "Brown")

yoel = Person("Yoel Chajes", 11, Male, "Brown")
tamar = Person("Tamer Ben-David", 11, Female, "Brown")
passit = Person("Passit Siach", 30, Female, "Brown")

moishik = Person("Moishik Lerner", 50, Male, "Gray")

harry = Person("Harry Potter", 16, Male, "Black")
ron = Person("Ron Weasley", 16, Male, "Red")
hermione = Person("Hermione Granger", 16, Female, "Blond")
mcgonogal = Person("Minerva McGonogal", 55, Female, "White")

malfoy = Person("Draco Malfoy", 16, Male, "Blond")
parskyn = Person("Pansky Parskin", 16, Female, "Brown")
snape = Person("Sevures Snape", 45, Male, "Black")

dumbeldore = Person("Albus Dumbeldore", 99, Male, "White")

#############
# All classes
#

bogeret = Class("Bogeret", idan, [amalia, raoul, tomerg, tomers])
nisha = Class("Nisha", passit, [yoel, tamar])
gryffindor = Class("Gryffindor", mcgonogal, [harry, ron, hermione])
slytherin = Class("Slytherin", snape, [malfoy, parskyn])

#############
# All schools
#

keshet = School("Keshet", moishik, [bogeret, nisha])
hogwarts = School("Hogwarts", dumbeldore, [gryffindor, slytherin])

show_school(keshet)
show_school(hogwarts)
sys.exit(0)

Generated by GNU enscript 1.6.4.