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

Add IDCClient() singleton feature #99

Merged
merged 5 commits into from
Jul 8, 2024

Conversation

LennyN95
Copy link
Contributor

@LennyN95 LennyN95 commented Jul 4, 2024

No need for a global client = IDCClient() call, instead IDCClient.client() can be used directly. Upon first call a new IDCClient instance will be initialized and reused for all following calls.

@vkt1414
Copy link
Collaborator

vkt1414 commented Jul 4, 2024

@LennyN95 do you recommend any tutorials or books to learn more about singletons?

@fedorov
Copy link
Member

fedorov commented Jul 5, 2024

@LennyN95 I don't object in principle, but can you give the rationale for adding this feature?

I don't think I ever had a need to create more than one client. Or maybe that's exactly the point - to prevent user from creating multiple clients, since this should not ever be needed, but indeed can easily be done before this change?

I assume you will resolve the conflicts.

@LennyN95
Copy link
Contributor Author

LennyN95 commented Jul 6, 2024

This here gives an excellent overview!

Note, I didn't implement an actual singleton but rather a singleton factory method, which I believe is the most suited for our use case. It simplifies access while not impacting flexibility and testability.

@LennyN95
Copy link
Contributor Author

LennyN95 commented Jul 6, 2024

Sure @fedorov, I've multiple reasons in mind:

  1. As you said, prevention from user's unnecessarily instantiating multiple instances.
  2. No need for a global client variable if we can hold it internally.
  3. Direct access via IDCClient.client().<any_api_method> saves one extra line.

@fedorov
Copy link
Member

fedorov commented Jul 8, 2024

@LennyN95 I resolved the conflicts.

@fedorov
Copy link
Member

fedorov commented Jul 8, 2024

@LennyN95 I hope you can look into CI errors!

@@ -9,3 +9,5 @@
from ._version import version as __version__

__all__ = ["__version__"]

from .index import IDCClient
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@fedorov I added this import such that we can import IDCClient directly, instead of importing index and then accessing it via index.IDCClient. I see this got removed, is this because we have any rule that prevents adding such imports on the package level or did it just got lost in the process? I prefer this way as it simplifies importing a lot and enhances ux in my opinion but I'm open for counter-arguments ;)

Some of the errors are related to the now missing import, so I wait for your reply before fixing them.

Thx for looking into this!!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The linter doesn't like the unused import... I push a workaround, let me know what you think ;)

Copy link
Member

Choose a reason for hiding this comment

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

Indeed, I just accepted the corrections that we proposed by the linter/ruff. Looks good to me!

@fedorov fedorov changed the title Feat_singleton Add IDC singleton feature Jul 8, 2024
@fedorov fedorov changed the title Add IDC singleton feature Add IDCClient() singleton feature Jul 8, 2024
@fedorov fedorov merged commit 7f16a37 into ImagingDataCommons:main Jul 8, 2024
10 checks passed
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.

3 participants