Get meta info about python modules

Get extra info about a python module, like imports, functions called inside other funcs etc.

Installation

pip install py-module-info

Why use this?

Well Idk, the inspect module in python requires you to import the modu…


This content originally appeared on DEV Community and was authored by Adwaith Rajesh

Get extra info about a python module, like imports, functions called inside other funcs etc.

Installation

pip install py-module-info

Why use this?

Well Idk, the inspect module in python requires you to import the module.
This package does not require you to import, but rather just looks at the python file like a normal text file and parses it. The problem with importing is that it may execute code that can harm your PC. So maybe ?

What does it do?

Get info about the imports used

# test.py

import json
import pprint as p
from typing import List as l
from json import load, dump as d
from py_module_info.main import ModuleInfo

To get the imported names use

from py_module_info import ModuleInfo
m = ModuleInfo("test.py")
imports = m.get_imports()
print(imports.get_imported_names())

# output
['json', 'pprint', 'List', 'load', 'dump', 'ModuleInfo']

# in order to get the alias names used
print(imports.get_imported_names(use_alias=True))
# output
['json', 'p', 'l', 'load', 'd', 'ModuleInfo']

To get the literal import string in the module use

from py_module_info import ModuleInfo
m = ModuleInfo("test.py")
imports = m.get_imports()
print(imports.get_import_strings())

# output
['import json', 'import pprint', 'from typing import List',
'from json import load', 'from json import dump', 'from py_module_info.main import ModuleInfo']

# use alias names insted
print(imports.get_import_strings(use_alias=True))

# output
['import json', 'import pprint as p', 'from typing import List as l',
'from json import load', 'from json import dump as d', 'from py_module_info.main import ModuleInfo']

Note

  • The imports will be split into individual imports:
 import test, json
  • will become
import test
import json

Get info about the function in the module.

# test.py

import json
from pprint import pprint

def foo():
    pprint({"Hello": "World"})


    with open("test.json") as f:
        print(json.load(f))

To get info about the functions use

from py_module_info import ModuleInfo

m = ModuleInfo("test.py")
print(m.get_funcs_info())

# output
{'foo': {'args': [], 'defaults': [], 'arg_count': 0, 'calls': ['json.load(f)', 'open("test.json")', 'pprint({"Hello": "World"})', 'print(json.load(f))']}}

The output includes the args passed in function, the defaults value for the the arguments, the argument count, and all the function calls that happened inside the function.

The function calls are all expanded and includes the entire call string.

In order to get just the function calls use

from py_module_info import ModuleInfo

m = ModuleInfo("test.py")
print(m.get_funcs_info(only_func_name=True))

# output
{'foo': {'args': [], 'defaults': [], 'arg_count': 0, 'calls': ['load', 'open', 'pprint', 'print']}}

A way to get the same data from classes will be added in the future version.


This content originally appeared on DEV Community and was authored by Adwaith Rajesh


Print Share Comment Cite Upload Translate Updates
APA

Adwaith Rajesh | Sciencx (2021-06-10T03:39:01+00:00) Get meta info about python modules. Retrieved from https://www.scien.cx/2021/06/10/get-meta-info-about-python-modules/

MLA
" » Get meta info about python modules." Adwaith Rajesh | Sciencx - Thursday June 10, 2021, https://www.scien.cx/2021/06/10/get-meta-info-about-python-modules/
HARVARD
Adwaith Rajesh | Sciencx Thursday June 10, 2021 » Get meta info about python modules., viewed ,<https://www.scien.cx/2021/06/10/get-meta-info-about-python-modules/>
VANCOUVER
Adwaith Rajesh | Sciencx - » Get meta info about python modules. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/10/get-meta-info-about-python-modules/
CHICAGO
" » Get meta info about python modules." Adwaith Rajesh | Sciencx - Accessed . https://www.scien.cx/2021/06/10/get-meta-info-about-python-modules/
IEEE
" » Get meta info about python modules." Adwaith Rajesh | Sciencx [Online]. Available: https://www.scien.cx/2021/06/10/get-meta-info-about-python-modules/. [Accessed: ]
rf:citation
» Get meta info about python modules | Adwaith Rajesh | Sciencx | https://www.scien.cx/2021/06/10/get-meta-info-about-python-modules/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.