SlideShare a Scribd company logo
Complete Python toolbox for
modern developers
Jan Giacomelli
Python Web Conference 2021
About me
● Tech lead and co-founder typless.com
● Author at testdriven.io
● Twitter: @jangiacomelli
Python development
● creating and re-creating virtual environments
● installing, uninstalling, and updating dependencies
● type checking
● writing and running tests
● following consistent code style
● avoiding security vulnerabilities
● documenting code
Modern Python Environments - pyenv
Lets you easily switch between multiple versions of Python.
● Install specific Python version: $ pyenv install 3.8.5
● List available Python versions: $ pyenv versions
Modern Python Environments - pyenv
● Select a specific version as a global default: $ pyenv global 3.8.5
● Select a specific version for the current project: $ pyenv local 3.9.0
Modern Python Environments - pip + venv
Fairly simple to use. Pre-installed with most versions of Python.
● Create virtual environment: $ python -m venv my_venv
● Activate virtual environment: $ source my_venv/bin/activate
● Install a dependency: (my_venv)$ python -m pip install <package-name>
● Create list of dependencies: (my_venv)$ python -m pip freeze > requirements.txt
Modern Python Environments - pip + venv
Modern Python Environments - Poetry
Powerful CLI used for creating and managing Python projects.
● Create a new project: $ poetry new <project-name>
● Install a dependency: $ poetry add [--dev] <package name>
● Run a command inside the virtual environment: $ poetry run python -m pytest
● Dependencies are managed inside pyproject.toml
Modern Python Environments - Poetry
Modern Python Environments - pipenv
Managing dependencies and virtual environments.
● Create virtual environment: $ pipenv --python 3.8
● Install a dependency: $ pipenv install [--dev] <package name>
● Run a command inside the virtual environment: $ pipenv run python -m pytest
● Dependencies are managed inside Pipfile
Modern Python Environments - pipenv
Which one to choose?
● pip + venv
● poetry
● pipenv
Python testing - pytest
Go-to testing framework for testing Python code.
Compared to unittest:
● less boilerplate code -> more readable test suites
● uses built-in assert statement -> more readable and easier to remember
● updated more frequently (not part of the Python standard library)
● simpler setting up and tearing down test state with its fixture system
● uses a functional approach
Python testing - pytest
Python testing - pytest
Python testing - pytest
Plugins:
● pytest-django - set of tools made specifically for testing Django applications
● pytest-xdist - run tests in parallel
● pytest-cov - adds code coverage support
● pytest-instafail - shows failures and errors immediately instead of waiting until the end of a run
Python testing - Mocking
Practice of replacing real objects with
mocked ones, which mimic their
behavior, at runtime.
Python testing - Mocking
● pytest’s monkeypatch
● Python’s built-in untttest.mock library
○ Mock
○ MagicMock
○ create_autospec
Python testing - Code coverage
A metric that tells you the ratio between the
number of lines executed during test runs and
the total number of all lines in your code base.
pytest-cov - adds code coverage support
Code quality
Code is considered to be of high quality when:
● It serves its purpose
● Its behavior can be tested
● It follows a consistent style
● It's understandable
● It doesn't contain security vulnerabilities
● It's documented well
● It's easy to maintain
Code quality - linters
Linters flag programming errors, bugs, stylistic errors, and suspicious constructs through source code
analysis.
Code quality - flake8
Wrapper around Pyflakes, pycodestyle, and McCabe.
Code quality - flake8
Code quality - formatters
Reformat your code based on a set of standards.
Code formatting is a dull job that should be performed by a computer.
● reduce merge conflicts
● easier to read code
● easier to find bugs
● easier to onboard new developers
Code quality - isort
Automatically separates imports into groups standard library, third-party, local, and alphabetically
orders them.
Code quality - black
Code formatter that's used to reformat your code based on the Black's code style guide.
Code quality - black
Code quality - Security Vulnerability Scanners
Your code is only as secure as its weakest link.
Code quality - bandit
Tool designed to find common security issues in Python code.
● hardcoded password strings
● deserializing untrusted code
● using pass in except blocks
Code quality - safety
Used to check your installed dependencies for known security vulnerabilities against Safety database.
Type checking - type hints
Type hints allow developers to annotate expected types for variables, function parameters, and
function returns inside Python code.
● not enforced by the Python interpreter
● better express the intent
● autocomplete
● less bugs
Type checking - type hints
Type checking - type hints
Type checking - mypy
A tool for type checking at compile-time.
Type checking - pydantic
Uses type hints to validate data on runtime.
● easy to use
● uses type casting
● use it when dealing with external data
Type checking - pydantic
Type checking - Marshmallow
Helps to validate complex data and load/dump data from/to native Python types.
● doesn't use type casting
● schema and class defined separately
Type checking -
Marshmallow
Type checking - Typeguard
It enforces types while your program is running.
● typechecked decorator
● comes with pytest plugin
● for classes and functions
Type checking - Typeguard
Documenting Python Code
Without proper documentation, it can be very difficult or impossible for internal and external
stakeholders to use and/or maintain your code.
● standalone resource
● should always be present
● how and when to use something
Documenting Python Code - Docstrings
A special "string literal that occurs as the first
statement in a module, function, class, or method
definition".
● __doc__ attribute of module/class/function
● multiline and single line
● different formats (Google, NumPy,
reStructuredText, Epytext)
● code examples (doctest)
Documenting Python Code - Sphinx
Converts your project's docstrings to HTML and CSS.
Documenting Python Code - Sphinx
Documenting Python Code - Sphinx
Documenting Python Code - OpenAPI
Standard format for describing, producing,
consuming, and visualizing RESTful APIs.
● used for SwaggerUI and ReDoc
● can be imported to Postman
● can be used to generate SDKs
● can be auto-generated
Documenting Python Code - OpenAPI
Conclusion
● creating and re-creating virtual environments - pyenv, pip + venv, poetry, pipenv
● installing, uninstalling, and updating dependencies - pip + venv, poetry, pipenv
● writing and running tests - pytest, pytest-cov, unittest.mock
● type checking - mypy, pydantic, marshmallow, typeguard
● following consistent code style - black, flake8, isort
● avoiding security vulnerabilities - bandit, security
● documenting code - Sphinx, OpenAPI
You can learn more about these tools in Complete Python Guide

