Skip to content

Instantly share code, notes, and snippets.

View jdelafon's full-sized avatar

Julien Delafontaine jdelafon

  • Lausanne, Switzerland
View GitHub Profile
@jdelafon
jdelafon / mandlebrot.js
Last active January 19, 2018 15:55
Mandelbrot fractal in a canvas
// Ref: http://progur.com/2017/02/create-mandelbrot-fractal-javascript.html
(function() {
var myCanvas = document.createElement("canvas");
myCanvas.width = 600;
myCanvas.height = 600;
document.body.appendChild(myCanvas);
var ctx = myCanvas.getContext("2d");
@jdelafon
jdelafon / simulateClick.js
Created December 11, 2017 13:45
Simulate a click on a DOM element in Javascript
function fireEventClick(elem){
if(document.createEvent){
var e = document.createEvent('MouseEvents');
e.initMouseEvent('click', /* Event type */
true, /* Can bubble */
true, /* Cancelable */
document.defaultView, /* View */
1, /* Mouse clicks */
0, /* Screen x */
0, /* Screen y */
@jdelafon
jdelafon / timer.py
Created July 21, 2017 13:52
Python timers
def timer(function):
""" A decorator that makes the decorated *function* return its execution time."""
@functools.wraps(function)
def wrapper(*args, **kwargs):
t1 = time.time()
result = function(*args, **kwargs)
t2 = time.time()
print(" @time '{}': {:.4f} s.".format(str(function), t2-t1))
return result
@jdelafon
jdelafon / currentTimestamp.js
Created March 13, 2017 10:16
JS current timestamp
// Compatible with java.sql.Timestamp/MySQL
(new Date (
(new Date((new Date(new Date())).toISOString())).getTime()
- ((new Date()).getTimezoneOffset()*60000)
)).toISOString().slice(0, 19).replace('T', ' ');
// "2017-03-13 11:08:56"
@jdelafon
jdelafon / github.jsx
Created July 26, 2016 14:11
GitHub svg icon
<svg width="36" height="36" viewBox="0 0 24 24">
<path
fill="#fff" d="M12,2A10,10 0 0,0 2,12C2,16.42 4.87,20.17 8.84,21.5C9.34,21.58
9.5,21.27 9.5,21C9.5,20.77 9.5,20.14 9.5,19.31C6.73,19.91 6.14,17.97 6.14,
17.97C5.68,16.81 5.03,16.5 5.03,16.5C4.12,15.88 5.1,15.9 5.1,15.9C6.1,15.97 6.63,
16.93 6.63,16.93C7.5,18.45 8.97,18 9.54,17.76C9.63,17.11 9.89,16.67 10.17,
16.42C7.95,16.17 5.62,15.31 5.62,11.5C5.62,10.39 6,9.5 6.65,8.79C6.55,8.54 6.2,
7.5 6.75,6.15C6.75,6.15 7.59,5.88 9.5,7.17C10.29,6.95 11.15,6.84 12,6.84C12.85,
6.84 13.71,6.95 14.5,7.17C16.41,5.88 17.25,6.15 17.25,6.15C17.8,7.5 17.45,8.54
17.35,8.79C18,9.5 18.38,10.39 18.38,11.5C18.38,15.32 16.04,16.16 13.81,
@jdelafon
jdelafon / ndim_array.js
Last active July 14, 2016 08:55
Initialize a n-dimensional array in Javascript
/*
* Recursive function to initialize a n-dimensional array.
* @param *dims:
*/
function emptyArray(dims) {
var arr = new Array(dims || 0);
var i = dims;
if (arguments.length > 1) {
var args = Array.prototype.slice.call(arguments, 1);
@jdelafon
jdelafon / selenium_js_click.py
Created July 6, 2016 15:09
When Selenium's click() method fails, use JS instead.
# elt: WebElement
def robust_click(self, elt):
"""Click on the element, for the click() method is bugged half of the time"""
self.driver.execute_script("arguments[0].scrollIntoView(true);", elt)
action = ActionChains(self.driver)
action.move_to_element(elt).click().perform()
def js_click(self, elt):
@jdelafon
jdelafon / streaming.scala
Created July 5, 2016 08:31
Streaming a binary file in Scala Play 2.4.5
import java.io.FileInputStream
import java.nio.file.Paths
import play.api.http.HttpEntity
import akka.stream.scaladsl.StreamConverters
import play.api.mvc._
import play.api.http.MimeTypes._
import play.api.http.HeaderNames._
import play.api.http.Status._
@jdelafon
jdelafon / receipes.scala
Last active July 3, 2017 12:44
Scala receipes
/**
* Return a Map[String,Any] of the field -> value pairs of the attributes of a class `c`.
*/
def classToMap(c: AnyRef): Map[String, Any] =
// 'fold' syntax, starting with an empty map
(Map[String, Any]() /: c.getClass.getDeclaredFields) {(a, f) =>
f.setAccessible(true)
a + (f.getName -> f.get(c))
}
@jdelafon
jdelafon / gaussian_mixtures.r
Created June 29, 2016 11:50
Demonstration of Gaussian mixture models for bioinfo-fr
set.seed(100)
data = c(rnorm(1000,mean=2,sd=1),rnorm(400,mean=6,sd=1.5))
# linear
plot(sample(data), rep(0,length(data)), pch=4)
# with hist
hist(data, freq=FALSE, breaks=100)
points(sample(data), rep(0,length(data)), pch=4)