Update page 'Day 9 homework - Sloetjes'

xuanyi.wang
2023-04-19 04:38:34 +02:00
parent 935d0d9f2f
commit 6085957086
+17 -1
@@ -1,4 +1,4 @@
1. Use `importlib.util.find_spec` to create a function that takes a Python module or package name, and returns the total number of bytes in the cached `.pyc` file, or the total number of bytes in the `.py` file if there is no cached bytecode. You can use the os.stat command to get the size of a file. **1. Use `importlib.util.find_spec` to create a function that takes a Python module or package name, and returns the total number of bytes in the cached `.pyc` file, or the total number of bytes in the `.py` file if there is no cached bytecode. You can use the os.stat command to get the size of a file.**
``` ```
import importlib.util import importlib.util
@@ -28,3 +28,19 @@ def get_module_size(module_name):
filename = module_path filename = module_path
return os.stat(filename).st_size return os.stat(filename).st_size
``` ```
**2. Create another function that uses the last function to see how many bytes are in all the modules listed in sys.modules.
**
```
def get_all_modules_size():
total_size = 0
for module_name, module in sys.modules.items():
if not hasattr(module, '__file__'):
continue
module_path = get_module_path(module_name)
if module_path is not None:
module_size = get_module_size(module_name)
total_size += module_size
return total_size
print(get_all_modules_size())
```