Skip to content

Instantly share code, notes, and snippets.

@adoc
adoc / gist:17053accfb0e190df9c4
Last active August 29, 2015 14:08
Pyramid Socket IO - interesting way to serve with a dynamic namespace. (includes pyramid.route magics)
from pyramid.interfaces import IRoutesMapper
from pyramid.request import Request
@view_config(route_name='socket_endpoint', renderer='json')
def socket_endpoint(request):
"""Create a socket endpoint for the client.
"""
if 'ns' in request.params:
@adoc
adoc / join.js
Created October 16, 2014 09:15
join.js - Direct port of Python std posixpath.join. (Thanks Mr. Peterson!)
// src: http://stackoverflow.com/a/646643
// Add `startsWith` and `endsWith` to the String prototype.
if (typeof String.prototype.startsWith != 'function') {
String.prototype.startsWith = function (str){
return this.slice(0, str.length) == str;
};
}
if (typeof String.prototype.endsWith != 'function') {
String.prototype.endsWith = function (str){
@adoc
adoc / gist:d6cfe3178f53d9d47bcf
Created September 12, 2014 09:08
python 3d distance calc for center point of minecraft spawners.
import math
skellie = (438, 26, -410)
zombie = (443, 16, -386)
c = (440, 21, -398)
def dist(x1, y1, z1, x2, y2, z2):
return (math.sqrt((x1-x2)**2+(y1-y2)**2+(z1-z2)**2))
for yn in range(c[1], 0, -1):
cn = c[0], yn, c[2]
@adoc
adoc / hmac_validator.py
Created August 22, 2014 07:10
py_hmac_validator: Formencode Hmac Validator.
class HmacValidator(formencode.validators.FancyValidator):
"""
"""
hashalg = hashlib.sha512
messages = {
'malformed': 'Malformed challenge hash.'
}
@adoc
adoc / sendmail.py
Created August 19, 2014 04:19
sendmail.py - Simple sendmail hook including email packaging.
import logging
log = logging.getLogger(__name__)
from subprocess import Popen, PIPE
from email.mime.multipart import MIMEMultipart
from email.mime.nonmultipart import MIMENonMultipart
from email.mime.text import MIMEText
@adoc
adoc / gist:4694d8072fb73b335d92
Created July 25, 2014 22:29
formencode upload / image validators
#---
import os
import shutil
import pickle
import cgi
import tempfile
import formencode
import hashlib
@adoc
adoc / pyr_validate_view.py
Last active August 29, 2015 14:03
pyr_validate_view.py: Pyamid View Decorator for Validation
def validate_model(params=None, match=None, headers=lambda r: tuple(),
json=json,
invalid_params_exc=pyramid.httpexceptions.HTTPBadRequest,
invalid_match_exc=pyramid.httpexceptions.HTTPNotFound
):
"""Basic validation decorator for usage in `view_config`.
Takes `params` and `match` as arguments.
`params` - Schema to use to and instruct to validate requests.params
`match` - Schema to use to and isntruct to validate request.match
@adoc
adoc / py2rangereplace.py
Created June 18, 2014 20:41
Python 2.x `range` replacement
try:
range = xrange
log.warning("Replacing `range` with `xrange`.")
except NameError:
pass
@adoc
adoc / cb.py
Last active August 29, 2015 14:02
"""
Bitcoin Daemon RPC wrapper for simple calculations, top address list,
maybe block explorer verification, etc.
"""
import time
import operator
import pickle
import json
@adoc
adoc / __init__.py
Created April 27, 2014 16:20
threadutils: Threading utility functions.
import time
import threading
THROTTLE = 0.01
class KillableThread(threading.Thread):
"""Subclass of threading.Thread with kill signal functionality.
"""