From 8c18d8e4cb2c2c1606bb173538bd13a74b8effce Mon Sep 17 00:00:00 2001 From: Karenzz Date: Thu, 13 Apr 2023 22:01:27 +0200 Subject: [PATCH] Delete 'httpserver.py' --- hw10.py | 14 +++++++++++ hw11.py | 12 ++++++++++ hw8.py | 47 ++++++++++++++++++++++++++++++++++++ hw9.py | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+) create mode 100644 hw10.py create mode 100644 hw11.py create mode 100644 hw8.py create mode 100644 hw9.py diff --git a/hw10.py b/hw10.py new file mode 100644 index 0000000..5bdd3cf --- /dev/null +++ b/hw10.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/hw11.py b/hw11.py new file mode 100644 index 0000000..43908c3 --- /dev/null +++ b/hw11.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/hw8.py b/hw8.py new file mode 100644 index 0000000..fbc3a06 --- /dev/null +++ b/hw8.py @@ -0,0 +1,47 @@ +import importlib.util +import os + +def get_module_size(module_name): + """Get the total number of bytes in the cached .pyc file or .py file of a module or package. + + Args: + module_name (str): The name of the module or package. + + Returns: + int: The total number of bytes in the cached .pyc file or .py file. + """ + spec = importlib.util.find_spec(module_name) + + if spec is not None and spec.has_location: + # If the module is found and has a location, check for .pyc file first + pyc_file_path = spec.origin + 'c' + if os.path.exists(pyc_file_path): + return os.stat(pyc_file_path).st_size + else: + # If .pyc file doesn't exist, check for .py file + py_file_path = spec.origin + if os.path.exists(py_file_path): + return os.stat(py_file_path).st_size + + return 0 # Return 0 if no .pyc or .py file found + + +import sys + +def get_total_module_size(): + """Get the total number of bytes for all modules listed in sys.modules. + + Returns: + int: The total number of bytes for all modules. + """ + total_size = 0 + for module_name in sys.modules: + module_size = get_module_size(module_name) + total_size += module_size + + return total_size + +if __name__ == '__main__' and __package__ is None: + __spec__ = importlib.util.spec_from_file_location('__main__', __file__) +total_size = get_total_module_size() +print("Total size of all modules in sys.modules: {} bytes".format(total_size)) \ No newline at end of file diff --git a/hw9.py b/hw9.py new file mode 100644 index 0000000..341b356 --- /dev/null +++ b/hw9.py @@ -0,0 +1,74 @@ +import os +import sys +import importlib.util +import types +from collections import defaultdict +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 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) + return 0 + + +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 and spec.origin.endswith(('.so', '.dll')): + return os.path.getsize(spec.origin) + return 0 + + +def generate_module_report(): + """Generate a report summarizing the module environment.""" + total_modules = len(sys.modules) + bytecode_size = sum(get_bytecode_size(module_name) for module_name in sys.modules) + dso_size = sum(get_dso_size(module_name) for module_name in sys.modules) + python_size = sum( + getsizeof(sys.modules[module_name]) - getsizeof(module_name) + for module_name in sys.modules + if isinstance(sys.modules[module_name], types.ModuleType) + ) + total_size = bytecode_size + dso_size + python_size + + # Calculate percentage of each type of module + bytecode_percent = bytecode_size / total_size * 100 + dso_percent = dso_size / total_size * 100 + python_percent = python_size / total_size * 100 + + # Generate the report + report = f"Total modules: {total_modules}\n" + report += f"Total size: {total_size} bytes\n" + report += f"Bytecode modules: {bytecode_percent:.2f}% ({bytecode_size} bytes)\n" + report += f"DSO modules: {dso_percent:.2f}% ({dso_size} bytes)\n" + report += f"Python code modules: {python_percent:.2f}% ({python_size} bytes)\n" + + # Generate histogram of module directory names + dir_histogram = defaultdict(int) + for module_name in sys.modules: + module_file = getattr(sys.modules[module_name], '__file__', None) + if module_file: + module_dir = os.path.dirname(module_file) + dir_histogram[module_dir] += 1 + + report += "\nModule Directory Histogram:\n" + for module_dir, count in dir_histogram.items(): + report += f"{module_dir}: {'*' * count}\n" + + return report + + +if __name__ == "__main__": + __spec__ = importlib.util.spec_from_file_location('__main__', __file__) +report = generate_module_report() +print(report) + + + + +