SlideShare una empresa de Scribd logo
1 de 48
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

Más contenido relacionado

La actualidad más candente

Introduction to Robot Framework (external)
Introduction to Robot Framework (external)Introduction to Robot Framework (external)
Introduction to Robot Framework (external)Zhe Li
 
Robot Framework Introduction
Robot Framework IntroductionRobot Framework Introduction
Robot Framework IntroductionPekka Klärck
 
Robot Framework with Python | Edureka
Robot Framework with Python | EdurekaRobot Framework with Python | Edureka
Robot Framework with Python | EdurekaEdureka!
 
ATDD Using Robot Framework
ATDD Using Robot FrameworkATDD Using Robot Framework
ATDD Using Robot FrameworkPekka Klärck
 
Pentester++
Pentester++Pentester++
Pentester++CTruncer
 
Learning Python with PyCharm EDU
Learning Python with PyCharm EDU Learning Python with PyCharm EDU
Learning Python with PyCharm EDU Sergey Aganezov
 
Robot framework
Robot frameworkRobot framework
Robot frameworkboriau
 
Windows Offender: Reverse Engineering Windows Defender's Antivirus Emulator
Windows Offender: Reverse Engineering Windows Defender's Antivirus EmulatorWindows Offender: Reverse Engineering Windows Defender's Antivirus Emulator
Windows Offender: Reverse Engineering Windows Defender's Antivirus EmulatorPriyanka Aash
 
Tracking and improving software quality with SonarQube
Tracking and improving software quality with SonarQubeTracking and improving software quality with SonarQube
Tracking and improving software quality with SonarQubePatroklos Papapetrou (Pat)
 
AntiVirus Evasion Reconstructed - Veil 3.0
AntiVirus Evasion Reconstructed - Veil 3.0AntiVirus Evasion Reconstructed - Veil 3.0
AntiVirus Evasion Reconstructed - Veil 3.0CTruncer
 
FRIDA 101 Android
FRIDA 101 AndroidFRIDA 101 Android
FRIDA 101 AndroidTony Thomas
 
A journey with Target Platforms
A journey with Target PlatformsA journey with Target Platforms
A journey with Target PlatformsMickael Istria
 
TDD in Go with Ginkgo and Gomega
TDD in Go with Ginkgo and GomegaTDD in Go with Ginkgo and Gomega
TDD in Go with Ginkgo and GomegaEddy Reyes
 
A Battle Against the Industry - Beating Antivirus for Meterpreter and More
A Battle Against the Industry - Beating Antivirus for Meterpreter and MoreA Battle Against the Industry - Beating Antivirus for Meterpreter and More
A Battle Against the Industry - Beating Antivirus for Meterpreter and MoreCTruncer
 
Code Quality Lightning Talk
Code Quality Lightning TalkCode Quality Lightning Talk
Code Quality Lightning TalkJonathan Gregory
 

La actualidad más candente (20)

Introduction to Robot Framework (external)
Introduction to Robot Framework (external)Introduction to Robot Framework (external)
Introduction to Robot Framework (external)
 
Robot Framework Introduction
Robot Framework IntroductionRobot Framework Introduction
Robot Framework Introduction
 
Robot Framework with Python | Edureka
Robot Framework with Python | EdurekaRobot Framework with Python | Edureka
Robot Framework with Python | Edureka
 
JavaCro'14 - Test Automation using RobotFramework Libraries – Stojan Peshov
JavaCro'14 - Test Automation using RobotFramework Libraries – Stojan PeshovJavaCro'14 - Test Automation using RobotFramework Libraries – Stojan Peshov
JavaCro'14 - Test Automation using RobotFramework Libraries – Stojan Peshov
 
ATDD Using Robot Framework
ATDD Using Robot FrameworkATDD Using Robot Framework
ATDD Using Robot Framework
 
Pentester++
Pentester++Pentester++
Pentester++
 
Learning Python with PyCharm EDU
Learning Python with PyCharm EDU Learning Python with PyCharm EDU
Learning Python with PyCharm EDU
 
Sonarqube + Docker
Sonarqube + DockerSonarqube + Docker
Sonarqube + Docker
 
Robot framework
Robot frameworkRobot framework
Robot framework
 
