Skip to content

Instantly share code, notes, and snippets.

View AlexisTM's full-sized avatar
🤠

Alexis Paques AlexisTM

🤠
View GitHub Profile
@AlexisTM
AlexisTM / debounce.py
Last active January 4, 2019 20:56 — forked from esromneb/debounce.py
This is a proper debounce function, the way a electrical engineer would think about it.
import time
""" This is a proper debounce function, the way a electrical engineer would think about it.
This wrapper never calls sleep. It has two counters: one for successful calls, and one for rejected calls.
If the wrapped function throws an exception, the counters and debounce timer are still correct """
class Debounce(object):
def __init__(self, period):
@AlexisTM
AlexisTM / redis-token-template.js
Created January 20, 2019 13:14
Redis token template
users/[username]:token-[token] = {
'location': 'Brussels',
'origin': 'weblogin',
'creation-ip': 'x.x.x.x',
'last-use-ip': 'x.x.x.x'
}
@AlexisTM
AlexisTM / token.js
Last active January 20, 2019 15:56
To create a token with expiration
// Express web app
const express = require('express');
const app = express()
const md5 = require('md5');
// Redis client
const redis = require("redis");
const client = redis.createClient({ host: '192.168.178.23' });
// Token generator
@AlexisTM
AlexisTM / token-frontend.js
Created January 20, 2019 13:36
Login on the front-end side using AJAX
// To be used to set a cookie
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 86400000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
// Login through an AJAX call
function login(username, password) {
@AlexisTM
AlexisTM / cookie.js
Created January 20, 2019 16:06
Parse a cookie
@AlexisTM
AlexisTM / minimal_gcs.py
Last active May 31, 2019 12:11
Minimal heartbeat for a Ground control station in ROS for PX4
#!/usr/bin/env python2
"""
Minimal heartbeat for a Ground control station in ROS for PX4.
It enables STATUS_TEXT streams.
Author: AlexisTM
"""
from threading import Thread
import rospy
@AlexisTM
AlexisTM / factorial.js
Created May 18, 2020 14:40
Very fast algorithm for the a first approximation of a very large factorial (10 decimals)
function factorial(number) {
let result = 0;
for(let i = number; i > 0; --i) {
result += Math.log10(i);
}
return result;
}
function scientific_factorial(number) {
let fact = factorial(number);
@AlexisTM
AlexisTM / singleton.hpp
Last active November 20, 2020 15:47
The safe singleton in C++
class Singleton {
public:
static Singleton& get() {
static Singleton instance;
return instance;
}
Singleton(Singleton&&) = delete;
Singleton& operator=(Singleton&&) = delete;
@AlexisTM
AlexisTM / merge.sh
Created November 24, 2020 13:53
Merge multiple compile_commands.json
# This is to be used with catkin_make or catkin_tools if we want to merge compile_commands.txt in a single file to allow some tools to work such as Sourcetail.
sudo apt install jq
jq -s 'map(.[])' PATH_TO_COMPILE_COMMANDS_ROOT/**/compile_commands.json > compile_commands.json
@AlexisTM
AlexisTM / magic.h
Last active June 11, 2021 06:13
C/C++ performance magic functions (prevent optimization)
/**
* Those functions are related to: https://www.youtube.com/watch?v=nXaxk27zwlk&t=2446s
*
* They are used to allow to benchmark functions with -O3 that would otherwise be removed because it is unused.
*
* escape(&object) prevents object to be optimized out
* "This ASM code has some unknowable side effects and possibly stored the pointer globally"
* clobber() tells the compiler some asm might be reading the whole memory
* "This ASM code could probably read/write to all memory" => observe all memory
*/