Skip to content

Instantly share code, notes, and snippets.

View emilisto's full-sized avatar

Emil Stenqvist emilisto

  • Stockholm
View GitHub Profile
/*
* @getPersistentVisitorId: Generates a unique visitor ID that is persisted between visits.
*
* We assume we're in an iframe, so for Safari users we use localStorage,
* and for everyone we use local domain cookies.
*/
var getPersistentVisitorId = (function() {
var key = 'silp_visitorid';
var method = allowsThirdPartyCookies() ? 'cookie' : 'localStorage';
var persistor = {
// We assume that this is running in an iframe which loads a page from a domain
// that is specific to where your widget is served from. This means that for
// browsers that support 3rd party cookies in iframes, the cookie will be set
// on this domain, and for those who don't, localStorage will be used instead.
ga('create', 'UA-XXXX-Y', {
'storage': 'none',
'clientId', getPersistentVisitorId()
});
{
"variables": {
"salt_role": "common"
},
"provisioners": [
{
"type": "shell",
"scripts": [
"scripts/ec2-ephemeral-tmp.sh"
],
@emilisto
emilisto / interact.py
Created August 16, 2013 11:10
A little tool I came up with to be able to quickly work with API's I'm unfamiliar with. Just call `inspect()` anywhere in the code and you'll get an interactive prompt where you can interact with all variables, see their attributes, methods etc.
import bpython
import inspect
def interact():
"""
Just run interace() anywhere in your code, and you'll get a sweet
interactive bpython interpreter with access to the scope of where you
called it. Great for investigating new API's.
"""
try:
@emilisto
emilisto / js-prototype-play.js
Created August 27, 2013 13:38
Some code I wrote to understand the pattern @jashkenas uses to setup the prototype chain in Backbone and Underscore.
var _ = require('underscore');
function printPrototypeChain(instance, asArray) {
var chain = []
while (instance = Object.getPrototypeOf(instance)) {
var name = instance.constructor.toString().match(/f.+n (.+)\(/);
chain.push(name && name[1]? name[1] : "(anonymous function expression)")
}
return asArray? chain : chain.join(" -> ")
}
@emilisto
emilisto / generate-certs.sh
Created August 29, 2013 14:17
Script I used to generate an SSL key and sign a certificate for using with a grunt development server.
#!/bin/bash
root_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ssl_dir=$root_dir/ssl
mkdir -p $root_dir/ssl
echo ">> Generating SSL key and certificate..."
rm -rf $ssl_dir/*
/*
*
* Simple benchmark of `pipe` (https://github.com/wowus/pipe) library
*
* $ gcc -O3 -pthread -o test pipe.c test.c
* $ ./test
* Consuming 2000000 items from the queue...
* Received 2000000 items in 0.52 seconds, average throughput of 3878081 items/s
*
*/
@emilisto
emilisto / segfault.c
Created March 18, 2014 13:34
C99 problem with strdup
/*
* $ gcc -std=c99 -o segfault segfault.c -lpthread
* segfault.c: In function ‘thread_fn’:
* segfault.c:7:5: warning: implicit declaration of function ‘strdup’ [-Wimplicit-function-declaration]
* segfault.c:7:17: warning: initialization makes pointer from integer without a cast [enabled by default]
* $ ./segfault
* Segmentation fault
*/
#include <stdio.h>
[user]
name = Emil Stenqvist
email = [email protected]
[branch]
autosetuprebase = always
[color]
diff = auto
status = auto
@emilisto
emilisto / timeit_and_print.py
Created December 27, 2014 18:11
Time it and print friendly output
from timeit import timeit
def timeit_and_print(stmt, setup='pass', number=100000):
t = timeit(stmt, setup=setup)
print 'Executed "%s" %d times in %.4f seconds, %.4f ms per call (%d calls per second)' % (
stmt, number, t, 1000 * t / number, int(number / t))