diff --git a/Day 9 homework - Sloetjes.md b/Day 9 homework - Sloetjes.md index 49eaf06..ba1f59e 100644 --- a/Day 9 homework - Sloetjes.md +++ b/Day 9 homework - Sloetjes.md @@ -83,26 +83,67 @@ print(login) ``` import sys -import os.path +import os -# Classify each module based on its __file__ attribute -bytecode_modules = [] -dso_modules = [] -source_modules = [] -for module_name, module in sys.modules.items(): - if module is None or not hasattr(module, '__file__'): - continue - filename = module.__file__ - if filename.endswith('.pyc'): - bytecode_modules.append(module_name) - elif filename.endswith(('.so', '.dll')): - dso_modules.append(module_name) - else: - source_modules.append(module_name) +def get_module_stats(): + byte_count = 0 + dso_count = 0 + py_count = 0 -# Calculate the percentages of each module type -total_modules = len(sys.modules) -bytecode_percent = len(bytecode_modules + for name, module in sys.modules.items(): + if module is None: + continue + + filename = getattr(module, '__file__', None) + if not filename: + continue + + if filename.endswith('.pyc') or filename.endswith('.pyo'): + byte_count += 1 + elif os.path.splitext(filename)[-1] in ('.so', '.dll'): + dso_count += 1 + else: + py_count += 1 + + total_count = byte_count + dso_count + py_count + + byte_percent = byte_count / total_count * 100 + dso_percent = dso_count / total_count * 100 + py_percent = py_count / total_count * 100 + + return { + 'bytecode': { + 'count': byte_count, + 'percent': byte_percent, + }, + 'DSO': { + 'count': dso_count, + 'percent': dso_percent, + }, + 'python': { + 'count': py_count, + 'percent': py_percent, + }, + 'total': total_count, + } + +def print_module_stats(): + stats = get_module_stats() + + print(f'Total modules: {stats["total"]}') + print(f'Bytecode modules: {stats["bytecode"]["count"]} ({stats["bytecode"]["percent"]:.2f}%)') + print(f'DSO modules: {stats["DSO"]["count"]} ({stats["DSO"]["percent"]:.2f}%)') + print(f'Python modules: {stats["python"]["count"]} ({stats["python"]["percent"]:.2f}%)') + +if __name__ == '__main__': + print_module_stats() +``` +###### Output: +``` +Total modules: 1011 +Bytecode modules: 0 (0.00%) +DSO modules: 51 (5.04%) +Python modules: 960 (94.96%) ``` **10. Add a feature to the script which looks at `file` for each module, trims the filename to get the directory name, then prints out a histogram of how many modules are in each directory name. If you can't find a text histogram output library in anaconda's built-in modules, just print out the `*` character for each module.**