Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a tool that removes functions that are not used in Python?

Tags:

python

I have the following situation: I am working on several projects which make use of library modules that I have written. The library modules contain several classes and functions. In each project, some subset of the code of the libraries is used.

However, when I publish a project for other users, I only want to give away the code that is used by that project rather than the whole modules. This means I would like, for a given project, to remove unused library functions from the library code (i.e. create a new reduced library). Is there any tool that can do this automatically?

EDIT

Some clarifications/replies:

  1. Regarding the "you should not do this in general" replies: The bottom line is that in practice, before I publish a project, I manually go through the library modules and remove unused code. As we are all programmers, we know that there is no reason to do something manually when you could easily explain to a computer how to do it. So practically, writing such a program is possible and should even not be too difficult (yes, it may not be super general). My question was if someone know whether such a tool exists, before I start thinking about implementing it by myself. Also, any thoughts about implementing this are welcome.
  2. I do not want to simply hide all my code. If I would have wanted to do that I would have probably not used Python. In fact, I want to publish the source code, but only the code which is relevant to the project in question.
  3. Regarding the "you are legally protected" comments: In my specific case, the legal/license protection does not help me. Also, the problem here is more general than some stealing the code. For example, it could be for the sake of clarity: if someone needs to use/develop the code, you don't want dozens of irrelevant functions to be included.
like image 248
Bitwise Avatar asked Jun 13 '13 13:06

Bitwise


2 Answers

My first advice to you would be to design your code with a stronger modularity, so that you can have all the functions you want to keep in as many python modules/eggs as you have to make it flexible to have just what you need for each of your projects. And I think that would be the only way to keep your code easily manageable and readable.

That said, if you really want to go the way you describe in your question, to my knowledge there's no tool that does exactly what you say, because it's an uncommon usage pattern.

But I don't think it would be hard to code a tool that does it using rope. It does static and dynamic code analysis (so you can find what imported objects are being used thus guess what is not used from your imported modules), and also gives many refactoring tools to move or remove code.

Anyway, I think to be able to really make a tool that find accurately all code that is being used in your current code, you need to make a full unit test coverage of your code or you shall be really methodical in how you import your module's code (using only from foo import bar and avoiding chaining imports between the modules).

like image 196
zmo Avatar answered Nov 05 '22 09:11

zmo


I agree with @zmo - one way to avoid future problems like this is to plan ahead and make your code as modular as possible. I would have suggested putting the classes and functions in much smaller files. This would mean that for every project you make, you would have to hand-select which of these smaller files to include. I'm not sure if that's feasible with the size of your projects right now. But for future projects it's a practice you may consider.

like image 34
mad-hay Avatar answered Nov 05 '22 10:11

mad-hay