From 73ff0f87949389d63062dd17cb8bc095f50f49f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 10:15:53 -0400 Subject: [PATCH 01/15] Added the encapsulation pkg --- conf/script/src/utils/meta_prog/encapsulation/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 conf/script/src/utils/meta_prog/encapsulation/__init__.py diff --git a/conf/script/src/utils/meta_prog/encapsulation/__init__.py b/conf/script/src/utils/meta_prog/encapsulation/__init__.py new file mode 100644 index 00000000..e69de29b From 3ed767678635ee935e7e813bcc8f174089ba5449 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 10:16:23 -0400 Subject: [PATCH 02/15] Added the empty export script --- conf/script/src/utils/meta_prog/encapsulation/export.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 conf/script/src/utils/meta_prog/encapsulation/export.py diff --git a/conf/script/src/utils/meta_prog/encapsulation/export.py b/conf/script/src/utils/meta_prog/encapsulation/export.py new file mode 100644 index 00000000..e69de29b From d77f7f544f088c00c21d28844775f1c09167c86a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 10:25:23 -0400 Subject: [PATCH 03/15] Copy-pasted the stackoverflow ref --- .../src/utils/meta_prog/encapsulation/export.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/conf/script/src/utils/meta_prog/encapsulation/export.py b/conf/script/src/utils/meta_prog/encapsulation/export.py index e69de29b..39f01fa2 100644 --- a/conf/script/src/utils/meta_prog/encapsulation/export.py +++ b/conf/script/src/utils/meta_prog/encapsulation/export.py @@ -0,0 +1,11 @@ +import sys + + +# https://stackoverflow.com/a/35710527/2924010 +def export(fn): + mod = sys.modules[fn.__module__] + if hasattr(mod, '__all__'): + mod.__all__.append(fn.__name__) + else: + mod.__all__ = [fn.__name__] + return fn From 506cdc499979631752388e9ff47ec90e7815638b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 10:25:27 -0400 Subject: [PATCH 04/15] Added test cases --- .../src/utils/meta_prog/encapsulation/export.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/conf/script/src/utils/meta_prog/encapsulation/export.py b/conf/script/src/utils/meta_prog/encapsulation/export.py index 39f01fa2..25341bd6 100644 --- a/conf/script/src/utils/meta_prog/encapsulation/export.py +++ b/conf/script/src/utils/meta_prog/encapsulation/export.py @@ -9,3 +9,17 @@ def export(fn): else: mod.__all__ = [fn.__name__] return fn + + +@export +def foo(): + ... + + +@export +class Bar: + + @export + @staticmethod + def __baz(): + print('baz') From a07de32c4ad8065119bfce0db21751845fd0c4cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 10:27:15 -0400 Subject: [PATCH 05/15] Improved module fetching --- conf/script/src/utils/meta_prog/encapsulation/export.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conf/script/src/utils/meta_prog/encapsulation/export.py b/conf/script/src/utils/meta_prog/encapsulation/export.py index 25341bd6..009669a3 100644 --- a/conf/script/src/utils/meta_prog/encapsulation/export.py +++ b/conf/script/src/utils/meta_prog/encapsulation/export.py @@ -1,9 +1,9 @@ -import sys +import inspect # https://stackoverflow.com/a/35710527/2924010 def export(fn): - mod = sys.modules[fn.__module__] + mod = inspect.getmodule(fn) if hasattr(mod, '__all__'): mod.__all__.append(fn.__name__) else: From c0302122d96d810061e9a83955df0250f2db6d16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 10:27:41 -0400 Subject: [PATCH 06/15] Renamed var --- conf/script/src/utils/meta_prog/encapsulation/export.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/conf/script/src/utils/meta_prog/encapsulation/export.py b/conf/script/src/utils/meta_prog/encapsulation/export.py index 009669a3..0fbb3349 100644 --- a/conf/script/src/utils/meta_prog/encapsulation/export.py +++ b/conf/script/src/utils/meta_prog/encapsulation/export.py @@ -3,11 +3,11 @@ # https://stackoverflow.com/a/35710527/2924010 def export(fn): - mod = inspect.getmodule(fn) - if hasattr(mod, '__all__'): - mod.__all__.append(fn.__name__) + module = inspect.getmodule(fn) + if hasattr(module, '__all__'): + module.__all__.append(fn.__name__) else: - mod.__all__ = [fn.__name__] + module.__all__ = [fn.__name__] return fn From 3c37a999510b7be7cdeb370e00d8f247c5876e04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 10:27:57 -0400 Subject: [PATCH 07/15] Renamed var --- conf/script/src/utils/meta_prog/encapsulation/export.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/conf/script/src/utils/meta_prog/encapsulation/export.py b/conf/script/src/utils/meta_prog/encapsulation/export.py index 0fbb3349..47251553 100644 --- a/conf/script/src/utils/meta_prog/encapsulation/export.py +++ b/conf/script/src/utils/meta_prog/encapsulation/export.py @@ -2,12 +2,12 @@ # https://stackoverflow.com/a/35710527/2924010 -def export(fn): - module = inspect.getmodule(fn) +def export(func): + module = inspect.getmodule(func) if hasattr(module, '__all__'): - module.__all__.append(fn.__name__) + module.__all__.append(func.__name__) else: - module.__all__ = [fn.__name__] + module.__all__ = [func.__name__] return fn From eeadc70bcf1ff5b57d5fb9b18ec49f0e2fd9a291 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 10:43:48 -0400 Subject: [PATCH 08/15] Added constant --- conf/script/src/utils/meta_prog/encapsulation/export.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/conf/script/src/utils/meta_prog/encapsulation/export.py b/conf/script/src/utils/meta_prog/encapsulation/export.py index 47251553..5396354b 100644 --- a/conf/script/src/utils/meta_prog/encapsulation/export.py +++ b/conf/script/src/utils/meta_prog/encapsulation/export.py @@ -1,11 +1,15 @@ import inspect +import typing # https://stackoverflow.com/a/35710527/2924010 def export(func): + attribute_name_all: typing.Final[str] = '__all__' + module = inspect.getmodule(func) - if hasattr(module, '__all__'): - module.__all__.append(func.__name__) + + if hasattr(module, attribute_name_all): + module.__all__.append(func.__qualname__) else: module.__all__ = [func.__name__] return fn From b1825fa3e9a0a58b7f8f15bd0c86589a2a2c1794 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 10:45:11 -0400 Subject: [PATCH 09/15] Pre-fetch the func api --- .../utils/meta_prog/encapsulation/export.py | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/conf/script/src/utils/meta_prog/encapsulation/export.py b/conf/script/src/utils/meta_prog/encapsulation/export.py index 5396354b..56fa9331 100644 --- a/conf/script/src/utils/meta_prog/encapsulation/export.py +++ b/conf/script/src/utils/meta_prog/encapsulation/export.py @@ -7,12 +7,18 @@ def export(func): attribute_name_all: typing.Final[str] = '__all__' module = inspect.getmodule(func) + func_api = func.__qualname__ if hasattr(module, attribute_name_all): - module.__all__.append(func.__qualname__) + module.__all__.append(func_api) else: - module.__all__ = [func.__name__] - return fn + module.__all__ = [func_api] + + print(func) + print(module) + print() + + return func @export @@ -22,8 +28,9 @@ def foo(): @export class Bar: + # @export + # @staticmethod + # def __baz(): + # print('baz') - @export - @staticmethod - def __baz(): - print('baz') + ... From c4948d5b60d15ef50be6a593dcda6b64b70eefdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 10:49:53 -0400 Subject: [PATCH 10/15] Use variables instead of hidden attributes --- conf/script/src/utils/meta_prog/encapsulation/export.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/conf/script/src/utils/meta_prog/encapsulation/export.py b/conf/script/src/utils/meta_prog/encapsulation/export.py index 56fa9331..f3cba7fa 100644 --- a/conf/script/src/utils/meta_prog/encapsulation/export.py +++ b/conf/script/src/utils/meta_prog/encapsulation/export.py @@ -7,12 +7,12 @@ def export(func): attribute_name_all: typing.Final[str] = '__all__' module = inspect.getmodule(func) + func_api = func.__qualname__ + module_api: list[str] = getattr(module, attribute_name_all, []) - if hasattr(module, attribute_name_all): - module.__all__.append(func_api) - else: - module.__all__ = [func_api] + module_api.append(func_api) + setattr(module, attribute_name_all, module_api) print(func) print(module) From fc08cecb53d2e1805039028359c9119eaf505419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 10:50:31 -0400 Subject: [PATCH 11/15] Reorder --- conf/script/src/utils/meta_prog/encapsulation/export.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/conf/script/src/utils/meta_prog/encapsulation/export.py b/conf/script/src/utils/meta_prog/encapsulation/export.py index f3cba7fa..96b7b7a4 100644 --- a/conf/script/src/utils/meta_prog/encapsulation/export.py +++ b/conf/script/src/utils/meta_prog/encapsulation/export.py @@ -7,17 +7,12 @@ def export(func): attribute_name_all: typing.Final[str] = '__all__' module = inspect.getmodule(func) - - func_api = func.__qualname__ module_api: list[str] = getattr(module, attribute_name_all, []) + func_api = func.__qualname__ module_api.append(func_api) setattr(module, attribute_name_all, module_api) - print(func) - print(module) - print() - return func From 6e7a7dc2afdfb231bf9c1f8150e599d5f78464b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 10:55:56 -0400 Subject: [PATCH 12/15] Optimization --- .../utils/meta_prog/encapsulation/export.py | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/conf/script/src/utils/meta_prog/encapsulation/export.py b/conf/script/src/utils/meta_prog/encapsulation/export.py index 96b7b7a4..c7b58be3 100644 --- a/conf/script/src/utils/meta_prog/encapsulation/export.py +++ b/conf/script/src/utils/meta_prog/encapsulation/export.py @@ -2,16 +2,21 @@ import typing -# https://stackoverflow.com/a/35710527/2924010 def export(func): attribute_name_all: typing.Final[str] = '__all__' + module_api: list[str] module = inspect.getmodule(func) - module_api: list[str] = getattr(module, attribute_name_all, []) func_api = func.__qualname__ + if not hasattr(module, attribute_name_all): + module_api = [] + setattr(module, attribute_name_all, module_api) + else: + module_api = getattr(module, attribute_name_all) + module_api.append(func_api) - setattr(module, attribute_name_all, module_api) + return func @@ -23,9 +28,8 @@ def foo(): @export class Bar: - # @export - # @staticmethod - # def __baz(): - # print('baz') - ... + @staticmethod + @export + def __baz(): + print('baz') From c3a5089b350bfb4bf5defbcd76ca2f1076f4462c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 11:15:49 -0400 Subject: [PATCH 13/15] Reformat --- .../src/utils/meta_prog/encapsulation/export.py | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/conf/script/src/utils/meta_prog/encapsulation/export.py b/conf/script/src/utils/meta_prog/encapsulation/export.py index c7b58be3..43a27a80 100644 --- a/conf/script/src/utils/meta_prog/encapsulation/export.py +++ b/conf/script/src/utils/meta_prog/encapsulation/export.py @@ -2,7 +2,7 @@ import typing -def export(func): +def export(func: typing.Callable): attribute_name_all: typing.Final[str] = '__all__' module_api: list[str] @@ -17,19 +17,4 @@ def export(func): module_api.append(func_api) - return func - - -@export -def foo(): - ... - - -@export -class Bar: - - @staticmethod - @export - def __baz(): - print('baz') From aa9347acb543444caf3cbf3f6d61118ee042f1aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 11:20:25 -0400 Subject: [PATCH 14/15] Added __all__ --- conf/script/src/utils/meta_prog/encapsulation/export.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/conf/script/src/utils/meta_prog/encapsulation/export.py b/conf/script/src/utils/meta_prog/encapsulation/export.py index 43a27a80..d7a72a21 100644 --- a/conf/script/src/utils/meta_prog/encapsulation/export.py +++ b/conf/script/src/utils/meta_prog/encapsulation/export.py @@ -1,6 +1,8 @@ import inspect import typing +__all__ = ['export'] + def export(func: typing.Callable): attribute_name_all: typing.Final[str] = '__all__' From 21cfce82764fe52cd341f49628b009a3a87387dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dubreuil?= Date: Sun, 18 Apr 2021 11:21:09 -0400 Subject: [PATCH 15/15] Import at package level --- conf/script/src/utils/meta_prog/encapsulation/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/conf/script/src/utils/meta_prog/encapsulation/__init__.py b/conf/script/src/utils/meta_prog/encapsulation/__init__.py index e69de29b..1cc9d89a 100644 --- a/conf/script/src/utils/meta_prog/encapsulation/__init__.py +++ b/conf/script/src/utils/meta_prog/encapsulation/__init__.py @@ -0,0 +1 @@ +from .export import *