Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Use of factory pattern in Python?

Reply
Thread Tools

Re: Use of factory pattern in Python?

 
 
Gabriel Genellina
Guest
Posts: n/a
 
      12-07-2006
At Thursday 7/12/2006 05:28, Nathan Harmston wrote:

>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.


The basic idea is the same, but instead of a long series of
if...elif...else you can use a central registry (a dictionary will
do) and dispatch on the name. Classes act as their own factories.

registry = {}

class Base(object):
kind = "Unknown"
register(Base)

class Gene(Base):
kind = "gene"
def __init__(self, *args, **kw): pass
register(Gene)

class Intron(Base):
kind = "intron"
def __init__(self, *args, **kw): pass
register(Intron)

def register(cls):
registry[cls.kind] = cls

def factory(kind, *args, **kw):
return registry[kind](*args, **kw)

If some arguments are always present, you can name them, you're not
limited to use the generic *args and **kw.


--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ˇgratis!
ˇAbrí tu cuenta ya! - http://correo.yahoo.com.ar
 
Reply With Quote
 
 
 
 
Nick Craig-Wood
Guest
Posts: n/a
 
      12-07-2006
Gabriel Genellina <(E-Mail Removed)> wrote:
> The basic idea is the same, but instead of a long series of
> if...elif...else you can use a central registry (a dictionary will
> do) and dispatch on the name. Classes act as their own factories.
>
> registry = {}
>
> class Base(object):
> kind = "Unknown"
> register(Base)
>
> class Gene(Base):
> kind = "gene"
> def __init__(self, *args, **kw): pass
> register(Gene)
>
> class Intron(Base):
> kind = "intron"
> def __init__(self, *args, **kw): pass
> register(Intron)
>
> def register(cls):
> registry[cls.kind] = cls
>
> def factory(kind, *args, **kw):
> return registry[kind](*args, **kw)
>
> If some arguments are always present, you can name them, you're not
> limited to use the generic *args and **kw.


If you don't want to use a register() function on each class you could
introspect like this (untested) to make the registry :-

registry = {}
for obj in sys.modules[__name__].__dict__.values():
try:
if issubclass(obj, Base):
registry[kind] = obj
except TypeError:
pass

There might be a neater way of writing the above!

--
Nick Craig-Wood <(E-Mail Removed)> -- http://www.craig-wood.com/nick
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use Regex to breakdown a pattern and use the pattern to breakdown a string ChrisC Perl Misc 4 06-25-2010 05:46 PM
Abstract factory and Factory pattern C# ASP .Net 4 07-31-2008 03:22 PM
Use of factory pattern in Python? Nathan Harmston Python 0 12-07-2006 08:28 AM
documents related to factory design pattern and Abstract foctory pattern. sunny C++ 1 12-07-2006 04:26 AM
Abstract Factory or Factory Method pattern question.... Medi Montaseri C++ 17 09-03-2003 06:50 AM



Advertisments