1
Day 9 homework
j edited this page 2023-04-18 11:28:27 +02:00
Today's homework will be to build a tool to introspect Python packages, from the perspective of the currently running Python interpreter.
- Use
importlib.util.find_specto create a function that takes a Python module or package name, and returns the total number of bytes in the cached.pycfile, or the total number of bytes in the.pyfile if there is no cached bytecode. You can use the os.stat command to get the size of a file. - Create another function that uses the last function to see how many bytes are in all the modules listed in sys.modules.
- Run your Python interpreter like you would normally, using Jupyter, and run the last function. How many bytes are being used?
- Now, run the same function, but start the Python interpreter in Powershell (Windows), or the regular shell (MacOS/Linux) with the
-Eflag, 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? - With only the
sysmodule loaded, look at thesys.modulesdict. What is the type of the value of theoskey? - Import the os module. What happens when you type
type(os)? - Restart the Python interpreter. Can you figure out how to call
os.getlogin()without sayingimport os? - If you can, explain why this works and quote the relevant sections of section 5 in the language reference.
- 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.soor.dll), and normal python code. - 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.