File Paramaterized Abstract Factory

C

Charles Krug

List:

I have an idea for an abstract factory that parameterizes the factory
with an input module.

The general ideom can be expressed thus:

class ThingFactory(object):
def CreateThisThing() : return ThisThing()
def CreateThatThing() : return ThatThing()
def CreateTheOtherThing() : return TheOtherThing()

With subclasses:

class HerThingFactory(ThingFactory):
def CreateThisThing() : return HerThisThing()
# etc

You'd invoke it in the usual way:

ThingClient(Factory=ThingFactory)

And Thing client would refer to:

ThingFactory.CreateThisThing()

Now, Pythonically speaking, as the envisioned application uses only one
ThingFactory at a time, I'd ordinarily want to just load a module
(HisFactory, HerFactory, KidsFactory, YadaYadaYadaFactory) that
implements the expected interface--in this case, supplying the CreateX
methods.

What I'd like is to do something like this:

factoryFile = sys.argv[1] # we assume that argv[1] implements a
# correct ThingMaker interface.

# A Miracle Occurs Here

UseFactory(factoryObject)

Where UseFactory looks like:

def UseFactory(factoryObject):

factoryObject.CreateThisThing() # returns the kind of thing


Rather a lot of preamble to learn what I'm trying to do.

Anywho, my problem is with the whole Miracle thing.

I've tried using __import__:

a = 'HerFactory'
__import(a)

Which returns:

<module 'HerFactory' from 'HerFactory.py'>

But after that I can't access the members.

Clearly I'm missing a step or seventeen.

What's the best way to do this?

Thanks


Charles
 
F

Fredrik Lundh

Charles said:
What I'd like is to do something like this:

factoryFile = sys.argv[1] # we assume that argv[1] implements a
# correct ThingMaker interface.

sys.argv[1] is a string, so I assume that you meant to say that
the module named by argv[1] implements the correct interface.
Anywho, my problem is with the whole Miracle thing.

I've tried using __import__:

a = 'HerFactory'
__import(a)

Which returns:

<module 'HerFactory' from 'HerFactory.py'>

try:

factoryObject = __import__(factoryFile)
print dir(factoryObject)

this should give you a list of the functions in the given module,
or an ImportError if it doesn't exist.
But after that I can't access the members.

what did you try, and what happened ? if at all possible, cut and
paste the code you tried (your examples above contained obvious
typos), and the actual error messages, so we don't have to guess.

</F>
 
C

Charles Krug

Charles said:
What I'd like is to do something like this:

factoryFile = sys.argv[1] # we assume that argv[1] implements a
# correct ThingMaker interface.

sys.argv[1] is a string, so I assume that you meant to say that
the module named by argv[1] implements the correct interface.
Yes.
Anywho, my problem is with the whole Miracle thing.

I've tried using __import__:

a = 'HerFactory'
__import(a)

Which returns:

<module 'HerFactory' from 'HerFactory.py'>

try:

factoryObject = __import__(factoryFile)
print dir(factoryObject)

Ah Ha!

That's what comes of working from memory instead of letting the computer
remember for me.
['MakeOtherThing', 'MakeThing', '__builtins__', '__doc__', '__file__',
'__name__']I made a Thing!

Wonderful! Exactly what I'm after, thanks.
this should give you a list of the functions in the given module,
or an ImportError if it doesn't exist.

Yes, that's what I was getting. Should have pasted that as well, but I
was feeling grumpy and impatient.

Thanx


Charles
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top