55
(c) 2015 eGenix.com Software, Skills and Services GmbH, [email protected] Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, [email protected] Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

(c) 2015 eGenix.com Software, Skills and Services GmbH, [email protected]

Python Idiomsto help you write

good code

EuroPython 2015Bilbao, Spain

Marc-André Lemburg

Page 2: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

2:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Speaker Introduction

Marc-André Lemburg

– Python since 1993/1994– Studied Mathematics– eGenix.com GmbH– Senior Software Architect– Consultant / Trainer– Python Core Developer– Python Software Foundation– EuroPython Society– Based in Düsseldorf, Germany

Page 3: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 20153:55

Python Idioms for cooking better CodePython Idioms for cooking better Code

Page 4: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

4:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Agenda

• Coding Conventions

• Common Patterns in Python

• Performance Idioms

• Coding Idioms

• Avoiding Gotchas

• Tools

• Questions

Page 5: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

5:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Agenda

• Coding Conventions

• Common Patterns in Python

• Performance Idioms

• Coding Idioms

• Avoiding Gotchas

• Tools

• Questions

Page 6: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

6:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Python Coding Conventions: The Basics

Page 7: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

7:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

PEP 8 – Python Coding Conventions

• Document: http://www.python.org/dev/peps/pep-0008/

• Many useful tips on how to writereadable Python code

• Guideline, not law :-)

• Can be used as basis for acorporate Python style guide

• Tool: pep8 package for checkingPEP 8 compliance

Page 8: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

8:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Coding Conventions: Python Module Layout

Generic Python module structure:

• Header

• Tools

• Body

Page 9: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

9:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Module Layout: Header Section

• Module Doc String

• Imports– Python ones first– 3rd party modules– application modules

• Constants– usually simple types (strings, integers)

• Globals– often private to the module– used for e.g. caches, static mappings, etc.

Page 10: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

10:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Module Layout: Tools Section

• Exceptions– used in the module– derived from standard exceptions– allow easily tracking origin of exceptions– often part of the module API

• Helper Functions– usually private to the module

Page 11: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

11:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Module Layout: Body Section

• Functions– usually part of the module API

• Classes– usually part of the module API

• Module execution (usually for testing or scripting)– if __name__ == '__main__':

main()

Page 12: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

12:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Module Layout: Overview

• Doc String

• Imports

• Constants & Globals

• Exceptions

• Helper Functions

• Functions & Classes

• Module execution (usually for testing or scripting)– if __name__ == '__main__':

main()

Page 13: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

13:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Module Guidelines

• Use descriptive names for modules– try to name after the most important class

or function

• Try not to use executable codeat the module top-level– put all such code into functions to

make the module import side-effect free– this is especially true for package __init__.py modules

• Always use absolute imports

Page 14: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

14:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Python Idioms

We're now going to take a tour

from high level

to low level

Page 15: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

15:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Agenda

• Coding Conventions

• Common Patterns in Python

• Performance Idioms

• Coding Idioms

• Avoiding Gotchas

• Tools

• Questions

Page 16: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

16:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Common Patterns in Python

• Duck Typing– API interface counts, not type– “When I see a bird that walks like a duck and swims like

a duck and quacks like a duck, I call that bird a duck.” – James W. Riley

• Examples:– Python sequence API, iterator API, index API, mapping

API, file API– ElementTree API for XML, Python DB-API– collections module provides ABCs

Page 17: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

17:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Common Patterns in Python

• Facade / Adapter– Make an object API compatible with another object– Turn any bird into a duck– API based, not class based

• Examples: – StringIO module (turn strings into file-like objects)– DB-API compatible modules (adapt database C APIs to a

standard Python API)

Page 18: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

18:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Common Patterns in Python

• Singletons– Objects that only exist once– usually not enforced in Python

(e.g. True, False are not protected in Py2, None is protected, all in Py3)

• Examples:– True, False, None, NotImplemented– Small integers

Page 19: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

19:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Common Patterns in Python

• Factories– Build objects in multiple ways– Python only has one .__init__() method per class– use factory functions returning instances, or

class methods to the same effect

• Examples:– mx.DateTime.DateTimeFrom()– datetime.fromtimestamp()

Page 20: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

20:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Common Patterns in Python

• Wrappers / Proxies– hide / control APIs– add information / verification to APIs– decorators often create wrappers (try to avoid this)

• Examples:– decorators, functools.partial, weakref, mxProxy

Page 21: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

21:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Common Patterns in Python

• Callbacks– functions/methods called when an event triggers– usually methods on a processing object (handler) or on

a subclass (handler method)– hooks to extend / customize APIs– very common:

runtime introspection for finding handlers

• Examples:– SAX parser, HTML parser, urllib2, threading– command line option processing– asynchronous processing

Page 22: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

22:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Common Patterns in Python

There's also a typical code evolution patternin Python ...

Page 23: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

23:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Code Evolution: From scripts to functions

• Typical situation:– Start with a script using top-level commands

– Turn common script sections into functions

– Pass around common parameters (e.g. context related variables)

– Group functions

Page 24: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

24:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Code Evolution: Abstracting functions to classes

• Next level:– Refactor function sections into methods of classes

– Instantiate classes to drive the application– Provide a user interface (e.g. command line, web or

GUI)– Add more input/output channels– More user customization

Page 25: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

25:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Code Evolution: Abstracting classes into components

• Evolution:– Create subclasses to provide more features– Split code into modules to more clarity– Group modules in packages– Build loosely coupled components based on classes

– Add packaging for better deployment– Add automated tests and builds– Enter build-test-release cycle– Enter the code refactor cycle

Page 26: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

26:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Agenda

• Coding Conventions

• Common Patterns in Python

• Performance Idioms

• Coding Idioms

• Avoiding Gotchas

• Tools

• Questions

Page 27: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

27:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Performance Idioms

… when performance matters

Page 28: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

28:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Looping over sequences

• Different methods possible:– for-loop, map(), list comprehensions, generator

• Fastest:

Use list comprehensions

[op(x) for x in seq]

when operating on sequences

Page 29: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

29:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Joining strings

• Using + (concat)– Good when concatenating a few strings– Copies strings together

• Using ''.join()– Great for concatenating many strings– Copies strings, but only once

• Using StringIO.write(), array.fromstring()or %-formatting– Much slower than the above two

Pyt hon rul es.

Page 30: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

30:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Exceptions

• Expensive in Python (but cheap in Python's C API)

• Exceptions should only be used forexceptional cases …

# Not a good idea:try:

fails_often()except ValueError:

runs_often()

Page 31: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

31:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

More performance hints

• Python wiki page:– https://wiki.python.org/moin/PythonSpeed/Performance

Tips

• My talk “When performance matters...”– http://www.egenix.com/library/presentations/PyCon-U

K-2014-When-performance-matters/

• Examples:– localizing variables: x = global_x– localizing method lookups: m = list.append– dict.setdefault() and collections.defaultdict

Page 32: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

32:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Agenda

• Coding Conventions

• Common Patterns in Python

• Performance Idioms

• Coding Idioms

• Avoiding Gotchas

• Tools

• Questions

Page 33: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

33:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Deeply nested if statements in loops

• Typical situation:

for x in items:

if cond1(x):

do_something()

if cond2(x):

do_something_else()

Page 34: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

34:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Deeply nested if statements in loops

• Staircase Syndrom:

for x in items:

if cond1(x):

do_something()

if cond2(x):

do_something_else()

Page 35: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

35:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Better: Use continue to avoid nesting

for x in items:

if not cond1(x):

continue

do_something()

if not cond2(x):

continue

do_something_else()

Page 36: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

36:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Avoiding Gotchas

… mutable or not, that is the question (well, at least one of them)

Page 37: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

37:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Avoid mutable state in module globals

• Typical situations at module scope:

db = database.connect(…)

settings = dict(verbose=1, log=False)

• Not thread safe… and no, thread locals are not a good idea

• Difficult to manage(access, resources, validations)

• Dangerous in larger projects

Page 38: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

38:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Better: Place mutable state in context objects

• Keep state in context objects

• Pass context to methods and functionsas first parameter

def read_data(context, key): ...def get_config(context): ...

or refactor code to use instance attributes:class Application: def __init__(self): self.context = ApplicationContext()

Page 39: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

39:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Function/method default parameters

• Use only immutable types

• For mutable types:– fallback to None or– immutable alternatives – Add mutable defaults at the

top of the function/method:

def func(x, a=None): if a is None: a = [] return x(a)

Page 40: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

40:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Class default attributes

• Only use immutable types– unless you really know what you're doing

• For mutable types:– fallback to None or– immutable alternatives – Add mutable type defaults in .__init__()

• Document all attributes using comments:# List of file namesfiles = None

Page 41: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

41:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Avoiding Gotchas

… and finally, some very basic hints to to write better code

Page 42: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

42:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Use the in operator in Python

• Interface to the obj.__contains__() method

• if x in some_list: …– better than using some_list.index(x) with try-except

• if x in some_dict: …– better than using some_dict.has_key(x)

• if x in some_set: …– better than ??? (only way to test membership :-))

• Function equivalent:a in b = operator.contains(b, a)

Page 43: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

43:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Boolean testing in Python

• Boolean singletons:True (=integer 1), False (= integer 0)

• Use: if x: print(“x is true”)Not: if x == True: print(“x is true”)Not: if x is True: print(“x is true”)

• Use: if not x: print(“x is false”)Not: if x == False: print(“x is false”)Not: if x is False: print(“x is false”)

• Function equivalent: bool(x)

Page 44: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

44:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Container empty testing in Python

• Empty containers (sequences, mappings, sets) are considered false in Python

• Use: if container: print(“not empty”)

Not: if len(container): print(“not empty”)

Not: if len(container) > 0: print(“not empty”)

Page 45: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

45:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Commas in Python: 1-Tuples

• Nasty in 1-tuples:x = (1,)x = 1, note the comma←

• Weird errors when not intended…>>> s = 'abc',>>> len(s)1>>> s[2]IndexError: tuple index out of range

Page 46: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

46:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Commas in Python: Listings

• Great in argument/parameter/item listings:

Python allows trailing “,” at end of listings...d = {'a': 1,

'b': 2, # insert something new here

}

even in functions/methods...f(x, y, z, a=1,

# insert more keyword args here)

Page 47: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

47:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Agenda

• Coding Conventions

• Common Patterns in Python

• Performance Idioms

• Coding Idioms

• Avoiding Gotchas

• Tools

• Questions

Page 48: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

48:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Tools

• The Python community has createdsome really good tools to help with developinggood code

Let's have a look at some …

Page 49: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

49:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Code Checking Tools

• pylint: https://pypi.python.org/pypi/pylint/– style/typo checking– good documentation: http://docs.pylint.org/

• pep8: https://pypi.python.org/pypi/pep8/ – style checking– dohttps://pypi.python.org/pypi/flake8/cumentation:

http://pep8.readthedocs.org/en/latest/)

• pyflakes:https://pypi.python.org/pypi/pyflakes/– logical checking– no documentation

Page 50: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

50:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Code Checking Tools

• flake8: – combines pep8, pyflakes and a complexity checker– documentation: http://flake8.readthedocs.org/en/latest/)

• pychecker: http://pychecker.sourceforge.net/– note: imports modules– not maintained anymore (predates the other tools),

but can still be useful

Page 51: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

51:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Agenda

• Coding Conventions

• Common Patterns in Python

• Performance Idioms

• Coding Idioms

• Avoiding Gotchas

• Tools

• Questions

Page 52: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

52:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Questions

>>> >>> raise Question()raise Question()

Page 53: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

53:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Thank you for listening

Beautiful is better than ugly.

Page 54: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

54:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Photo References

CC BY / CC BY-SA licensed photos: Public domain clip arts:

https://www.flickr.com/photos/runlevel0/6297898421/ https://openclipart.org/detail/8879/rubber-duckhttps://openclipart.org/detail/215201/evolution

All other photos and images are used by permission.

Page 55: Python Idioms...(c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-André Lemburg

55:55

(c) 2015 eGenix.com GmbH, [email protected] EuroPython 2015

Contact

eGenix.com Software, Skills and Services GmbHMarc-André LemburgPastor-Löh-Str. 48D-40764 LangenfeldGermany

eMail: [email protected]: +49 211 9304112Fax: +49 211 3005250Web: http://www.egenix.com/