python-flavin - minimizing imports
Posted: Sat Apr 19, 2014 10:32 pm
Python-Flavin is designed to take your final .py file and create a new copy of it containing only the minimal set of classes/functions/variables used.
I.e. its specifically useful for microcontrollers where unused code takes up valuable room.
Its designed to follow all nested imports as deeply as possible and prune away unused code.
It only works on modules(imports) written in python. Modules implemented in C do not get pruned.
Its not designed to do this perfectly but instead to attempt to remove all the unused aspects of imported modules.
I started this project a while back. I'm getting back into it and writing about it here.
E.g. if you imported the collection module because you wanted ordered dictionaries - then you want to remove all the other functions in that module that you aren't using. You also want to traverse the entire set of nested imports to prune away functions that collections (say) might import so it can do its work.
The process works like this:
- A modified version of vulture traverses all import hierarchies and then processes each file determining the unused functions, classes, and variables. (Vulture uses the wonderful ast (abstract syntax trees) module to parse the python code directly.)
- A modified version of Rope (a python refactoring library) will then be used to extract/move and rename the imports to minimize the code.
Currently the implementation is just starting and can:
- identify the unused functions and variables in the nested import tree.
- create copies of the imports ready for minimizing.
No code submitted yet on the refactoring, and not all import syntaxes can be processed yet.
I will update info here as time progresses.
https://github.com/Neon22/python-flavin
I.e. its specifically useful for microcontrollers where unused code takes up valuable room.
Its designed to follow all nested imports as deeply as possible and prune away unused code.
It only works on modules(imports) written in python. Modules implemented in C do not get pruned.
Its not designed to do this perfectly but instead to attempt to remove all the unused aspects of imported modules.
I started this project a while back. I'm getting back into it and writing about it here.
E.g. if you imported the collection module because you wanted ordered dictionaries - then you want to remove all the other functions in that module that you aren't using. You also want to traverse the entire set of nested imports to prune away functions that collections (say) might import so it can do its work.
The process works like this:
- A modified version of vulture traverses all import hierarchies and then processes each file determining the unused functions, classes, and variables. (Vulture uses the wonderful ast (abstract syntax trees) module to parse the python code directly.)
- A modified version of Rope (a python refactoring library) will then be used to extract/move and rename the imports to minimize the code.
Currently the implementation is just starting and can:
- identify the unused functions and variables in the nested import tree.
- create copies of the imports ready for minimizing.
No code submitted yet on the refactoring, and not all import syntaxes can be processed yet.
I will update info here as time progresses.
https://github.com/Neon22/python-flavin