Skip to content

Instantly share code, notes, and snippets.

View BibMartin's full-sized avatar

Martin Journois BibMartin

View GitHub Profile
@BibMartin
BibMartin / b32str.py
Last active May 23, 2022 09:22
Encode your ints into a short base32 string (case-unsensitive letters and digits)
import math
import base64
def int_to_b32str(x):
base = int(math.log2(1 + x) / 8) + 1 # How many bytes ?
bytestring = x.to_bytes(base, 'big')
b32str = base64.b32encode(bytestring).decode()
return b32str.rstrip('=') # remove trailing "="
def b32str_to_int(b):
@BibMartin
BibMartin / image_clipboard.py
Created May 18, 2022 08:25
Wanna copy images and paste it in a notebook
# !pip install clipboard
# !pip install Pillow
import clipboard
from PIL import ImageGrab, Image
import base64
def clipboard_to_image():
"""Reads image in the clipboard and outputs a PIL.Image"""
image = ImageGrab.grabclipboard()
@BibMartin
BibMartin / logging_tips.py
Created May 6, 2022 10:53
Python logging tips
import sys
import logging
# 1 - Choose a logger (where logs should come from)
# logger = logging.getLogger() # Root logger ; if you're in a notebook of end-user script
logger = logging.getLogger(__name__) # Lib-named logger ; if you're in a module
# logger = logging.getLogger(None if __name__=='__main__' else __name__)
# 2 - Choose the (minimal) log level
logger.setLevel(logging.DEBUG)
@BibMartin
BibMartin / folium GrayScaleTiles.ipynb
Created September 1, 2021 09:41
A folium element transform the tiles into grayscale (based on https://github.com/Zverik/leaflet-grayscale/)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@BibMartin
BibMartin / tiles demo.ipynb
Created May 15, 2020 17:56
Three tiles systems : Squares, Triangles, Hexagons ; with a hash system to encode them
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@BibMartin
BibMartin / test-ioloop.py
Created November 29, 2017 06:00
Using multiprocessing to run a blocking function with a timeout.
from tornado import ioloop, gen, concurrent
import pandas as pd
import time
import asyncio
from multiprocessing import TimeoutError, Process
from multiprocessing.dummy import Pool as ThreadPool
class F(object):
def __init__(self):
@BibMartin
BibMartin / Darw graphs with branca.ipynb
Last active October 13, 2017 14:37
Darw graphs with branca
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@BibMartin
BibMartin / index.html
Last active September 15, 2017 20:06
The PPDM road
<!DOCTYPE html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/leaflet.markercluster-src.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/leaflet.markercluster.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.css" />
@BibMartin
BibMartin / gist:42f8c429169620f24376f8ab8f30a5b3
Last active March 18, 2022 10:11
Kill a process (and all it's children) based on a `ps -fA | grep` output
## Brutal shortcuts:
# Kill everything launched with "python registry.py"
kill -SIGTERM -$(ps aux | grep 'python registry.py' | awk '{print $2}')
# Same in a function
grepkill() { kill -SIGTERM -$(ps aux | grep $1 | awk '{print $2}'); }
# Restart a uvicorn server
git reset --hard ; kill -SIGTERM $(ps aux | grep 'uvicorn server:app' | grep -v "grep" | awk '{print $2}'); nohup uvicorn server:app --host 127.0.0.1 --port 8000 > server.log &
@BibMartin
BibMartin / gist:b0219727266515fa2af059df7f75b967
Last active May 9, 2017 12:21
Hack pandas.DataFrame to have unstacked JSON structure
from pandas import DataFrame, MultiIndex
def __getattribute__(self, x):
try:
return _parent__getattribute__(self, x)
except AttributeError:
columns = _parent__getattribute__(self, 'columns')
cols = list(set([x.split('.')[0] for x in columns]))
if x in cols:
_prefix = x + '.'