More Related Content

What's hot (20)

PDF
Introduction to Robot Framework (external)
Zhe Li
 
PDF
Robot Framework Introduction
Pekka Klärck
 
PDF
Robot Framework with Python | Edureka
Edureka!
 
PDF
JavaCro'14 - Test Automation using RobotFramework Libraries – Stojan Peshov
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
PDF
ATDD Using Robot Framework
Pekka Klärck
 
PDF
Pentester++
CTruncer
 
PDF
Learning Python with PyCharm EDU
Sergey Aganezov
 
PDF
Sonarqube + Docker
Estefanía Fernández Muñoz
 
PPTX
Robot framework
boriau
 
PPTX
The Veil-Framework
VeilFramework
 
PDF
JenkinsPy workshop
Haifa Ftirich
 
PDF
Windows Offender: Reverse Engineering Windows Defender's Antivirus Emulator
Priyanka Aash
 
PDF
Tracking and improving software quality with SonarQube
Patroklos Papapetrou (Pat)
 
PDF
AntiVirus Evasion Reconstructed - Veil 3.0
CTruncer
 
PDF
FRIDA 101 Android
Tony Thomas
 
PDF
Rfselenium2 redhat
Joonas Jauhiainen
 
PDF
A journey with Target Platforms
Mickael Istria
 
PPTX
TDD in Go with Ginkgo and Gomega
Eddy Reyes
 
PDF
A Battle Against the Industry - Beating Antivirus for Meterpreter and More
CTruncer
 
PDF
Code Quality Lightning Talk
Jonathan Gregory
 
Introduction to Robot Framework (external)
Zhe Li
 
Robot Framework Introduction
Pekka Klärck
 
Robot Framework with Python | Edureka
Edureka!
 
JavaCro'14 - Test Automation using RobotFramework Libraries – Stojan Peshov
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
ATDD Using Robot Framework
Pekka Klärck
 
Pentester++
CTruncer
 
Learning Python with PyCharm EDU
Sergey Aganezov
 
Sonarqube + Docker
Estefanía Fernández Muñoz
 
Robot framework
boriau
 
The Veil-Framework
VeilFramework
 
JenkinsPy workshop
Haifa Ftirich
 
Windows Offender: Reverse Engineering Windows Defender's Antivirus Emulator
Priyanka Aash
 
Tracking and improving software quality with SonarQube
Patroklos Papapetrou (Pat)
 
AntiVirus Evasion Reconstructed - Veil 3.0
CTruncer
 
FRIDA 101 Android
Tony Thomas
 
Rfselenium2 redhat
Joonas Jauhiainen
 
A journey with Target Platforms
Mickael Istria
 
TDD in Go with Ginkgo and Gomega
Eddy Reyes
 
A Battle Against the Industry - Beating Antivirus for Meterpreter and More
CTruncer
 
Code Quality Lightning Talk
Jonathan Gregory
 

Similar to Complete python toolbox for modern developers (20)

PPTX
Python Mastery: A Comprehensive Guide to Setting Up Your Development Environment
Python Devloper
 
PDF
Introduction to python
Agung Wahyudi
 
PPTX
Introduction to Python Programming in Civil Engineering
Rushikesh Kolhe
 
PPTX
Python-Development (1).pptx PYTHON PROGRAMMING
DrNeetuSharma5
 
