Skip to content

Instantly share code, notes, and snippets.

View eskil's full-sized avatar

Eskil Heyn Olsen eskil

View GitHub Profile
@eskil
eskil / gdivelog new dive trigger
Created March 18, 2012 20:32
The ORM is inside me.
DROP TRIGGER Dive_Insert;
/*
if no dives predate this then
if lowest number is > 1:
lowest number - 1
else
1
else
pick most recent +1
"CREATE TRIGGER Dive_Update AFTER "
"UPDATE OF dive_datetime ON Dive BEGIN "
"UPDATE Dive "
"SET dive_number= "
" (SELECT CASE "
" WHEN dive_id = NEW.dive_id THEN "
" /* The updated dive, set the number to the number of older dives */ "
" 1 + (SELECT COUNT(*) FROM Dive WHERE dive_datetime < NEW.dive_datetime) "
" WHEN NEW.dive_datetime > OLD.dive_datetime "
" /* All other dive, move by +/- 1 depending on whether we've moved the datetime "
@eskil
eskil / pycurl_curlmulti_ssl.py
Created April 4, 2012 00:48
Example code doing SSL using pycurl and CurlMulti
# Example code doing SSL using pycurl and CurlMulti
import pycurl
import cStringIO
response_buffer = cStringIO.StringIO()
multi = pycurl.CurlMulti()
curl = pycurl.Curl()
curl.setopt(pycurl.URL, 'https://www.yammer.com')
curl.setopt(pycurl.SSL_VERIFYPEER, 1)
@eskil
eskil / m2crypto-certificates.py
Created April 8, 2012 17:07
Example of generating CA certs and CA signed certs using python m2crypto.
"""
Tools for creating a CA cert and signed server certs.
Divined from http://svn.osafoundation.org/m2crypto/trunk/tests/test_x509.py
The mk_temporary_xxx calls return a NamedTemporaryFile with certs.
Usage ;
# Create a temporary CA cert and it's private key
cacert, cakey = mk_temporary_cacert()
@eskil
eskil / prettyretarded
Created May 18, 2012 20:07
Emacs config to use the right tabs for yelp codes...
(defun my-yelp-python-mode-hook ()
(setq tab-width 4)
(setq py-indent-offset 4)
(setq indent-tabs-mode (or (string-match "/pg/yelp-main/" (buffer-file-name))
(string-match "/pg/yelp_conn/" (buffer-file-name))
(string-match "/pg/yelp_lib/" (buffer-file-name))
(and (string-match "/pg/services/" (buffer-file-name))
(not (or (string-match "/zygote/" (buffer-file-name))
(string-match "/google/" (buffer-file-name))
)
@eskil
eskil / ctassert.h
Created July 10, 2012 01:18
Example of how to implement static assert using template specialisation.
template<bool> struct _compile_time_assert;
template<> struct _compile_time_assert<true> {};
#define ctassert(expr) if (0) { int flaming_goat_sausage = sizeof(_compile_time_assert<(bool)(expr)>); flaming_goat_sausage++; }
int main (int argc, char *argv[]) {
ctassert (sizeof (int) == 4);
ctassert ((sizeof (int) == 5) == false);
//BLAM
ctassert (sizeof (int) == 5);
@eskil
eskil / chunkserv.cc
Created July 10, 2012 20:27
C++ version of chunkservs TimeWindowedStats, using boost.python.
/*
C++11 version of chunkserv.util.TimeWindowedStats
A dumb test that feeds a ton of entries into both the python and C++ version shows that;
Python
- insert_events time 0.237569570541
- compute_stats time 19.1441180706
C++
- insert_events time 0.234213590622
@eskil
eskil / gist:3628248
Created September 4, 2012 23:57
Example of templates computing object sizes for slab allocators
/*
Test that implements some template meta programming.
- two_to_the_power_of<Power> calculates 2^Power into it's ::value.
- rightshits<V> calculates how many times V can be rightshifted
until it's 1 or 0, result is in ::value.
- if_t<bool,A,B> places A or B in ::value, depending on the bool
@eskil
eskil / flask-security unit testing.
Created January 4, 2013 07:24
Steps to run flask-security unit-tests on a mac.
To run flask_security unit-tests:
virtualenv --no-site-packages env/
. env/bin/activate
pip install nose simplejson Flask-SQLAlchemy Flask-MongoEngine Flask-Mail py-bcrypt Flask Flask-Login Flask-Principal itsdangerous passlib
Apply this diff to disable mongo tests and use sqlite instead of postgres:
diff --git a/tests/functional_tests.py b/tests/functional_tests.py
@eskil
eskil / fixbase
Last active December 16, 2015 13:59
Script to rebase -i origin/master and auto mark fixes as 'f'
#!/bin/bash
# Put in ~/bin/fixbaseedit.sh
# alias fixbase="EDITOR=~/bin/fixbaseedit.sh git rebase -i origin/master"
# You can also just make a script that does "git commit -a -m fix" and
# the rebase for you.
sed -i.old \
-e '2,$s/pick \(.*\) fixup/f \1 .../g' \