How to pass parameter to a module?
M-a-S
NO-MAIL at hotmail.com
Thu Sep 18 13:58:33 EDT 2003
I thought about the third module. It doesn't sound good.
I hoped there're some tricks with __dict__, frames and
other __...__ objects.
For now, this is what does the task:
# sub_2.py
''' The documentation will look like this:
To set the size of dictionary and search depth,
do before including (default values are shown):
import string
string.sub_2_dictionary_size = 10 # in megabytes
string.sub_2_search_depth = 1000 # in nodes
string.sub_2_max_responce_time = 100 # seconds
'''
import string # it's used in sub_2 anyway
# parameters
try: _DICT_SZ = string.sub_2_dictionary_size
except: _DICT_SZ = 10
try: _DEPTH = string.sub_2_search_depth
except: _DEPTH = 1000
try: _RTIME = string.sub_2_max_responce_time
except: _RTIME = 100
# now the stuff
def do_the_job():
return "Processing with dict=%d depth=%d rtime=%d" % (_DICT_SZ,_DEPTH,_RTIME)
# EOF
# sub_1.py
import string # actually, some module, which is used in sub_2
string.sub_2_dictionary_size = 20 # megabytes
string.sub_2_search_depth = 5000 # nodes
# let's leave sub_2_max_responce_time default
import sub_2
print sub_2.do_the_job()
# EOF
M-a-S
"Daniel Dittmar" <daniel.dittmar at sap.com> wrote in message news:bkc62g$5k0$1 at news1.wdf.sap-ag.de...
> M-a-S wrote:
> > I'd like to parametrize a module. That is, to set and pass
>
> Create an additional module sub_2_parameters:
> # sub_1.py -- main program
> import sub_2_parameters
> sub_2_parameters.extern = "OK"
> import sub_2
>
> # sub_2.py -- parametrized module, parameter is the 'extern' var
> import sub_2_parameters
> try:
> noway = sub_2_parameters.extern
> except:
> noway = 'no extern'
> # EOF
>
> Note that modules are imported only once so you cannot import sub_2 with one
> set of parameters into one module and with another set of variables into
> another.
>
> Depending on your problem, it might be better to use the builtin execfile,
> because you can pass a dictionary with the parameters to the call.
>
> Daniel
More information about the Python-list
mailing list