Skip to content

Instantly share code, notes, and snippets.

View textbook's full-sized avatar

Jonathan Sharpe textbook

View GitHub Profile
@textbook
textbook / demo.py
Last active November 20, 2016 09:32
Non-data descriptors as applied to http://stackoverflow.com/q/40695883/3001761
VERB_QUIET = 0
class VerbosityLevel(object):
def __init__(self, level):
self.level = level
def __get__(self, obj, _):
return obj.level >= self.level
@textbook
textbook / jsharpe.theme.bash
Created June 23, 2016 09:11
Adds virtualenv to Pivotal's default (bobby) bash_it theme
#!/usr/bin/env bash
SCM_THEME_PROMPT_DIRTY=" ${red}✗"
SCM_THEME_PROMPT_CLEAN=" ${bold_green}✓"
SCM_THEME_PROMPT_PREFIX=" |"
SCM_THEME_PROMPT_SUFFIX="${green}|"
GIT_THEME_PROMPT_DIRTY=" ${red}✗"
GIT_THEME_PROMPT_CLEAN=" ${bold_green}✓"
GIT_THEME_PROMPT_PREFIX=" ${green}|"
GIT_THEME_PROMPT_SUFFIX="${green}|"
@textbook
textbook / Flask on CF.md
Last active March 8, 2016 15:02 — forked from ihuston/Flask on CF.md
Simple Flask application for Cloud Foundry

Simple Python Flask app on Cloud Foundry

This is a (very) simple Flask application that shows how the built-in Python buildpack detection on Cloud Foundry works.

To push to Cloud Foundry, log in and then use

$ cf push myapp-name

Python on Cloud Foundry

@textbook
textbook / keymap.cson
Last active July 9, 2016 16:52
JetBrains-style Atom keymap
# JetBrains-style
'.platform-darwin':
'cmd-1': 'tree-view:toggle'
'cmd-shift-a': 'command-palette:toggle'
'.platform-darwin .tree-view':
'cmd-1': 'tree-view:toggle'
'.platform-darwin atom-text-editor':
'cmd-b': 'symbols-view:go-to-declaration'
'cmd-d': 'editor:duplicate-lines'
'cmd-l': 'go-to-line:toggle'
@textbook
textbook / imdb_venn.py
Last active February 19, 2021 23:41
Want to identify an actor from multiple movies they've been in? I've got your back.
"""Find the overlap of actors between multiple movies."""
from __future__ import print_function
import argparse
from operator import itemgetter
from sys import argv, exit
try:
from imdb import IMDb
class Switch(object):
"""A class for faking switch syntax with a context manager.
Args:
value (object): The stored value to compare any cases to.
Example:
>>> with Switch(1) as case:
... if case(1):
@textbook
textbook / valid_input.py
Last active August 29, 2015 14:15
Class-based user input validation (comment at http://codereview.stackexchange.com/q/80525/32391)
"""Functionality for validating user inputs."""
# pylint: disable=too-few-public-methods
from __future__ import print_function
import getpass
import re
import string
import sys
class Cached(object):
"""Cache classes with positional arguments."""
_cache = {}
def __new__(cls, *args):
if args not in cls._cache:
cls._cache[args] = super(Cached, cls).__new__(cls)
return cls._cache[args]
import datetime
from operator import itemgetter
def get_int_input(prompt, min_=0, max_=None):
"""Get a valid integer input."""
while True:
try:
i = int(input(prompt))
except ValueError:
print("Please enter an integer.")
@textbook
textbook / testing.py
Last active December 27, 2015 01:59
Collection of useful bits and pieces of Python code, plus a few simple test routines - now pylint compliant
"""Simple unit testing functionality."""
from __future__ import print_function
def _run_function_test(function, expected, args=None, kwargs=None):
"""Check whether function returns/raises expected with supplied args."""
if args is None:
args = tuple()
if kwargs is None:
kwargs = dict()