Quiz

  1. I think that I'm having a hard time understanding how everything works together-Could you walk through a few examples on how different parts of the people app work together to process a request and what's the output?

    I'm sure you're not alone. That's the single most frequently asked question.

    In CS 111, you understand small blocks of code. In 230, medium blocks of code, maybe larger. In 304, we understand large, multi-part programs.

  2. Can you give us a brief review on the cursor and execute methods.

    A cursor is a object with methods to send SQL to the server, and fetch the results back. Those are execute and fetchone/fetchall respectively.

    
        curs = dbi.dict_cursor(conn)
        # this uses a prepared query, since the 'month' comes
        # from an untrustworthy source
        curs.execute('''
            select name,birthdate from person 
            where month(birthdate) = %s''',
                     [month])
        return curs.fetchall()
    
    
  3. When we import code, are we really ""adding code"" to our program, or are we saving it to the cache to read from?

    When we run a program, code gets read from disk and goes into memory (RAM). Importing reads more code from disk into RAM.

    So, yes, you are really adding code to your program. Your program happens to be build out of modules.

    We can also read other text files into our program, such as template files or CNF files. Instead of reading them each time we want them, we can save them in memory. That's also called caching, but a different kind of caching than browser caches.

  4. Irrelevant, but can you talk about caching?

    Caching is a ubiquitous technique to avoid doing something repeatedly. Here is a kind of cache:

    
    def long_complicated_computation(x):
        # code omitted for sake of sanity
        return ans
    
    compute_cache = None
    
    def compute(x):
        global compute_cache
        if compute_cache is not None and compute_cache[0] == x:
            return compute_cache[1]
        else:
            y = long_complicated_computation(x)
            compute_cache = (x, y)        
            return y
    
    
  5. Can I get a clarification on why the people app will be so useful to us? It's very helpful to see as an example/walk-through, but will we be importing it a lot? (I also feel like I will understand this better when we talk it through in class)

    Your assignments and projects will be built in a similar manner, out of similar parts, using similar coding techniques.

    You will never import it.

    Metaphor: You will be building cars in this course. This is a car that I built.

  6. I would also appreciate more practice using @app.route().

    You will practice it a zillion times in this course. If your project has an "/profile" page, you will write something like:

    
    @app.route('/profile')
    def profile():
        # create profile page for current logged_in user        
        return render_template('profile.html', username = ...)
    
    
  7. Why is '__name__' a magic invocation?

    That's what the Python language gives us. It's a nice way to allow one file to be either a module, useable in other programs, and also to be a program itself. Sometimes, the program tests the module, but sometimes it does something useful.

  8. Does Flask() alone actually do anything? Does Flask(__name__) have other decorator methods besides .route() and .before_first_request?

    Flask() is the constructor for the app object, which is the main organizing part of our program: our app.

    The Flask module has lots of decorators, even as it has discarded before_first_request.

  9. Also, I think I get some error associated with shared libraries when trying to run the commands for running the people_app.

    Sorry. I've fixed that now.

  10. I feel confused on the html template. Can you explain the tags you use in people.html?

    Sure. Let's look at the template file

  11. Do we ideally only directly put HTML code in app.py for HTTP methods? or are they used in other cases as well?

    We will almost never put HTML in our app.py files. That was baby steps before we get to the real stuff.

  12. For parametrized routes like this: @app.route('/people-born-in/<int:month>'), is the `int` part in the brackets or does it just convert the month to type `int` since user input is received as a string?

    Yes, it's inside the brackets. It tells Flask to convert the string to an int.

  13. Not a question, but I really liked the format of this reading! In particular, going through small sections of the code and explaining things line-by-line helped me connect some of the loose concepts we've discussed in class to this concrete example.

    I'm so glad! It was labor intensive, so I'm happy that it helped you.