Sunday, 24 April 2022

Python module implemented like a package

I have an application containing a package, thus:

* fruits/
  | citrus/
  | | __init__.py
  | | oranges.py
  | | mandarins.py
  | | satsumas.py
  | | ... etc

The Python files under fruits/citrus/ contain definitions of about 300 subclasses of fruits.citrus.Citrus.

They live in separate files like this only for administrative reasons. Nobody should know or care about fruits.citrus.mandarins etc unless they're working on the fruits.citrus package. To put it another way, I would like this package to pretend it's a module.

I have tried putting this in __init__.py:

from fruits.citrus.oranges import *
from fruits.citrus.mandarins import *
from fruits.citrus.satsumas import *
[...]

g = list(globals().items())

__all__ = list([
    name for name, value in g
    if value.__class__==type and
    issubclass(value, Citrus)
    ])

This sort of works, in that you can see fruits.citrus.Tangerine. But pydoc and sphinx still list oranges, mandarins, etc as package contents. And Tangerine is both at fruits.citrus.Tangerine and at fruits.citrus.mandarins.Tangerine, so things are even more complicated than when I started.

There has to be a way to do this, but everything I tried has been a lemon. Help?



from Python module implemented like a package

No comments:

Post a Comment