STARTastro Python Workshops
Code Development & Github
Establishing good coding practice
Clearly define the purpose and goal of our code, and the relevant inputs and outputs
Map the work flow of your code in pseudocode, breaking down tasks into distinct functions
Document as you code, and set up test routines
Develop good troubleshooting practices
Port your code into a separate *.py file as you complete functions
Save, track, collaborate, and share with git/github
Embrace AI tools - after you've gotten the hang of things
Here's the situation…
You are inside a perfectly dark room that has one exit. You know there is a flashlight somewhere in the room, you just don't know where it is. Also, the walls are covered in poison ivy, so you don't want to touch them.
Write an algorithm that will get you out of the room safely and quickly.
HELP!
Clearly define the purpose and goal of our code, and the relevant inputs and outputs
Clearly define the purpose and goal of our code, and the relevant inputs and outputs
Goal: Get out of the room!
What are possible inputs?
What are possible outputs/actions?
Clearly define the purpose and goal of our code, and the relevant inputs and outputs
Goal: Get out of the room!
What are possible inputs?
Seeing (with light)
Feeling (with hands)
Hearing (with ears)
Smelling (with nose)
Tasting (with tongue)
What are possible outputs/actions?
Clearly define the purpose and goal of our code, and the relevant inputs and outputs
Goal: Get out of the room!
Cry for help
Stomp feet
Walk around (purposefully, randomly)
Reach out to find wall/door
What are possible inputs?
Seeing (with light)
Feeling (with hands)
Hearing (with ears)
Smelling (with nose)
Tasting (with tongue)
What are possible outputs/actions?
Reach down to find flashlight
Grab the flashlight
Turn flashlight on
Find the door
Open door and get out
Map the work flow of your code in pseudocode, breaking down tasks into distinct functions
Map the work flow of your code in pseudocode, breaking down tasks into distinct functions
Basic plan:
Map the work flow of your code in pseudocode, breaking down tasks into distinct functions
Basic plan:
Specific actions needed:
How should we move?
How do we avoid poison ivy?
Map the work flow of your code in pseudocode, breaking down tasks into distinct functions
Basic plan:
START
reach down for flashlight
Did we find it?
Turn on flashlight
Find the door
Go to door
Open door
Get out!
Take a random step
Y
N
Option 1
Map the work flow of your code in pseudocode, breaking down tasks into distinct functions
Basic plan:
START
reach down for flashlight
Did we find it?
Turn on flashlight
Find the door
Go to door
Open door
Get out!
Take a random step
Y
N
Find flashlight
Exit room
Move
Option 1
Map the work flow of your code in pseudocode, breaking down tasks into distinct functions
START
reach down for flashlight
Did we find it?
Turn on flashlight
Find the door
Go to door
Open door
Get out!
Make a noise and listen for echo
Are we near wall?
Take a random step
Take a step parallel to wall
Option 2
Y
N
N
Y
Find flashlight
Exit room
Move
Map the work flow of your code in pseudocode, breaking down tasks into distinct functions
START
reach down for flashlight
Did we find it?
Turn on flashlight
Find the door
Go to door
Open door
Get out!
Make a noise and listen for echo
Take a step following a pre-defined search pattern, but don't step into wall
Option 3
Y
N
Find flashlight
Exit room
Move
Document as you code, and set up test routines
Document as you code, and set up test routines
Define the objective and inputs/outputs of function
Explain purpose of conditionals
Document as you code, and set up test routines
Explain purpose of conditionals
DOCSTRING
Describes program and how to use it
Document as you code, and set up test routines
DOCSTRINGS allow you to get help on a function
Document as you code, and set up test routines
Test functions help ensure your code is working properly
Set up conditions where you know the answer, then test by using assert statements:
assert [conditional], [error message]
conditional
error message
just making sure function runs if called
Develop good troubleshooting practices
Pay attention to the error thread - track back to where in your code things are going wrong
Add in temporary print statements to understand what your variables are doing
Anticipate problems by adding in try…except statements
Make sure you're using a function correctly (see DOCSTRING)
Common solutions can often be found in stackoverflow
[sometimes] AI can help too
Port your code into a separate *.py file as you complete functions
A productive strategy is the develop and test functions in notebook, then move them into a python document and import in
escape.py code in Sublime Text
Save, track, collaborate, and share with git/github
Github is a web repository that allows you to share all kinds of files, but is particularly well suited for sharing code
git is a set of commands that allows you to track different versions of files and commit them to a repository (like Github!)
git+github is a powerful system to save, track, update, restore, collaborate, and share your code (and other stuff too!)
https://github.com/
Key steps to git/github
See https://docs.github.com/en/get-started/start-your-journey/hello-world
Branches, mergers, forks, etc. - there is a lot you can do with git/github!
Git Cheat Sheet
Explore AI tools - after you've gotten the hang of things
For many well-defined and constrained tasks, AI can generate functional code quickly and usually error-free - but be sure to test!
However, just stacking AI code together doesn't allow you to understand what the code is doing or how to code more efficiently - use wisely!
ChatGPT
Questions