diff --git a/Day 9 homework.md b/Day 9 homework.md new file mode 100644 index 0000000..ba64a88 --- /dev/null +++ b/Day 9 homework.md @@ -0,0 +1,12 @@ +Today's homework will be to build a tool to introspect Python packages, from the perspective of the currently running Python interpreter. + +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. +2. Create another function that uses the last function to see how many bytes are in all the modules listed in sys.modules. +3. Run your Python interpreter like you would normally, using Jupyter, and run the last function. How many bytes are being used? +4. Now, run the same function, but start the Python interpreter in Powershell (Windows), or the regular shell (MacOS/Linux) with the `-E` flag, which ignores environment variables and should only load the modules that came with the original build of the Python interpreter. How many bytes are being used now? +5. With only the `sys` module loaded, look at the `sys.modules` dict. What is the type of the value of the `os` key? +6. Import the os module. What happens when you type `type(os)`? +7. Restart the Python interpreter. Can you figure out how to call `os.getlogin()` without saying `import os`? +8. If you can, explain why this works and quote the relevant sections of section 5 in the language reference. +9. Using the functions you created above and `sys.modules`, create a module designed to be run from the command line (`python -m myscript.py`) that generates a report summarizing the module environment, and break down the percentages of bytecode modules, DSO modules (these have `__file__` end in `.so` or `.dll`), and normal python code. +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. \ No newline at end of file