Use of factory pattern in Python?

Nathan Harmston ratchetgrid at googlemail.com
Thu Dec 7 03:28:39 EST 2006


Hi,

Im trying to find the best way to do a certain  task and my so far I
have found that using something similar to factory pattern might be
the best bet.

I m parsing a file to be stored as a graph (using NetworkX). Each row
in the file corresponds to a node in the graph. However rows have
different types and different numbers of attributes. ( and their
corresponding nodes in the future will have methods )
eg

chr1 SGD gene 5 8 id=1 name=3 dbref=6
chr1 SGD intron 5 6 id=5 notes="spam"
chr1 SGD exon 7 8 id=5

so I was thinking of having a factory class to return the individual
objects for each row......ie

class Factory():
        # if passed a gene return a gene object
        # if passed an intron return an intron object
        # if passed an exom return an exon object

Is this a good way of doing this, or is there a better way to do this
in Python, this is probably the way I d do it in Java.

Many Thanks

Nathan



More information about the Python-list mailing list