Skip to content

Instantly share code, notes, and snippets.

@pgjones
pgjones / .emacs
Last active June 6, 2023 07:42
Good emacs init file, colours and no tabs
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(column-number-mode t)
'(inhibit-startup-screen t)
'(line-number-mode t)
'(load-home-init-file t t)
'(package-selected-packages (quote (terraform-mode))))
[core]
editor = emacs
[user]
name = pgjones
email =
[pull]
rebase = true
from random import choice
from string import Formatter
from timeit import timeit
from werkzeug.routing import Map, Rule
from werkzeug.routing.matcher import TableMatcher, TreeMatcher
# With thanks to https://github.com/richardolsson/falcon-routing-survey for the paths below
PATHS = [
@pgjones
pgjones / json_test_client.py
Last active July 15, 2022 14:56
A replacement flask test client that allows easy JSON API testing
import json
from flask.testing import FlaskClient
from flask.wrappers import Response
class JSONResponseWrapper(Response):
"""Extends the BaseResponse to add a get_json method.
This should be used as the response wrapper in the TestClient.
@pgjones
pgjones / test_flask_cookie.py
Created February 12, 2017 16:00
Example of how to use the Flask `test_request_context` with a cookie set
@pgjones
pgjones / brackets.py
Last active May 26, 2022 14:43
Rainwater trials
pairs = {
")": "(",
"}": "{",
"]": "[",
}
class Solution:
def isValid(self, s: str) -> bool:
stack = []
for c in s:
@pgjones
pgjones / threadPool.js
Created August 21, 2016 20:59
Simple javascript thread pool example
ThreadPool = function(script, size) {
this.threads = [];
this.tasks = [];
for (var ithread = 0; ithread < size; ithread++) {
this.threads.push(new lxst.FilterThread(script, this));
}
};
ThreadPool.prototype.queueTask = function(task, callback) {
const task = {task: task, callback: callback};
@pgjones
pgjones / schema.sql
Last active December 22, 2020 15:09
DROP TABLE IF EXISTS todos;
CREATE TABLE todos (
id INTEGER PRIMARY KEY AUTOINCREMENT,
complete BOOLEAN NOT NULL DEFAULT FALSE,
due TIMESTAMPTZ,
task TEXT NOT NULL
);
@pgjones
pgjones / app.py
Last active January 15, 2020 15:00
flask-sockets example requires: Flask, Flask-Sockets, Flask-SocketIO
from geventwebsocket.handler import WebSocketHandler
from gevent.pywsgi import WSGIServer
from flask import Flask, request, render_template_string
from flask_sockets import Sockets
import json
app = Flask(__name__)
sockets = Sockets(app)
template = """
@pgjones
pgjones / gcra.py
Last active September 16, 2019 04:02
Generic Cell Rate Algorithm example
class RateLimit:
def __init__(self, count: int, period: timedelta) -> None:
self.count = count
self.period = period
@property
def inverse(self) -> float:
return self.period.total_seconds() / self.count