Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid executing entire python script when a function is called without main after importing to another python script

Tags:

python

I have a python script which goes like this, which I'm NOT allowed to modify:

#file1.py

def add(a, b):
    print(a + b)

add(1, 2)
#no main function whatsoever

and I HAVE TO IMPORT file1.py in my source code and use the function, I'm not allowed to rewrite the same function on my own.

So, my code goes like this:

#my_code.py

import file1
#or
#from file1 import add

def main():     
    file1.add(1, 3)

if __name__ == '__main__':
    main()

which, Obviously results in:

3
4

Is there any possible way to simply invoke the function and return, without letting that add(1, 2) in file1.py being invoked ?

I simply need the output on console of the function I invoke in my main function, not the file1.py one.

The current make-shift thing I'm doing is:

import os
import file1
os.system('cls')

#rest of the code follows

It's not very efficient or the brightest idea, but it's enough to get my work done and get my assignment checked.

like image 340
JustAnotherDev Avatar asked Oct 16 '25 19:10

JustAnotherDev


2 Answers

Not sure if this is any better than the cls method you have but: overwite print, load the module, reset print.

import builtins

old_print = print
def p(x):
    pass
builtins.print = p
import file1
builtins.print = old_print

def main():     
    file1.add(1, 3)

if __name__ == '__main__':
    main()
like image 79
001 Avatar answered Oct 18 '25 13:10

001


If you can't modify a file that executes actions directly without guadring it by checking the __name__ variable, there is nothing you can do, short of copying that file to another one you can modify, and modify that one.

This process of copying the file, and disabling the function calls can even be automated - but as you can see, it is a modification on the source code.

Short of it, maybe (and I said -maybe-) a customization of the import mechanism prior to importing the target file could modify its code prior to execution, by suppressing toplevel calls to locally defined functions, for example, or name mangling the functions and injecting mock functions into the namespace, or a similar mechanism - but that is hard to do and error prone - prefer copying the original file and fixing it instead.

You have to understand that you describe a case in which the original file is essentially hard coded to run the function, and nothing short of changing it, be it manually editing, or programatically changing what the file does, can change that. And that the doing this: automatically changing what the file does, is just "changing the original file" the same way, just automated, and a task that can be very complicated to automate.

like image 20
jsbueno Avatar answered Oct 18 '25 14:10

jsbueno



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!