
class Question:
    pass

class Survey:
    pass

class Value:
    def get_value(self):
        raise NotImplementedError()

class Empty(d.Value):
    pass

class Profile(d.Value):
    def __init__(self, name):
        self.name = name

### Base classes

class q:
    class Question:
        pass
    class Survey:
        pass


### Definition

# Questions

class Sex(q.Question):
    question = "Are you male or female?"
    options = ("Male", "Female")

class Children(q.Question):
    question = "How many children have you had?"
    options = range(0, 10)

class Age(q.Question):
    question = "What is your age?"
    options = range(0, 120)

class MarStat(q.Question):
    question = "What is your marital status?"
    options = [
        ("NeverMar", "Never been maried"),
        ("Married" , "Married"),
        ("Separate", "Separated"),
        ("Divorced", "Divorced"),
        ("Widowed" , "Widowed")
    ]

class Spouse(q.Question):
    question = "What is your spouse's age?"
    options = range(0, 120)

class Work(q.Question):
    question = "Are you working for pay or profit?"
    options = ("Yes", "No")

class Radio(q.Question):
    question = "Do you regularly listen to the radio?"
    options = ("Yes", "No")

# Survey

class Survey(q.Survey):
    rules = (
        Sex,
        { (Sex, "is", "Female") : ( 
            Children
        ) },
        Age,
        { (Age, ">", 16): (
            MarStat,
            { ((MarStat, "is", "Married"), "or", (MarStat, "is", "Separate")) : ( 
                Spouse
            ) },
            Work
        ) },
        Radio
    )


### Printer

class SurveyPrinter:
    def __init__(self, survey):
        self.survey = survey

    def _serialize(self):
        self.index = 0
        self.questions = []
        self.conditions = {}
        self.revindex = {}
        self._iterate(self.survey.rules)

    def _iterate(self, root, conditions=[]):
        for rule in root:
            t = type(rule).__name__
            if t == 'classobj' and issubclass(rule, q.Question):
                self.index += 1
                self.questions.append(rule)
                self.revindex[rule] = self.index
                self.conditions[rule] = conditions
            elif t == 'dict':
                cond = rule.keys()[0]
                sub = rule.values()[0]
                if type(sub).__name__ != 'tuple':
                    sub = rule.values()
                self._iterate(sub, conditions + [cond])

    def init(self):
        self._serialize()

    def print_questions(self):
        for q in self.questions:
            to = type(q.options).__name__
            if to == 'tuple':
                options = ", ".join(q.options)
            elif to == 'list':
                if type(q.options[0]).__name__ == 'tuple':
                    options = "\n       ".join(map(lambda x: "%s [%s]" % (x[1], x[0]), q.options))
                else:
                    options = "%d..%d" % (q.options[0], q.options[len(q.options)-1]+1)

            print "%2d. %s [%s]\n    => %s" % (self.revindex[q], q.question, q.__name__, options)


    def _repr_condition(self, con):
        a = con[0]
        op = con[1]
        b = con[2]

        ta = type(a).__name__
        tb = type(b).__name__

        if ta == 'tuple':
            a = "(%s)" % self._repr_condition(a)
        elif ta == 'classobj':
            a = a.__name__

        if tb == 'tuple':
            b = "(%s)" % self._repr_condition(b)
        elif tb == 'classobj':
            b = b.__name__

        return "%s %s %s" % (a, op, b)

    def print_conditions(self):
        for q in self.questions:
            cons = self.conditions[q]
            if len(cons) == 0:
                print "%2d. %s => None" % (self.revindex[q], q.__name__)
            else:
                print "%2d. %s =>" % (self.revindex[q], q.__name__)
                for con in cons:
                    print "    - %s" % self._repr_condition(con)

if __name__ == '__main__':
    sp = SurveyPrinter(Survey)
    sp.init()
    
    print "Questions:"
    sp.print_questions()
    
    print
    print "Conditions:"
    sp.print_conditions()


@@ -235,1373 +235,8 @@
 me%0A%0A
-### Base classes%0A%0Aclass q:%0A    class Question:%0A        pass%0A    class Survey:%0A        pass%0A%0A%0A### Definition%0A%0A# Questions%0A%0Aclass Sex(q.Question):%0A    question = %22Are you male or female?%22%0A    options = (%22Male%22, %22Female%22)%0A%0Aclass Children(q.Question):%0A    question = %22How many children have you had?%22%0A    options = range(0, 10)%0A%0Aclass Age(q.Question):%0A    question = %22What is your age?%22%0A    options = range(0, 120)%0A%0Aclass MarStat(q.Question):%0A    question = %22What is your marital status?%22%0A    options = %5B%0A        (%22NeverMar%22, %22Never been maried%22),%0A        (%22Married%22 , %22Married%22),%0A        (%22Separate%22, %22Separated%22),%0A        (%22Divorced%22, %22Divorced%22),%0A        (%22Widowed%22 , %22Widowed%22)%0A    %5D%0A%0Aclass Spouse(q.Question):%0A    question = %22What is your spouse's age?%22%0A    options = range(0, 120)%0A%0Aclass Work(q.Question):%0A    question = %22Are you working for pay or profit?%22%0A    options = (%22Yes%22, %22No%22)%0A%0Aclass Radio(q.Question):%0A    question = %22Do you regularly listen to the radio?%22%0A    options = (%22Yes%22, %22No%22)%0A%0A# Survey%0A%0Aclass Survey(q.Survey):%0A    rules = (%0A        Sex,%0A        %7B (Sex, %22is%22, %22Female%22) : ( %0A            Children%0A        ) %7D,%0A        Age,%0A        %7B (Age, %22%3E%22, 16): (%0A            MarStat,%0A            %7B ((MarStat, %22is%22, %22Married%22), %22or%22, (MarStat, %22is%22, %22Separate%22)) : ( %0A                Spouse%0A            ) %7D,%0A            Work%0A        ) %7D,%0A        Radio%0A    )%0A
 %0A%0A##
@@ -2595,195 +2595,4 @@
 n)%0A%0A
-if __name__ == '__main__':%0A    sp = SurveyPrinter(Survey)%0A    sp.init()%0A    %0A    print %22Questions:%22%0A    sp.print_questions()%0A    %0A    print%0A    print %22Conditions:%22%0A    sp.print_conditions()%0A%0A

Remove unused codes.