diff --git a/assessment2.py b/assessment2.py new file mode 100644 index 0000000..fbc3a06 --- /dev/null +++ b/assessment2.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/http-performance/testhh.py b/http-performance/testhh.py index 0f87350..716e12a 100644 --- a/http-performance/testhh.py +++ b/http-performance/testhh.py @@ -1 +1,54 @@ -# for test \ No newline at end of file +''' +import os +from flask import Flask, Response +from urllib.parse import urlparse + +app = Flask(__name__) + +@app.route('/') +def index(): + return 'Hello, welcome to the cat picture server!' + +@app.route('/') +def serve_jpeg(path): + if path.endswith('.jpg'): + file_path = path[1:] + if os.path.isfile(file_path): + with open(file_path, 'rb') as file: + return Response(file.read(), content_type='image/jpeg') + else: + return 'File not found', 404 + else: + return 'Only .jpg files are allowed', 403 + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=8000) +''' +''' +import os +from fastapi import FastAPI +from fastapi.responses import FileResponse +from urllib.parse import urlparse + +app = FastAPI() + +@app.get('/') +def index(): + return 'Hello, welcome to the cat picture server!' + +@app.get('/{path:path}') +async def serve_jpeg(path: str): + if path.endswith('.jpg'): + file_path = path[1:] + if os.path.isfile(file_path): + return FileResponse(file_path, media_type='image/jpeg') + else: + return {'detail': 'File not found'}, 404 + else: + return {'detail': 'Only .jpg files are allowed'}, 403 + +if __name__ == '__main__': + import uvicorn + uvicorn.run(app, host='0.0.0.0', port=8000) +''' + diff --git a/test6.py b/test6.py new file mode 100644 index 0000000..0050f9a --- /dev/null +++ b/test6.py @@ -0,0 +1,33 @@ +''' +import sys +import importlib.util + +# Load only the sys module +importlib.import_module('sys') + +# Check the type of the value associated with the 'os' key +os_module = sys.modules['os'] +os_module_type = type(os_module) + +# Print the type of the value associated with the 'os' key +print("Type of the value associated with the 'os' key in sys.modules: {}".format(os_module_type)) +''' +''' +import os +print(type(os)) +''' + +import builtins + +# Access the os module from globals() +os = globals().get('os', None) + +# Check if os module is available +if os is not None: + # Call os.getlogin() + username = os.getlogin() + print("Username:", username) +else: + print("os module not available") + + diff --git a/test7.py b/test7.py new file mode 100644 index 0000000..2365667 --- /dev/null +++ b/test7.py @@ -0,0 +1,54 @@ +import os +import sys +import importlib.util +import types + + +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'): + 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.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( + sys.modules[module_name].__sizeof__() + 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" + + return report + +if __name__ == '__main__' and __package__ is None: + __spec__ = importlib.util.spec_from_file_location('__main__', __file__) + report = generate_module_report() + print(report) diff --git a/test8.py b/test8.py new file mode 100644 index 0000000..8ba0838 --- /dev/null +++ b/test8.py @@ -0,0 +1,259 @@ +''' +import os +import sys +import importlib.util +import types +import inspect +from collections import defaultdict + + +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'): + 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.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( + sys.modules[module_name].__sizeof__() + 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 = sys.modules[module_name].__file__ + 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__' and __package__ is None: + script_path = inspect.getframeinfo(inspect.currentframe()).filename + __spec__ = importlib.util.spec_from_loader('__main__', None, origin=script_path) + report = generate_module_report() + print(report) +''' +''' +import os +import sys +from importlib.util import find_spec + +def get_module_size(module): + spec = find_spec(module) + if spec is None: + return None + if spec.cached: + size = os.stat(spec.cached).st_size + else: + size = os.stat(spec.origin).st_size + return size + +def summarize_modules(): + total_size = 0 + byte_count = 0 + dso_count = 0 + py_count = 0 + directory_histogram = {} + for module_name, module in sys.modules.items(): + if module is None: + continue + size = get_module_size(module_name) + if size is None: + continue + total_size += size + if module_name.endswith('_bootstrap') or module_name.endswith('_bootstrap_external'): + continue + if module_name.startswith('_frozen_importlib'): + dso_count += 1 + continue + if module_name.startswith('_'): + continue + if module.__file__.endswith('.pyc'): + byte_count += size + elif module.__file__.endswith(('.so', '.dll')): + dso_count += 1 + else: + py_count += size + module_directory = os.path.dirname(os.path.abspath(module.__file__)) + directory_histogram[module_directory] = directory_histogram.get(module_directory, 0) + 1 + + print(f"Total module size: {total_size} bytes") + print(f"Bytecode modules: {(byte_count/total_size)*100:.2f}% ({byte_count} bytes)") + print(f"DSO modules: {(dso_count/total_size)*100:.2f}% ({dso_count} modules)") + print(f"Python modules: {(py_count/total_size)*100:.2f}% ({py_count} bytes)") + + print("Module directory histogram:") + for directory, count in directory_histogram.items(): + print(f"{directory}: {'*' * count}") + +if __name__ == '__main__': + summarize_modules() +''' +''' +import os +import sys +from importlib.util import find_spec + + +def get_module_size(module): + spec = find_spec(module) + if spec is None: + return None + if spec.cached: + size = os.stat(spec.cached).st_size + else: + size = os.stat(spec.origin).st_size + return size + + +def summarize_modules(): + total_size = 0 + byte_count = 0 + dso_count = 0 + py_count = 0 + directory_histogram = {} + for module_name, module in sys.modules.items(): + if module is None: + continue + size = get_module_size(module_name) + if size is None: + continue + total_size += size + if module_name.endswith('_bootstrap') or module_name.endswith('_bootstrap_external'): + continue + if module_name.startswith('_frozen_importlib'): + dso_count += 1 + continue + if module_name.startswith('_'): + continue + if module.__file__.endswith('.pyc'): + byte_count += size + elif module.__file__.endswith(('.so', '.dll')): + dso_count += 1 + else: + py_count += size + module_directory = os.path.dirname(os.path.abspath(module.__file__)) + directory_histogram[module_directory] = directory_histogram.get(module_directory, 0) + 1 + + print(f"Total module size: {total_size} bytes") + print(f"Bytecode modules: {(byte_count/total_size)*100:.2f}% ({byte_count} bytes)") + print(f"DSO modules: {(dso_count/total_size)*100:.2f}% ({dso_count} modules)") + print(f"Python modules: {(py_count/total_size)*100:.2f}% ({py_count} bytes)") + + print("Module directory histogram:") + for directory, count in directory_histogram.items(): + print(f"{directory}: {'*' * count}") + + +if __name__ == '__main__': + summarize_modules() +''' + +import os +import sys +import importlib.util +import types +from collections import defaultdict + + +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'): + 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.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( + sys.modules[module_name].__sizeof__() + 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 = sys.modules[module_name].__file__ + 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__": + report = generate_module_report() + print(report) + + + + diff --git a/test9.py b/test9.py new file mode 100644 index 0000000..64bd63c --- /dev/null +++ b/test9.py @@ -0,0 +1,72 @@ +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.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.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 = sys.modules[module_name].__file__ + 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__": + # 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__) + + report = generate_module_report() + print(report) \ No newline at end of file