K
kj
The subject line says it all.
Thanks!
kynn
Thanks!
kynn
The subject line says it all.
Aahz said:You are probably trying to remove a screw with a hammer -- why don't you
tell us what you really want to do and we'll come up with a Pythonic
solution?
You are probably trying to remove a screw with a hammer
-- why don't you
tell us what you really want to do and we'll come up with a Pythonic
solution?
I would want to know the answer in any case. *Can* it be done in
Python at all?
No.
OK, if you must know:
With Perl one can set a module-global variable before the module
is loaded. This provides a very handy backdoor during testing.
E.g.
# in t/some_test.t script
...
BEGIN { $My::Module::TESTING = 1; }
use My::Module;
...
and in My/Module.pm:
package My::Module;
our $TESTING ||= 0; # set to 0 unless already initialized to !0
...
if ($TESTING) {
# throw testing switches
}
This does not work in Python, because setting my.module.TESTING
variable can happen only after my.module has been imported, but by
this point, the module's top-level code has already been executed,
so setting my.module.TESTING would have no effect. But one way to
get a similar effect would be to have my.module set its TESTING
(or whatever) variable equal to the value of this variable in the
*importing* module.
With Perl one can set a module-global variable before the module is
loaded. This provides a very handy backdoor during testing. E.g.
This does not work in Python, because setting my.module.TESTING variable
can happen only after my.module has been imported, but by this point,
the module's top-level code has already been executed, so setting
my.module.TESTING would have no effect.
Well, I don't know what kj is trying to do, but my project is another
(!) configuration program. (Don't worry, I won't release it... unless
somebody is interested, of course !)
So here's the idea so far:
The configuration data is stored in a python module (call it
settings.py). In order to be able to do things like add settings to it,
save the file after changes are made, etc., settings.py will import the
configuration module, called configure.py.
A sample might look like this:
<settings.py>
import configure
paths = configure.Item()
paths.tables = 'c:\\app\\data'
paths.temp = 'c:\\temp'
</settings.py>
And in the main program I would have:
<some_app.py>
import settings
main_table = dbf.Table('%s\\main' % paths.tables)
# user can modify path locations, and does, so update # we'll say it
changes to \work\temp
settings.paths.temp = user_setting()
settings.configure.save()
</some_app.py>
And of course, at this point settings.py now looks like
<settings.py>
import configure
paths = configure.Item()
paths.tables = 'c:\\app\\data'
paths.temp = 'c:\\work\\temp'
</settings.py>
Now, the tricky part is the line
settings.configure.save()
How will save know which module it's supposed to be re-writing?
AK said:so:
# moduleA.py
import moduleB
# moduleB.py
import sys
stuff = sys._getframe(1).f_locals
print stuff
Prints:
{'__builtins__': <module '__builtin__' (built-in)>,
'__file__': 'C:\\Documents and Settings\\<userName>\\My Documents\
\python\\moduleA.py',
'__name__': '__main__',
'__doc__': None}
Looks like you could query stuff['__file__'] to pull what you're
after.
?
AK said:# moduleA.py
import moduleB# moduleB.py
import sys
stuff = sys._getframe(1).f_locals
print stuff
{'__builtins__': <module '__builtin__' (built-in)>,
'__file__': 'C:\\Documents and Settings\\<userName>\\My Documents\
\python\\moduleA.py',
'__name__': '__main__',
'__doc__': None}Looks like you could query stuff['__file__'] to pull what you're
after.
?
The leading _ in _getframe indicates a private function to sys (aka
implementation detail); in other words, something that could easily
change between one Python version and the next.
I'm using the inspect module (for the moment, at least), and my question
boils down to: Will it work correctly on all versions of Python in the
2.x range? 3.x range?
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.