The Veil-Framework
The Veil-FrameworkThe Veil-Framework
The Veil-Framework
 
JenkinsPy workshop
JenkinsPy workshop JenkinsPy workshop
JenkinsPy workshop
 
Windows Offender: Reverse Engineering Windows Defender's Antivirus Emulator
Windows Offender: Reverse Engineering Windows Defender's Antivirus EmulatorWindows Offender: Reverse Engineering Windows Defender's Antivirus Emulator
Windows Offender: Reverse Engineering Windows Defender's Antivirus Emulator
 
Tracking and improving software quality with SonarQube
Tracking and improving software quality with SonarQubeTracking and improving software quality with SonarQube
Tracking and improving software quality with SonarQube
 
AntiVirus Evasion Reconstructed - Veil 3.0
AntiVirus Evasion Reconstructed - Veil 3.0AntiVirus Evasion Reconstructed - Veil 3.0
AntiVirus Evasion Reconstructed - Veil 3.0
 
FRIDA 101 Android
FRIDA 101 AndroidFRIDA 101 Android
FRIDA 101 Android
 
Rfselenium2 redhat
Rfselenium2 redhatRfselenium2 redhat
Rfselenium2 redhat
 
A journey with Target Platforms
A journey with Target PlatformsA journey with Target Platforms
A journey with Target Platforms
 
TDD in Go with Ginkgo and Gomega
TDD in Go with Ginkgo and GomegaTDD in Go with Ginkgo and Gomega
TDD in Go with Ginkgo and Gomega
 
A Battle Against the Industry - Beating Antivirus for Meterpreter and More
A Battle Against the Industry - Beating Antivirus for Meterpreter and MoreA Battle Against the Industry - Beating Antivirus for Meterpreter and More
A Battle Against the Industry - Beating Antivirus for Meterpreter and More
 
Code Quality Lightning Talk
Code Quality Lightning TalkCode Quality Lightning Talk
Code Quality Lightning Talk
 

Similar a Complete python toolbox for modern developers

Creating a reasonable project boilerplate
Creating a reasonable project boilerplateCreating a reasonable project boilerplate
Creating a reasonable project boilerplateStanislav Petrov
 
Python_Introduction_Good_PPT.pptx
Python_Introduction_Good_PPT.pptxPython_Introduction_Good_PPT.pptx
Python_Introduction_Good_PPT.pptxlemonchoos
 
Build and deploy scientific Python Applications
Build and deploy scientific Python Applications  Build and deploy scientific Python Applications
Build and deploy scientific Python Applications Ramakrishna Reddy
 
The New York Times: Sustainable Systems, Powered by Python
The New York Times: Sustainable Systems, Powered by PythonThe New York Times: Sustainable Systems, Powered by Python
The New York Times: Sustainable Systems, Powered by PythonAll Things Open
 
Python Mastery: A Comprehensive Guide to Setting Up Your Development Environment
Python Mastery: A Comprehensive Guide to Setting Up Your Development EnvironmentPython Mastery: A Comprehensive Guide to Setting Up Your Development Environment
Python Mastery: A Comprehensive Guide to Setting Up Your Development EnvironmentPython Devloper
 
Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020
Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020
Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020Andrew Yatsenko
 
Software development practices in python
Software development practices in pythonSoftware development practices in python
Software development practices in pythonJimmy Lai
 
PhpStorm: Symfony2 Plugin
PhpStorm: Symfony2 PluginPhpStorm: Symfony2 Plugin
PhpStorm: Symfony2 PluginHaehnchen
 
Python Developer Certification
Python Developer CertificationPython Developer Certification
Python Developer CertificationVskills
 
Tutorial on-python-programming
Tutorial on-python-programmingTutorial on-python-programming
Tutorial on-python-programmingChetan Giridhar
 
Introduction to Python.pdf
Introduction to Python.pdfIntroduction to Python.pdf
Introduction to Python.pdfRahul Mogal
 
Возможности интерпретатора Python в NX-OS
Возможности интерпретатора Python в NX-OSВозможности интерпретатора Python в NX-OS
Возможности интерпретатора Python в NX-OSCisco Russia
 
Python tutorial for beginners - Tib academy
Python tutorial for beginners - Tib academyPython tutorial for beginners - Tib academy
Python tutorial for beginners - Tib academyTIB Academy
 
