python idioms
play

Python Idioms to help you write good code EuroPython 2015 Bilbao, - PowerPoint PPT Presentation

Python Idioms to help you write good code EuroPython 2015 Bilbao, Spain Marc-Andr Lemburg (c) 2015 eGenix.com Software, Skills and Services GmbH, info@egenix.com Speaker Introduction Marc-Andr Lemburg Python since 1993/1994


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

  2. 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 (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 2:55

  3. Python Idioms for cooking better Code Python Idioms for cooking better Code (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 3:55

  4. Agenda • Coding Conventions • Common Patterns in Python • Performance Idioms • Coding Idioms • Avoiding Gotchas • Tools • Questions (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 4:55

  5. Agenda • Coding Conventions • Common Patterns in Python • Performance Idioms • Coding Idioms • Avoiding Gotchas • Tools • Questions (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 5:55

  6. Python Coding Conventions: The Basics (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 6:55

  7. PEP 8 – Python Coding Conventions • Document: http://www.python.org/dev/peps/pep-0008/ • Many useful tips on how to write readable Python code • Guideline, not law :-) • Can be used as basis for a corporate Python style guide • Tool: pep8 package for checking PEP 8 compliance (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 7:55

  8. Coding Conventions: Python Module Layout Generic Python module structure: • Header • Tools • Body (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 8:55

  9. Module Layout: Header Section • Module Doc String • Imports – Python ones first – 3 rd party modules – application modules • Constants – usually simple types (strings, integers) • Globals – often private to the module – used for e.g. caches, static mappings, etc. (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 9:55

  10. 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 (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 10:55

  11. 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() (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 11:55

  12. Module Layout: Overview • Doc String • Imports • Constants & Globals • Exceptions • Helper Functions • Functions & Classes • Module execution (usually for testing or scripting) – if __name__ == '__main__': main() (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 12:55

  13. Module Guidelines • Use descriptive names for modules – try to name after the most important class or function • Try not to use executable code at 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 (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 13:55

  14. Python Idioms We're now going to take a tour from high level to low level (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 14:55

  15. Agenda • Coding Conventions • Common Patterns in Python • Performance Idioms • Coding Idioms • Avoiding Gotchas • Tools • Questions (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 15:55

  16. 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 (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 16:55

  17. 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) (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 17:55

  18. 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 (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 18:55

  19. 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() (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 19:55

  20. 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 (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 20:55

  21. 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 (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 21:55

  22. Common Patterns in Python There's also a typical code evolution pattern in Python ... (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 22:55

  23. 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 (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 23:55

  24. 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 (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 24:55

  25. 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 (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 25:55

  26. Agenda • Coding Conventions • Common Patterns in Python • Performance Idioms • Coding Idioms • Avoiding Gotchas • Tools • Questions (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 26:55

  27. Performance Idioms … when performance matters (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 27:55

  28. 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 (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 28:55

  29. 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. (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 29:55

  30. Exceptions • Expensive in Python (but cheap in Python's C API) • Exceptions should only be used for exceptional cases … # Not a good idea: try: fails_often() except ValueError: runs_often() (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 30:55

  31. 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 (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 31:55

  32. Agenda • Coding Conventions • Common Patterns in Python • Performance Idioms • Coding Idioms • Avoiding Gotchas • Tools • Questions (c) 2015 eGenix.com GmbH, info@egenix.com EuroPython 2015 32:55

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend