Skip to content

Instantly share code, notes, and snippets.

@benob
benob / bm25.js
Created March 25, 2021 19:55
simple in-memory search engine with bm25 a.k.a. okapi ranking function
"use strict"
var fs = require('fs');
var stopwords = fs.readFileSync('stopwords.txt').toString().split('\n').reduce((set, line) => {set.add(line.trim()); return set}, new Set());
var index = {
termFrequency: new Map(),
documentLength: [],
averageDocumentLength: 0,
k1: 1.2,
@benob
benob / ssl-client.nim
Created February 9, 2021 21:43
self-signed certificate verifying client in nim
import openssl
import ssl_config
from ssl_certs import scanSSLCertificates
import nativesockets
import uri
import os
import strutils
type Socket = ref object
fd: SocketHandle
@benob
benob / server.nim
Created February 7, 2021 17:54
Nim server that checks client identity
import net
var
sslContext = newContext(certFile = "fullchain.pem", keyFile = "privkey.pem", verifyMode = CVerifyPeer)
socket = newSocket(domain = AF_INET6)
socket.setSockOpt(OptReuseAddr, true)
socket.setSockOpt(OptReusePort, false)
socket.bindAddr(Port(1965), "::")
socket.listen()
sslContext.wrapSocket(socket)
@benob
benob / docker_extract.py
Created September 14, 2020 19:44
Extract a docker image retrieved with docker_pull.py (https://github.com/NotGlop/docker-drag)
# Extract a docker image retrieved with docker_pull.py (https://github.com/NotGlop/docker-drag)
import tarfile
import sys, json
# The tar contains a manifest.json which lists layers; each layer is a tar archive
with tarfile.TarFile(sys.argv[1]) as tf:
with tf.extractfile("manifest.json") as manifest_fp:
manifest = json.loads(manifest_fp.read())
for layername in manifest[0]['Layers']:
with tf.extractfile(layername) as layer_fp:
@benob
benob / brogue-ce.nix
Created May 24, 2020 19:33
nix package for brogue community edition
#{ stdenv, fetchurl, SDL2, SDL2_image makeDesktopItem }:
with import <nixpkgs> {};
stdenv.mkDerivation rec {
pname = "brogue-ce";
version = "1.8.3";
src = fetchFromGitHub {
owner = "tmewett";
repo = "BrogueCE";
@benob
benob / exif_rotate.py
Created May 27, 2019 07:32
Rotate pictures according to EXIF orientation tag, in python using PIL. Supports all 8 orientations with a simple transposition operation.
# Proper exif rotation with PIL. Handles all the cases in https://github.com/recurser/exif-orientation-examples
from PIL import Image
image = Image.open(filename)
exif = image._getexif()
ORIENTATION = 274
if exif is not None and ORIENTATION in exif:
orientation = exif[ORIENTATION]
method = {2: Image.FLIP_LEFT_RIGHT, 4: Image.FLIP_TOP_BOTTOM, 8: Image.ROTATE_90, 3: Image.ROTATE_180, 6: Image.ROTATE_270, 5: Image.TRANSPOSE, 7: Image.TRANSVERSE}
if orientation in method:
@benob
benob / exif_rotate.py
Created May 27, 2019 07:32
Rotate pictures according to EXIF orientation tag, in python using PIL. Supports all 8 orientations with a simple transposition operation.
# Proper exif rotation with PIL. Handles all the cases in https://github.com/recurser/exif-orientation-examples
from PIL import Image
image = Image.open(filename)
exif = image._getexif()
ORIENTATION = 274
if exif is not None and ORIENTATION in exif:
orientation = exif[ORIENTATION]
method = {2: Image.FLIP_LEFT_RIGHT, 4: Image.FLIP_TOP_BOTTOM, 8: Image.ROTATE_90, 3: Image.ROTATE_180, 6: Image.ROTATE_270, 5: Image.TRANSPOSE, 7: Image.TRANSVERSE}
if orientation in method:
@benob
benob / exif_rotate.py
Created May 27, 2019 07:32
Rotate pictures according to EXIF orientation tag, in python using PIL. Supports all 8 orientations with a simple transposition operation.
# Proper exif rotation with PIL. Handles all the cases in https://github.com/recurser/exif-orientation-examples
from PIL import Image
image = Image.open(filename)
exif = image._getexif()
ORIENTATION = 274
if exif is not None and ORIENTATION in exif:
orientation = exif[ORIENTATION]
method = {2: Image.FLIP_LEFT_RIGHT, 4: Image.FLIP_TOP_BOTTOM, 8: Image.ROTATE_90, 3: Image.ROTATE_180, 6: Image.ROTATE_270, 5: Image.TRANSPOSE, 7: Image.TRANSVERSE}
if orientation in method:
@benob
benob / exif_rotate.py
Created May 27, 2019 07:32
Rotate pictures according to EXIF orientation tag, in python using PIL. Supports all 8 orientations with a simple transposition operation.
# Proper exif rotation with PIL. Handles all the cases in https://github.com/recurser/exif-orientation-examples
from PIL import Image
image = Image.open(filename)
exif = image._getexif()
ORIENTATION = 274
if exif is not None and ORIENTATION in exif:
orientation = exif[ORIENTATION]
method = {2: Image.FLIP_LEFT_RIGHT, 4: Image.FLIP_TOP_BOTTOM, 8: Image.ROTATE_90, 3: Image.ROTATE_180, 6: Image.ROTATE_270, 5: Image.TRANSPOSE, 7: Image.TRANSVERSE}
if orientation in method:
@benob
benob / stbttf.h
Last active June 15, 2023 12:14
Example for using stb_truetype with SDL2 renderer. Only supports ASCII characters.
#ifndef __STBTTF_H__
#define __STBTTF_H__
#include <SDL2/SDL.h>
#include "stb_rect_pack.h"
#include "stb_truetype.h"
/* STBTTF: A quick and dirty SDL2 text renderer based on stb_truetype and stdb_rect_pack.
* Benoit Favre 2019