Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way to split a python module into submodules? [closed]

Tags:

python

module

I am working in a gitlab API library in python. It started as a pet project to understand more about modules so I didn't think about it at the start and started writing all the code in the __init__.py inside the module dir.

Of course, now that is has grown, I can see that the organization is quite poor, there is way to much method for one class, testing has become difficult, checking the code is quite confusing as there is a lot in there.

So I have thougth of splitting it in several submodules, you know the tipical from X import Y so the code is more readable, testeable, smaller but I have found that I have absolutely no idea how to implement it as I got plenty of shared variables and such all over the class with all methods using class variables....

So, is there any good documentation on creating python modules with "submodules"? How to share objects variables between classes? Any clear modules that I should be checking that have the login on one submodule and the functions in other, for example?

Cheers!

like image 941
Itxaka Avatar asked Oct 20 '22 19:10

Itxaka


1 Answers

This question has been asked a lot and the problem with this kind of question is it will be rather subjective (at best). Sure there are lots of Best Practices such as:

  • Logical grouping of functionality into modules
  • Keeping your modules terse and not too complex
  • Designing a sensible API

etc...

If you find it confusing, difficult to use, chances are others will too. Test your API and design on your co-workers and friends. Get their view of how it feels ot use your library.

Some useful references:

  • Should I create each class in its own .py file?
  • http://docs.python-guide.org/en/latest/writing/structure/
  • Folder and file organization for Python development
like image 93
James Mills Avatar answered Oct 23 '22 10:10

James Mills