Skip to content

Instantly share code, notes, and snippets.

@tirkarthi
Created February 11, 2020 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tirkarthi/1995d5115bdaa504cb7530c5ffd80374 to your computer and use it in GitHub Desktop.
Save tirkarthi/1995d5115bdaa504cb7530c5ffd80374 to your computer and use it in GitHub Desktop.
marp
true

Python best practices

Karthikeyan Singaravelan /(Software Engineer at Platform Portal | Python core developer)/


Agenda

  • Better abstractions
  • Developer tooling
  • Testing
  • Performance

Philosopy of Python

  • 30 years of active development.
  • Zen of Python.
  • PEP8 as a standard.

Better abstractions

  • Learn more about dunder methods and protocols.
  • Using properties and caching.
  • Abstractions in standard library.

Context managers for resource management

  • Context manager protocol __enter__ and __exit__ for resource management.
  • contextlib module provides handy wrappers.
try:
    db = Database()
    db.query()
finally:
    db.close()
with Database() as db:
    db.query()

Properties and caching

  • Properties for computational attributes. Cache them if they are immutable.
class Person:
    first_name: str
    last_name: str

    def get_full_name(self):
        return f"{first_name} {last_name}"

    @property
    def full_name(self):
        return f"{first_name} {last_name}"

Abstractions in standard library.

  • collections - namedtuple, defaultdict, Counter.
  • itertools - functional constructs
  • command line interface for modules - zipfile, tarfile, json, etc.
  • Dataclasses.

Collections module example

collection = {}

for student, mark in marks:
    if mark not in collection:
        collection[mark] = []
    else:
        collection[mark].append(student)
from collections import defaultdict

collection = defaultdict(list)

for student, mark in marks:
    collection[mark].append(student)

Turn on warnings

  • Helps catch potential errors in future.
  • Ensures smooth upgrade process.

Developer Tooling

  • IPython and notebook for interactive sessions.
  • timeit for benchmarking. pyperformance from PyPI.
  • CProfile for profiling.
  • Automatic formatting - Choose one tool - autopep8, yapf, black.
  • pdb for debugging. ipdb, pudb from PyPI.
  • Optional typing with mypy.

Testing

  • Use pytest for concise tests.
  • Use coverage for code coverage.
  • Use unittest.mock for mocking tests.
  • Linters : flake8 and pylint.

Performance

  • pypy is a JIT for computation intensive code.
  • Cython for writing C interface code.
  • Use native interface libraries. E.g. numpy, pandas, scipy, etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment