Python But Python is already great!!! Readability Massive - - PowerPoint PPT Presentation

python but python is already great
SMART_READER_LITE
LIVE PREVIEW

Python But Python is already great!!! Readability Massive - - PowerPoint PPT Presentation

Writing Good Python But Python is already great!!! Readability Massive ecosystem Libraries, Frameworks and Tools Vibrant community Many other things 2 Writing Good Python Hello! I am Prashant Currently a


slide-1
SLIDE 1

Writing Good Python

slide-2
SLIDE 2

But Python is already great!!! ◆ Readability ◆ Massive ecosystem – Libraries,

Frameworks and Tools

◆ Vibrant community ◆ Many other things …

2

slide-3
SLIDE 3

“Writing” Good Python

slide-4
SLIDE 4

Hello!

I am Prashant

Currently a Software Engineer at HubSpot

4

slide-5
SLIDE 5

Maintainability 𝑵 ≈ 𝟐 𝑼 ∗ (𝑺) ◆ M = Maintainability ◆ T = Amount of time it takes a developer to make a change ◆ R = Risk that change will break something. ◆ Cannot be strictly defined ◆ Can be judged by readability, coupling, consistency etc.

5

slide-6
SLIDE 6

Example

6

slide-7
SLIDE 7

Python Style Guide ◆ Batteries included principle ◆ PEP 8 - https://www.python.org/dev/peps/pep-0008/ ◆ PEP 257 - https://www.python.org/dev/peps/pep-0257/

7

slide-8
SLIDE 8

Pylint ◆ pip install pylint ◆ Coding standards ◆ Error detection ◆ Refactoring – Duplicated code ◆ Customizable – Configure which errors/conventions are important

using pylintrc file. Can write plugins to add a personal feature

◆ https://www.pylint.org/

8

slide-9
SLIDE 9

9

slide-10
SLIDE 10

◆ List of pylint messages –

https://github.com/janjur/readable- pylint-messages

◆ Alternatives – flake8, pyflakes etc.

10

slide-11
SLIDE 11

11

Example after Pylint fixes

slide-12
SLIDE 12

What about PEP 257?

12

slide-13
SLIDE 13

Pydocstyle

13

◆ pip install pydocstyle ◆ If we use consistent

docstrings then there are several tools which can generate automatic documentation from code.

slide-14
SLIDE 14

14

slide-15
SLIDE 15

Mypy ◆ pip install mypy ◆ Optional static typing for Python ◆ PEP 484 ◆ No runtime overload

15

slide-16
SLIDE 16

16

slide-17
SLIDE 17

◆ How to type hint types other than

primitives?

◆ typing module – Any, Union, Tuple,

Callable, List etc.

◆ Can create our own types ◆ typing.TYPE_CHECKING – A special

constant that is assumed to be True by 3rd party static type checkers. It is False at runtime.

17

slide-18
SLIDE 18

Bandit and Black

Icing on the Cake

18

slide-19
SLIDE 19

Bandit ◆ pip install bandit ◆ Can detect security issues in the Python code ◆ https://github.com/PyCQA/bandit/tree/master/examples

19

slide-20
SLIDE 20

Black ◆ pip install black ◆ Code formatter

20

slide-21
SLIDE 21

Where to ensure?

21

slide-22
SLIDE 22

22

After Push Before Commit While Writing

slide-23
SLIDE 23

Pre-commit ◆ pip install pre-commit ◆ https://pre-commit.com/ ◆ It is a multi-language package manager for pre-commit hooks. You

specify a list of hooks you want and it manages the installation and execution.

◆ Configured using .pre-commit-config.yaml ◆ List of available hooks – https://pre-commit.com/hooks.html

23

slide-24
SLIDE 24

Sample configuration

24

◆ pre-commit install ◆ pre-commit run

slide-25
SLIDE 25

The Zen of Python

25

slide-26
SLIDE 26

Thanks!

Any questions?

You can find me at https://linkedin.com/in/pc9795

26