Re: How to pass a reference to the current module

2007-08-04 Thread Paul Rubin
James Stroud <[EMAIL PROTECTED]> writes: > FunctionUser.do_something_with(globals(), 'doit', 7) How about instead of "import FunctionUser", require from FunctionUser import do_something In FunctionUser.py, write: frob = (some object that gets necessary stuff from module environment)

Re: How to pass a reference to the current module

2007-08-04 Thread Peter Otten
James Stroud wrote: > Yes, those were typos. And to be consistent, the whole listing of the > configuration file should be (note: 'doit'->do_something_with): > > [foo] > param1 = float > param2 = 4 > > [option1] > __module__ = UserDefined1 > __function__ = do_something_with > param1 = str > para

Re: How to pass a reference to the current module

2007-08-04 Thread James Stroud
Steven D'Aprano wrote: > I suggest you're falling for the anti-pattern of "Big Design Up Front", > and are overly complicating your system "just in case it's useful". Why > not just _insist_ that main.py and UserDefined1.py must be different > modules? You're the application developer, you're allow

Re: How to pass a reference to the current module

2007-08-03 Thread James Stroud
Paul Rubin wrote: > Hmm, it's a pain that there's no clean way to get at the current > module. PEP 3130 shows some icky and unreliable ways, e.g. > >func = getattr(sys.modules[__name__], 'f') > > PEP 3130's goal was to add a clean way to do this. Unfortunately it > was rejected. Yes, this

Re: How to pass a reference to the current module

2007-08-03 Thread Paul Rubin
Steven D'Aprano <[EMAIL PROTECTED]> writes: > I'll point at the difficulty in using getattr for extracting a function > from the current module: > >>> func = getattr(???, 'f') # what should go there? Hmm, it's a pain that there's no clean way to get at the current module. PEP 3130 shows some icky

Re: How to pass a reference to the current module

2007-08-03 Thread Steven D'Aprano
On Fri, 03 Aug 2007 21:06:23 -0700, James Stroud wrote: > Its very close. However, there is the possibiltiy that main.py and > UserDefined1.py are the same module. In such a case I'm guessing that I > need to resort to the gymnastics of frame inspection I mentioned > earlier. I suggest you'r

Re: How to pass a reference to the current module

2007-08-03 Thread Steven D'Aprano
On Fri, 03 Aug 2007 18:45:21 -0700, Paul Rubin wrote: > Steven D'Aprano <[EMAIL PROTECTED]> writes: >> namespace = module.__dict__ >> return namespace[name] > > Am I missing something? It's likely that I just don't understand > the problem, but I don't understand these dictionary ext

Re: How to pass a reference to the current module

2007-08-03 Thread James Stroud
Paul Rubin wrote: > James Stroud <[EMAIL PROTECTED]> writes: > >> ModuleBehavior >> == >>UserDefined1 Imports FunctionUser >>ThirdParty Contains User Functions (May be ==UserDefined1) >>FunctionU

Re: How to pass a reference to the current module

2007-08-03 Thread James Stroud
Carsten Haese wrote: > On Fri, 2007-08-03 at 18:31 -0700, James Stroud wrote: >>Carsten Haese wrote: >>You sound like my former thesis adviser. > > > Thanks. I guess. Yes, its a compliment in disguise--just check his CV. >>OK. From an external source, such as configuration file, the user will

Re: How to pass a reference to the current module

2007-08-03 Thread Carsten Haese
On Fri, 2007-08-03 at 18:31 -0700, James Stroud wrote: > Carsten Haese wrote: > > please describe less abstractly what you're trying to do. > > You sound like my former thesis adviser. Thanks. I guess. > OK. From an external source, such as configuration file, the user will > specify the *name*

Re: How to pass a reference to the current module

2007-08-03 Thread Paul Rubin
James Stroud <[EMAIL PROTECTED]> writes: > ModuleBehavior >== > UserDefined1 Imports FunctionUser > ThirdParty Contains User Functions (May be ==UserDefined1) > FunctionUser do_something_with

