Python Projects#
Q&A
Q: can we go more in depth on the importance of file paths and how they benefit us?
A: Yes! |We’ll start talking about this today in scripts/modules
Q: how do we see all the functions within a module available to us
A:dir(module)will display all of them
Q: Can you combine importing comands?
A: You can import more than one thing from a given module be separating using commas; You can import multiple things into a single notebook, but each would need its own import statement
Course Announcements
Due this week:
CL7 due Fri
Take E2
Activity: pandas#
The penguins dataset is a famous dataset provided within the seaborn package. It’s been included on PrairieLearn for you in the file penguins.csv.
Read the dataset in and tehn explore the dataset to answer the following questions:
How many different species of penguin are included in this dataset?
What is the average mass of penguins in this dataset?
(Bonus - time permitting) What is the average mass of female penguins in this dataset?
Include your answer on the Google form here: https://forms.gle/H1nPwkhR1QoGDn4i7
import pandas as pd
# if outside of PL
import seaborn as sns
df = sns.load_dataset('penguins')
# if inside PL, read file in using read_csv...
# How many different species of penguin are included in this dataset?
# What is the average mass of penguins in this dataset?
# What is the average mass of female penguins in this dataset
Python Projects#
introduce the project
scripts
modules
Project-based Course#
Today starts the transition to a project-based course!
Project Options#
Choose ONE:
OPTION ONE: Complete the final project
Two parts: Project (5pts) + Oral exam (15pts)
Project: Due 11:59 Sun 6/7 after week 10 (submitted on PrairieLearn or Canvas)
Oral Exam: 15min in-person during finals week sign-up (link also on Canvas Assignment and Piazza Post)
Pros: opportunity to learn the most, you’re in full control, opportunities for extra credit (going above & beyond; GitHub), can ask staff for help/ask questions throughout
Con: takes a lot more time (historical avg: 10h)
OPTION TWO: Complete the final exams
Two parts: in-person exam (15pts) + take-home (5pts)
in-person: 45min slot in TTC-CBTF during finals week; cumulative
take-home: untimed; released Fri of week 10; due Tues (6/9) of finals week (11:59 PM)
a guided mini-project (focus: scientfific computing, testing, code style, documentation)
Pro: takes less time (historical avg: 2.5h)
Caveats if you take the final exam:
The highest grade you can get in the course is an A (not an A+)
There is no additional opportunities for extra credit
You have to complete on your own (take-home is open Internet; closed other humans)
Pick one (project or exam), NOT both (We have lots of grading at the end of the quarter!). If you do both, I will use the lower score.
Project Overview#
must implement something using Python code that demonstrates you learned something in this course (not grading on complexity)
completed individually
uses good code practices
What you will turn in: folder on PrairieLearn OR zip file with your project on Canvas
>= 1 Jupyter notebook
includes project description
demonstrates how your project runs (likely only a few lines of code) OR describes how to run project
runs your testing suite
describes how you used (or didn’t) GenAI to complete your project
includes explanation of how you went above and beyond (if applicable)
>= 1 module (having an additional script is optional)
has at least three (3) unique/original functions or methods w/ documentation
a test file with >= 3 tests
Project Topics#
Encryption (A2)
Chatbots (A3)
Artificial Agents (A4)
A Data Analysis (A5)
Choose your own adventure (Develop your own project idea!)
To brainstorm: Think of something in your daily life you want to automate, something that would help you at work/in lab/etc., or a topic/game/activity you really like
Process:#
Complete project on 1) PrairieLearn OR 2) download template from website
Brainstorm an idea
Design what “pieces” (functions/classes/etc.) you need to execute that idea
Start writing code + tests
Submit on 1) PrairieLearn or 2) Canvas (zipped)
Take oral exam
Why template + Canvas?#
Certain packages do not work on PL (game packages; audio; GUI)
Working outside of PL will be helpful in the future
You are not dependent on the Internet working well
Project advice:#
Work steadily over time
Work to first create a minimal viable product
modular design
multiple, independent pieces
Rapid prototyping + testing
Why oral exam?#
In a GenAI world, you don’t need to prove you wrote the code, but you do need to understand it
We’ll ask questions about your specific project like:
explain this function
what does this part here do?
how does this work?
how would you change this to accomplish _____?
if we changed this to ______ instead, what would happen?
etc.
Project Grading#
Component |
Grade Value |
|---|---|
File Structure |
1% |
Text Descriptions |
4% |
Project Code |
5% |
Code Style |
5% |
Code Documentation |
5% |
Code Tests |
5% |
Oral Exam |
75% |
Extra Credit |
4% |
GitHub Extra Credit |
1% |
Extra Credit:#
Go beyond the requirements of the project / press yourself to learn something new (up to 4%)
Put your project on GitHub (1%)
Example Projects:#
Previous Examples: COGS18/Projects
Note: these were completed prior to GenAI so requirements were slightly different (No GenAI statement; had off-limit topics; used pytest instead of unittest for testing; no oral exam)
Activity: Final Projects#
Please complete the following so we have a sense of what people’s plans are. Your responses here are not binding; you can change your mind later: https://forms.gle/BU7eZUbLZGTgSwyF8
Python Files#
Script vs. Module File#
Scripts#
Module Files#
Remember: if you’re writing code, you cannot just click through to the file you want. You need to specify using code where the file you want is.
This is where understanding file paths is critically important.
Importing Custom Code I#
Why do this?
Having a whole bunch of functions in a Jupyter Notebook will start to get clutered.
Also, they’re not reusable later without copying them into your new notebook.
Modules avoid this!
module: remote.py#
If you want to practice using import with the remote example in lecture, store the code in the next cell in a text file saved as remote.py. Be sure this is saved in the same directory (folder) as the notebook from which you’re trying to call it.
def my_remote_function(input_1, input_2):
"""A function from far away.
This function returns the sum of the two inputs.
"""
return input_1 + input_2
def choice(list_to_choose_from):
"""Choose and return an item from a list.
Notes: I am a custom choice function: I am NOT from `random`.
Hint: my favorite is the last list item.
"""
return list_to_choose_from[-1]
class MyNumbers():
kind_of_thing = 'numbers'
def __init__(self, num1, num2):
self.num1 = num1
self.num2 = num2
def add(self):
return self.num1 + self.num2
def subtract(self):
return self.num2 - self.num1
# Import some custom code
from remote import my_remote_function
# Investigate our imported function
my_remote_function?
# Run our function
my_remote_function(2, 1)
Importing Custom Code II#
# Import a class class from an external module
from remote import MyNumbers
# Define an instance of our custom class
nums = MyNumbers(2, 3)
type(nums)
# Check
nums.add()
# Check the definition of the code we imported
nums.add??
Name Conflicts#
from random import choice
# choice is currently from random module
choice?
choice([1, 2, 3, 4, 5])
from remote import choice
# now it's from my remote module
choice?
choice([1, 2, 3, 4, 5])
While you can have functions with the same name in two different places…do your best to avoid this.
It’s Python legal but bad for your nerves.
A note on *#
If you see from module import * this means to import everything (read as ‘from module import everything’).
This is generally considered not to be the best, as it is then unclear in your code exactly where the functionality came from.
# a valid way to import
from random import choice
choice([2,3,4])
# a valid way to import
import random
random.choice([2,3,4])
## AVOID THIS; WHY I DIDN'T PUT IT IN YOUR NOTES; DON'T DO IT
from random import *
choice([2,3,4,])
script: remote_script.py#
If you want to run your own script example in lecture, store the code in the next cell in a text file saved as remote_script.py. Be sure this is saved in the same directory (folder) as the notebook from which you’re trying to call it.
from remote import MyNumbers
nums = MyNumbers(2, 3)
print("nums value: ", nums.num1, nums.num2)
print("nums type: ", type(nums))
# run the script
!python remote_script.py
Activity: scripts & modules#
Please complete the Google Form: https://forms.gle/E8RzoFQiWA1DVfcA8