I'm writing an application for scientific data analysis and I'm wondering what's the best way to structure the code to avoid (or address) the circular import problem. Currently I'm using a mix of OO and procedural programming.
Other questions address this issue but in a more abstract way. Here I'm looking for a solution that is optimal in a more specific context.
I have a class Container
defined in DataLib.py
whose data consist in lists and/or arrays. With all methods and supporting functions DataLib.py
is quite large (~1000 lines).
I have a second module SelectionLib.py
(~400 lines) that contains only functions to "filter" the data in Container
according to different criteria. These functions return new Container
objects (with filtered data) and thus SelectionLib.py
needs to import Container
from DataLib.py
. Note that, logically, these functions are "methods" for "Container", they are just implemented using python functions.
Now, I want to add some high level method to Container
so that a complex analysis can be performed with a single function of method call. And by "complex analysis" I mean an arbitrary number of Container
methods call, local function (defined in DataLib.py) and filter functions (defined inSelectionLib.py
).
So the problem is that DataLib.py
needs to import SelectionLib.py
to use the filter functions, but SelectionLib.py
already imports DataLib.py
.
Right know my hackish solution is to run the two files with run -i ... from IPython so it is like having a big single file and I avoid the circular import. But at the same time this scripts are difficult to integrate for example in a GUI.
How do you suggest to solve this problem:
use pure OO and inheritance and split the object in 3:
CoreContainer
->SelectionContainer
->HighLevelContainer
Restructuring the code (everything in one file?)
Some sort of Import trickery (put imports at the end)
Any feedback is appreciated!