M
M
Max Payne2018-04-18 02:05:34
Python
Max Payne, 2018-04-18 02:05:34

How to correctly distribute class methods into modules?

And so, there is a project. Roughly speaking, 4 py files. 2 files of static settings, and 2 files with classes inside. One file is launched, in which several instances of the class from the second file are created, and the first one then multiprocesses with them: Based on some data sent, it takes some function from the second class and executes it with the necessary arguments.
There is an idea to divide the second class (~220 kb) into modules due to the redundancy of methods in it and the lack of logic inside the class as such.
The class contains methods that can be divided into several types
1.1 Class methods that can be made static and taken out of the class without problems.
1.2 Class methods using the methods from paragraph 1.1
1.3 Class methods using methods from paragraph 1.1 or 1.2 + using class arguments.
As I see the solution:
Split the class into two modules, commands and tools. Tools will contain all the methods from paragraph 1.1 and other side methods, and commands will contain exactly the functions that the program performs.
BUT!
What, in that case, to do with the functions using class arguments?
How, besides sys.path, can I link the commands and tools directories, because the modules in tools are not visible from commands? (It is solved by placing tools inside commands, or by creating tools.py with a set of functions, but both options seem wrong - suddenly you need from the first class call, prescribe from commands.tools.db import db looks somehow dumb, like from commands.tools import db)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2018-04-19
@YardalGedal

if you have broken functions into three parts, then why are you making two modules? Logically, you need three static function modules, class 1.2 and class 1.3.
But without knowing what's going on in the classes, it's hard to break it down into modules normally. Rather, you need to act like this:
1. Imagine what interface the initial class 2 should have. Those. what actions he should perform in the outside world. These will be public methods, so to speak. the rest are private.
2. Present the structure of the 2nd class one level less. Those. all methods are divided into topics (more or less independent). These will be the classes of the next level of abstraction. Class 2 will be responsible for creating these classes and installing dependencies between them.
3. Next, repeat steps 1 and 2 for each subclass. etc.
But I repeat it is difficult to speak in general, not knowing what is there in the original class.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question