Skip to content

Instantly share code, notes, and snippets.

View gpiffault's full-sized avatar

Grégoire Piffault gpiffault

View GitHub Profile
@gpiffault
gpiffault / polyline-decode.js
Created August 2, 2019 08:55
Decode google encoded polyline
// https://developers.google.com/maps/documentation/utilities/polylinealgorithm
function polylineDecode (str, precision) {
// Decode str to int array
let strIndex = 0
let values = []
while (strIndex < str.length) {
let byte
let result = 0
let shift = 0
do {
import math
class Bitfield:
def __init__(self, size):
self.size = size
self.bytes = bytearray(math.ceil(size / 8))
def __len__(self):
return self.size
@gpiffault
gpiffault / websocket.py
Last active January 1, 2021 14:31
Simple websocket implementation with ThreadingHTTPServer example
#!/usr/bin/env python3
import base64
import hashlib
import http.server
def ws_accept(key):
if isinstance(key, str):
key = key.encode('ascii')
return base64.standard_b64encode(hashlib.sha1(
@gpiffault
gpiffault / chronoprint.py
Last active November 18, 2021 10:47
Handy print with timing
import time
class ChronoPrint:
"""Usage:
cp = ChronoPrint()
foo()
cp("foo") # prints: 1.21 foo
bar()
cp("bar") # prints: 0.72 bar
"""
function wrap(min, max, value) {
while (value < min) value += max - min
while (value > max) value -= max - min
return value
}
function clamp(min, max, value) {
return Math.min(Math.max(min, value), max)
}
@gpiffault
gpiffault / tardiff.py
Created January 17, 2022 09:18
Compare two tar archives, compare file contents with md5
#!/usr/bin/env python3
import hashlib
import tarfile
def hash_dict(tar_path):
tar = tarfile.open(tar_path)
result = {}
for member in tar:
if member.name.endswith(".pyc"):