analyze.py - For analyzing Python code¶
A module of useful functions for analyzing Python code.
- pyminifier.analyze.enumerate_builtins(tokens)[source]¶
Returns a list of all the builtins being used in tokens.
- pyminifier.analyze.enumerate_dynamic_imports(tokens)[source]¶
Returns a dictionary of all dynamically imported modules (those inside of classes or functions) in the form of {<func or class name>: [<modules>]}
- Example:
>>> enumerate_dynamic_modules(tokens) {'myfunc': ['zlib', 'base64']}
- pyminifier.analyze.enumerate_global_imports(tokens)[source]¶
Returns a list of all globally imported modules (skips modules imported inside of classes, methods, or functions).
- Example:
>>> enumerate_global_modules(tokens) ['sys', 'os', 'tokenize', 're']
- pyminifier.analyze.enumerate_import_methods(tokens)[source]¶
Returns a list of imported module methods (such as re.compile) inside tokens.
- pyminifier.analyze.enumerate_imports(tokens)[source]¶
Iterates over tokens and returns a list of all imported modules.
Note: This is intelligent about the use of the ‘as’ keyword.
- pyminifier.analyze.enumerate_keyword_args(tokens)[source]¶
Iterates over tokens and returns a dictionary with function names as the keys and lists of keyword arguments as the values.
- pyminifier.analyze.enumerate_local_modules(tokens, path)[source]¶
Returns a list of modules inside tokens that are local to path.
Note: Will recursively look inside path for said modules.
- pyminifier.analyze.enumerate_method_calls(tokens, modules)[source]¶
Returns a list of all object (not module) method calls in the given tokens.
modules is expected to be a list of all global modules imported into the source code we’re working on.
- For example:
>>> enumerate_method_calls(tokens) ['re.compile', 'sys.argv', 'f.write']