Skip to content

Instantly share code, notes, and snippets.

@joelburton
Created August 10, 2020 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelburton/5bec1996f0bcbf0db04cbccf93b8bc0d to your computer and use it in GitHub Desktop.
Save joelburton/5bec1996f0bcbf0db04cbccf93b8bc0d to your computer and use it in GitHub Desktop.
Study Hall covering breakpoints and OO.

Study Hall Topics

Resources / URLs / Endpoints

http://api.joelburton.com/recipes/brownies?color=red

URL: "resource" : "/recipes/brownies"

REST "resource" (subject)

  • "brownies" : resource recipe
  • "recipes" : resource of all my recipes

HTTP methods ("verbs") -> (verb)

GET /recipes => list of my resources of recipes

combo of verb+subject resource + method

different endpoint:

GET /recipes POST /recipes (creates a resource in recipes) PUT /recipes/brownies (update brownie resource)

safe:

  • doesn't change anything
  • only safe method => GET
  • all else => unsafe

idempotent:

  • doing it multiple times same as once
  • GET / PUT / PATCH / DELETE
<form method="POST">
  <input name="book-title">
  <button>Search</button>
</form>
         => GET /books?book-title=the+bell+jar
         => POST /books

Relationships

  • "is-a" : "joel is a human", "a human is a mammal"

  • "has-a" : "an invoice has a client [eg i bill apple]"

  • "has-many" : "my company has many open invoices"

company -> invoices <- client

  • /joelco/invoices/500 { clientcode: "apple" }

  • /joelco/clients/apple {name: ..., address: ....}

Sessions, Cookies, and db.session

db.session

entirely unrelated to browser sessions

  • it just wraps a database transaction
  • you have to .commit to keep work or .rollback to clean fouled transactions

Cookies

A way we can maintain "state" over HTTP: have browser remember & re-present

Session

An abstraction over using cookies directly — uses them under the hood

Browser stores one (or more) cookies that hold the session data

Flask handles sending/validating authenticity of sessions

A magic dictionary because, even though the session object you import is global, it is magically about this browser

Debugging

  • breaking or reinforcing your mental model
  • why and how to read error messages
  • stacktrace/backtrace/traceback
  • what debuggers can answer for us:
    • where am and how did I get here?
    • what was I called with?
    • why was I called with that?
    • what am I going to return?
    • what are my variables?
    • how would my program work differently if x was equal to 32?
    • watch code run!

Some commands I’ll show:

pip install ipdb   # a prettier/nicer pdb for python
export PYTHONBREAKPOINT=ipdb.set_trace
flask run --without-threads --no-reload

Object Orientation

Big ideas in OO:

  • abstraction
  • encapsulation
  • polymorphism

Different areas:

  • “data oo”
  • “specialized subclass/machine”
    • we will briefly visit this while learning how to absolutely crush your siblings in hangman

Unit Testing in Python

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment