forked from SVIG/python-bootcamp
update
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user