Import confusion
Kristian Ovaska
kristian.ovaska at helsinki.fi
Thu Apr 12 05:52:20 EDT 2001
David Bolen <db3l at fitlinxx.com>:
>As long as you realize the potential maintenance "expense" of this
>approach as your code will no longer clearly identify where such
>objects came from
We're talking about small scripts here, so the expense is quite small.
You could say they are executed in a limited environment, or
"sandbox". Runtime constants and functions are clearly documented. One
is supposed to be able to use them without any knowledge of the
internal structure of my program.
>But we could also just agree to disagree on the "handiness" of
>referencing an imported object including the module name. It does
>help your original issue though since everything would reference the
>same object in the loaded constant module.
I solved the original problem this way:
def execCode(self, code, outFile):
if not code:
return
import runtime
runtime._setRuntime(self, outFile)
for name in dir(runtime):
if name[0]!='_':
exec '%s = runtime.%s' % (name, name)
sys.stdout = outFile
exec 'pass'
exec code+'\n'
sys.stdout = sys.__stdout__
Example script could be:
if len(pages)>1:
print '<!-- Navigation bar -->'
print '<table class="navbar">'
print '<tr>'
columns = len(pages)
for p in pages:
print '<td width="'+str(100/columns)+'%"',
if p.file==page.file:
print 'class="active"',
print '><a href="'+p.file+'">'+p.title+'</a></td>'
print '</table>'
print '<hr>\\n'
This prints a navigation bar for a "flat" HTML page structure. page is
current page and pages is the list of all pages. The naming of
constants could be better, I haven't given it much thought.
--
Kristian Ovaska <kristian.ovaska at helsinki.fi>
More information about the Python-list
mailing list