Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Zen and the Art of Python

1,415 views

Published on

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!

Published in: Software
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (Unlimited) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... Download Full EPUB Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... ACCESS WEBSITE for All Ebooks ......................................................................................................................... Download Full PDF EBOOK here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... Download EPUB Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... Download doc Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

Zen and the Art of Python

  1. 1. Zen and the Art of Python Clayton Parker - Pythology 101 Zen and the Art of Python - Clayton Parker - Pythology 101
  2. 2. Who am I? Director of Engineering at Six Feet Up claytron on the internets
  3. 3. The Zen of Python and The Python Style Guide (PEP8)
  4. 4. The Zen of Python $ python -m this >>> import this
  5. 5. The Zen of Python $ python -m this The 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!
  6. 6. Beautiful is better than ugly
  7. 7. Readability Counts
  8. 8. PEP 8 We have a set of rules for that!
  9. 9. PEP 8 Prevailing style wins
  10. 10. 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.
  11. 11. Indentation 4 space indents Tabs only if the prevailing style Never mix tabs and spaces!
  12. 12. Whitespace
  13. 13. important_var = 5 awesome_var = 15 awesome_var+=10 my_dict ={ 'spam':'eggs','ham':'parrot'} my_list=[3, 2,1] another_list = [8, 4,5,6 ] extra_list=my_list+another_list sorted ( combined_list,reverse = True)
  14. 14. important_var = 5 awesome_var = 15 awesome_var += 10 my_dict = {'spam': 'eggs', 'ham': 'parrot'} my_list = [3, 2, 1] another_list = [8, 4, 5, 6] extra_list = my_list + another_list sorted(combined_list, reverse=True)
  15. 15. Max Line Length A hotly debated subject!
  16. 16. things = ['overwrite', 'photobathic', 'tranquillization', 'resiny', 'runt', 'Siganus'] extra_special_things = [thing for thing in extra_shiny_things if thing == 'elpidite'] ################################################################ 79 columns --|
  17. 17. ################################################################ 79 columns --| things = [ 'overwrite', 'photobathic', 'tranquillization', 'resiny', 'runt', 'Siganus', ] extra_special_things = [ thing for thing in extra_shiny_things if thing == 'elpidite' ]
  18. 18. ################################################################ 79 columns --| if event.new_state.id == 'offline' and (state == 'published' or state == 'external'): workflow.doActionFor(obj, 'reject', workflow='custom_workflow', comment='Reject')
  19. 19. ################################################################ 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', )
  20. 20. ################################################################ 79 columns --| long_string = "Lorem ipsum dolor sit amet, consectetur adipiscing. Cras cursus elit."
  21. 21. ################################################################ 79 columns --| long_string = ( "Lorem ipsum dolor sit amet, consectetur adipiscing." "Cras cursus elit." )
  22. 22. Explicit is better than implicit
  23. 23. import os, sys from my.package import *
  24. 24. import os import sys from my.package import Octopus, Blowfish
  25. 25. There should be one-- and preferably only one --obvious way to do it
  26. 26. # Bad type(obj) is type(1) # Good isinstance(obj, int)
  27. 27. # Bad my_variable == None # Good my_variable is None
  28. 28. # Bad not len(my_list) > 0 # Good not my_list
  29. 29. # Bad boolean_value == False # Good not boolean_value
  30. 30. Flat is better than nested
  31. 31. for item in items: if some_check(item): # do some magic if another_check(item): # more magic operate_on(item)
  32. 32. for item in items: if not some_check(item): continue if not another_check(item): continue # do some magic # more magic operate_on(item)
  33. 33. aws_region = None for 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']
  34. 34. aws_region = None entries = query_response.get('entry_list', {}) values = entries.get('name_value_list', {}) if isinstance(values, dict): aws_region = values.get('aws_region', {}).get('value', None)
  35. 35. Tools flake8 - Combination of pep8 and pyflakes PyLint / Frosted - More in-depth linting of code autopep8 - Automatic PEP8 conformance
  36. 36. Links The Zen of Python - PEP 20 Zen discussed on Stackoverflow The Python Style Guide - PEP 8 Writing Idiomatic Python Book
  37. 37. Questions?

×