Skip to content

Instantly share code, notes, and snippets.

View ntulip's full-sized avatar

Nick Tulip ntulip

View GitHub Profile
@ntulip
ntulip / redis_sets.sh
Created April 25, 2012 02:04
redis_sets.sh
# How beautiful and simple it is to store relationships in redis
# Think - I have 4 friends, you have 4 friends, but how many friends
# do we have in common? And did I tell you it handles sorted sets?
# Sorted sets are like sets but with a score. The score provides sorting
# and ranking capabilities
redis 127.0.0.1:6379> sadd friends:leto ghanima paul chani jessica
(integer) 4
redis 127.0.0.1:6379> sadd friends:duncan paul jessica alia
(integer) 3
def resize(img, box, fit, out):
'''Downsample the image.
@param img: Image - an Image-object
@param box: tuple(x, y) - the bounding box of the result image
@param fix: boolean - crop the image to fill the box
@param out: file-like-object - save the image into the output stream
'''
#preresize image with factor 2, 4, 8 and fast algorithm
factor = 1
while img.size[0]/factor > 2*box[0] and img.size[1]*2/factor > 2*box[1]:
@ntulip
ntulip / slug.php
Created July 6, 2011 18:11
slug.php
/*
* http://necenzurat.net/2010/05/26/clean-up-and-url-like-wordpress-does.dev
* Create a slug for friendly URL's a-la wordpress
*/
function clean_url($str) {
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $str);
$clean = strtolower(trim($clean, '-'));
$clean = preg_replace("/[\/_|+ -]+/", '-', $clean);
return $clean;
@ntulip
ntulip / EncodeLatLng.js
Created June 20, 2011 13:06
10:10 code
// http://blog.jgc.org/2010/06/1010-code.html
var alphabet = 'ABCDEFGHJKMNPQRVWXY0123456789';
var base = alphabet.length;
function calculate_tt(lat, lon) {
lat += 90.0;
lon += 180.0;
lat *= 10000.0;
lon *= 10000.0;
www:
requirements:
- faye
- jade
@ntulip
ntulip / confidence.py
Created February 21, 2011 15:08
Reddit's comment ranking
# http://amix.dk/blog/post/19588?source=google
from math import sqrt
def _confidence(ups, downs):
n = ups + downs
if n == 0:
return 0
z = 1.0 #1.0 = 85%, 1.6 = 95%
@ntulip
ntulip / HtmlSanitizer.cs
Created February 7, 2011 14:22
HTML Sanitizer for C#
/**
Copyright (c) 2009 Open Lab, http://www.open-lab.com/
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
@ntulip
ntulip / webapp.rb
Created November 16, 2010 16:42 — forked from igrigorik/webapp.rb
require 'rubygems'
require 'rack'
class Object
def webapp
class << self
define_method :call do |env|
func, *attrs = env['PATH_INFO'].split('/').reject(&:empty?)
[200, {}, send(func, *attrs)]
end
require 'fileutils'
start_time = Time.now
SOURCE_DB = {
:name => 'db_name',
:user => 'db_user',
:password => 'db_pass',
:host => 'localhost'
@ntulip
ntulip / test.cs
Created October 22, 2010 16:32 — forked from gorsuch/test.cs
using System;
using System.IO;
using System.Text;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
namespace AWSUploadTest
{