Skip to content

Instantly share code, notes, and snippets.

View kjaquier's full-sized avatar

Kevin Jaquier kjaquier

  • Lausanne, Switzerland
View GitHub Profile
@kjaquier
kjaquier / trace.js
Last active May 24, 2016 15:47
Prints name, arguments and result when decorated function is called.
function trace(f) {
return function() {
var args = Array.from(arguments);
var res = f.apply(null, args);
console.debug("%s(%O) = %O", f.name, args, res);
return res;
};
}
@kjaquier
kjaquier / parse_accept_language.py
Created December 9, 2015 17:46
Parses the Accept-Language HTTP header and returns the list of locales by order of preference.
def parse_http_accept_language_header(langs_str):
"""Returns the list of languages from a string formatted
according to the Accept-Language HTTP header, by order of
preference.
"""
def get_locale_q_pairs(lang_list):
for language in lang_list:
locale_q_pair = language.split(";")
if len(locale_q_pair) == 1:
@kjaquier
kjaquier / timezones.js
Created February 20, 2015 16:46
List of common timezones put in an Angular service. Generated with http://pythonhosted.org//pytz/#helpers (common_timezones).
"use strict";
angular.module('timezones', [])
.value('CommonTimezones', [
"Africa/Abidjan",
"Africa/Accra",
"Africa/Addis_Ababa",
"Africa/Algiers",
"Africa/Asmara",
@kjaquier
kjaquier / git-mode-emploi.md
Last active September 2, 2015 16:53
Mode d'emploi des fonctionnalités de bases de git, réalisé lorsque j'apprenais à utiliser git.

Git : aide-mémoire

Notation EBNF

Partie optionnelle : [...]

Partie répétable 0 à N fois : {...}

@kjaquier
kjaquier / ng-fixed-http.js
Created February 10, 2015 14:39
Fix for Angular's $http to use form encoding (instead of json) when sending POST/PUT requests. See http://stackoverflow.com/questions/19254029/angularjs-http-post-does-not-send-data
angular.module('ngFixedHttp', [])
.factory('FixedHttp', ['$http', function($http) {
// Can't use $http.post() because JSON format is not
// recognized by the web server
// See : http://stackoverflow.com/questions/19254029/angularjs-http-post-does-not-send-data
// This service is supposed to be used as a replacement for $http
// /!\ Not tested
// /!\ /!\ Doesn't work with nested JSON objects (must write custom compile function for that) /!\ /!\
@kjaquier
kjaquier / longOperation.js
Last active August 29, 2015 14:10
Perform a long computation with pauses between steps, for the UI to be able to render and not freeze.
// Inspired from : http://www.sitepoint.com/multi-threading-javascript/
longOperation: function (fn, initData, interval) {
var data = initData, busy = false;
var result = $q.defer();
var taskId;
var task = function() {
if(!busy) {
busy = true;
var res = fn(data);
data = res.data;
@kjaquier
kjaquier / json_template.js
Last active August 29, 2015 14:08
JSON Templates
//
// Simple template system with plain JS objects, using CSS selectors
// Two versions :
// * JSON
// - based on JSON objects, each element is a property
// * JSON-ML :
// - inspired from the JSON-ML markup language (http://www.jsonml.org/)
// - each element is a list
// - element type can contains id and classes like the first version (so, not standard json-ml)
//
@kjaquier
kjaquier / dict_to_xml.py
Last active September 17, 2019 16:03
Serialize a Python dictionary to XML. Useful when working with both JSON and XML files. Doesn't work with object, lists, tuples or generators.
# -*- coding: Utf-8 -*-
import xml.etree.ElementTree as ET
def create_xml_tree(root, dict_tree):
# Node : recursively create tree nodes
if type(dict_tree) == dict:
for k, v in dict_tree.iteritems():
create_xml_tree(ET.SubElement(root, k), v)
return root
@kjaquier
kjaquier / lazy.py
Created July 16, 2014 13:54
Function decorator for lazy evaluation. Only for argument-less functions. Shouldn't be used for functions with side-effects.
def lazy(fun):
class Memoizer(object):
def __init__(self, fun):
self.fun = fun
self.set = False
self.res = None
def __call__(self):
if not self.set:
self.set = True
@kjaquier
kjaquier / similar_words.py
Last active August 29, 2015 14:03
Generate words with similar spelling and/or sounding from a list of words given in arguments.
import sys
#
# Generation config
#
PRINT_COLS = 8
LETTERS_CHANGED = 2
#