Re: File Paramaterized Abstract Factory



On 2006-01-11, Fredrik Lundh <fredrik@xxxxxxxxxxxxxx> 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
.



Relevant Pages

  • Re: Is it possible to Change String Delimiter without using an interface Spec?
    ... > If you want to use TransferText the only way to specify a non-standard ... >> the data has a ' as the string delimiter. ... >> Dim strSRCdir As String, ... >> On Local Error GoTo ImportError ...
    (microsoft.public.access.externaldata)
  • Re: no win32com.client
    ... > In 2.4.1 the following is generated from a script that ran in 2.2: ... > import sys, string, os, win32com.client ... > ImportError: No module named win32com.client ...
    (comp.lang.python)
  • Re: File Paramaterized Abstract Factory
    ... Charles Krug wrote: ... sys.argvis a string, so I assume that you meant to say that ... or an ImportError if it doesn't exist. ... > But after that I can't access the members. ...
    (comp.lang.python)
  • Is it possible to Change String Delimiter without using an interface Spec?
    ... the data has a ' as the string delimiter. ... Dim strSRCdir As String, strSRCfile As String ... On Local Error GoTo ImportError ...
    (microsoft.public.access.externaldata)
  • question(s) about declaring variables
    ... Letting a variable be a ... variant or declaring it as a string. ... Why do all code examples use "As String to" declare each variable like this: ... I realize that declaring them like this: ...
    (microsoft.public.vb.general.discussion)