Skip to content

Instantly share code, notes, and snippets.

Avatar

Stefan van der Walt stefanv

View GitHub Profile
View typed-libraries.rst

Typing Guidance for Python Libraries

Much of Python’s popularity can be attributed to the rich collection of Python libraries available to developers. Authors of these libraries play an important role in improving the experience for Python developers. This document provides some recommendations and guidance for Python library authors.

These recommendations are intended to provide the following benefits:

@stefanv
stefanv / http_server_python
Last active Sep 23, 2021
Multi-threaded HTTP server in Python
View http_server_python
#!/usr/bin/env python3
# See https://github.com/Nakiami/MultithreadedSimpleHTTPServer/blob/master/MultithreadedSimpleHTTPServer.py
# Python 3.x
from socketserver import ThreadingMixIn
from http.server import SimpleHTTPRequestHandler, HTTPServer
class ThreadingSimpleServer(ThreadingMixIn, HTTPServer):
pass
View monitor-image.py
#!/usr/bin/env python3
import subprocess
import time
import os
from datetime import datetime
# Check after every X minutes
delay = 60
View validate.py
import numpy as np
validators_online = 102
consensus_size = 28
replacement_factor = 4
validators = np.arange(validators_online)
replacement_N = len(validators) // replacement_factor
N = 10000
@stefanv
stefanv / problem10.py
Last active Dec 10, 2020
Advent of Code :: Problem 10
View problem10.py
adapters = np.sort(adapters)
start = 0
end = np.max(adapters) + 3
cache = {}
def next_in_line(last_link):
if last_link in cache:
return cache[last_link]
@stefanv
stefanv / i3_autorandr
Created Aug 27, 2020
Automated setup of side-by-side displays in i3
View i3_autorandr
#!/bin/bash
# Modified from: https://faq.i3wm.org/question/2332/flexible-monitor-setup/
SWITCH_WS="${HOME}/scripts/i3-switch-ws"
# Switch screens on, in case they were asleep
xset dpms force on
# Find laptop display (DEFAULT)
DEFAULT=`xrandr | grep -e eDP.*connected | cut -f 1 -d ' '`
@stefanv
stefanv / dotemacs-zenburn.el
Created May 15, 2020
Customizing zenburn for org-mode to highlight all headings
View dotemacs-zenburn.el
(use-package zenburn-theme
:config
(setq org-fontify-whole-heading-line t
org-fontify-quote-and-verse-blocks t)
(load-theme 'zenburn t)
(zenburn-with-color-variables
(custom-theme-set-faces
'zenburn
`(org-level-1 ((t (:inherit ,z-variable-pitch
:foreground ,zenburn-orange
@stefanv
stefanv / fetch_prs
Last active Dec 5, 2019
NumPy: nr of PRs per contributor
View fetch_prs
#!/bin/bash
curl "https://api.github.com/repos/numpy/numpy/pulls?page=1&per_page=100" > prs0.json
curl "https://api.github.com/repos/numpy/numpy/pulls?page=2&per_page=100" > prs1.json
curl "https://api.github.com/repos/numpy/numpy/pulls?page=3&per_page=100" > prs2.json
@stefanv
stefanv / autocropjpg.py
Created Dec 4, 2019
Auto-crop jpeg image without recompressing (requires Python 3 with scikit-image, and jpegtran)
View autocropjpg.py
#!/usr/bin/env python
import subprocess
import sys
from skimage import io, filters
import numpy as np
filename = sys.argv[1]
base, ext = filename.rsplit('.', 1)
outfile = f'{base} cropped.{ext}'