1 of 26

STARTastro Python Workshops

Code Development & Github

2 of 26

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

3 of 26

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!

4 of 26

Clearly define the purpose and goal of our code, and the relevant inputs and outputs

5 of 26

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?

6 of 26

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?

7 of 26

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

8 of 26

Map the work flow of your code in pseudocode, breaking down tasks into distinct functions

9 of 26

Map the work flow of your code in pseudocode, breaking down tasks into distinct functions

Basic plan:

  • Feel around for flashlight
  • If I find it, use it to get to door and get out
  • If I don't find it, move around and try again

10 of 26

Map the work flow of your code in pseudocode, breaking down tasks into distinct functions

Basic plan:

  • Feel around for flashlight
  • If I find it, use it to get to door and get out
  • If I don't find it, move around and try again

Specific actions needed:

  • turn on flashlight
  • find door
  • open door
  • get out

How should we move?

How do we avoid poison ivy?

11 of 26

Map the work flow of your code in pseudocode, breaking down tasks into distinct functions

Basic plan:

  • Feel around for flashlight
  • If I find it, use it to get to door and get out
  • If I don't find it, move around and try again

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

12 of 26

Map the work flow of your code in pseudocode, breaking down tasks into distinct functions

Basic plan:

  • Feel around for flashlight
  • If I find it, use it to get to door and get out
  • If I don't find it, move around and try again

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

13 of 26

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

14 of 26

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

15 of 26

Document as you code, and set up test routines

16 of 26

Document as you code, and set up test routines

Define the objective and inputs/outputs of function

Explain purpose of conditionals

17 of 26

Document as you code, and set up test routines

Explain purpose of conditionals

DOCSTRING

Describes program and how to use it

18 of 26

Document as you code, and set up test routines

DOCSTRINGS allow you to get help on a function

19 of 26

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

20 of 26

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

21 of 26

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

22 of 26

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/

23 of 26

Key steps to git/github

See https://docs.github.com/en/get-started/start-your-journey/hello-world

  1. Set up an account in github
  2. Start a new repository on github
  3. git clone the repository to your local computer
  4. Add/update files
  5. git status
  6. git add .
  7. git commit -m "message"
  8. git push

24 of 26

Branches, mergers, forks, etc. - there is a lot you can do with git/github!

25 of 26

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

26 of 26

Questions