Skip to content

Latest commit

 

History

History
172 lines (96 loc) · 2.99 KB

File metadata and controls

172 lines (96 loc) · 2.99 KB

Lecture 6

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?


Project tip

Split your logic:

  • Data ingestion (Extract)
  • Data cleaning/processing (Transform)
  • Presentation (Streamlit stuff)

How?


Modules

Files! Things you import!


Search path

  1. Show Python's module search path.

    import sys
    sys.path
  2. Look in those directories.

  3. Look at csv.py.

  4. Make a csv.py file the current directory.

    print("Aidan csv")
  5. Try import csv.


Refactoring

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?


Packages

Folders!


How have you been organizing data?


Data structures

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?


Abstraction


Object-Oriented Programming (OOP)

Intro from Python course


Let's refactor the phone code into classes.

Note to self: domestic vs. international numbers


Code/test coverage

pytest-cov


pytest --cov --cov-report html
open htmlcov/index.html

Branching


Complexity



Revisit expectations


Object-Oriented Programming (OOP) & Classes

  • "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?"

Generators / Memory Management

  • "How should developers decide when to use generators instead of loading data into memory directly?"
  • "When should I choose a generator over a list?"