Skip to content

Instantly share code, notes, and snippets.

Avatar

Mark Betz Markbnj

View GitHub Profile
@Markbnj
Markbnj / getch.py
Created Aug 26, 2015
Get a single key press from the terminal in python
View getch.py
import termios
import sys
import tty
def _getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
@Markbnj
Markbnj / environment-specific-settings.py
Last active Aug 29, 2015
Environment-specific settings for python 2 modules
View environment-specific-settings.py
# Environment-specific settings for python 2 modules
# This code placed in __init__.py will allow a module to maintain
# environment-specific settings in files located in a settings folder
# within the module.
#
# Example:
#
# ├── mymodule.py
# ├── __init__.py
@Markbnj
Markbnj / debug-scrapyd-ipdb.py
Last active Aug 29, 2015
User ipdb to debug a running spider in scrapyd
View debug-scrapyd-ipdb.py
# The scrapyd daemon adds a lot of power and flexibility to scrapy, but it also adds a layer of indirection
# (another process) that can make debugging a running spider troublesome. Use this code to launch scrapyd
# in ipdb (or pdb). You'll be able to breakpoint and catch exceptions inside the spiders it launches.
import scrapyd.runner
import ipdb
ipdb.runcall(scrapyd.runner.main)
@Markbnj
Markbnj / simple-google-places-wrapper.py
Created Aug 11, 2015
A simple google places wrapper using requests
View simple-google-places-wrapper.py
import requests
import json
GOOGLE_API_KEY = "<A google API key>"
TEXT_SEARCH_TEMPLATE = "https://maps.googleapis.com/maps/api/place/textsearch/json?key={}&query={}"
NEARBY_SEARCH_TEMPLATE = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key={}&location={},{}&radius={}"
DETAIL_SEARCH_ID_TEMPLATE = "https://maps.googleapis.com/maps/api/place/details/json?key={}&placeid={}"
@Markbnj
Markbnj / ec2-discover-instances.py
Last active Aug 29, 2015
Discover ec2 instances by tag and tag value
View ec2-discover-instances.py
from boto import ec2
class Resource(object):
"""
Represents a single resource returned from the Discovery class's
get_resources method.
"""
def __init__(self, host_name, private_host_name, ip, private_ip, tags):
self.__host_name = host_name
self.__private_host_name = private_host_name
@Markbnj
Markbnj / set-proxy.sh
Created Aug 11, 2015
Set http/https proxy address on a console session
View set-proxy.sh
# sets the http/s proxy for the current shell to
# the passed argument, which should be in the form
# http://xxx.xxx.xxx.xxx:xxxx
export http_proxy=$1
export https_proxy=$http_proxy
export no_proxy="localhost,127.0.0.1"
@Markbnj
Markbnj / Dockerfile
Created Aug 11, 2015
Simple dockerfile for an Ubuntu-based base image for python services
View Dockerfile
# DockerFile for building a base image for python services
FROM ubuntu:15.04
MAINTAINER Someone <someone@some.where>
# common stuff
RUN echo "#!/bin/sh\nexit 0" > /usr/sbin/policy-rc.d && \
mkdir /home/root && \
dpkg-reconfigure locales && \
locale-gen en_US en_US.UTF-8 && \
@Markbnj
Markbnj / remove-containers.sh
Created Aug 11, 2015
Stop and remove all Docker containers
View remove-containers.sh
sudo docker stop $(sudo docker ps -a -q)
sudo docker rm $(sudo docker ps -a -q)
@Markbnj
Markbnj / remove-images.sh
Created Aug 11, 2015
Remove all Docker images
View remove-images.sh
sudo docker rmi $(sudo docker images -a -q)
View haproxy-install.sh
sudo apt-add-repository ppa:vbernat/haproxy-1.5
sudo apt-get update
sudo apt-get -qy install haproxy
sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.orig
# To enable logging edit /etc/rsyslog.conf to match the following:
#
# provides UDP syslog reception
# $ModLoad imudp
# $UDPServerRun 514
You can’t perform that action at this time.