473,378 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

File Paramaterized Abstract Factory

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
Jan 11 '06 #1
2 1160
Charles Krug wrote:
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>

Jan 11 '06 #2
On 2006-01-11, Fredrik Lundh <fr*****@pythonware.com> wrote:
Charles Krug wrote:
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.
factoryFile = 'ThingMaker'
factoryObject = __import__(factoryFile)
print dir(factoryObject) ['MakeOtherThing', 'MakeThing', '__builtins__', '__doc__', '__file__',
'__name__'] factoryObject.MakeThing()

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
Jan 11 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
3
by: Sebastian Faust | last post by:
Hi, I am currently reading the chapter about abstract factorys from "Modern C++ Design" and wondering how it would be possible to create an object which has a constructor with parameters. At the...
4
by: max | last post by:
Hello, I analyze this design pattern for a long time but I do not understand how this pattern work and what the purpose is? (I looked a this site...
33
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or...
2
by: Julia | last post by:
Hi, I have an application composed from layers like the following A --B ---C A is the top layer C uses an Abstract Factory to Create Concrete classes
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
6
by: Nindi | last post by:
5 Files Singleton.h The Singleton Factory.h The factory creating new objects. The Base class of the hierachy stores a typelist identifying the signature of the constructors to be called...
16
by: RSH | last post by:
Hi, I am a fairly seasoned developer and have recently started looking at software design patterns. I am starting with the Abstract Factory Pattern and was wondering if someone could help me...
4
by: C# | last post by:
http://www.questpond.com/FreeDesign1.htm I was going through the above videos. Can i conclude Abstract factory patterns are extensions of factory patterns. I have made the conclusion after...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.