Skip to content

Instantly share code, notes, and snippets.

@perrygeo
perrygeo / tiled_assert.py
Last active June 14, 2018 15:26
Tiled=true fails silently
#!/usr/bin/env python
from pprint import pprint
from functools import reduce
import operator
import numpy as np
from affine import Affine
from rasterio.crs import CRS
import rasterio
import random
import time
import json
import threading
import click
def send_message(msg, async=True):
"""Logs the message to an output stream.
@perrygeo
perrygeo / log_calls.py
Created March 20, 2018 20:20
Decorator to log all call parameters and return values for a function
import sys
def log_calls(fh=sys.stderr):
"""Logs the function, args, kwargs and return value to a file handle
"""
def decorator(func):
def wrapper(*args, **kwargs):
retval = func(*args, **kwargs)
print(func, args, kwargs, retval, file=fh)
@perrygeo
perrygeo / Dockerfile
Last active February 27, 2023 12:00
Minimal debian image with Python 3.6 and geo python tools
FROM python:3.6-slim-stretch
ADD requirements.txt /tmp/requirements.txt
RUN apt-get update && \
apt-get install -y \
build-essential \
make \
gcc \
locales \
@perrygeo
perrygeo / deprecated_kwarg.py
Last active January 9, 2018 00:31
Decorator for aliasing kwargs in Python functions
#!/usr/bin/env python
import warnings
from functools import wraps
def deprecated_kwarg(old, new, version):
def decorator(func):
@wraps(func)
def func_wrapper(*args, **kwargs):
new_kwargs = kwargs.copy()
#!/usr/bin/env python
from fitparse import FitFile
import click
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
def get_records(fitfile, fields=None):
if fields is None:

Parallelism and Concurrency in Python

There should be one-- and preferably only one --obvious way to do it.

-- Tim Peters, Zen of Python

When we need our programs to run faster, the first place we often look is parallelism and concurrency. By doing more things at once, we hope for significant speed gains. Python has a number of techniques optimized for different use cases; there is no one obvious way to do it! So how do you decide the correct approach?

Here's my quick take on the primary tools for parallel execution in Python, their strengths and weaknesses. I'll focus mainly on the tools in the standard library, but I'll touch on a few third-party modules that provide interesting features.

all: deps clean install test
.PHONY: docs
install:
python setup.py build_ext
pip install -e .[all]
deps:
pip install -r requirements-dev.txt
import logging
import sys
# ----------- Library code ------------------- #
logger = logging.getLogger(__name__)
# think of this as a global restriction
# even if the handlers want access to lower levels, they don't get it
logger.setLevel(logging.INFO)
@perrygeo
perrygeo / lmgtfy.py
Created March 10, 2017 14:52
help() is not helpful. let me google that for you
# LMGTFY
# For best results, have python autoload it
# add to $(sitepackages)/sitecustomize.py
# alias sitepackages="python -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())'"
import webbrowser
from urllib2 import quote
def lmgtfy(x):
search = "Python {}".format(x.__name__)