0

I am working on a project and I developed a library and I would like to export only a subset of functions and objects of this library to a new library. Is there a way to do this algorithmically instead of just copying and pasting the desired functions and classes (with it dependences) to the new library?

To give you a little of context. I am working in a Machine Learning project, in which I have a main library which is the developer library. In this library I have lots of models and utils that are useful in the research context, but I only use a little subgroup of these models, objects and functions in production.

My question is, is there a way to define some kind of file, or something similar in which I write down which functions and and objects I want to export to a new library, so in my new library I have only the desired functions and objects and not the whole library.

For example with my main library I can:

from main_lib.models import ModelA, ModelB, ModelC...
from main_lib.utils import parse, save_data, load_data
from main_lib.images import augmentate, change_format

but in prod I only use main_lib.models.ModelB and main_lib.parse. So I would like to ideally export only those two to a new lib prod_lib from which I can also do

from prod_lib.models import ModelB...
from prod_lib.utils import parse

and here for example ModelB uses change_format, so this magical solution should also know that ModelB uses main_lib.images.change_format and export it as well.

So ideally, when I think I have a new release, I just export those submodules and functions to the new lib. One of the main motivations behind my questions is that each of this different models ModelA, ModelB etc sometimes have a lot of configurations files that I don't want to move to the prod environment but are needed in the dev one.

Of course I just can do it by hand, and just copy the needed methods, but duplicating code and doing things by hand probably is the worst solution. Or maybe I am thinking wrong the problem and there is another approach to organize the library which also may solve my problem.

I think that being able to export just a subgroup of functions will answer my question, but any other suggestion is very welcomed.

Iván
  • 320
  • 3
  • 13

1 Answers1

0

You can use an __init__.py file, which lets you choose what to “export” specifically. You can look this up in the official documentation or refer to this post Learn more about init.py files in the official documentation or stackoverflow.com/questions/448271/what-is-init-py-for

There is however, a caveat: even if variables are not in the __init__.py file, They can still be accessed outside the module, but with the exact directory. To illustrate, here is an example

|-main.py
|-lib
  |-__init__.py
  |-libFile.py
  |-libFile2.py

__init__.py

from lib.libFile import myFunc
from lib.libFile2 import myClass

main.py

from lib import myFunc, myClass

There is a caveat, however. If you have a class in libFile called myClass2, it can still be accessed this way

from lib.libFile import myClass2 #still works

This way, you can “export” certain parts of your library

Luke-zhang-04
  • 782
  • 7
  • 11