Zen and the Art of Python

37
Zen and the Art of Python Clayton Parker - Pythology 101 Zen and the Art of Python - Clayton Parker - Pythology 101

description

In this talk we will explore the Zen of Python and the famous PEP8 Style Guide. Learn about the tenets of the Python language and how you can apply them to make your code beautiful and functional. After all, Readability Counts!

Transcript of Zen and the Art of Python

Page 1: Zen and the Art of Python

Zen and the Art of PythonClayton Parker - Pythology 101

Zen and the Art of Python - Clayton Parker - Pythology 101

Page 2: Zen and the Art of Python

Who am I?Director of Engineering at Six Feet Upclaytron on the internets

Page 3: Zen and the Art of Python

The Zen of Pythonand

The Python Style Guide(PEP8)

Page 4: Zen and the Art of Python

The Zen of Python$ python -m this

>>> import this

Page 5: Zen and the Art of Python

The Zen of Python$ python -m thisThe Zen of Python, by Tim Peters

Beautiful is better than ugly.Explicit is better than implicit.Simple is better than complex.Complex is better than complicated.Flat is better than nested.Sparse is better than dense.Readability counts.Special cases aren't special enough to break the rules.Although practicality beats purity.Errors should never pass silently.Unless explicitly silenced.In the face of ambiguity, refuse the temptation to guess.There should be one-- and preferably only one --obvious way to do it.Although that way may not be obvious at first unless you're Dutch.Now is better than never.Although never is often better than *right* now.If the implementation is hard to explain, it's a bad idea.If the implementation is easy to explain, it may be a good idea.Namespaces are one honking great idea -- let's do more of those!

Page 6: Zen and the Art of Python

Beautiful is better than ugly

Page 7: Zen and the Art of Python

Readability Counts

Page 8: Zen and the Art of Python

PEP 8We have a set of rules for that!

Page 9: Zen and the Art of Python

PEP 8Prevailing style wins

Page 10: Zen and the Art of Python

Comments# Comments start with a space after the comment symbol. Use complete# sentences and proper grammar when writing comments. Comments should# be in English unless you are certain the readers will *not* be# English speaking.

# Long flowing text should be kept to under 72 columns like above.

x = 5 # Use inline comments sparingly.

Page 11: Zen and the Art of Python

Indentation4 space indentsTabs only if the prevailing styleNever mix tabs and spaces!

Page 12: Zen and the Art of Python

Whitespace

Page 13: Zen and the Art of Python

important_var = 5awesome_var = 15awesome_var+=10my_dict ={ 'spam':'eggs','ham':'parrot'}my_list=[3, 2,1]another_list = [8, 4,5,6 ]extra_list=my_list+another_listsorted ( combined_list,reverse = True)

Page 14: Zen and the Art of Python

important_var = 5awesome_var = 15awesome_var += 10my_dict = {'spam': 'eggs', 'ham': 'parrot'}my_list = [3, 2, 1]another_list = [8, 4, 5, 6]extra_list = my_list + another_listsorted(combined_list, reverse=True)

Page 15: Zen and the Art of Python

Max Line LengthA hotly debated subject!

Page 16: Zen and the Art of Python

things = ['overwrite', 'photobathic', 'tranquillization', 'resiny', 'runt', 'Siganus']extra_special_things = [thing for thing in extra_shiny_things if thing == 'elpidite']################################################################ 79 columns --|

Page 17: Zen and the Art of Python

################################################################ 79 columns --|things = [ 'overwrite', 'photobathic', 'tranquillization', 'resiny', 'runt', 'Siganus',]extra_special_things = [ thing for thing in extra_shiny_things if thing == 'elpidite']

Page 18: Zen and the Art of Python

################################################################ 79 columns --|if event.new_state.id == 'offline' and (state == 'published' or state == 'external'): workflow.doActionFor(obj, 'reject', workflow='custom_workflow', comment='Reject')

Page 19: Zen and the Art of Python

################################################################ 79 columns --|offline = event.new_state.id == 'offline'published = state in ['published', 'external']

if offline and published: workflow.doActionFor( obj, 'reject', workflow='custom_workflow', comment='Reject content automatically', )

Page 20: Zen and the Art of Python

################################################################ 79 columns --|long_string = "Lorem ipsum dolor sit amet, consectetur adipiscing. Cras cursus elit."

Page 21: Zen and the Art of Python

################################################################ 79 columns --|long_string = ( "Lorem ipsum dolor sit amet, consectetur adipiscing." "Cras cursus elit.")

Page 22: Zen and the Art of Python

Explicit is better than implicit

Page 23: Zen and the Art of Python

import os, sysfrom my.package import *

Page 24: Zen and the Art of Python

import osimport sysfrom my.package import Octopus, Blowfish

Page 25: Zen and the Art of Python

There should be one-- andpreferably only one --obviousway to do it

Page 26: Zen and the Art of Python

# Badtype(obj) is type(1)

# Goodisinstance(obj, int)

Page 27: Zen and the Art of Python

# Badmy_variable == None

# Goodmy_variable is None

Page 28: Zen and the Art of Python

# Badnot len(my_list) > 0

# Goodnot my_list

Page 29: Zen and the Art of Python

# Badboolean_value == False

# Goodnot boolean_value

Page 30: Zen and the Art of Python

Flat is better than nested

Page 31: Zen and the Art of Python

for item in items: if some_check(item): # do some magic if another_check(item): # more magic operate_on(item)

Page 32: Zen and the Art of Python

for item in items: if not some_check(item): continue if not another_check(item): continue # do some magic # more magic operate_on(item)

Page 33: Zen and the Art of Python

aws_region = Nonefor k,v in query_response.items(): if k == 'entry_list': for i in v: for k, v2 in i.items(): if k == 'name_value_list': if isinstance(v2, dict): for k2, v3 in v2.items(): if k2 == 'aws_region': aws_region = v3['value']

Page 34: Zen and the Art of Python

aws_region = Noneentries = query_response.get('entry_list', {})values = entries.get('name_value_list', {})if isinstance(values, dict): aws_region = values.get('aws_region', {}).get('value', None)

Page 35: Zen and the Art of Python

Toolsflake8 - Combination of pep8 and pyflakesPyLint / Frosted - More in-depth linting of codeautopep8 - Automatic PEP8 conformance

Page 37: Zen and the Art of Python

Questions?