Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Detect code which gets never executed in production

I need to do refactoring in a big legacy Python code base.

Often I think "these lines don't get executed in production any more".

But I am unsure.

There are some tests which touch these lines. But I can't tell for sure if really no usage happens in production.

What can I do in this situation?

This question is about coverage on a production system. This question is not about coverage during testing/CI.

I don't want to comment out that lines, since I don't want to produce an error in the production system.

like image 665
guettli Avatar asked May 07 '18 07:05

guettli


2 Answers

Common practice is to use logging inside that lines of code. e.g. you have a block of code you think is not in use. You add try catch block in the beginning of that block of code. Inside trycatch you add line to a specific json named same as your suspicious block of code.

try:
    with open("block1.dat", "rb") as file:
        activity = pickle.load(file)

    curtime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    currentact = "dt = {}; code done that: var1 = {}, 
        var2 = {}".format(curdate, var1, var2)

    activity.append(currentact)

    file = open("block1.dat", "ab")
    pickle.dump(activity, file)
    file.close()
except Exception: pass

You can use telegram api to log code to. After a while You'll get info how often your code works and what does it do. Then you monitor for a while and if nothing happens in a month, You can comment the block.

like image 130
Sergey Zaitsev Avatar answered Dec 02 '22 22:12

Sergey Zaitsev


Is the production system deterministic? Is it interactive? Does control flow depend on input data? Do you have access to all possible inputs? Do the tests exist for a reason or just because?

I'd be careful removing code based on what is needed based on logging unless I knew there are no exceptional situations that occur rarely.

I would follow the common code paths to try to understand the codebase piece by piece in order to figure out what can be simplified. It's hard to give more specific advice without knowing more about the system you're dealing with.

like image 33
Jari Komppa Avatar answered Dec 02 '22 22:12

Jari Komppa