Re: How to pass a reference to the current module

2007-08-03 Thread James Stroud
Steven D'Aprano wrote: > On Fri, 03 Aug 2007 17:22:40 -0700, James Stroud wrote: > > >>Basically, what I am trying to acomplish is to be able to do this in any >>arbitrary module or __main__: >> >> >>funcname = determined_externally() >>ModuleUser.do_something_with(AModule, funcname) >> >> >>Ide

Re: How to pass a reference to the current module

2007-08-03 Thread Paul Rubin
Steven D'Aprano <[EMAIL PROTECTED]> writes: > namespace = module.__dict__ > return namespace[name] Am I missing something? It's likely that I just don't understand the problem, but I don't understand these dictionary extractions for what I thought would be an attribute lookup: Py

Re: How to pass a reference to the current module

2007-08-03 Thread Steven D'Aprano
On Fri, 03 Aug 2007 18:32:54 -0700, James Stroud wrote: > Carsten Haese wrote: >> This seems to confirm my suspicion that the do_something_with function >> doesn't actually need a reference to the module, it only needs a >> reference to the function to call. >> >> Maybe your hurdle is how to obta

Re: How to pass a reference to the current module

2007-08-03 Thread Paul Rubin
James Stroud <[EMAIL PROTECTED]> writes: > > please describe less abstractly what you're trying to do. > You sound like my former thesis adviser. I couldn't understand what you were trying to do either. > OK. From an external source, such as configuration file, the user will > specify the *name*

Re: How to pass a reference to the current module

2007-08-03 Thread Steven D'Aprano
On Fri, 03 Aug 2007 17:22:40 -0700, James Stroud wrote: > Basically, what I am trying to acomplish is to be able to do this in any > arbitrary module or __main__: > > > funcname = determined_externally() > ModuleUser.do_something_with(AModule, funcname) > > > Ideally, it would be nice to leav

Re: How to pass a reference to the current module

2007-08-03 Thread James Stroud
Carsten Haese wrote: > This seems to confirm my suspicion that the do_something_with function > doesn't actually need a reference to the module, it only needs a > reference to the function to call. > > Maybe your hurdle is how to obtain a reference to a function from the > current module when all

Re: How to pass a reference to the current module

2007-08-03 Thread James Stroud
Carsten Haese wrote: > please describe less abstractly what you're trying to do. You sound like my former thesis adviser. OK. From an external source, such as configuration file, the user will specify the *name* of a function somehow with the assumption that the function will be coded somewhere

Re: How to pass a reference to the current module

2007-08-03 Thread Carsten Haese
On Fri, 2007-08-03 at 17:53 -0700, James Stroud wrote: > James Stroud wrote: > > Basically, what I am trying to acomplish is to be able to do this in any > > arbitrary module or __main__: > > > > > > funcname = determined_externally() > > ModuleUser.do_something_with(AModule, funcname) > > > >

Re: How to pass a reference to the current module

2007-08-03 Thread Carsten Haese
On Fri, 2007-08-03 at 17:22 -0700, James Stroud wrote: > Hello All, > > Say I have this code: > > > import AModule > import ModuleUser > > ModuleUser.do_something_with(AModule, 'some_function_name') > > > But say the functions are defined in the same module that contains the > above code. I

Re: How to pass a reference to the current module

2007-08-03 Thread James Stroud
James Stroud wrote: > James Stroud wrote: > >> Basically, what I am trying to acomplish is to be able to do this in >> any arbitrary module or __main__: >> >> >> funcname = determined_externally() >> ModuleUser.do_something_with(AModule, funcname) >> >> >> Ideally, it would be nice to leave out A

Re: How to pass a reference to the current module

2007-08-03 Thread James Stroud
James Stroud wrote: > Basically, what I am trying to acomplish is to be able to do this in any > arbitrary module or __main__: > > > funcname = determined_externally() > ModuleUser.do_something_with(AModule, funcname) > > > Ideally, it would be nice to leave out AModule if the functions were