Skip to content

Latest commit

 

History

History
35 lines (26 loc) · 1.95 KB

python.md

File metadata and controls

35 lines (26 loc) · 1.95 KB

Best Practices for Python

Baseline

Libraries

For consistency, prefer the following libraries to others that perform the same tasks:

  • Fabric for project tasks
  • Flask for light web apps or Django for heavy web apps
  • boto for accessing AWS programmatically
  • pytz for manipulating timezones
  • psycopg2 for accessing Postgres
  • lxml for XML/DOM manipulation
  • Requests for working over HTTP

Specifics

  • When testing for nulls, always use if foo is None rather than if !foo so foo == 0 does not cause bugs.
  • Always initialize and store datetimes in the UTC timezone. Never use naive datetimes for any reason.
  • Always use with when accessing resources that need to be closed.
  • Always access blocking resources (files, databases) as little as possible.
  • When accessing a dictionary element that may not exist, use get(). For example, os.environ.get('DEPLOYMENT_TARGET', None).
  • Project settings that will be used by both fabric and other code should be isolated in app_config.py. fabfile.py and Django's settings.py should import from this file to prevent duplication.
  • Imports should be organized into three blocks: stdlib modules, third-party modules and our own modules. Each group should be alphabetized.
  • Avoid from foo import *. It is the mindkiller.
  • Functions that are de facto "private" (only called within a module or only called within a class) should be prefixed with a single _, e.g. _slugify.

Flask

  • All views should return with make_response.