2

Suppose there is such a class in my program. How can I determine which of these functions are used, how do I record the detected ones and how do I get them to work when I run the program again? I really researched this topic. I really tried hard. I have been researching this for maybe a month. I have no place other than here to get help and understand this. I wanted to make this statement in order not to be misunderstood. I just want to understand the point.Thank you in advance for your help.

class DataCleanAndMerge:
    
    def __init__(self):
        self.fnumb = 1
        self.snumb = 1
    
    def plus(self):
        return fnumb + snumb
    
    def equals(self):
        return fnumb == snumb
sabcan
  • 407
  • 3
  • 15

1 Answers1

2

From another question, this may help:

import inspect

functions_used = []

class DataCleanAndMerge:
    
    def __init__(self):
        self.fnumb = 1
        self.snumb = 1
    
    def plus(self):
        func_name = inspect.currentframe().f_code.co_name
        if func_name not in functions_used:
            functions_used.append(func_name)
        return self.fnumb + self.snumb
    
    def equals(self):
        func_name = inspect.currentframe().f_code.co_name
        if func_name not in functions_used:
            functions_used.append(func_name)
        return self.fnumb == self.snumb

And if you want to save that information, even after execution is finished, just keep the data in a file. You can read it into a list at the beginning, and then write the list to the file when you're done:

import inspect
import os.path

class DataCleanAndMerge:
        
    def __init__(self):
        self.fnumb = 1
        self.snumb = 1
        
    def plus(self):
        func_name = inspect.currentframe().f_code.co_name
        if func_name not in functions_used:
            functions_used.append(func_name)
        return self.fnumb + self.snumb
        
    def equals(self):
        func_name = inspect.currentframe().f_code.co_name
        if func_name not in functions_used:
            functions_used.append(func_name)
        return self.fnumb == self.snumb


if __name__ == '__main__':

    functions_used = []
    methods_file = "methods_used.txt"
# Check if file exists. Create it if needed.
    if not os.path.exists(methods_file):
        with open(methods_file, "w") as f: pass
    with open(methods_file, "r") as fin:
        for x in ' '.join(fin.read().split('\n')).split():
            functions_used.append(x)
    print('Functions Used: {}'.format(functions_used))
    c = DataCleanAndMerge()
    if len(functions_used)>0:
        c.equals()
    c.plus()
    print('Functions Used: {}'.format(functions_used))
    with open(methods_file, "w") as fout:
        for x in functions_used:
            fout.write("{}\n".format(x))

There are a few other options, but this seems to be the safest way to get a list of functions used. Of course, if you are trying to get the list without actually editing the class and its methods, this will not be of much use.

mole1000
  • 161
  • 6
  • thank you so much. I will do my work on the inspect module. I will follow this path.After registering the methods, I will run it with exec. I understood the logic. I will develop in this direction. Thank you very much for your help. – sabcan Apr 06 '21 at 20:13