Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Singleton Registry #68

Open
kozlov721 opened this issue Jan 11, 2024 · 0 comments
Open

Singleton Registry #68

kozlov721 opened this issue Jan 11, 2024 · 0 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@kozlov721
Copy link
Collaborator

kozlov721 commented Jan 11, 2024

Singleton Registry Pattern

In the current implementation, the Registry have to be first instantiated somewhere and the instance have to be then imported whenever one wants to use the registry.

Disadvantages

It might not be always clear where should one define the Registry instances. If defined in wrong place, it can cause issues with circular imports or similar import errors.

Solution

It would be advantageous for the registry instances to act as singletons based on the provided names.
That would mean that every time a registry with the same name is instantiated, the firstly created instance is returned instead of creating a new empty registry. This will make the registries a bit more convenient to use and avoids any possible import errors.

Current Method

# ---- foo.py ---- 

from luxonis_ml.utils import Registry

REGISTRY = Registry(name="registry_name")

@REGISTRY.register_module()
class Foo:
    pass
# ---- bar.py ----

from foo import REGISTRY

REGISTRY.get("Foo")
# >>> <class '__foo__.Foo'>

Proposed Method

# ---- foo.py ---- 

from luxonis_ml.utils import Registry

# If defined for the first time, creates a global static `Registry` instance.
REGISTRY = Registry("registry_name")

@REGISTRY.register_module()
class Foo:
    pass
# ---- bar.py ----

# no need to import REGISTRY from foo.py
from luxonis_ml.utils import Registry

Registry("registry_name").get("Foo")
# >>> <class '__foo__.Foo'>

To Consider

  • Should the names be case sensitive or not?
@kozlov721 kozlov721 added enhancement New feature or request good first issue Good for newcomers labels Jan 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant