1 of 43

Along with the code

Tool, Tips and Techniques

to make your Python programming life

enjoyable

Yothin (Man) Muangsommuk

@yothinix

2 of 43

Prerequisite

  • Text Editor: Microsoft Visual Studio Code
  • Python interpreter (current latest version is 3.10) via Windows Store
  • Python extension for Visual Studio Code

3 of 43

The 5 Cores of Programming Languages

  • Syntax: How do you write language constructs?
  • Semantics: What do programs mean?
  • Idioms: What are typical patterns for using language?
  • Libraries: What facilities does the language provide?
  • Tools: What do language provide to make your job easier?

4 of 43

The 5 Cores of Programming Languages

  • Syntax: How do you write language constructs?
  • Semantics: What do programs mean?
  • Idioms: What are typical patterns for using language?
  • Libraries: What facilities does the language provide?
  • Tools: What do language provide to make your job easier?

5 of 43

Virtualenvs

  • An isolated environment for your project, So that dependency will not leaked to other projects.
  • Included with Python as standard library venv since Python 3.5
  • Create virtualenv
    • Both Windows/Mac: python -m venv venv
  • Activate the virtual environment
    • Windows: .\venv\Scripts\Activate.ps1
    • Mac: ./venv/bin/activate

6 of 43

A quick fix to activate virtualenv on Windows Powershell

This happen because Windows PowerShell not allow to execute the script by default. We need to tell Powershell to allow executing the script from following step

7 of 43

A quick fix to activate virtualenv on Windows Powershell

  1. Open Windows PowerShell by search in a menu bar then select Run as Administrator

8 of 43

A quick fix to activate virtualenv on Windows Powershell

2. Entering command: set-executionpolicy RemoteSigned then hit Enter

3. When command ask for confirm type: Y then hit Enter

9 of 43

A quick fix to activate virtualenv on Windows Powershell

4. Try to activate virtualenv on VSCode again with .\venv\Scripts\Activate.ps1 it should work now. Can see by having little green (venv) in front of the new line

10 of 43

Installing dependencies

  • When installing dependency use command pip install <package> for example pip install pytest
  • If the install was successful there should not be any error display in red text
  • It might have a warning to upgrade pip in yellow text we can ignore it for now

11 of 43

Maintaining dependencies rules

  • Using pip freeze to list the current dependencies of the project and output the response to requirements.txt using >
  • Always pin the dependencies version with == *
  • Install the new package via command pip install -r requirements.txt

* See more in PEP-044 https://www.python.org/dev/peps/pep-0440/#compatible-release

12 of 43

How can I know that my code work?

13 of 43

14 of 43

15 of 43

16 of 43

?

17 of 43

18 of 43

How can I know that my code work?

19 of 43

20 of 43

21 of 43

Unit Testing

22 of 43

Unit testing

  • A code that execute another code and validate it result would still be the same on a particular state
  • Unit mean small component, normally a function or method of class
  • F.I.R.S.T Principles *
    • Fast
    • Independent
    • Repeatable
    • Self-Validating
    • Timely
  • AAA Pattern **
    • Arrange
    • Act
    • Assert

* See more in F.I.R.S.T Principles ของ Unit Test�** Also can describe as Given, When, Then if we seen in Behavior

23 of 43

Test Driven Development (TDD)

Originate from Test-First Programming concept in XP in 1999

Test Driven Development can be divided into 5 stages

  • Adding test
  • Run all test, new test failed
  • Write simplest code to make new test passes
  • Run all test, all also passes
  • Refactor as needed

24 of 43

25 of 43

Debugger

26 of 43

27 of 43

28 of 43

Python debugger (pdb)

  • Included with standard library (pdb)
  • Debugger can stop on any place in the program using import pdb; pdb.set_trace()
  • Alternatively can use breakpoint() included since Python 3.7
  • ipdb: The better version integrate with IPython
    • Install via pip install ipdb
    • Can use the same command as pdb just replace pdb with ipdb
  • Set the breakpoint to use ipdb
    • Window $env:PYTHONBREAKPOINT = 'ipdb.set_trace'; python .\main.py
    • Mac: PYTHONBREAKPOINT=ipdb.set_trace; python ./main.py

29 of 43

What can we do with debugger

  • Everything that you can do with Python IDLE
  • N (next) - This will execute the next statement in your code
  • L (list) - Display where you are in the code
  • S (step) - Go to the function definition that about to execute
  • C (continue) - Continue executing the code from breakpoint
  • Q (quit) - Exit the debugger
  • If you don’t know what to do just type H (help)

30 of 43

VSCode Python debugger

  • Same idea as pdb with graphical interface integrated with VSCode
  • Bundle with Python extension for VSCode
  • Can put breakpoint on by clicking before the line number that we want
  • Activate debugger by select Debug Python file in terminal

31 of 43

Coding Style

32 of 43

33 of 43

34 of 43

35 of 43

PEP 8 - Style Guide for Python Code

  • Indentation: Using 4 spaces per indentation
  • Maximum Line Length: 79 characters
  • Line break before operator
  • 2 blank lines at top level (Import, function, class), 1 blank line before method definition
  • Import grouped in following: Standard library, 3rd Party, Local application
  • Avoid wildcard imports
  • Pet Peeves: Space after trailing comma, Space between operator
  • At least, 2 spaces before inline comment
  • Naming convention: ClassName, CONSTANT, function_or_method
  • etc.

36 of 43

37 of 43

Linter

38 of 43

Flake8 - Your go to Python code linter

  • Actually based on pycodestyle, pyflakes, mccabe to check both style and quality of the code
  • Can be configurable for custom rule that suits your need, e.g. extend maximum line length
  • Can be extended with plugins
  • Install via pip install flake8
  • Usage via flake8 <File/Directory>

39 of 43

Formatter

40 of 43

Black - the uncompromising Python code formatter

  • Opiniate code formatter
  • Deterministic build
  • Work out of the box without need configuration
  • Can be customize by configuration like change default behavior for string formatting
  • Install via pip install black
  • Usage via black <File/Directory>

41 of 43

42 of 43

Key takeaway

  • There are 5 core in Programming language: Syntax, Semantics, Idioms, Library, Tools knowing all can help you be a good programmer
  • Always create virtualenv for every project you work on, pin the dependencies and maintain it in requirements.txt
  • Unit test and Test Driven Development help you build your program with confident by each step
  • When you encounter problem in your program debugger (pdb) is your best friend
  • There is no need to argue about coding style, just let linter (flake8) and formatter (black) do the hard work for you

43 of 43