Python: Detect code which gets never executed in production

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.


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.