Python Forum
informatio from importer to importee
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
informatio from importer to importee
#1
i have a module that defines some functions that aid debugging. it checks an environment variable. if that environment variable is defined, it defines the functions to do their thing, which for most involves printing. if that environment variable is not defined then the functions are defined to do nothing and return so that the script can be run without any debugging output to easily see if the output is correct.

what i would like to have is a way for the code doing the importing to specify which environment variable is to be checked when it does the check to determine what it will define. however, since the debugging module comes from a different file, it does not share either global or local namespaces with the code doing the importing. that means this will not be as simple as setting a global variable and checking it. i also would like to have a way for the script to force the debugging functions to be enabled regardless of any environment variable.

the workaround idea i have it to set another environment variable in the script before it imports the module, and have the module check this other environment variable to get the name to use.

so my questions are: how safe is this? what kinds of issues do you see in this? is there a better alternative?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
I think it would be better to have a set_up function in the module. The then importing module can call improtee.set_up() and pass whatever information is needed to it as parameters, and importee.set_up() can set up the debugging functions and start them doing what they need to do.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
if i do it with a setup function, how will the importee/module define things in the importer's global space?

currently with the importer not needing to change anything, the import is done like this:
from debugtoys import pf,pr,pv,sl
and if it needs to force the debugging to be enabled it does:
os.environ['debugtoys']='True'
from debugtoys import pf,pr,pv,sl
i'm guessing what i will need to do is:
import debugtoys
pf,pr,pv,sl = debugtoys.set_up(eval(os.environ['enable_debug']))
is that the kind thing you are suggesting? i want to keep the default case especially simple.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
i was recoding the debugtoys module to work with a setup function. then i decided that instead of passing an environment variable name, i would expand the flexibility and pass the result of whatever test the importer wanted to be used, which might not be from environment variables at all. then i was thinking that logic could go around the original import like:
if eval(os.environ['ROSES_ARE_RED']):
    from debugtoys import pf,pr,pv,sl
else:
    def pf(*a,**o): return
    def pr(*a,**o): return
    def pv(*a,**o): return
    def sl(*a,**o): return
or maybe not. this setup function could also provide the dummy functions when the debugging is disabled. so i am recoding it that way now.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
(Nov-19-2018, 12:15 AM)Skaperen Wrote: if i do it with a setup function, how will the importee/module define things in the importer's global space?

With a return value, just like you showed. Though I was thinking something much simpler:

import debugtoys
pf,pr,pv,sl = debugtoys.set_up(debug = True)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Custom importer and errors Fips 6 398 Apr-14-2024, 02:34 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020