Code Projects & Wrap Up#

Q&A

Q: what is the difference between using # and using the docstrings?
A: Comments (#) are for developers - people understanding/reading the code; Docstrings are for code users

Q: why do we type the description in quotes instead of hashtags?
A: The quotes indicate that it is official documentation, making it available outside of the code cell, as it’s attached to the object. Comments are only viewable when looking directly at the code

Course Announcements

  • CL8 due Friday

  • Complete either final project or exam:

    • Final Project:

      • submit project by Sun (PL or Canvas)

      • take oral exam next week at assigned slot

    • Final Exam:

      • take in-person at assigned slot during finals week

      • submit take-home by Tues of finals week (released this Fri at 5P)

  • Post-Course Survey will also be available Fri at 5PM; ideally you’d complete this once you’re done with everything - due Sun 6/14 after finals week; link will be in Canvas assignment

  • Please complete your SETs

Today’s Plan#

  • Final Project

  • Final Exam

  • Code Projects

  • Wrap Up

Final Project Summary#

Required/Graded on:

  • Notebook: text (intro, GenAI statement), demo, tests

  • module: code (at least 3 functions/methods), documentation (numpy-style docstrings, comments), code style

  • test file: unit tests that meaningfully test at least 3 functions/methods, uses unittest

  • oral exam: ability to explain/answer questions about your submitted project

Notes/reminders:

  • delete unecessary files

  • should be clear how your project runs or how to run your project from notebook - we start there with grading

  • oral exam is conversational - goal is to demonstrate you understand the code you turned in

Extra Credit:

  • going above and beyond requirements (up to 4%); specified in notebook

  • putting project on GitHub - you’ll submit URL to this on post-course survey if you do it

In-Person Exam#

What to Bring:

  • An ID

  • Your Brain

Reminders:

  • You will put your belongings in a locker; leave time for this

  • You are NOT allowed to bring in water/coffee

  • They will provide scratch paper & a calculator

  • You’ll need to sign into your PL

Location: TTC-CBTF - Applied Physics & Mathematics (AP&M) B349 (basement)

Exam Window: 6/6-6/13; 45 min

In-Person Final Exam Summary (15 pts; 45 min)#

  • 12 MC (6 pts total; 0.5 pt each)

    • Topics: Variables, Operators, Functions, Conditionals, Loops, Methods, Classes, Imports/File Paths, Scientific Computing (2), Unit Testing, Documentation, Code Style

  • 6 short answer (5 pts total)

    • Topics: Variables; Operatorsx2; functions, loops, method

  • 1 Code Reading & Debugging (2 pts): class

  • Testing Question (2 pts; unittest)

Notes:

  • Practice exam will be very good practice for all of these

  • Information Provided:

    • pandas functions/methods

    • unittest framework w/ list of assert statements discussed in class

Take-home Final Exam Summary (5 pts; untimed; due Tues of finals week)#

  • focuses on material after E2

  • completed on PL

    • unlimited attempts for autograded portion

    • manual grading happens after - these will show up as 0pts while taking the exam…we’ll grade them after you complete

  • start in the provided notebook; will guide you what to do

  • The mini-project will:

    • use scientific computing (pandas)

    • require you to write code for a single class

    • use unittest to test two methods

    • class and code tests will be stored in a .py module

    • require docstrings and good code style

Topics from E1 & E2 (can’t forget)#

  • Variables

  • Operators

  • Functions

  • Conditionals

  • Loops

  • Objects/Classes

Post E2 Topics#

Scientific Computing#

  • numpy arrays

    • general use (homogenous, numeric information stored in arrays

    • how to use methods

  • pandas

    • DataFrame (conceptual understanding; purpose)

    • how to use methods

Note: do NOT need to memorize numpy/pandas methods

Testing#

  • unittest framework

  • how to write test functions using unittest

Code Style#

  • PEP8 Guidelines discussed in class

  • Be able to edit your own code for code style

Documentation#

  • why we document our code

  • different purpose between code comments and docstrings

  • basics and components of numpy-style docstrings

Note: you do NOT need to write docstrings from scratch on in-person

Code Projects#

What if you wanted to make a chatbot for your final project? You could…

import random 

def have_a_chat():
    """Main function to run our chatbot."""
    
    chat = True

    while chat:

        # Get a message from the user
        msg = input('INPUT :\t') 
        out_msg = None
        
        # Check if the input is a question
        input_string = msg
        if '?' in input_string:
            question = True
        else:
            question = False

        # Check for an end msg 
        if 'quit' in input_string:
            out_msg = 'Bye!'
            chat = False
            
        # If we don't have an output yet, but the input was a question, 
        # return msg related to it being a question
        if not out_msg and question:
            out_msg = "I'm too shy to answer questions. What do you want to talk about?"

        # Catch-all to say something if msg not caught & processed so far
        if not out_msg:
            out_msg = random.choice(['Good.', 'Okay', 'Huh?', 'Yeah!', 'Thanks!'])

        print('OUTPUT:', out_msg)
have_a_chat()

….but the above is doing way too much in a single function. It’s hard to debug.

What the above function does:

  1. takes an input

  2. checks if input is a question

  3. checks if input is supposed to end the chat

  4. return appropriate response if question, end chat, or other

That’s four different things! Functions should do a single thing…

Refactoring#

Refactoring is the process of restructuring existing computer code, without changing its external behaviour.

Think of this as restructuring and final edits on your essay.

Nesting Functions - If you have a whole bunch of functions, if statements, and for/while loops together within a single function, you probably want (need?) to refactor.

Clean functions accomplish a single task!

DRY: Don’t Repeat Yourself

Refactored Example: Chatbot#

def get_input():
    """ask user for an input message"""
    
    msg = input('INPUT :\t')
    out_msg = None
    
    return msg, out_msg

def is_question(input_string):
    """determine if input from user is a question"""
    
    if '?' in input_string:
        output = True
    else:
        output = False
    
    return output

def end_chat(input_string):
    """identify if user says 'quit' in input and end chat"""
    
    if 'quit' in input_string:
        output = 'Bye'
        chat = False
    else:
        output = None
        chat = True
        
    return output, chat

def return_message(out_msg, question):
    """generic responses for the chatbot to return"""
        
    # If we don't have an output yet, but the input was a question, 
    # return msg related to it being a question
    if not out_msg and question:
        out_msg = "I'm too shy to answer questions. What do you want to talk about?"

    # Catch-all to say something if msg not caught & processed so far
    if not out_msg:
        out_msg = random.choice(['Good.', 'Okay', 'Huh?', 'Yeah!', 'Thanks!'])
        
    return out_msg

def have_a_chat():
    """Main function to run our chatbot."""
    
    chat = True

    while chat:

        # Get input message from the user
        msg, out_msg = get_input()
        
        # Check if the input is a question
        question = is_question(msg)
         
        # Check for an end msg 
        out_msg, chat = end_chat(msg)
       
        # specify what to return
        out_msg = return_message(out_msg = out_msg, question = question)
        
        print('OUTPUT:', out_msg)
have_a_chat()  

Demo: Project Files#

  • module (imports!)

  • test file (unittest)

Questions?#

What questions remain?

Wrap Up#

COGS 18: The Goal#

To teach you a skill - of how to do things with Python.

…but also: to help you approach unfamiliar tasks, think critically, and engage with technology.

Also, you’ve been more formally trained than many people out in the world programming.

Where We’ve Been:#

  • Python & Jupyter

  • Variables

  • Operators

  • Conditionals

  • Functions

  • Lists, Tuples & Dictionaries

  • Loops

  • Methods

  • Objects & Classes

  • Scientific Computing

  • Documentation, Code Style, Code Testing

  • Putting it all together in a Project!

Activity: Wrap-up#

Please complete the following Google Form: https://forms.gle/4ATEMp31jnuCg51M7

How to Continue with Coding#

  • Write Code

  • Read Code

  • Learn and follow standard procedures

  • Do code reviews

  • Interact with the community

  • Build a code portfolio

Where do you go from here? (things)#

  • git & GitHub - version control, code!

  • Code in production: deubuggers (pdb)

  • Dependancy-management (PyPI, conda)

  • IDEs: VSCode

  • GenAI: Codex, Claude Code

  • if want_more_data_science:

    • go to COGS 9

  • if you want to do more_data_science:

    • and Python: take COGS 108

    • and R: take COGS 137

  • if you want machine learning:

    • COGS 118 series and COGS 185

  • if interested_in_research:

    • look for labs, tell them you can code

  • if you want_something_else:

    • go do it!

Acknowledgments#

Thank you to the TAs for their help this quarter.

Thank you to you students for you time, effort, and patience.