- For analyzing Python code

A module of useful functions for analyzing Python code.


Returns a list of all the builtins being used in tokens.


Returns a dictionary of all dynamically imported modules (those inside of classes or functions) in the form of {<func or class name>: [<modules>]}

>>> enumerate_dynamic_modules(tokens)
{'myfunc': ['zlib', 'base64']}

Returns a list of all globally imported modules (skips modules imported inside of classes, methods, or functions).

>>> enumerate_global_modules(tokens)
['sys', 'os', 'tokenize', 're']

Returns a list of imported module methods (such as re.compile) inside tokens.


Iterates over tokens and returns a list of all imported modules.

Note: This is intelligent about the use of the ‘as’ keyword.


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']

Returns the shebang string in tokens if it exists. None if not.