Python Forum
modifying what a module defines
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
modifying what a module defines
#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


Messages In This Thread
modifying what a module defines - by Skaperen - Aug-22-2019, 10:02 PM
RE: modifying what a module defines - by fishhook - Aug-23-2019, 06:39 AM
RE: modifying what a module defines - by Skaperen - Aug-24-2019, 07:26 PM
RE: modifying what a module defines - by newbieAuggie2019 - Aug-25-2019, 10:17 AM
RE: modifying what a module defines - by fishhook - Aug-26-2019, 06:29 AM
RE: modifying what a module defines - by Skaperen - Aug-26-2019, 07:34 PM
RE: modifying what a module defines - by Skaperen - Aug-28-2019, 07:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  input defines variable but it's not defined tozqo 5 7,384 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