Organizing code
Lab 5 follow-up
Did the profiling reveal anything surprising/interesting/useful?
How have you been organizing code (in this course and before)?
…within files?
…between files?
Split your logic:
- Data ingestion (Extract)
- Data cleaning/processing (Transform)
- Presentation (Streamlit stuff)
How?
Files! Things you import!
-
Show Python's module search path.
import sys sys.path
-
Look in those directories.
-
Look at
csv.py. -
Make a
csv.pyfile the current directory.print("Aidan csv")
-
Try
import csv.
Let's refactor phone code from last lecture.
Simulate an API call:
from time import sleep
sleep(5)See get_data(). How do we keep the tests from being slow?
Folders!
How have you been organizing data?
a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data
Encyclopedia of Computer Science, 2003
What data structures exist in Python?
Let's refactor the phone code into classes.
Note to self: domestic vs. international numbers
pytest --cov --cov-report html
open htmlcov/index.html- The result of control flow
- Not the Git kind
Revisit expectations
- "x = function(variable): Wouldn't this already store a value? Then what is the purpose of making the class?"
- "How do Python classes allow us to better map real-world concepts into code compared to using basic data structures like nested dictionaries or lists?"
- "How should developers decide when to use generators instead of loading data into memory directly?"
- "When should I choose a generator over a list?"