Quiz

  1. Could we go over python dictionaries again?

    Sure. Python dictionaries are store key/value pairs. Some examples:

    
        d1 = dict(name='fred', age=17)
        d1['age'] # evaluates to 17
        d1['house'] = 'gryffindor'  # stores a new pair
        d1['addr'] # raises an exception
        d1.get('addr') # evaluates to None
        d1.pop('age') # removes that pair
    
    
    W3Schools has a good introduction to Python dictionaries
  2. When should we use custom exceptions vs. built-in exceptions?

    The purpose of an exception is to report what went wrong. If there's a built-in exception that does a good job of describing that; use it. E.g. DivideByZero. But InvalidCreditCard would have to be a custom exception.

  3. Can we practice the .execute, I'm still a little confused

    Sure. That's part of the PyMySQL API. Let's try the following in a Python REPL:

    
    import cs304dbi as dbi
    >>> dbi.conf('wmdb')
    >>> conn = dbi.connect()
    >>> curs = dbi.dict_cursor(conn)
    >>> curs.execute('''select * from movie where title like '%Harry%'; ''')
    5
    >>> vals = curs.fetchall()
    >>> vals[0]['title']
    'When Harry Met Sally'
    >>> curs.execute('''select * from movie where title like %s; ''', ['%Potter%'])
    4
    >>> vals = curs.fetchall()
    >>> vals[0]['title']
    'Harry Potter and the Prisoner of Azkaban'
    >>> 
    
    
  4. How is the app.py module connected or related to pymysql and cs304dbi? In other words, could you go over the diagram you included at the beginning of the People App reading?

    I'd be glad to: overview

    the import statement adds new abilities to our python program. XKCD Python

  5. Can we go over quiz question 4 about url_for() and make handler functions clear?
        When we use the url_for() function
        A. We supply the URL we want
        B. We supply the name of the handler function
        C. We supply a route
        D. All of the above
        

    Let's see it in action. Here's the code for app.py

    So, you can see that the argument to url_for is the name of the handler function.

    A handler function is the function that is called when the browser sends a request with the paired endpoint (url).

  6. Could you talk more about the route decorator?

    Yes. The route decorator associates an endpoint with a function.

    The endpoint can have parameters, which Flask will parse for us.

    The decorator can also specify what HTTP methods are supported. Default is methods=['GET']

  7. Can you please go over people_born_in_month() handler function again? / Can we please go over parameterised routes and handler functions?

    Sure. We'll look at the code

    This function is invoked when a request comes in that matches /people-born-in/NNN, where NNN is some number.

    Flask parses the URL and invokes our function with the number as an argument.

    Our function is responsible for the response to the browser.

  8. what is the difference between these notations: {name} and {{name}}?

    Great question. The idea of templating is common.

    The single braces is for f-strings.

    The double braces is for Jinja2, our file templating language.

  9. When I try to access and run the app.py file in ~cs304flask/pub/downloads/people_app (in the virtual environment), I receive an error:
    ModuleNotFoundError: No module named 'cs304dbi'

    Last time, we copied the cs304dbi.py file into our virtual environment. Maybe you missed that step?

  10. I think I'm overall a little confused but practice should clear up my confusion / I don't have any specific questions, but think I'll feel more comfortable after reviewing this more

    We'll do some practice!