Code Projects¶
modular design
minimal viable product
rapid prototyping
project organization
refactoring
Project Check-In¶
Do you have an idea of what you want to do for your project?
A) Absolutely no Idea
B) Some idea, but not sure
C) Sort of
D) Fairly good idea
E) I know exactly what I want to do
How much code do you have written for your project?
A) None
B) A little or some pseudocode
C) Some
D) A lot
E) Most (I’m mostly or completely done)
How confident do you feel about what you need to do to finish the project?
A) I have no idea what to do from here
B) I have some idea of how to finish the project
C) I have a reasonable idea of what to do from here
D) I have a very good idea of what to do
E) I completely know / am basically finished
Modular Design¶
Minimal Viable Product¶
Rapid Prototyping¶
Some examples¶
Chatbot: start and end a chat
Data Analysis: read a dataset in, make a basic plot
Car Inventory: object with method that adds a car to the Inventory
Artificial Agents: simple bot moves around randomly
Project Organization¶
Organizing Code¶
Notebooks: good for interactive development
For when seeing the inputs and outputs of code running needs to be seen start to finish
file ends in
.ipynb
Modules: for storing mature Python code, that you can import
you don’t use the functions in there, just define them
file ends in
.py
Scripts: a Python file for executing a particular task
this takes an input and does something start to finish
file ends in
.py
Project Workflow¶
Typical project workflow:
Develop code interactively in a Jupyter notebook/text editor
As functions & classes become mature, move them to Python files that you then
import
As you do so, go back through them to check for code style, add documentation, and write some code tests
At the end of a project, (maybe) write a standalone script that runs the project
Code Review¶
This is what you’ll have the chance to do in coding lab this week!
Refactoring¶
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
Refactoring Example: Chatbot¶
Clicker Question #1¶
How many “things” does this function accomplish?
A) 0 | B) 1 | C) 2 | D) 3 | E) 4+
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()
INPUT : hi
OUTPUT: Huh?
INPUT : who are you?
OUTPUT: I'm too shy to answer questions. What do you want to talk about?
INPUT : ok
OUTPUT: Okay
INPUT : huh?
OUTPUT: I'm too shy to answer questions. What do you want to talk about?
INPUT : quit
OUTPUT: Bye!
Refactored Example: Chatbot¶
What this function does:
takes an input
checks if input is a question
checks if input is supposed to end the chat
return appropriate response if question, end chat, or other
That’s four different things! Functions should do a single thing…
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_list):
"""identify if user says 'quit' in input and end chat"""
if 'quit' in input_list:
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()
INPUT : hi
OUTPUT: Thanks!
INPUT : how are you?
OUTPUT: I'm too shy to answer questions. What do you want to talk about?
INPUT : python
OUTPUT: Thanks!
INPUT : quit
OUTPUT: Bye
Project Notes¶
Write some code
Test to make sure it works
Check code style (naming, spacing, etc.)
Add documentation
Move to module (if necessary)
Add unit tests
Note on imports
: If you want to be able to use modules (imports) within a module/script, be sure to import it at the top.