[PDF] [PDF] Best Practices - UZH - Physik-Institut

22 jui 2020 · pydocstyle (e g python3 -m pydocstyle my_product py) ▷ flake8 (e g in docstring ▷ Use docstring conventions: PEP257 and/or numpy 



Previous PDF Next PDF





[PDF] pydocstyle Documentation - Read the Docs

18 mar 2021 · --convention= choose the basic list of checked errors by specifying an existing convention Possible conventions: pep257, numpy, google 



[PDF] Writing Good Python - EuroPython 2020

Customizable – Configure which errors/conventions are important using pylintrc file Can write plugins pip install pydocstyle ◇ If we use consistent docstrings 



[PDF] Best Practices - UZH - Physik-Institut

22 jui 2020 · pydocstyle (e g python3 -m pydocstyle my_product py) ▷ flake8 (e g in docstring ▷ Use docstring conventions: PEP257 and/or numpy 



[PDF] Data Mining with Python - DTU Informatics

17 oct 2014 · pydocstyle does not test for the Numpy convention Once (or while) your have documented your code properly you can translate it into several 



[PDF] Preventing headaches with linters and automated checks

docs: pydocstyle ○ security: bandit, dodgy, pyt, hacking, safety, dependency- check ○ packaging: pyroma, check-manifest ○ cpython: cpychecker ○ spelling: 



[PDF] Vscode python format on save

the Python extension, displays all pydocstyle errors in the Convention category ( C) pycodestyle (pep8) Command line arguments and configuration files See the  

[PDF] pydocstyle d205

[PDF] pymysql tutorial pdf

[PDF] pyramid pattern in java

[PDF] pyramid program in java using scanner

[PDF] pyramide de maslow

[PDF] pythagorean theorem converse practice

[PDF] python

[PDF] python 3 install pdfminer

[PDF] python 3 modules list pdf

[PDF] python 3 object oriented programming 3rd pdf

[PDF] python 3 pdfminer example

[PDF] python 3 pdfminer3k example

[PDF] python 3 tutorial

[PDF] python 3.7 documentation pdf

[PDF] python 7zip extract

Nicola Chiapolini, July 10, 20231 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

Best Practices

Nicola Chiapolini

Physik-Institut

University of Zurich

July 10, 2023

Nicola Chiapolini, July 10, 20232 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

Introduction

We write code regularly

We have not been formally trainedBest Practices

evolved from experience increase productivity decrease stress still evolve with tools and languagesDevelopment Methodologies e.g. Agile Programming or Test Driven Development lots of buzzwords still many helpful ideas

Nicola Chiapolini, July 10, 20233 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

OutlineIntroduction

Style & Documentation

Special Statements

KIS(S) & DRY

Refactoring

Development Methodologies

Nicola Chiapolini, July 10, 20234 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

OutlineIntroduction

Style & Documentation

Special Statements

KIS(S) & DRY

Refactoring

Development Methodologies

Nicola Chiapolini, July 10, 20235 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

Coding Style

readability counts (often more than brevity or speed) give thingsintention revealingnames

For example:???????instead of?

Nicola Chiapolini, July 10, 20235 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

Coding Style

readability counts (often more than brevity or speed)

See also:

Ottinger" sRules f orNaming Example

Nicola Chiapolini, July 10, 20236 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

Formatting Code

use coding conventions, e.g: PEP-8 conventions specify layout OR use a consistent style (especially when collaborating)

Nicola Chiapolini, July 10, 20237 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

Formatting Code: ToolsChecker

pylint pycodestyle pydocstyle flake8 autopep8 yapf3 black

Nicola Chiapolini, July 10, 20238 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

Finding Bugs with PylintExample

Nicola Chiapolini, July 10, 20238 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

Finding Bugs with PylintExample

Nicola Chiapolini, July 10, 20239 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

Documenting Code: DocstringsExample

at least a single line also for yourself is on-line help too Document arguments and return objects, including types

For complex algorithms, document every line,

and include equations in docstring

Use docstring conventions:

PEP257

and/or n umpy

Nicola Chiapolini, July 10, 202310 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

Example Docstring

Nicola Chiapolini, July 10, 202311 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

tools generate website from docstrings pydoc when project gets bigger how-to

Nicola Chiapolini, July 10, 202312 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

OutlineIntroduction

Style & Documentation

Special Statements

KIS(S) & DRY

Refactoring

Development Methodologies

Nicola Chiapolini, July 10, 202313 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

Don"t use thestar import:???? ?????? ?????? ?

not obvious what you need

Put all imports at the beginning of the file...

...unless you have a very good reasonExample

Nicola Chiapolini, July 10, 202314 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

??????: Pitfalls Python evaluates the imported code at import time.

Nicola Chiapolini, July 10, 202315 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

Exceptions

use???,??????and????? don"t usespecialreturn values:

Fail early, fail often

use b uilt-inExceptions

Nicola Chiapolini, July 10, 202316 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

Not intended for checks needed in production.Example

Nicola Chiapolini, July 10, 202316 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

Not intended for checks needed in production.Example

Nicola Chiapolini, July 10, 202316 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

Not intended for checks needed in production.Example

Nicola Chiapolini, July 10, 202317 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

????: Type Hints and Static Checking ensures variables and functions are used correctlyExample

Nicola Chiapolini, July 10, 202318 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

????: Execute with Python ?must be integer once fixed, we get the next error:????must be string

Nicola Chiapolini, July 10, 202319 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

????: Adding Type Hints can add type info to variables and function definitionsExample

Nicola Chiapolini, July 10, 202320 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

????: Check with???? spots both problems must be run separately (ideally automatically)

Nicola Chiapolini, July 10, 202321 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

????: SummaryCons additional work for programmer defining correct types can be tricky type hints cheat sheet code gets more verbose and maybe confusingPros prevent a lot of possible bugs valuable even if not all code covered

Nicola Chiapolini, July 10, 202322 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

OutlineIntroduction

Style & Documentation

Special Statements

KIS(S) & DRY

Refactoring

Development Methodologies

Nicola Chiapolini, July 10, 202323 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

Keep it Simple (Stupid) - KIS(S) Principle

Keep it SimpleDebugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. - Brian W. Kernighan

Nicola Chiapolini, July 10, 202324 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

Don"t Repeat Yourself (DRY)

No copy & paste!

Not just lines code, but knowledge of all sorts

Do not express the same piece of knowledge in two places... ...or you will have to update it everywhere

It is not a question ofifthis may fail, butwhen

Nicola Chiapolini, July 10, 202325 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

Don"t Repeat Yourself (DRY): TypesExample

Copy-and-paste a snippet, instead of refactoring it into a function

Repeated implementation of utility methods

because you don"t remember because developers don"t talk to each other Version number in source code, website, readme, package

If you detect duplication: refactor!

Nicola Chiapolini, July 10, 202326 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

OutlineIntroduction

Style & Documentation

Special Statements

KIS(S) & DRY

Refactoring

Development Methodologies

Nicola Chiapolini, July 10, 202327 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

Refactoring

re-organise your code without changing its functionality rethink earlier design decisions break large code blocks apart rename and restructure code will improve the readability and modularity will usually reduce the lines of code

Nicola Chiapolini, July 10, 202328 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

Common Refactoring Operations

Rename class/method/module/package/function

Move class/method/module/package/function

Encapsulate code in method/function

Change method/function signature

Organise imports (remove unused and sort)

Always refactor one step at a time, and ensure code still works version control

Nicola Chiapolini, July 10, 202329 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

split into functions use libraries/built-ins fix bug

Nicola Chiapolini, July 10, 202329 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

use libraries/built-ins fix bug

Nicola Chiapolini, July 10, 202329 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

fix bug

Nicola Chiapolini, July 10, 202329 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

fix bug

Nicola Chiapolini, July 10, 202330 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

OutlineIntroduction

Style & Documentation

Special Statements

KIS(S) & DRY

Refactoring

Development Methodologies

Nicola Chiapolini, July 10, 202331 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

What is a Development Methodology?Consists of:

process used for development tools to support this processHelp answer questions like:

How far ahead should I plan?

What should I prioritise?

When do I write tests and documentation?Right methodology depends on scenario.

Nicola Chiapolini, July 10, 202331 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

What is a Development Methodology?Consists of:

process used for development tools to support this processHelp answer questions like:

How far ahead should I plan?

What should I prioritise?

When do I write tests and documentation?Right methodology depends on scenario.

Nicola Chiapolini, July 10, 202332 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

sequential from manufacturing and construction

Nicola Chiapolini, July 10, 202333 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

Agile Methods (late 90"s)

minimal planning, small development iterations frequent input from environment very adaptive, since nothing is set in stone

Nicola Chiapolini, July 10, 202334 / 34IntroductionStyle & Documentation Special Statemen tsKIS(S) & DR YRef actoringDe velopmentMethodologies

Test Driven Development (TDD)Write simplest

version of codeWrite tests to check codeRun tests; debug until tests passRefactor

Define unit tests first!

Develop one unit at a time!

more tomorrow

Nicola Chiapolini, July 10, 20231 / 1Virtualenv

An Almost Unrelated Note: Using VirtualEnvThe Problem different tools need different versions of a module your Linux distribution does not include a moduleThe Solution: virtualenv update the search-paths to include folders in?????? run your code or install libraries with pip undo changes to search-pathsquotesdbs_dbs19.pdfusesText_25