PPTX
First of all, what is Python? According t
bhatamith15
 
PDF
Streamlining Python Development: A Guide to a Modern Project Setup
Florian Wilhelm
 
PPTX
Software Programming with Python II.pptx
GevitaChinnaiah
 
PDF
Expert Python Programming - Second Edition Michał Jaworski
naterysandr
 
PDF
Python pocket reference 5ed. Edition Mark Lutz
mulauwalydh
 
PPTX
Python-Yesterday Today Tomorrow(What's new?)
Mohan Arumugam
 
PPTX
Best Python IDE in AI and Data Analytics
diptinama1986
 
PDF
Mastering the Interview: 50 Common Interview Questions Demystified
MalcolmDupri
 
PDF
From Basics to Advanced: A Comprehensive Python Programming Guide
pallavichauhan2525
 
PDF
Python. Why to learn?
Oleh Korkh
 
PPTX
The New York Times: Sustainable Systems, Powered by Python
All Things Open
 
PDF
Christian Strappazzon - Presentazione Python Milano - Codemotion Milano 2017
Codemotion
 
PDF
Programming with Python - Basic
Mosky Liu
 
PPTX
A deep dive into python and it's position in the programming landscape.pptx
Murugan Murugan
 
PDF
Ways To Become A Good Python Developer
CodeMonk
 
PDF
A Whirlwind Tour Of Python
Asia Smith
 
Python Mastery: A Comprehensive Guide to Setting Up Your Development Environment
Python Devloper
 
Introduction to python
Agung Wahyudi
 
Introduction to Python Programming in Civil Engineering
Rushikesh Kolhe
 
Python-Development (1).pptx PYTHON PROGRAMMING
DrNeetuSharma5
 
First of all, what is Python? According t
bhatamith15
 
Streamlining Python Development: A Guide to a Modern Project Setup
Florian Wilhelm
 
Software Programming with Python II.pptx
GevitaChinnaiah
 
Expert Python Programming - Second Edition Michał Jaworski
naterysandr
 
Python pocket reference 5ed. Edition Mark Lutz
mulauwalydh
 
Python-Yesterday Today Tomorrow(What's new?)
Mohan Arumugam
 
Best Python IDE in AI and Data Analytics
diptinama1986
 
Mastering the Interview: 50 Common Interview Questions Demystified
MalcolmDupri
 
From Basics to Advanced: A Comprehensive Python Programming Guide
pallavichauhan2525
 
Python. Why to learn?
Oleh Korkh
 
The New York Times: Sustainable Systems, Powered by Python
All Things Open
 
Christian Strappazzon - Presentazione Python Milano - Codemotion Milano 2017
Codemotion
 
Programming with Python - Basic
Mosky Liu
 
A deep dive into python and it's position in the programming landscape.pptx
Murugan Murugan
 
Ways To Become A Good Python Developer
CodeMonk
 
A Whirlwind Tour Of Python
Asia Smith
 
Ad

Recently uploaded (20)

PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Tally software_Introduction_Presentation
AditiBansal54083
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Ad

Complete python toolbox for modern developers

  • 1. Complete Python toolbox for modern developers Jan Giacomelli Python Web Conference 2021
  • 2. About me ● Tech lead and co-founder typless.com ● Author at testdriven.io ● Twitter: @jangiacomelli
  • 3. Python development ● creating and re-creating virtual environments ● installing, uninstalling, and updating dependencies ● type checking ● writing and running tests ● following consistent code style ● avoiding security vulnerabilities ● documenting code
  • 4. Modern Python Environments - pyenv Lets you easily switch between multiple versions of Python. ● Install specific Python version: $ pyenv install 3.8.5 ● List available Python versions: $ pyenv versions
  • 5. Modern Python Environments - pyenv ● Select a specific version as a global default: $ pyenv global 3.8.5 ● Select a specific version for the current project: $ pyenv local 3.9.0
  • 6. Modern Python Environments - pip + venv Fairly simple to use. Pre-installed with most versions of Python. ● Create virtual environment: $ python -m venv my_venv ● Activate virtual environment: $ source my_venv/bin/activate ● Install a dependency: (my_venv)$ python -m pip install <package-name> ● Create list of dependencies: (my_venv)$ python -m pip freeze > requirements.txt
  • 8. Modern Python Environments - Poetry Powerful CLI used for creating and managing Python projects. ● Create a new project: $ poetry new <project-name> ● Install a dependency: $ poetry add [--dev] <package name> ● Run a command inside the virtual environment: $ poetry run python -m pytest ● Dependencies are managed inside pyproject.toml
  • 10. Modern Python Environments - pipenv Managing dependencies and virtual environments. ● Create virtual environment: $ pipenv --python 3.8 ● Install a dependency: $ pipenv install [--dev] <package name> ● Run a command inside the virtual environment: $ pipenv run python -m pytest ● Dependencies are managed inside Pipfile
  • 12. Which one to choose? ● pip + venv ● poetry ● pipenv
  • 13. Python testing - pytest Go-to testing framework for testing Python code. Compared to unittest: ● less boilerplate code -> more readable test suites ● uses built-in assert statement -> more readable and easier to remember ● updated more frequently (not part of the Python standard library) ● simpler setting up and tearing down test state with its fixture system ● uses a functional approach
  • 16. Python testing - pytest Plugins: ● pytest-django - set of tools made specifically for testing Django applications ● pytest-xdist - run tests in parallel ● pytest-cov - adds code coverage support ● pytest-instafail - shows failures and errors immediately instead of waiting until the end of a run
  • 17. Python testing - Mocking Practice of replacing real objects with mocked ones, which mimic their behavior, at runtime.
  • 18. Python testing - Mocking ● pytest’s monkeypatch ● Python’s built-in untttest.mock library ○ Mock ○ MagicMock ○ create_autospec
  • 19. Python testing - Code coverage A metric that tells you the ratio between the number of lines executed during test runs and the total number of all lines in your code base. pytest-cov - adds code coverage support
  • 20. Code quality Code is considered to be of high quality when: ● It serves its purpose ● Its behavior can be tested ● It follows a consistent style ● It's understandable ● It doesn't contain security vulnerabilities ● It's documented well ● It's easy to maintain
  • 21. Code quality - linters Linters flag programming errors, bugs, stylistic errors, and suspicious constructs through source code analysis.
  • 22. Code quality - flake8 Wrapper around Pyflakes, pycodestyle, and McCabe.
  • 23. Code quality - flake8
  • 24. Code quality - formatters Reformat your code based on a set of standards. Code formatting is a dull job that should be performed by a computer. ● reduce merge conflicts ● easier to read code ● easier to find bugs ● easier to onboard new developers
  • 25. Code quality - isort Automatically separates imports into groups standard library, third-party, local, and alphabetically orders them.
  • 26. Code quality - black Code formatter that's used to reformat your code based on the Black's code style guide.
  • 27. Code quality - black
  • 28. Code quality - Security Vulnerability Scanners Your code is only as secure as its weakest link.
  • 29. Code quality - bandit Tool designed to find common security issues in Python code. ● hardcoded password strings ● deserializing untrusted code ● using pass in except blocks
  • 30. Code quality - safety Used to check your installed dependencies for known security vulnerabilities against Safety database.
  • 31. Type checking - type hints Type hints allow developers to annotate expected types for variables, function parameters, and function returns inside Python code. ● not enforced by the Python interpreter ● better express the intent ● autocomplete ● less bugs
  • 32. Type checking - type hints
  • 33. Type checking - type hints
  • 34. Type checking - mypy A tool for type checking at compile-time.
  • 35. Type checking - pydantic Uses type hints to validate data on runtime. ● easy to use ● uses type casting ● use it when dealing with external data
  • 36. Type checking - pydantic
  • 37. Type checking - Marshmallow Helps to validate complex data and load/dump data from/to native Python types. ● doesn't use type casting ● schema and class defined separately
  • 39. Type checking - Typeguard It enforces types while your program is running. ● typechecked decorator ● comes with pytest plugin ● for classes and functions
  • 40. Type checking - Typeguard
  • 41. Documenting Python Code Without proper documentation, it can be very difficult or impossible for internal and external stakeholders to use and/or maintain your code. ● standalone resource ● should always be present ● how and when to use something
  • 42. Documenting Python Code - Docstrings A special "string literal that occurs as the first statement in a module, function, class, or method definition". ● __doc__ attribute of module/class/function ● multiline and single line ● different formats (Google, NumPy, reStructuredText, Epytext) ● code examples (doctest)
  • 43. Documenting Python Code - Sphinx Converts your project's docstrings to HTML and CSS.
  • 46. Documenting Python Code - OpenAPI Standard format for describing, producing, consuming, and visualizing RESTful APIs. ● used for SwaggerUI and ReDoc ● can be imported to Postman ● can be used to generate SDKs ● can be auto-generated
  • 48. Conclusion ● creating and re-creating virtual environments - pyenv, pip + venv, poetry, pipenv ● installing, uninstalling, and updating dependencies - pip + venv, poetry, pipenv ● writing and running tests - pytest, pytest-cov, unittest.mock ● type checking - mypy, pydantic, marshmallow, typeguard ● following consistent code style - black, flake8, isort ● avoiding security vulnerabilities - bandit, security ● documenting code - Sphinx, OpenAPI You can learn more about these tools in Complete Python Guide

Editor's Notes

  • #9: Lock file
  • #13: There are also other options.
  • #22: PEP-8 - lower case function names, logic errors like posibility for undefined variable