Why automate your work flow, and how to approach the process

Questions for students to consider:

1) What happens when you get a new dataset that you need to analyze in the same way you analyzed a previous data set?

2) What processes do you do often? How do you implement these?

3) Do you have a clear workflow you could replicate?

4) Or even better, could you plug this new data set into your old workflow?

Learning Objectives of Automation Module:

Lesson 1 (10-15min)

  • Employ best practices of naming a variable including: don’t use existing function names, avoid periods in names, don’t use numbers at the beginning of a variable name.

Lesson 2 (10-15min)

  • Define DRY and provide examples of how you would implement DRY in your code; Don’t repeat yourself!
  • Identify code that can be modularized following DRY and implement a modular workflow using functions.

Lesson 3 (60 min)

  • Know how to construct a function: variables, function name, syntax, documentation, return values
  • Demonstrate use of function within the notebook / code.
  • Construct and compose function documentation that clearly defines inputs, output variables and behaviour.

Lesson 4

  • Organize a set of functions within a python (.py) script and use it (import it into) in a Jupyter notebook.

Lesson 5

  • Use asserts to test validity of function inputs and outputs

Lesson 6

-

Basic Overview of the suggested workflow using Socrative

  • Use Socrative quiz to collect answers from student activities (students can run their code in their notebooks, and post to socrative). This will allow the instructor to see what solutions students came up with, and identify any places where misconceptions and confusion are coming up. Using Socrative quizes also allows for a record of the student work to be analyzed after class to see how students are learning and where they are having troubles.
  • sharing of prepared Socrative Quizes designed to be used with the automation module can be shared by URL links to each teacher so they do not have to be remade.

Review of good variable practices

Learning Objective: Employ best practices of naming a variable including: don’t use existing function names, avoid periods in names, don’t use numbers at the beginning of a variable name

Types of variables:

  • strings, integers, etc..

References: https://www.tutorialspoint.com/python3/python_variable_types.htm

Naming conventions that should be followed

Rules


In [9]:
# write out three variables, assign a number, string, list 
x = 'Asia' # String
y = 1952  # an integer
z = 1.5 # a floating point number

cal_1 = y * z
print cal_1

# or
x, y = 'Asia', 'Africa'
w = x
w = x + x #concatinating strings (combinging strings)
print w

h = 'Africa'

list_1 = ['Asia', 'Africa', 'Europe'] # list
print list_1


2928.0
AsiaAsia
['Asia', 'Africa', 'Europe']

In [ ]:
# Questions for students:
# 1) what do you think will happen with this code? x * z
# 2) what do you think will happen with this code? list_1[0]
# 3) what do you think will happen with this code? list_1[1:2]

Indexing

Python indexing is from 0 to length of list - 1

Example:

list_1 = ['Asia', 'Africa', 'Europe']

Asia index = 0, Africa index = 1,
Europe index = 2


In [ ]:
list_1 # this is not a very descriptive and identifiable variable
countries = ['Asia', 'Africa', 'Europe']

Scope of variables

global vs. local in functions

Global variables

Global variables are available in the environment your script is working in. Every variable we have made at this point is a global variable.

Local variables

Local variables will be useful to understand when we start using functions in the automation of our code. Local variables only exist in the function environment, not the global environment your linear workflow code is.

Other useful conventions with variables to follow

1) set-up variables at the begining of your page, after importing libraries 2) use variables instead of file names, or exact values or strings so that if you need to change the value of something you don't have to search through all your code to make sure you made the change everywhere, simply change the value of the variable at the top. -- This will also make your code more reproducible in the end.


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: