Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Adwaith Rajesh
Adwaith Rajesh

Posted on • Edited on

     

Get meta info about python modules

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


Installation

pipinstallpy-module-info
Enter fullscreen modeExit fullscreen mode

Why use this?

Well Idk, theinspect 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.pyimportjsonimportpprintaspfromtypingimportListaslfromjsonimportload,dumpasdfrompy_module_info.mainimportModuleInfo
Enter fullscreen modeExit fullscreen mode

To get the imported names use

frompy_module_infoimportModuleInfom=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 usedprint(imports.get_imported_names(use_alias=True))# output['json','p','l','load','d','ModuleInfo']
Enter fullscreen modeExit fullscreen mode

To get the literal import string in the module use

frompy_module_infoimportModuleInfom=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 instedprint(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']
Enter fullscreen modeExit fullscreen mode

Note

  • The imports will be split into individual imports:
importtest,json
Enter fullscreen modeExit fullscreen mode
  • will become
importtestimportjson
Enter fullscreen modeExit fullscreen mode

Get info about the function in the module.

# test.pyimportjsonfrompprintimportpprintdeffoo():pprint({"Hello":"World"})withopen("test.json")asf:print(json.load(f))
Enter fullscreen modeExit fullscreen mode

To get info about the functions use

frompy_module_infoimportModuleInfom=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))']}}
Enter fullscreen modeExit fullscreen mode

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

frompy_module_infoimportModuleInfom=ModuleInfo("test.py")print(m.get_funcs_info(only_func_name=True))# output{'foo':{'args':[],'defaults':[],'arg_count':0,'calls':['load','open','pprint','print']}}
Enter fullscreen modeExit fullscreen mode

Get info about the classes in a module

# test.pyclassFoo(A.Test):def__init__(self,name):self.name=namedefn():passclassBar():defa():print('Hello')
Enter fullscreen modeExit fullscreen mode

To get meta info about the classes in the module use

frompy_module_infoimportModuleInfom=ModuleInfo("test.py")print(m.get_classes_info())# output{'Foo':{'bases':['A.Test'],'methods':{'__init__':{'args':['self','name'],'defaults':[],'arg_count':2,'calls':[]},'n':{'args':[],'defaults':[],'arg_count':0,'calls':[]}}},'Bar':{'bases':[],'methods':{'a':{'args':[],'defaults':[],'arg_count':0,'calls':["print('Hello')"]}}}}
Enter fullscreen modeExit fullscreen mode

Currently the output includes the information about the base classes and the info about different methods in the class.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

A programmer by hobby. Loves tech and opensource. Python enthusiast.
  • Location
    Kannur Kerala
  • Education
    Sophomore
  • Work
    Freelancer
  • Joined

More fromAdwaith Rajesh

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp