[Tutor] What Design Pattern for Document class (NOT URGENT)

Knacktus knacktus at googlemail.com
Sat Sep 11 11:03:33 CEST 2010


Hi Karim,

it's difficult to comment as to me the problem is not quite clear.
But I try ;-)

You have a complex xml and need to read different data types. For each 
type you have certain rules, how to extract them from the xml (docrules).

You could create a DocRule class, that do nothing but hold instructions 
of how to read and write the xml for each data, e.g. xpathes. For each 
data type you create an instance of that class.

Then, in your main xml Reader class you define a method, that accepts a 
doc rule instance as argument.

doc_rule_1 = DocRule("some_pattern")
doc_rule_2 = DocRule("another_pattern")

class Reader(object):

     def __init__(self, filename):
         ... # create the etree

     def get_data_by_doc_rule(self, doc_rule):
         ... # search the tree based on the doc_rule
         ... # you can use any type here if it has the correct mehtods

In your "get_data_by_doc_rule" method you can use any type for the 
doc_rule, that has corresponding attributes and methods ("duck typing"). 
So you have a lot of flexibility of how to create your doc_rules.

Is that what you're looking for?

Jan


More information about the Tutor mailing list