1 of 51

PYTHON BOOTCAMP: BUILDING SOLID CODE�A FRAMEWORK TO THE FUTURE

SOHEB MANDHAI

2 of 51

RESOURCES – FOLLOW ALONG

Aim: To make you aware of what python can do for you. It’s up to you to follow up these threads and read the documentation!

3 of 51

GETTING STARTED

4 of 51

INSTALLATION

  • Vanilla:
    • https://www.python.org/
    • Download > Choose the python version > Pick the appropriate installer for your OS
    • Note: You will need to install non-standard packages manually
  • Anaconda (Recommended):
    • Go to https://www.anaconda.com/products/distribution
    • Choose the appropriate installation for your OS
    • Benefit: Comes pre-shipped with most scientific packages, ready to use. Also comes with Spyder IDE.

​

5 of 51

ENVIRONMENTS

6 of 51

WHAT ARE ENVIRONMENTS? (BASIC)

Local python installations

Bespoke packages installed – Can use pip/conda

Lightweight

Reproducible

Great for development environments

7 of 51

BUILT IN ENVIRONMENT HANDLERS

venv – Included with Python > 3.3. Creates lightweight environments.

Usage: python –m venv myenv

virtualenv – Older but more feature-rich than venv

Usage: virtualenv myenv

pyenv – Used to manage python versions and installations

Usage: pyenv install 3.XX.X

8 of 51

ADDITIONAL QUICK NOTES:

  • Environment Control:
    • Can create backups of your installed packages
    • Can create isolated environments to test software
    • Can be used to minimise bloatware when shipping applications
  • IDEs – Integrated Development Environments
    • Excellent for testing
    • Debugging tools
    • Useful for managing code and identifying errors/typos early on
    • Variable display is useful for looking at stored values
    • Can run multiple instances of a code quickly
  • Python Script Available Here: https://drive.google.com/file/d/1WXiNCu9LGAA7ljnuDQ88cZBvS8TcGb12/view?usp=sharing

​

9 of 51

TANGENT: TYPES OF FUNCTIONS AND OBJECTS

10 of 51

LAMBDA

  • A small anonymous function
  • Can take many arguments but consists of one expression
  • Useful for mathematical operations
  • Python reference – lambda
  • Example:

​

11 of 51

FUNCTIONS

  • Define the name of the function
  • Parse arguments and keyword variables
  • Describe the purpose
  • Return the output

12 of 51

CLASSES

  • Makes use of Object-Oriented programming methods
  • Blueprints for creating objects/instances
  • Useful for creating complicated registries for subjects
  • Example:
  • __init__ is a special “method” to initialise the class
  • self is used to refer to variables unique to the class “instance”

Create instances of a class

Recalling variables stored in a class

13 of 51

GENERATORS

  • Useful for controlling iterations over a code block – like a for loop
  • Uses yield methods
  • Has special routines
  • You can use for loops to iterate over generators!

Stopping condition prevents generator from running forever

14 of 51

DECORATORS (BASIC)

  • An approach to handling nested functions!
  • A quick way to modify different functions with the same function (wrapper)

A simpler way of wrapping a function!

15 of 51

PARALLELISATION�(BASIC)

16 of 51

PARALLELISATION – GENERAL NOTES

  • Serial (e.g. for loops) vs Parallelisation (e.g. map)
  • Threads vs Processors (GIL = Global interpreter Lock)

​

​

​

​

​

    • Queuing is essential when doing multi-threading!
  • map: Python’s in-built map function lets you parse an array of arguments and apply a function to them sequentially.
  • MPI - lets multiple programs or processes work together and share data across computers to perform tasks in parallel.

​

​

​

​

Threads

Processes

Share the same memory space

Have separate memory spaces

Lightweight and fast to start

Heavier and slower to start

Limited by GIL — good for I/O tasks

True parallelism — good for CPU tasks

17 of 51

Workbook:

18 of 51

DEBUGGING

19 of 51

PSEUDOCODE (PRECURSOR)

  • Write out your algorithm in basic English – create a step by step guide of your logic:
  • Benefits: This lets anyone translate your algorithm to another programming language if needed

​

For example, Making Tea – The Algorithm:

    • Acquire Cup, Tea Bag, Milk, Sugar
    • Go to Kettle
    • Check Kettle Water
    • If water is low:
      1. Refill water
    • Turn kettle on
    • Wait
    • Place Tea Bag in cup with Sugar (NOT MILK – I WILL FIND YOU)
    • While water == not hot:
      • Wait
      • If kettle == done:
        1. water= hot
    • Pour Water into cup
    • Stir
    • Add Milk
    • Drink

20 of 51

DEBUGGING �(LOGIC TESTING)

  • Check variable explorer in an IDE to ensure values are what you expect
  • Narrate your code to a friend or a rubber duck (Rubber Ducking)
  • Use print statements around offending areas
  • Follow the traceback

Credit: V & CO Coaching

21 of 51

TRACEBACK (BASIC)

  • Useful for finding offending snippets of code causing errors
  • Line of offence is usually given
  • Error message indicates the nature of the issue
  • If error is unclear – search documentation for library/function or StackOverFlow

22 of 51

ERROR HANDLING

Block

Purpose

Example

try

Encloses potentially failing code

Opening file, dividing numbers

except FileNotFoundError

Catches a specific error

Missing file

except ZeroDivisionError

Catches another specific error

Divide by zero

except Exception

Catches any other unhandled error

Safety net

finally

Always runs, regardless of success/failure

Cleanup, file closing

23 of 51

METHODS

  • Printing (Basic)
  • Variable Exploring (Basic – Requires IDE)
  • Error Handling (try, except, finally) (Intermediate)
  • Unit Test (Intermediate)
  • Using pdb (the python debugger) (Intermediate)
  • Using wrappers and decorators (Advanced)

