Skip to content

Instantly share code, notes, and snippets.

View flyte's full-sized avatar
🎯
Focusing

Ellis Percival flyte

🎯
Focusing
  • London
View GitHub Profile
@flyte
flyte / csv.py
Last active December 20, 2015 03:18
Format a single value for use in an RFC4180 compliant CSV file. Join multiple of the values returned from this function with a comma to create a row.
def _format_csv_value(self, value):
"""Takes a single CSV value string and formats it in compliance with RFC4180.
Multiple values can be joined together by putting them in a list and using ",".join(the_list).
http://tools.ietf.org/html/rfc4180#page-3
:param value: A single value destined to be output among multiple in a CSV row
:return: The escaped and/or quoted string if necessary, otherwise simply returns <value>.
"""
for x in [",", '"', "\n", "\r\n"]:
@flyte
flyte / render_tmx.py
Created August 23, 2013 19:56
Draw TMX tiles in pygame and save them as an image
import pygame, pytmx, argparse
def draw_tmx(tmx, window):
# For each layer
for l in xrange(0, len(tmx.tilelayers)):
# For each y tile coordinate
for y in xrange(0, tmx.height):
# For each x tile coordinate
for x in xrange(0, tmx.width):
# try not strictly necessary, but a hangover from some old bad code
@flyte
flyte / image_resize.py
Last active December 25, 2015 11:59
Resize an image to multiple different sizes based on their long edge size.
from PIL import Image
import os
import argparse
p = argparse.ArgumentParser()
p.add_argument("img_filename")
sizes = {
"thumbnail": 100,
"small": 240,
@flyte
flyte / split_img.py
Last active December 28, 2015 20:29
Split img down the middle vertically.
from PIL import Image
def split_img(img_path):
"""
Splits an image vertically down the middle and returns a tuple
containing (left_half, right_half) Pillow Image objects.
"""
img = Image.open(img_path)
width, height = img.size
half_width = int(width/2)
@flyte
flyte / check_replication.py
Last active December 29, 2015 08:19
Nagios plugin to check CouchDB replication status
#!/usr/bin/python
import json, urllib2, sys, argparse
OK = 0
WARNING = 1
CRITICAL = 2
UNKNOWN = 3
p = argparse.ArgumentParser()
@flyte
flyte / archive_to_glacier.py
Created November 25, 2013 18:20
tar.gz a directory and push it to Glacier.
from boto import glacier
from datetime import datetime
from os.path import isdir
import argparse
import tarfile
try:
# Python 2.7
from collections import OrderedDict
except ImportError:
@flyte
flyte / fake_swipe.py
Last active December 30, 2015 06:09
Fake a "swipe" UDP packet from a network based RFID reader by spoofing the source IP.
from collections import OrderedDict
import argparse
import logging
# Suppress ipv6 warning when importing scapy
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import IP, UDP, send, L3RawSocket, conf
PACKET_FORMAT = "tag=%s&%s&seq=0x%04x"
@flyte
flyte / ts_make_serveradmin.py
Last active December 30, 2015 23:49
Telnets to a TeamSpeak 3 server and sets the given user to be in the serveradmin group. User must be on the TS server at runtime.
from telnetlib import Telnet
from getpass import getpass
from time import sleep
import re
import argparse
import sys
p = argparse.ArgumentParser()
p.add_argument("-s", "--server", required=True)
p.add_argument("-p", "--port", default=10011, type=int)
@flyte
flyte / get_all_site_links.py
Created December 16, 2013 01:22
Gets links to all pages on a website.
@flyte
flyte / firstcc_major_disruption.py
Created December 17, 2013 10:40
Get text from First Capital Connect major disruption website
from mechanize import Browser
from BeautifulSoup import BeautifulSoup
if __name__ == "__main__":
br = Browser()
page = br.open("http://www.firstcapitalconnect.co.uk/plan-your-journey/major-disruption/")
soup = BeautifulSoup(page.get_data())
s = soup.find("div", {"class": "single-container"})
lines = s.findAll("span")
text = ""