Skip to content

Instantly share code, notes, and snippets.

View demux's full-sized avatar

Arnar Yngvason demux

  • Reykjavík, Iceland
View GitHub Profile
@demux
demux / NsCache.php
Last active June 8, 2017 15:35
I drafted this proposal to solve the problem of deleting all keys with a certain prefix from `memcached`, but we decided to go with `redis` instead, as I'd previously suggested. This is an incomplete Kohana Controller.
<?php
// If we hadn't gone with `redis` I would have implemented something like this
// for abstracing away the "complicated" stuff...
class NsCache() {
// ...
/**
* Get cache from namespace
from schematics.models import Model
from schematics.types import BaseType, StringType, BooleanType, IntType
from schematics.types.compound import ListType, ModelType as _ModelType, DictType
from schematics.transforms import blacklist, export_loop
import ruamel.yaml as yaml
from ruamel.yaml.comments import CommentedMap
from ruamel.yaml.scalarstring import PreservedScalarString
from ruamel.yaml.compat import string_types, ordereddict
@demux
demux / common.js
Created January 28, 2016 22:49
Sort an expression like "abc(d)(ef)(ghi)"
import {isArray, cloneDeep} from 'lodash'
export class ExtendableError extends Error {
constructor(message) {
super(message)
this.name = this.constructor.name
this.message = message
Error.captureStackTrace(this, this.constructor.name)
}
class SimpleEventEmitter:
_events: []
_onceWrapper: (type) ->
wrapper = ~>
@off(type, wrapper)
fn.apply(this, arguments)
on: (type, fn, once=false) !->
@_events[type] = @_events[type]? or []
$(function(){
var url = $("script[src$='findfilehosttest.js']").attr('src')
var matches = url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i)
var domain = matches && matches[1]
alert(domain)
})
function R($param, $default=null, $type='string') {
if(!isset($_REQUEST[$param]))
return $default;
$r = $_REQUEST[$param];
if($type === 'bool' || $type === 'boolean') {
if($r === 'false' || $r === 'no')
return false;
return !empty($r);
}
settype($r, $type);
function nearestInList(number, list, returnDistance) {
var distance = Infinity;
var n = null;
for(var i in list) {
var _distance = Math.abs(list[i] - number);
if(_distance < distance) {
distance = _distance;
n = list[i];
}
@demux
demux / 0_reuse_code.js
Created October 22, 2013 14:19
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@demux
demux / exchange-rates.php
Created June 24, 2013 14:12
Convert XML exchange rates from arionbanki.is to JSON or JSONP for money.js (http://josscrowcroft.github.io/money.js/)
<?php
$xml_url = 'http://www.arionbanki.is/markadir/gjaldmidlar/gengi/xml-export';
$data = simplexml_load_file($xml_url);
$out = array(
'timestamp' => time(),
'base' => 'ISK',
'rates' => array()
);
@demux
demux / lp_upload_unittest.py
Created June 15, 2012 16:18
Unit test for liveproject upload api
# -*- coding: utf-8 -*-
from django.utils import unittest
from django.test.client import Client
import simplejson as json
import hashlib
from settings import PROJECT_PATH
from main.models import Event