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

Itxaka

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!

Sean Perry

In general this is referred to as 'refactoring'. Lots of good Python and general computer science literature out there if you look for it.

Up front based on what you have described it sounds like you need to stop using the data stored in your classes directly and start using member functions for access.

class A(object):
    def __init__(self, value):
        self.value = value

a = A(1)
print a.value
a.value = 5
print a.value

See how I modified value directly? This can lead to all kinds of assumptions in code. You are better off with using methods to retrieve values and make changes. Note, mindless getFoo/setFoo is also bad. Try to have your methods expose behavior not knobs to twist.

class A(object):
    def __init__(self, value):
        self.value = value

    def is_safe(self):
        if value > 10:
            return False
        return True

    def increase_value(self, increment):
        if increment <= 0:
            raise ValueError("only positive increments are allowed")
        self.value += increment

See how the logic is self contained? We never want an instance of A to get smaller so we only allow increments and name the function increase to indicate this. setValue does not tell what or why.

As for modules and submodules, group matching things together. "matching" is based on your own code's needs. All of the user functions? All of the DB functions? Putting things in modules allows for the possibility of plugging in a different module with the same exposed API. Think authentication from a SQL DB or from a local text file.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is the proper way to construct a Python package/module referencing a .PYD?

From Dev

Proper way to release in Git with submodules

From Dev

What is the proper way to register signal handlers in Python?

From Dev

What is the proper way to write getter and setter in Python?

From Dev

What is the proper way to parse a Lucene Query in python?

From Dev

What is the proper way to manage multiple python versions?

From Dev

What is the proper way to use codecs' encoding in Python?

From Dev

What is the proper way to construct class, object in python

From Dev

What is the proper way to use @property in python

From Dev

Proper way to include a module in AngularJS

From Dev

Is this the proper way to use the timeit module?

From Java

What is the proper way to determine if an object is a bytes-like object in Python?

From Dev

What is the proper way to handle periodic housekeeping tasks in Python/Pyramid/CherryPy?

From Dev

what is the proper way to set a global socket timeout in python?

From Dev

What's the proper way to break nested function/constructor calls in Python?

From Dev

What is the proper way to print a nested list with the highest value in Python

From Dev

What is the proper way to trigger an action after value assignment in Python?

From Dev

What is the proper way of sending a large amount of data over sockets in Python?

From Dev

What is the proper way to ACTUALLY SEND mail from (Python) code?

From Dev

What is the proper way to install an application with the Python packet manager (pip)?

From Dev

How to not import imported submodules in a Python module?

From Dev

What is the proper way to wait for connections?

From Dev

What is the proper way to wait for connections?

From Dev

What is the proper way to use continue?

From Dev

What is a proper way to create argument var names in a module for a new Class creation?

From Dev

what is the proper way to publish a React component module in NPM without external dependencies inside?

From Dev

What is the proper way to transfer a file over a network with the shutil.copy module?

From Dev

Proper way to split big thrift file

From Dev

Proper way to extend Python class

Related Related

  1. 1

    What is the proper way to construct a Python package/module referencing a .PYD?

  2. 2

    Proper way to release in Git with submodules

  3. 3

    What is the proper way to register signal handlers in Python?

  4. 4

    What is the proper way to write getter and setter in Python?

  5. 5

    What is the proper way to parse a Lucene Query in python?

  6. 6

    What is the proper way to manage multiple python versions?

  7. 7

    What is the proper way to use codecs' encoding in Python?

  8. 8

    What is the proper way to construct class, object in python

  9. 9

    What is the proper way to use @property in python

  10. 10

    Proper way to include a module in AngularJS

  11. 11

    Is this the proper way to use the timeit module?

  12. 12

    What is the proper way to determine if an object is a bytes-like object in Python?

  13. 13

    What is the proper way to handle periodic housekeeping tasks in Python/Pyramid/CherryPy?

  14. 14

    what is the proper way to set a global socket timeout in python?

  15. 15

    What's the proper way to break nested function/constructor calls in Python?

  16. 16

    What is the proper way to print a nested list with the highest value in Python

  17. 17

    What is the proper way to trigger an action after value assignment in Python?

  18. 18

    What is the proper way of sending a large amount of data over sockets in Python?

  19. 19

    What is the proper way to ACTUALLY SEND mail from (Python) code?

  20. 20

    What is the proper way to install an application with the Python packet manager (pip)?

  21. 21

    How to not import imported submodules in a Python module?

  22. 22

    What is the proper way to wait for connections?

  23. 23

    What is the proper way to wait for connections?

  24. 24

    What is the proper way to use continue?

  25. 25

    What is a proper way to create argument var names in a module for a new Class creation?

  26. 26

    what is the proper way to publish a React component module in NPM without external dependencies inside?

  27. 27

    What is the proper way to transfer a file over a network with the shutil.copy module?

  28. 28

    Proper way to split big thrift file

  29. 29

    Proper way to extend Python class

HotTag

Archive