Skip to content

Instantly share code, notes, and snippets.

View xsleonard's full-sized avatar

Steve Leonard xsleonard

View GitHub Profile
def secure_url_for(endpoint, *args, **kwargs):
# Force generate https url. Necessarily is _external
kwargs['_external'] = True
url = url_for(endpoint, *args, **kwargs)
parsed = urlparse(url)
parsed._replace(scheme='https')
return urlunparse(parsed)
@xsleonard
xsleonard / gist:5043853
Created February 27, 2013 00:48
fix missing subreddit submissions
from r2.models import Subreddit
from r2.lib.db.queries import get_links
def fix_sr(name):
sr = Subreddit._by_name(name)
for sort in ('hot', 'new', 'controversial', 'top'):
get_links(sr, sort, 'all').update()
@xsleonard
xsleonard / gist:5508581
Created May 3, 2013 11:24
voluptuous validation failure
from voluptuous import Schema
data = {
'InactiveMails': 26,
'Bounces': [
{
'Name': 'All',
'Count': 30
},
{
import re
import copy
import BeautifulSoup
class HTMLSanitizer(object):
# replace hexadecimal character reference by decimal one
_hexentity_massage = (copy.copy(BeautifulSoup.MARKUP_MASSAGE) +
[(re.compile('&#x([^;]+);'),
@xsleonard
xsleonard / mtgox exchange rate calculator
Created May 22, 2013 14:40
mtgox exchange rate calculator
#!/usr/bin/env python
'''
USAGE:
./rate.py <amount:float> <bid|ask>
'''
import sys
import requests
@xsleonard
xsleonard / curl_ssl_cookies.cpp
Created July 20, 2013 21:17
SSL and cookie handling with libcurl
#include <stdio.h>
#include <curl/curl.h>
/* Example cookie handling copied from:
* http://curl.haxx.se/libcurl/c/cookie_interface.html
* Example SSL request adapted from:
* http://curl.haxx.se/libcurl/c/https.html
*/
@xsleonard
xsleonard / curl_ssl_cookies.c
Created July 20, 2013 21:30
SSL and cookie handling with libcurl
#include <stdio.h>
#include <curl/curl.h>
/* Compiling example:
*
* gcc -o curltest curl_ssl_cookies.c -L./lib/ -I./lib/include -lcurl -Wl,-rpath ./lib
*
*/
/* Example cookie handling copied from:
@xsleonard
xsleonard / db.py
Created September 26, 2013 16:50
sqlalchemy schema create script
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from forq import db, create_app
def drop():
db.drop_all()
print 'Dropping completed successful!'
@xsleonard
xsleonard / gist:7341172
Created November 6, 2013 18:10
hex string to byte array, C
unsigned char* hexstr_to_char(const char* hexstr)
{
size_t len = strlen(hexstr);
IF_ASSERT(len % 2 != 0)
return NULL;
size_t final_len = len / 2;
unsigned char* chrs = (unsigned char*)malloc((final_len+1) * sizeof(*chrs));
for (size_t i=0, j=0; j<final_len; i+=2, j++)
chrs[j] = (hexstr[i] % 32 + 9) % 25 * 16 + (hexstr[i+1] % 32 + 9) % 25;
chrs[final_len] = '\0';
@xsleonard
xsleonard / phoneperms.py
Created January 11, 2014 19:00
Phone number -> letter permutations
from sys import argv
from string import ascii_uppercase
def map_number(number):
""" Maps a number's digits to its possible phone letters """
sets = []
for n in str(number):
assert n != '0'
pos = (int(n) - 1) * 3