​

Workbook:

24 of 51

TANGENT: SCOPES

25 of 51

IMPORTANCE OF SCOPE

  • Used to describe the access to variables within functions
  • global variables are accessible to all functions
  • local variables are accessible within the function/nested functions
  • You can change variables in the global scope by declaring the variable using the global keyword

Global Access

Local Access

Global Access in Local Scope

26 of 51

SCOPE OVERVIEW

For nested functions

Credit: Data Camp

27 of 51

VISUALISATION

28 of 51

MATPLOTLIB

  • Check out Josh Hayes’ Guide: https://github.com/joshjchayes/matplotlib_guide
  • Great for visualising data
  • Well documented
  • Main package used for plotting in Python

​

Image Credit: Mandhai et al., 2022

29 of 51

SEABORN

Based on Matplotlib

Can load datasets

Convenient for producing statistical graphs

Functions ready to use for creating common types of graphs

Image Credit: Seaborn

30 of 51

PILLOW

  • Site: https://pillow.readthedocs.io/en/stable/handbook/overview.html
  • Designed for loading images
  • Great for manipulating images
    • Can be used for art/preparing image data for creating training datasets
    • Can work in tandem with matplotlib
  • Can load images directly from a URL
  • Using pillow – you can create LUTs and image filters

31 of 51

ACTIVITY TIME

  • On the padlet:
    • State: What do you like to see in graphs and plots?
    • Which features do you tend to dislike in plots?
    • Do you have any cool figures? If so, add them do the padlet.
  • URL: https://padlet.com/soheb_mandhai/jacs-2022-2023-padlet-xpcfpmv22utnsrza

​

32 of 51

SOLID DESIGN PRINCIPLES

33 of 51

34 of 51

SINGLE RESPONSIBILITY PRINCIPLE (SRP)

Analogy: A chef that focuses solely on cooking – not waiting tables and cleaning dishes.

Example: User data and IO are separate classes

Violation: A function that handles multiple roles

35 of 51

OPEN/CLOSED PRINCIPLE (OCP)

Analogy: Applications on your phone which add functionality without altering the OS.

Example: Choosing notification broadcasting type

Violation: Change core functionality to add features

36 of 51

LISKOV SUBSTITUTION PRINCIPLE (LSP)

Analogy: Replacing a normal battery with a rechargeable one (sub-type)

Example: Distinguishing birds that can fly

Violation: Relying on broken logic and causing errors.

37 of 51

INTERFACE SEGREGATION PRINCIPLE (ISP)

Analogy: A restaurant that has specific sections/menu for different dietary subtypes.

Example: Distinguishing roles for different workers

Violation: Making classes use methods they don’t need.

38 of 51

DEPENDENCY INVERSION PRINCIPLE (DIP)

Analogy: A USB port that allows you connect a mouse, keyboard, hard drive etc. without connecting them to the motherboard directly.

Example: Turning on a device using a global interface (remote control).

Violation: Exposing low-level complexity at the interface level.

39 of 51

EXERCISE

  1. Go to the Padlet using the QR code on the right
  2. Choose a design principle
  3. Give an example of how that principle can be applied

​

Example:

Scenario: A function calculates and returns the mass, temperature, and brightness of an astrophysical system.

Violation: Single Responsibility Principle

Fix: Separate each sub-calculation into its own function. Pass them through a bigger function/class.

https://padlet.com/soheb_mandhai/jbca-jacs-xpcfpmv22utnsrza

40 of 51

GOOD PRACTICES

41 of 51

THINGS TO DO:

  1. Good Comments:
    • Commenting your code will make it easier to revisit in the future.
    • Lets other people understand what you have done.
    • Makes it easier to identify mistakes in logic
  2. Sensible Variable Names: Give your variables useful names that link back to the context
  3. Documenting your functions: Creating a doc-string will make it easier for you to understand the structure and inputs of your function.
    • Use ?*function name here* to check docstring

42 of 51

TIPS & COMMON PITFALLS

43 of 51

NO/POOR COMMENTING

YES, I’M GUILTY OF THIS

  • Comment your code:
    • Helpful if anyone else reads it
    • Will save you time in the future
    • “The worst collaborator you can ever have is you from a year ago – they will never answer emails” – Dr. Josh Hayes, 2022

44 of 51

EXCESSIVE LOOPING

  • Excessive looping is inefficient and can slow down your code
  • Some packages like NumPy have routines to avoid looping for mathematical calculations using precompiled C functions.
  • Itertools and map (uses multiprocessing/threading) are alternatives that can be used if you must have multiple loops

YES, I’M GUILTY OF THIS x2

45 of 51

CONFUSING VARIABLE NAMES

  • Pick sensible names that suit the context of the values contained in the variable.
  • Bad variable names can cause trouble when debugging/re-reading the code years later.

YES, I’M GUILTY OF THIS x3

46 of 51

COMMON LIBRARIES

47 of 51

CLOSING REMARKS

Learn and implement software development practices early – your future self will thank you.

48 of 51

ADDITIONAL RESOURCES

49 of 51

NEED HELP?

StackOverFlow

Your Colleagues

W3Schools

Coding Academy

Code Signal

Many More…

50 of 51

WANT TO BE A PYTHON JEDI?

CHECK OUT RENE’S JACS MATERIAL FROM PREVIOUS YEARS - HTTPS://GITHUB.COM/BRETONR/PYTHON_SKILLS

51 of 51

PEP-8 DOCUMENTATION

STYLE GUIDE FOR PYTHON CODE- https://peps.python.org/pep-0008/

​