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 / gotcha
Created November 2, 2013 22:33
Python gotcha.
>>> def test(a=0,b="salut",c=[],d=[]):
... a += 1
... b += "!"
... c += [2]
... d = d + [42]
... print a, b, c, d
...
>>> test()
1 salut! [2] [42]
@kjaquier
kjaquier / current_exception_details.py
Last active January 1, 2016 13:19
Informations d'exception
def current_exception_details():
"""Retourne un tuple contenant les les informations sur l'exception
en cours de traitement
Retour : (
Nom du fichier,
Ligne,
Nom de l'exception,
Type de l'exception,
Message de l'exception
@kjaquier
kjaquier / ClosureIterator.java
Last active August 29, 2015 13:57
How to create a simple iterator in Java with just one method.
public class ClosureIterator {
private static interface Iterator<T> {
T next();
}
private static class Container<T> {
private T value;
public Container(T value) {
@kjaquier
kjaquier / Async.java
Created May 21, 2014 09:13
Slave task that allows for asynchronous operations based on a FIFO queue.
import java.util.LinkedList;
/**
* Slave task that allows for asynchronous operations based on a FIFO queue.
*/
public class Async extends Thread {
private LinkedList<Runnable> tasks = new LinkedList<Runnable>();
private boolean stop = false;
@kjaquier
kjaquier / BitSet.java
Last active August 29, 2015 14:01
Enum based and memory efficient flag set.
public class BitSet<T extends Enum<?>> {
private byte[] bytes;
@SafeVarargs
public BitSet(T... elements) {
bytes = new byte[elements.length / 8 + 1];
}
private int bitMask(int ordinal) {
@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
#
@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 / 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 / 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 / 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;