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
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ $ pip install --upgrade idc-index
Instantiate `IDCClient`, which provides the interface for main operations.

```python
from idc_index import index
from idc_index import IDCClient

client = index.IDCClient()
client = IDCClient.client()
```

You can use [IDC Portal](https://imaging.datacommons.cancer.gov/explore) to
Expand Down
18 changes: 18 additions & 0 deletions idc_index/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ class IDCClient:
CITATION_FORMAT_JSON = "application/vnd.citationstyles.csl+json"
CITATION_FORMAT_BIBTEX = "application/x-bibtex"

# Singleton pattern
# NOTE: In the future, one may want to use multiple clients e.g. for sub-datasets so a attribute-singleton as shown bewlo seems a better option.
# _instance: IDCClient
# def __new__(cls):
# if not hasattr(cls, "_instance") or getattr(cls, "_instance") is None:
# instance = super(IDCClient, cls).__new__(cls)
# setattr(cls, "_instance", instance)
# return cls._instance

_client: IDCClient

@classmethod
def client(cls) -> IDCClient:
if not hasattr(cls, "_client") or getattr(cls, "_client") is None:
setattr(cls, "_client", IDCClient())

return cls._client

def __init__(self):
file_path = idc_index_data.IDC_INDEX_PARQUET_FILEPATH

Expand Down
33 changes: 31 additions & 2 deletions tests/idcindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import pandas as pd
import pytest
from click.testing import CliRunner
from idc_index import cli, index
from idc_index import IDCClient, cli

# Run tests using the following command from the root of the repository:
# python -m unittest -vv tests/idcindex.py
Expand All @@ -25,7 +25,7 @@ def _change_test_dir(request, monkeypatch):

class TestIDCClient(unittest.TestCase):
def setUp(self):
self.client = index.IDCClient()
self.client = IDCClient()
self.download_from_manifest = cli.download_from_manifest
self.download_from_selection = cli.download_from_selection
self.download = cli.download
Expand Down Expand Up @@ -423,6 +423,35 @@ def test_cli_download_from_manifest(self):
)
assert len(os.listdir(temp_dir)) != 0

def test_singleton_attribute(self):
# singleton, initialized on first use
i1 = IDCClient.client()
i2 = IDCClient.client()

# new instances created via constructor (through init)
i3 = IDCClient()
i4 = self.client

# all must be not none
assert i1 is not None
assert i2 is not None
assert i3 is not None
assert i4 is not None

# singletons must return the same instance
assert i1 == i2

# new instances must be different
assert i1 != i3
assert i1 != i4
assert i3 != i4

# all must be instances of IDCClient
assert isinstance(i1, IDCClient)
assert isinstance(i2, IDCClient)
assert isinstance(i3, IDCClient)
assert isinstance(i4, IDCClient)

def test_cli_download(self):
runner = CliRunner()
with runner.isolated_filesystem():
Expand Down
Loading