From 9a57b58a043e448548ea53c7c330138085f98add Mon Sep 17 00:00:00 2001 From: Kushaan Gupta Date: Fri, 13 Sep 2024 21:56:41 -0400 Subject: [PATCH 1/5] add underscore prefix to lazy import --- neuro_py/__init__.py | 6 +++--- neuro_py/behavior/__init__.py | 6 +++--- neuro_py/ensemble/__init__.py | 6 +++--- neuro_py/io/__init__.py | 6 +++--- neuro_py/lfp/__init__.py | 6 +++--- neuro_py/plotting/__init__.py | 6 +++--- neuro_py/process/__init__.py | 6 +++--- neuro_py/session/__init__.py | 6 +++--- neuro_py/spikes/__init__.py | 6 +++--- neuro_py/stats/__init__.py | 6 +++--- neuro_py/tuning/__init__.py | 6 +++--- 11 files changed, 33 insertions(+), 33 deletions(-) diff --git a/neuro_py/__init__.py b/neuro_py/__init__.py index 2a559be..9cce42d 100644 --- a/neuro_py/__init__.py +++ b/neuro_py/__init__.py @@ -1,4 +1,4 @@ -import lazy_loader as lazy +import lazy_loader as _lazy -(__getattr__, __dir__, __all__) = lazy.attach_stub(__name__, __file__) -del lazy \ No newline at end of file +(__getattr__, __dir__, __all__) = _lazy.attach_stub(__name__, __file__) +del _lazy diff --git a/neuro_py/behavior/__init__.py b/neuro_py/behavior/__init__.py index 9ff4bc3..9cce42d 100644 --- a/neuro_py/behavior/__init__.py +++ b/neuro_py/behavior/__init__.py @@ -1,4 +1,4 @@ -import lazy_loader as lazy +import lazy_loader as _lazy -(__getattr__, __dir__, __all__) = lazy.attach_stub(__name__, __file__) -del lazy +(__getattr__, __dir__, __all__) = _lazy.attach_stub(__name__, __file__) +del _lazy diff --git a/neuro_py/ensemble/__init__.py b/neuro_py/ensemble/__init__.py index 9ff4bc3..9cce42d 100644 --- a/neuro_py/ensemble/__init__.py +++ b/neuro_py/ensemble/__init__.py @@ -1,4 +1,4 @@ -import lazy_loader as lazy +import lazy_loader as _lazy -(__getattr__, __dir__, __all__) = lazy.attach_stub(__name__, __file__) -del lazy +(__getattr__, __dir__, __all__) = _lazy.attach_stub(__name__, __file__) +del _lazy diff --git a/neuro_py/io/__init__.py b/neuro_py/io/__init__.py index 9ff4bc3..9cce42d 100644 --- a/neuro_py/io/__init__.py +++ b/neuro_py/io/__init__.py @@ -1,4 +1,4 @@ -import lazy_loader as lazy +import lazy_loader as _lazy -(__getattr__, __dir__, __all__) = lazy.attach_stub(__name__, __file__) -del lazy +(__getattr__, __dir__, __all__) = _lazy.attach_stub(__name__, __file__) +del _lazy diff --git a/neuro_py/lfp/__init__.py b/neuro_py/lfp/__init__.py index 9ff4bc3..9cce42d 100644 --- a/neuro_py/lfp/__init__.py +++ b/neuro_py/lfp/__init__.py @@ -1,4 +1,4 @@ -import lazy_loader as lazy +import lazy_loader as _lazy -(__getattr__, __dir__, __all__) = lazy.attach_stub(__name__, __file__) -del lazy +(__getattr__, __dir__, __all__) = _lazy.attach_stub(__name__, __file__) +del _lazy diff --git a/neuro_py/plotting/__init__.py b/neuro_py/plotting/__init__.py index 9ff4bc3..9cce42d 100644 --- a/neuro_py/plotting/__init__.py +++ b/neuro_py/plotting/__init__.py @@ -1,4 +1,4 @@ -import lazy_loader as lazy +import lazy_loader as _lazy -(__getattr__, __dir__, __all__) = lazy.attach_stub(__name__, __file__) -del lazy +(__getattr__, __dir__, __all__) = _lazy.attach_stub(__name__, __file__) +del _lazy diff --git a/neuro_py/process/__init__.py b/neuro_py/process/__init__.py index 9ff4bc3..9cce42d 100644 --- a/neuro_py/process/__init__.py +++ b/neuro_py/process/__init__.py @@ -1,4 +1,4 @@ -import lazy_loader as lazy +import lazy_loader as _lazy -(__getattr__, __dir__, __all__) = lazy.attach_stub(__name__, __file__) -del lazy +(__getattr__, __dir__, __all__) = _lazy.attach_stub(__name__, __file__) +del _lazy diff --git a/neuro_py/session/__init__.py b/neuro_py/session/__init__.py index 9ff4bc3..9cce42d 100644 --- a/neuro_py/session/__init__.py +++ b/neuro_py/session/__init__.py @@ -1,4 +1,4 @@ -import lazy_loader as lazy +import lazy_loader as _lazy -(__getattr__, __dir__, __all__) = lazy.attach_stub(__name__, __file__) -del lazy +(__getattr__, __dir__, __all__) = _lazy.attach_stub(__name__, __file__) +del _lazy diff --git a/neuro_py/spikes/__init__.py b/neuro_py/spikes/__init__.py index 9ff4bc3..9cce42d 100644 --- a/neuro_py/spikes/__init__.py +++ b/neuro_py/spikes/__init__.py @@ -1,4 +1,4 @@ -import lazy_loader as lazy +import lazy_loader as _lazy -(__getattr__, __dir__, __all__) = lazy.attach_stub(__name__, __file__) -del lazy +(__getattr__, __dir__, __all__) = _lazy.attach_stub(__name__, __file__) +del _lazy diff --git a/neuro_py/stats/__init__.py b/neuro_py/stats/__init__.py index 9ff4bc3..9cce42d 100644 --- a/neuro_py/stats/__init__.py +++ b/neuro_py/stats/__init__.py @@ -1,4 +1,4 @@ -import lazy_loader as lazy +import lazy_loader as _lazy -(__getattr__, __dir__, __all__) = lazy.attach_stub(__name__, __file__) -del lazy +(__getattr__, __dir__, __all__) = _lazy.attach_stub(__name__, __file__) +del _lazy diff --git a/neuro_py/tuning/__init__.py b/neuro_py/tuning/__init__.py index 9ff4bc3..9cce42d 100644 --- a/neuro_py/tuning/__init__.py +++ b/neuro_py/tuning/__init__.py @@ -1,4 +1,4 @@ -import lazy_loader as lazy +import lazy_loader as _lazy -(__getattr__, __dir__, __all__) = lazy.attach_stub(__name__, __file__) -del lazy +(__getattr__, __dir__, __all__) = _lazy.attach_stub(__name__, __file__) +del _lazy From 93decab07f94585f529775a7d3e20964d879da8d Mon Sep 17 00:00:00 2001 From: Kushaan Gupta Date: Fri, 13 Sep 2024 22:16:26 -0400 Subject: [PATCH 2/5] add lazy loader unit test --- tests/test_lazy_loader.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 tests/test_lazy_loader.py diff --git a/tests/test_lazy_loader.py b/tests/test_lazy_loader.py new file mode 100644 index 0000000..2d29b3f --- /dev/null +++ b/tests/test_lazy_loader.py @@ -0,0 +1,34 @@ +import sys +from gc import get_referents + +import neuro_py + + +def getsize(obj): + """sum size of object & members.""" + seen_ids = set() + size = 0 + objects = [obj] + while objects: + need_referents = [] + for obj in objects: + # print(obj) + if id(obj) not in seen_ids: + seen_ids.add(id(obj)) + size += sys.getsizeof(obj) + need_referents.append(obj) + objects = get_referents(*need_referents) + return size + +def test_lazy_loader(): + # default module size + module_size = getsize(neuro_py) + + # load submodules + neuro_py.io.loadLFP + + # get new module size + new_module_size = getsize(neuro_py) + + # check that the new module size is larger than the default + assert new_module_size > module_size From 2bff9fdf5b54c0c02fb5364574b6ebf0dbbfca23 Mon Sep 17 00:00:00 2001 From: Kushaan Gupta Date: Mon, 16 Sep 2024 13:53:43 -0400 Subject: [PATCH 3/5] reload neuro_py to handle test error --- tests/test_lazy_loader.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_lazy_loader.py b/tests/test_lazy_loader.py index 2d29b3f..5e952c8 100644 --- a/tests/test_lazy_loader.py +++ b/tests/test_lazy_loader.py @@ -21,6 +21,11 @@ def getsize(obj): return size def test_lazy_loader(): + # if neuro_py is loaded, remove it and reload it + if 'neuro_py' in sys.modules: + del sys.modules['neuro_py'] + import neuro_py + # default module size module_size = getsize(neuro_py) From 11191956a4050e45218e5993bdbee7f465482bdd Mon Sep 17 00:00:00 2001 From: Kushaan Gupta Date: Mon, 16 Sep 2024 16:40:47 -0400 Subject: [PATCH 4/5] remove lazy loader test as nested size calc makes the sizes equal --- tests/test_lazy_loader.py | 39 --------------------------------------- 1 file changed, 39 deletions(-) delete mode 100644 tests/test_lazy_loader.py diff --git a/tests/test_lazy_loader.py b/tests/test_lazy_loader.py deleted file mode 100644 index 5e952c8..0000000 --- a/tests/test_lazy_loader.py +++ /dev/null @@ -1,39 +0,0 @@ -import sys -from gc import get_referents - -import neuro_py - - -def getsize(obj): - """sum size of object & members.""" - seen_ids = set() - size = 0 - objects = [obj] - while objects: - need_referents = [] - for obj in objects: - # print(obj) - if id(obj) not in seen_ids: - seen_ids.add(id(obj)) - size += sys.getsizeof(obj) - need_referents.append(obj) - objects = get_referents(*need_referents) - return size - -def test_lazy_loader(): - # if neuro_py is loaded, remove it and reload it - if 'neuro_py' in sys.modules: - del sys.modules['neuro_py'] - import neuro_py - - # default module size - module_size = getsize(neuro_py) - - # load submodules - neuro_py.io.loadLFP - - # get new module size - new_module_size = getsize(neuro_py) - - # check that the new module size is larger than the default - assert new_module_size > module_size From b3e2b2553274b431317c8e62f4f1960e4a008c36 Mon Sep 17 00:00:00 2001 From: Kushaan Gupta Date: Fri, 20 Sep 2024 23:24:26 -0400 Subject: [PATCH 5/5] add legit lazy loading test --- tests/test_lazy_loading.py | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 tests/test_lazy_loading.py diff --git a/tests/test_lazy_loading.py b/tests/test_lazy_loading.py new file mode 100644 index 0000000..1a12446 --- /dev/null +++ b/tests/test_lazy_loading.py @@ -0,0 +1,41 @@ +import sys + + +def test_lazy_loading(): + # clear all neuro_py submodules + for module in list(sys.modules.keys()): + if module.startswith("neuro_py"): + sys.modules.pop(module) + + import neuro_py + + npy_submodules = set( + x.split(".")[1] + for x in sys.modules.keys() + if ( + x.startswith("neuro_py.") + and "__" not in x + and not x.split(".")[1].startswith("_") + and sys.modules[x] is not None + ) + ) + + # ensure that no submodules have been imported yet + assert npy_submodules == set(), \ + f"Submodules already loaded: {npy_submodules}" + + # ensure that only the accessed submodule has been imported + neuro_py.behavior + npy_submodules = set( + x.split(".")[1] + for x in sys.modules.keys() + if ( + x.startswith("neuro_py.") + and "__" not in x + and not x.split(".")[1].startswith("_") + and sys.modules[x] is not None + ) + ) + assert npy_submodules == { + "behavior" + }, f"Submodules already loaded: {npy_submodules}"