From 9933b0b88026fd91036847943b9e6c268f8b5860 Mon Sep 17 00:00:00 2001 From: Karen Zeng Date: Wed, 19 Apr 2023 19:10:54 -0400 Subject: [PATCH] update --- module-inspector/module-inspector.py | 5 +++++ test10.py | 14 ++++++++++++++ test11.py | 12 ++++++++++++ test9.py | 18 ++++++++++-------- 4 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 test10.py create mode 100644 test11.py diff --git a/module-inspector/module-inspector.py b/module-inspector/module-inspector.py index 0837792..2489c57 100644 --- a/module-inspector/module-inspector.py +++ b/module-inspector/module-inspector.py @@ -2,6 +2,7 @@ import sys import tkinter as tk from tkinter import ttk + def on_treeview_heading_click(column, tree): data = [(tree.set(child, column), child) for child in tree.get_children("")] reverse = (column == tree.heading(column)["text"]) and not column.startswith("_") @@ -38,6 +39,10 @@ tree.column('#0', width=20, stretch=False) tree.tag_configure("selected", background="#ccc") +# Call the on_treeview_heading_click function when a column heading is clicked +for col in tree["columns"]: + tree.heading(col, text=col, anchor=tk.W, command=lambda col=col: on_treeview_heading_click(col, tree)) +root.mainloop() # Call the on_treeview_heading_click function when a column heading is clicked for col in tree["columns"]: tree.heading(col, text=col, anchor=tk.W, command=lambda col=col: on_treeview_heading_click(col, tree)) diff --git a/test10.py b/test10.py new file mode 100644 index 0000000..5bdd3cf --- /dev/null +++ b/test10.py @@ -0,0 +1,14 @@ +import sys + +# Iterate through the modules in sys.modules +for module_name, module in sys.modules.items(): + # Check if the module has a loader attribute + if hasattr(module, '__loader__'): + loader = module.__loader__ + # Determine the type of loader + if type(loader).__name__ == 'SourceFileLoader': + print(f"Module '{module_name}' has a source loader.") + elif type(loader).__name__ == 'BuiltinImporter': + print(f"Module '{module_name}' has a built-in loader.") + elif type(loader).__name__ == 'ExtensionFileLoader': + print(f"Module '{module_name}' has an extension loader.") diff --git a/test11.py b/test11.py new file mode 100644 index 0000000..43908c3 --- /dev/null +++ b/test11.py @@ -0,0 +1,12 @@ +from cx_Freeze import setup, Executable + +executables = [Executable('module-inspector.py')] + +setup( + name='wow', + version='1.0', + description='Module-inspector Application', + executables=executables +) + + diff --git a/test9.py b/test9.py index 64bd63c..341b356 100644 --- a/test9.py +++ b/test9.py @@ -9,7 +9,7 @@ from sys import getsizeof def get_bytecode_size(module_name): """Get the size of the cached .pyc bytecode file for a module.""" spec = importlib.util.find_spec(module_name) - if spec and spec.origin.endswith('.py'): + if spec and spec.origin and spec.origin.endswith('.py'): pyc_file = importlib.util.cache_from_source(spec.origin) if os.path.exists(pyc_file): return os.path.getsize(pyc_file) @@ -19,7 +19,7 @@ def get_bytecode_size(module_name): def get_dso_size(module_name): """Get the size of the DSO (Dynamic Shared Object) module file for a module.""" spec = importlib.util.find_spec(module_name) - if spec and spec.origin.endswith(('.so', '.dll')): + if spec and spec.origin and spec.origin.endswith(('.so', '.dll')): return os.path.getsize(spec.origin) return 0 @@ -51,7 +51,7 @@ def generate_module_report(): # Generate histogram of module directory names dir_histogram = defaultdict(int) for module_name in sys.modules: - module_file = sys.modules[module_name].__file__ + module_file = getattr(sys.modules[module_name], '__file__', None) if module_file: module_dir = os.path.dirname(module_file) dir_histogram[module_dir] += 1 @@ -64,9 +64,11 @@ def generate_module_report(): if __name__ == "__main__": - # Set __spec__ attribute for the main module - if not hasattr(sys.modules['__main__'], '__spec__'): - sys.modules['__main__'].__spec__ = importlib.util.spec_from_file_location('__main__', __file__) + __spec__ = importlib.util.spec_from_file_location('__main__', __file__) +report = generate_module_report() +print(report) + + + + - report = generate_module_report() - print(report) \ No newline at end of file