Skip to content

Conversation

@sarlinpe
Copy link
Member

@sarlinpe sarlinpe commented Sep 4, 2024

Move the C++ extension to pycolmap._core such that we can augment PyCOLMAP with pure Python modules. Notes:

  • Other projects would often place the extension in a second package _pycolmap, but this pollutes the site-packages directory
  • In help(pycolmap), this shows types like pycolmap._core.Reconstruction instead of pycolmap.Reconstruction... @B1ueber2y Did you manage to fix this in limap?

@B1ueber2y
Copy link
Contributor

I did it in a quite dirty way here: https://github.com/cvg/limap/blob/main/limap/__init__.py. I am sure there are better alternatives (at least it should be switched to explicit import)

@sarlinpe sarlinpe marked this pull request as ready for review September 25, 2024 08:59
@sarlinpe
Copy link
Member Author

Types in help(pycolmap) still appear as pycolmap._core but this doesn't matter much because we now have the doc pages. Overall this new package structure follows the one of pytorch.

@B1ueber2y
Copy link
Contributor

B1ueber2y commented Sep 25, 2024

Thanks! This works in my test.

However, are you sure that this is the only way to achieve this? "import *" looks quite aggressive and is widely considered to be avoided in all costs.

@sarlinpe
Copy link
Member Author

This is a common solution (for example as done in torch) and the only alternative to manually listing the symbols to be imported (which is impractical for us).

@ahojnnes
Copy link
Contributor

LGTM, also given that other notable libraries follow the same approach.

@sarlinpe sarlinpe merged commit e1e7505 into main Sep 26, 2024
@sarlinpe sarlinpe deleted the sarlinpe/mixed-py branch September 26, 2024 07:23
@@ -0,0 +1,15 @@
from ._core import * # noqa F403
Copy link
Contributor

@pablospe pablospe Oct 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something to consider. To avoid importing internal symbols, the following code snippet reduces the risk of name conflicts and keeps the pycolmap namespace clean:

from . import _core as _core_module

# Loop over all the members of the module, adding the symbols to the current
# module's namespace (i.e., pycolmap package's), and skipping internal symbols
# (names starting with '_').
for _name, _symbol in vars(_core_module).items():
    if not _name.startswith("_"):
        globals()[_name] = _symbol

# Cleanup.
del _core_module, _name, _symbol

This solution ensures that only the relevant symbols from _core are shown, without explicitly listing pycolmap._core when using help(pycolmap). Please note that I haven't tested this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants