Zen and the Art of Python

Post on 05-Jun-2015

370 views 3 download

Tags:

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

Zen and the Art of PythonClayton Parker - Pythology 101

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

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

The Zen of Pythonand

The Python Style Guide(PEP8)

The Zen of Python$ python -m this

>>> import this

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!

Beautiful is better than ugly

Readability Counts

PEP 8We have a set of rules for that!

PEP 8Prevailing style wins

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.

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

Whitespace

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)

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)

Max Line LengthA hotly debated subject!

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

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

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

################################################################ 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', )

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

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

Explicit is better than implicit

import os, sysfrom my.package import *

import osimport sysfrom my.package import Octopus, Blowfish

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

# Badtype(obj) is type(1)

# Goodisinstance(obj, int)

# Badmy_variable == None

# Goodmy_variable is None

# Badnot len(my_list) > 0

# Goodnot my_list

# Badboolean_value == False

# Goodnot boolean_value

Flat is better than nested

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

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

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']

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)

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

Questions?