How to extract functions used in a python code file?

前端 未结 2 1374
陌清茗
陌清茗 2021-02-06 00:32

I would like to create a list of all the functions used in a code file. For example if we have following code in a file named \'add_random.py\'

`

import          


        
2条回答
  •  旧时难觅i
    2021-02-06 01:06

    In general, this problem is undecidable, consider for example getattribute(random, "random")().

    If you want static analysis, the best there is now is jedi

    If you accept dynamic solutions, then cover coverage is your best friend. It will show all used functions, rather than only directly referenced though.

    Finally you can always roll your own dynamic instrumentation along the lines of:

    import random
    import logging
    
    class Proxy(object):
        def __getattr__(self, name):
            logging.debug("tried to use random.%s", name)
            return getattribute(_random, name)
    
    _random = random
    random = Proxy()
    

提交回复
热议问题