Skip to content

Instantly share code, notes, and snippets.

View johnbenjaminlewis's full-sized avatar

Ben Lewis johnbenjaminlewis

View GitHub Profile
import datetime
from typing import Union
def get_start_of_week(dt: Union[datetime.datetime, datetime.date], weeks_from_now: int = 0) -> datetime.datetime:
""" Given a `datetime.date` or `datetime.datetime` instance, return
a `datetime.datetime` instance corresponding to the first day
(i.e., Sunday) of that week.
This function makes it really easy to calculate days-of-the-week from now.
@johnbenjaminlewis
johnbenjaminlewis / README.md
Created September 16, 2019 16:39 — forked from rstacruz/README.md
How to install Docker in Mac, Windows, and Linux

Getting Docker

Docker is available for Linux, MacOS, and Windows.

MacOS

Docker for Mac is best installed with Homebrew and Homebrew Cask. For other ways to install on MacOS, see Install Docker for Mac in Docker's docs.

brew cask install docker # Install Docker
@johnbenjaminlewis
johnbenjaminlewis / gcloud_bug.sh
Last active June 18, 2018 18:18
Google Cloud bug re-creation
#!/bin/bash
set -e
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
GCLOUD_ORG_ID=""
GCLOUD_BILLING_ACCOUNT_ID=""
# These variables should be edited for project id uniqueness
RANDOM_STR="kasdfj" # for entropy
import functools
import itertools
import logging
import time
log = logging.getLogger(__name__)
def retry(times, timeout=0.2, exceptions=None):
@johnbenjaminlewis
johnbenjaminlewis / context_decorator.py
Last active June 23, 2016 01:12
Context Decorator
"""Python 3 has a ContextDecorator class mixin that allows context
managers to be used as decorators also. Even better, the decorator
has optional arguments.
If python2, $ pip install contextlib2
"""
from __future__ import absolute_import
import functools
@johnbenjaminlewis
johnbenjaminlewis / subdomain_dispatcher.py
Last active May 4, 2016 13:07
Simple subdomain dispatcher for Flask
#!/usr/bin/env python
""" Run multiple flask apps in the same process, hosted at different domain names.
I choose a domain alias for my app, say `myapp.com` then add to my hosts file. Then
my hosts file will have these entries::
127.0.0.1 myapp.com www.myapp.com admin.myapp.com
You can use a `defaultdict` with `redirect_create_app` to redirect all requested hostnames
not in `SubdomainDispatcher.registry` to a default location. As an example, after starting the server
class SentinelInt(int):
"""Makes unique sentinel values that look like integers but equality
is overridden.
>>> SentinelInt(0) == SentinelInt(0)
False
>>> s = SentinelInt(0)
>>> s == s
True
"""
import functools
import itertools
import logging
import time
log = logging.getLogger(__name__)
def retry(times, timeout=0.2, exceptions=None):
@johnbenjaminlewis
johnbenjaminlewis / verify_packages.py
Last active October 19, 2015 19:53
Python package verification: make sure directories with .py files are actual Python packages
""" We have discovered some test packages weren't autodiscovered
because they didn't have an __init__.py file. This module walks through
the directory tree to ensure that each directory with a Python file also
has __init__.py.
TODO: add directory exclusion handling.
Cool. Now walk it out
"""
from functools import wraps
@johnbenjaminlewis
johnbenjaminlewis / transform_exception.py
Last active October 15, 2015 15:09
Transform one exception to another while preserving original stack trace
from contextlib import contextmanager
import sys
@contextmanager
def transform_exception(new_exception, new_message=None, exceptions=None):
"""Ok, this is hacky. Takes any exception listed in `exceptions`,
reraises as `new_exception` type but preserves the old stack trace.
This is useful if you are writing a library and want to transform a