Python Forum
modifying what a module defines
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
modifying what a module defines
#1
i have a module that defines some classes and functions. i want to also include some debug tools in it as well. my goal is to get everything in one file. but i don't want the debug tools to be defined every time the module is imported. i'd like to have a way that the code that is importing this module can indicate if it wants the debug tools, too. is there any way to do this within Python3? i'm looking for alternatives to setting (in the code doing the import) and getting (in the module) an environment variable. any suggestions?
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
modules are just regular python's objects, you can set and get attributes via setattr/getattr functions
Reply
#3
(Aug-22-2019, 10:02 PM)Skaperen Wrote: i have a module that defines some classes and functions. i want to also include some debug tools in it as well. my goal is to get everything in one file. but i don't want the debug tools to be defined every time the module is imported. i'd like to have a way that the code that is importing this module can indicate if it wants the debug tools, too. is there any way to do this within Python3? i'm looking for alternatives to setting (in the code doing the import) and getting (in the module) an environment variable. any suggestions?

Hi!

Not sure if I could help you, as it seems that with so many posts, you probably know much more than I do...

I'm learning and I was following the instructions to build a calculator, so I needed to import a module called something like calc_functions.py, but while I was learning how to build the calculator, I needed to define some functions to work with the function keys on the calculator, as a factorials key, a number base 10 to Roman numbers key, or a number base 10 to binary key, but it was not working as the calc_functions file had everything already done there, so I made another file, that I called myCalc_functions.py and defined there my functions. Of course, I had to call this new file, instead of the other one, to be able to work. Even now, I created a myCalc_functions_02.py where I change a very long tuple and dictionary into a tuple of tuples.

Okay, what I mean is, if you haven't a better way, couldn't you just have 2 modules files that you could import, one with the debug tools and another one without them, and choose to import one or the other file at the beginning of the program and made the necessary modifications?

Well, it was just a thought...

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#4
my thoughts have already gone over that. i have already done that. the need in this case is about distributing the module as a single file. my tentative solution is for the module to define a function that can be called, after the module is imported, to make the changes to enable the debugging tools. but it would be a large and complicated function, so i am still hoping for another solution.
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
(Aug-24-2019, 07:26 PM)Skaperen Wrote: the need in this case is about distributing the module as a single file. my tentative solution is for the module to define a function that can be called, after the module is imported, to make the changes to enable the debugging tools.
Hi again!

I tried and I failed, trying to import the module later on in the program, as it seems it has to be imported from the very first line(s) of the program, so I tried for other solutions.

It seems that you can call for a module as a script, but you have to make some changes to it. To show how I made it, I'm going to show you first the module I wrote (based on many sites I checked). In this module, I define a print_eureka() function that prints 3 times the message "Eureka!", if it is called. This means that you could also called the debugging tools file, as an option, like I did:

# eurekaprinter.py

# Defining what the print_eureka() is going to do:
multiply = 3
def print_eureka():
    print("Eureka!" * multiply)

# This added script makes the print_eureka() function from the eurekaprinter module to be called on:
if __name__ == '__main__':
    print_eureka()
So, that file would be the module to be called (in this case, eurekaprinter). To the original module, you have to add (as I have already done, and shown at the bottom of the file):
# This added script makes the print_eureka() function from the eurekaprinter module to be called on:
if __name__ == '__main__':
    print_eureka()
where print_eureka() corresponds to the function you have defined.

Now, to show how it can be optionally requested inside a program, I have written the following program:

# final_program_example.py
#
# CURRENT STATE OF THE PROGRAM:
#
# It works! Eureka!
#


import eurekaprinter
option=input("\nPlease, press the: \n\n'1' key, and then the 'ENTER' ('RETURN') key,\
 if you want to work WITHOUT IMPORTING THE EUREKAPRINTER module file.\n\n'2' key, and then the 'ENTER' ('RETURN') key,\
 if you want to work IMPORTING THE EUREKAPRINTER module file.\n")
while option != "1" and option != "2":
    option = input("\nSorry. You must type in one of the numbers '1', or '2'.\n1 or 2:\n")

if option == "1":
    print("\nOkay, you have decided to work WITHOUT THE EUREKAPRINTER.\n\n")

if option == "2":
    print("\nOkay, you have decided to work WITH THE EUREKAPRINTER.\n\n")
    eurekaprinter.print_eureka()  