Pipeline as code for your infrastructure as Code
Pipeline as code for your infrastructure as CodePipeline as code for your infrastructure as Code
Pipeline as code for your infrastructure as CodeKris Buytaert
 
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides:  Let's build macOS CLI Utilities using SwiftMobileConf 2021 Slides:  Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides: Let's build macOS CLI Utilities using SwiftDiego Freniche Brito
 
IDE and Toolset For Magento Development
IDE and Toolset For Magento DevelopmentIDE and Toolset For Magento Development
IDE and Toolset For Magento DevelopmentAbid Malik
 
Introduction to Python Basics Programming
Introduction to Python Basics ProgrammingIntroduction to Python Basics Programming
Introduction to Python Basics ProgrammingRaveendra R
 

Similar a Complete python toolbox for modern developers (20)

Creating a reasonable project boilerplate
Creating a reasonable project boilerplateCreating a reasonable project boilerplate
Creating a reasonable project boilerplate
 
Python_Introduction_Good_PPT.pptx
Python_Introduction_Good_PPT.pptxPython_Introduction_Good_PPT.pptx
Python_Introduction_Good_PPT.pptx
 
Build and deploy scientific Python Applications
Build and deploy scientific Python Applications  Build and deploy scientific Python Applications
Build and deploy scientific Python Applications
 
The New York Times: Sustainable Systems, Powered by Python
The New York Times: Sustainable Systems, Powered by PythonThe New York Times: Sustainable Systems, Powered by Python
The New York Times: Sustainable Systems, Powered by Python
 
Python Mastery: A Comprehensive Guide to Setting Up Your Development Environment
Python Mastery: A Comprehensive Guide to Setting Up Your Development EnvironmentPython Mastery: A Comprehensive Guide to Setting Up Your Development Environment
Python Mastery: A Comprehensive Guide to Setting Up Your Development Environment
 
Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020
Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020
Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020
 
Software development practices in python
Software development practices in pythonSoftware development practices in python
Software development practices in python
 
PhpStorm: Symfony2 Plugin
PhpStorm: Symfony2 PluginPhpStorm: Symfony2 Plugin
PhpStorm: Symfony2 Plugin
 
Python Developer Certification
Python Developer CertificationPython Developer Certification
Python Developer Certification
 
Tutorial on-python-programming
Tutorial on-python-programmingTutorial on-python-programming
Tutorial on-python-programming
 
Pyhton-1a-Basics.pdf
Pyhton-1a-Basics.pdfPyhton-1a-Basics.pdf
Pyhton-1a-Basics.pdf
 
Introduction to Python.pdf
Introduction to Python.pdfIntroduction to Python.pdf
Introduction to Python.pdf
 
Возможности интерпретатора Python в NX-OS
Возможности интерпретатора Python в NX-OSВозможности интерпретатора Python в NX-OS
Возможности интерпретатора Python в NX-OS
 
Python programming 2nd
Python programming 2ndPython programming 2nd
Python programming 2nd
 
Python Tutorial for Beginner
Python Tutorial for BeginnerPython Tutorial for Beginner
Python Tutorial for Beginner
 
Python tutorial for beginners - Tib academy
Python tutorial for beginners - Tib academyPython tutorial for beginners - Tib academy
Python tutorial for beginners - Tib academy
 
Pipeline as code for your infrastructure as Code
Pipeline as code for your infrastructure as CodePipeline as code for your infrastructure as Code
Pipeline as code for your infrastructure as Code
 
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides:  Let's build macOS CLI Utilities using SwiftMobileConf 2021 Slides:  Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
 
IDE and Toolset For Magento Development
IDE and Toolset For Magento DevelopmentIDE and Toolset For Magento Development
IDE and Toolset For Magento Development
 
Introduction to Python Basics Programming
Introduction to Python Basics ProgrammingIntroduction to Python Basics Programming
Introduction to Python Basics Programming
 

Último

Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfYashikaSharma391629
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLionel Briand
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxAndreas Kunz
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 

Último (20)

Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and Repair
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 

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

Notas del editor

  1. Lock file
  2. There are also other options.
  3. PEP-8 - lower case function names, logic errors like posibility for undefined variable