PYTHON BOOTCAMP: BUILDING SOLID CODE�A FRAMEWORK TO THE FUTURE
SOHEB MANDHAI
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!
GETTING STARTED
INSTALLATION
ENVIRONMENTS
WHAT ARE ENVIRONMENTS? (BASIC)
Local python installations
Bespoke packages installed – Can use pip/conda
Lightweight
Reproducible
Great for development environments
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
ADDITIONAL QUICK NOTES:
TANGENT: TYPES OF FUNCTIONS AND OBJECTS
LAMBDA
FUNCTIONS
CLASSES
Create instances of a class
Recalling variables stored in a class
GENERATORS
Stopping condition prevents generator from running forever
DECORATORS (BASIC)
A simpler way of wrapping a function!
PARALLELISATION�(BASIC)
PARALLELISATION – GENERAL NOTES
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 |
Workbook:
DEBUGGING
PSEUDOCODE (PRECURSOR)
For example, Making Tea – The Algorithm:
DEBUGGING �(LOGIC TESTING)
Credit: V & CO Coaching
TRACEBACK (BASIC)
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 |
METHODS
Workbook:
TANGENT: SCOPES
IMPORTANCE OF SCOPE
Global Access
Local Access
Global Access in Local Scope
SCOPE OVERVIEW
For nested functions
Credit: Data Camp
VISUALISATION
MATPLOTLIB
Image Credit: Mandhai et al., 2022
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
PILLOW
Image Credit: LogRocket Blog - https://blog.logrocket.com/image-processing-in-python-using-pillow/
ACTIVITY TIME
SOLID DESIGN PRINCIPLES
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
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
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.
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.
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.
EXERCISE
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
GOOD PRACTICES
THINGS TO DO:
TIPS & COMMON PITFALLS
NO/POOR COMMENTING
YES, I’M GUILTY OF THIS
EXCESSIVE LOOPING
YES, I’M GUILTY OF THIS x2
CONFUSING VARIABLE NAMES
YES, I’M GUILTY OF THIS x3
COMMON LIBRARIES
CLOSING REMARKS
Learn and implement software development practices early – your future self will thank you.
ADDITIONAL RESOURCES
NEED HELP?
StackOverFlow
Your Colleagues
W3Schools
Coding Academy
Code Signal
Many More…
WANT TO BE A PYTHON JEDI?
CHECK OUT RENE’S JACS MATERIAL FROM PREVIOUS YEARS - HTTPS://GITHUB.COM/BRETONR/PYTHON_SKILLS
PEP-8 DOCUMENTATION
STYLE GUIDE FOR PYTHON CODE- https://peps.python.org/pep-0008/