print(u'\u21e7'+ " Up here it should have appeared a message three times , if you have chosen option 2,\
(i.e., you have decided to work WITH THE EUREKAPRINTER file), because we\
 are calling the print_eureka() function that is inside the module we have chosen to use\
 (like calling the debugging tools inside the module we have chosen to use).")
print("\nThis is a test. If you have decided to work WITHOUT THE EUREKAPRINTER module file,\
(you have chosen option 1), it won't produce an error\
 as it is an optative (not necessary) file like your debugging tools file.\
 But it will give you an error, as\
 it won't execute the print_eureka() function, if we need it (as it won't use the debugging\
 tools file having chosen option 1).\n\n\
So, IN THEORY, with option 2, you could import the module as a script.\
  It seems that this is possible, so option 2 will give you three times a message.")
And now, some inputs and outputs:

Output:
Please, press the: '1' key, and then the 'ENTER' ('RETURN') key, if you want to work WITHOUT IMPORTING THE EUREKAPRINTER module file. '2' key, and then the 'ENTER' ('RETURN') key, if you want to work IMPORTING THE EUREKAPRINTER module file. a Sorry. You must type in one of the numbers '1', or '2'. 1 or 2: 4 Sorry. You must type in one of the numbers '1', or '2'. 1 or 2: 1 Okay, you have decided to work WITHOUT THE EUREKAPRINTER. ⇧ Up here it should have appeared a message three times , if you have chosen option 2,(i.e., you have decided to work WITH THE EUREKAPRINTER file), because we are calling the print_eureka() function that is inside the module we have chosen to use (like calling the debugging tools inside the module we have chosen to use). This is a test. If you have decided to work WITHOUT THE EUREKAPRINTER module file,(you have chosen option 1), it won't produce an error as it is an optative (not necessary) file like your debugging tools file. But it will give you an error, as it won't execute the print_eureka() function, if we need it (as it won't use the debugging tools file having chosen option 1). So, IN THEORY, with option 2, you could import the module as a script. It seems that this is possible, so option 2 will give you three times a message.
As you can see, you can enter only '1' or '2' to choose from. If it is '1'. you have decided to work WITHOUT the optional module, but it won't give you any error, unless you decide to use a function from that module (like it would give you an error, if you ask to debug without having installed the module).

More inputs and outputs:

Output:
Please, press the: '1' key, and then the 'ENTER' ('RETURN') key, if you want to work WITHOUT IMPORTING THE EUREKAPRINTER module file. '2' key, and then the 'ENTER' ('RETURN') key, if you want to work IMPORTING THE EUREKAPRINTER module file. b Sorry. You must type in one of the numbers '1', or '2'. 1 or 2: 7 Sorry. You must type in one of the numbers '1', or '2'. 1 or 2: 2 Okay, you have decided to work WITH THE EUREKAPRINTER. Eureka!Eureka!Eureka! ⇧ Up here it should have appeared a message three times , if you have chosen option 2,(i.e., you have decided to work WITH THE EUREKAPRINTER file), because we are calling the print_eureka() function that is inside the module we have chosen to use (like calling the debugging tools inside the module we have chosen to use). This is a test. If you have decided to work WITHOUT THE EUREKAPRINTER module file,(you have chosen option 1), it won't produce an error as it is an optative (not necessary) file like your debugging tools file. But it will give you an error, as it won't execute the print_eureka() function, if we need it (as it won't use the debugging tools file having chosen option 1). So, IN THEORY, with option 2, you could import the module as a script. It seems that this is possible, so option 2 will give you three times a message.
Finally, we have chosen option 2, that is to say, to run the optional module. That makes print the message "Eureka!" three times. In your case, it should execute the debugging tools.

Just in case, I'll show you now, how if you have decided not to work with the eurekaprinter module (in your case, your debugging module), but then ask for a function that is insided that module, it will give you an error:

Output:
Please, press the: '1' key, and then the 'ENTER' ('RETURN') key, if you want to work WITHOUT IMPORTING THE EUREKAPRINTER module file. '2' key, and then the 'ENTER' ('RETURN') key, if you want to work IMPORTING THE EUREKAPRINTER module file. 1 Okay, you have decided to work WITHOUT THE EUREKAPRINTER. ⇧ Up here it should have appeared a message three times , if you have chosen option 2,(i.e., you have decided to work WITH THE EUREKAPRINTER file), because we are calling the print_eureka() function that is inside the module we have chosen to use (like calling the debugging tools inside the module we have chosen to use). This is a test. If you have decided to work WITHOUT THE EUREKAPRINTER module file,(you have chosen option 1), it won't produce an error as it is an optative (not necessary) file like your debugging tools file. But it will give you an error, as it won't execute the print_eureka() function, if we need it (as it won't use the debugging tools file having chosen option 1). So, IN THEORY, with option 2, you could import the module as a script. It seems that this is possible, so option 2 will give you three times a message. >>> print_eureka()
Error:
Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> print_eureka() NameError: name 'print_eureka' is not defined
Oops! I just noticed, that I didn't comment on something important (sorry for this LOOOOONG post). At the beginning of the program, you have to import the module (here, it appears on line 9), but option 1 or option 2 is what actually makes it be executed or not:
# final_program_example.py
#
# CURRENT STATE OF THE PROGRAM:
#
# It works! Eureka!
#
 
 
import eurekaprinter
option=input("\nPlease, press the: \n\n'1' key, and then the 'ENTER' ('RETURN') key,\
 if you want to work WITHOUT IMPORTING THE EUREKAPRINTER module file.\n\n'2' key, and then the 'ENTER' ('RETURN') key,\
 if you want to work IMPORTING THE EUREKAPRINTER module file.\n")
while option != "1" and option != "2":
    option = input("\nSorry. You must type in one of the numbers '1', or '2'.\n1 or 2:\n")
 
if option == "1":
    print("\nOkay, you have decided to work WITHOUT THE EUREKAPRINTER.\n\n")
 
if option == "2":
    print("\nOkay, you have decided to work WITH THE EUREKAPRINTER.\n\n")
    eurekaprinter.print_eureka()  
The proof is the above outputs.

I hope it helps,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#6
if option == "1":
    print("\nOkay, you have decided to work WITHOUT THE EUREKAPRINTER.\n\n")
  
if option == "2":
    import eurekaprinter
    print("\nOkay, you have decided to work WITH THE EUREKAPRINTER.\n\n")
    eurekaprinter.print_eureka()
Reply
#7
i ended up using the environment variable technique. it also lets me do it by setting that environment variable as i run the script.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
(Aug-26-2019, 06:29 AM)fishhook Wrote:
if option == "1":
    print("\nOkay, you have decided to work WITHOUT THE EUREKAPRINTER.\n\n")
  
if option == "2":
    import eurekaprinter
    print("\nOkay, you have decided to work WITH THE EUREKAPRINTER.\n\n")
    eurekaprinter.print_eureka()
Hi!

I was not sure what you meant with that (hey, I was so sleepy I didn't notice your line 5, where you import the eurekaprinter module), so when I noticed your line 5, I was not sure if it meant, leaving my line 9 (that's where I imported the module) in or out. I tried both ways (with my line 9 in and with my line 9 out) with your line 5, and both works, so I think your code is neater. Thanks!

All the best,

(Aug-26-2019, 07:34 PM)Skaperen Wrote: i ended up using the environment variable technique. it also lets me do it by setting that environment variable as i run the script.

Hi!

That's great! I have no idea about the environment variable technique, but I found your dilemma quite interesting, and I wanted to find out if, with my little knowledge, I could provide some thoughts or maybe a possible solution, even if it is not written with a neat piece of code.

Thanks for bringing in the problem! It was a nice challenge for me!

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#9
the module just examines specific environment variables to determine any variations on what it does, such as which functions to define. the code that imports that module can set an environment variable, or it can be set when the script is run.
import os,sys
if eval(get(os.environ,'SFCDEBUG','0')):
    def pr(*args,**kwargs):
        if 'file' not in kwargs:
            kwargs['file']=sys.stderr
        if 'flush' not in kwargs:
            kwargs['flush']=True
        return print(*args,**kwargs)
else:
    def pr(*args,**kwargs):
        return
then all those calls to pr() do nothing if SFCDEBUG was undefined or defined to something untrue, so the developer can see the script run without the extra output, before she takes all those pr() calls out (or she can just leave them in).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Modifying code cheburashka 1 1,296 Dec-13-2021, 01:01 PM
Last Post: Kebap
  Modifying Lists RedSkeleton007 12 6,818 Nov-22-2017, 04:31 AM
Last Post: heiner55
  input defines variable but it's not defined tozqo 5 7,265 Jun-05-2017, 02:45 AM
Last Post: tozqo

Forum Jump:

User Panel Messages

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