Skip to content

Instantly share code, notes, and snippets.

@pbdeuchler
pbdeuchler / Parser.py
Created June 20, 2011 19:42
Quick python script for parsing CSV files
#!/usr/bin/python -tt
#Didn't you hear? The bird is the word!
#@version: 0.1
import sys
def main(filename):
f = open(filename, "r")
f2 = open("log.txt", "w")
count = 0
for line in f:
@pbdeuchler
pbdeuchler / gist:1041093
Created June 22, 2011 20:33
Trying to dynamically import modules/call methods within said modules
#import
pathname = os.path.dirname(sys.argv[0]) + "/"
pathname = pathname + "/modules/"
__modules__ = [ os.path.basename(f)[:-3] for f in glob.glob(os.path.dirname(pathname)+"/*.py")]
for module in __modules__:
__import__(module)
#calling checkMe()
@pbdeuchler
pbdeuchler / gist:1045428
Created June 24, 2011 19:02
Python ReST interface
import web
import json
from mimerender import mimerender
#Render statements
render_xml = lambda message: '<message>%s</message>' % message
render_json = lambda **args: json.dumps(args)
render_html = lambda message: '<html><body>%s</body></html>' % message
render_txt = lambda message: message
@pbdeuchler
pbdeuchler / gist:1049551
Created June 27, 2011 19:02
Valid JSON POST request for SkyBot
{
"name": "test",
"ip": "172.16.160.101",
"port": "8080",
"protocol": "rest",
"version": "0.2"
"rules": {
"1": [
"string",
"chatname"
@pbdeuchler
pbdeuchler / gist:1049792
Created June 27, 2011 20:44
A JSON PUT request made by SkyBot upon rule recognition
{
"number": "3",
"body": "i feel pretty",
"author": "voxeobob",
"chat": "Voxeo Engineering",
"time": "1309211072"
}
@pbdeuchler
pbdeuchler / gist:1056826
Created June 30, 2011 18:16
A sample SkyBot Module for polling support alerts
#!/usr/bin/python -tt
'''
Didn't you hear? The bird is the word!
@author: Philip Deuchler
@version: 0.2 Beta
'''
import urllib2
import json
import web
@pbdeuchler
pbdeuchler / main.py
Created February 22, 2012 19:01
CSCI 345 Project 1
import shelve
import os
import sys
#Global vars
SHELF = os.getcwd() + '/' +'store' #variable for db name
LOC = os.getcwd() + '/files/' #variable for corpus location (os.getcwd() gets the current directory)
write = sys.stdout.write #For progress bar
db = {}
@pbdeuchler
pbdeuchler / gist:4066816
Created November 13, 2012 16:35
Read Only Site Flag for Django (Request Processor)
def read_only_processor(request):
if read_only_flag():
if request.method in ['GET', 'HEAD']:
return {
'read_only': True,
'error_message': 'Sorry, this site is read only right now!"
}
else:
#return a "Read only" response
else:
@pbdeuchler
pbdeuchler / map.go
Created July 16, 2013 02:24
Proof of concept Go map function
package main
import (
"fmt"
"strconv"
"reflect"
)
type mapFunc func(interface{}) (interface{})
@pbdeuchler
pbdeuchler / adaptive_resize.py
Created May 22, 2014 19:13
Adaptive resize for avatars using Python and PIL/Pillow, square crop from center plus an optional resizing
def adaptive_resize(img, size=None): #img is a PIL/Pillow Image object, size is a tuple of desired dimensions
if size is not None: # resize image
if size[0] != size[1]:
raise Exception("Provided target dimensions must be equal")
smallest_side = min(img.size[0], img.size[1])
if size[0] < smallest_side: # make sure we actually need to resize
resize_ratio = float(size[0]) / float(smallest_side)
resize_x = int(math.floor(img.size[0] * resize_ratio))
resize_y = int(math.floor(img.size[1] * resize_ratio))
img = img.resize((resize_x, resize_y))