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