Skip to content

Instantly share code, notes, and snippets.

View madsmtm's full-sized avatar

Mads Marquart madsmtm

  • Copenhagen, Denmark
  • 09:18 (UTC +02:00)
View GitHub Profile
@madsmtm
madsmtm / method_call_expression_pseudo_code.rs
Created December 1, 2023 05:23
Pseudo-code for Rust method-call expressions
/// Pseudo-code for how [method-call expressions] work.
///
/// [method-call expressions]: https://doc.rust-lang.org/reference/expressions/method-call-expr.html
fn lookup_method(mut T: Type, method_name: &str) -> Method {
// The first step is to build a list of candidate receiver types.
let mut candidate_receiver_types = vec![T];
// Obtain these by repeatedly dereferencing the receiver expression's
// type, adding each type encountered to the list,
while let Some(U) = <T as Deref>::Target {
@madsmtm
madsmtm / incomplete-facebook-schema.graphql
Last active September 5, 2019 14:50
A very incomplete description of Facebook's GraphQL schema (from 2017)
schema {
Query: Query
Mutation: Mutation
}
type Query() {
viewer: Viewer!
entities_named: SearchableEntitiesQuery!
}
@madsmtm
madsmtm / postgresql-on-delete-performance.sql
Created April 25, 2019 13:20
PostgreSQL ON DELETE performance analysis - SET NULL vs. SET DEFAULT vs. CASCADE
-- Initialize tables
DROP TABLE IF EXISTS table1, table2;
CREATE TABLE table1 (col integer PRIMARY KEY);
CREATE TABLE table2 (
col integer REFERENCES table1(col)
ON DELETE SET DEFAULT -- Experiment with different ON DELETE clauses
);
-- Optionally create an index
CREATE INDEX ON table2(col);
@madsmtm
madsmtm / flask_automatic_jsonify.py
Created March 26, 2019 09:01
Automatic JSON serialization for Flask
# Credit to:
# - http://derrickgilland.com/posts/automatic-json-serialization-in-flask-views/
# - https://blog.miguelgrinberg.com/post/customizing-the-flask-response-class
from flask import Response, Flask, jsonify
class AutoJSON(Response):
@classmethod
def force_type(cls, response, environ=None):
"""Override to convert list & dict returns to json."""
@madsmtm
madsmtm / messenger-colors.md
Created February 28, 2019 22:37
Facebook Messenger thread colors
Internal ID Color Label Gradient
205488546921017 #ff7ca8 Candy #ff8fb2 #a797ff #00e5ff
370940413392601 #1adb5b Citrus #ffd200 #6edf00 #00dfbb
164535220883264 #f01d6a Berry #005fff #9200ff #ff2e19
930060997172551 #ff9c19 Mango #ffdc2d #ff9616 #ff4f00
417639218648241 #0edcde Aqua #19c9ff #00e6d2 #0ee6b7
196241301102133 #0084ff Default Blue N/A
1928399724138152 #44bec7 Teal Blue N/A
174636906462322 #ffc300 Yellow N/A
@madsmtm
madsmtm / httpbin_sansio_get.py
Last active December 9, 2018 23:42
Rudimentary, Sans-IO `https://httpbin.org/get`, with a syncronous implementation on the side
import json
import h11
from urllib.parse import urlencode
class Get:
"""Sans-IO implementation of `/get`"""
def __init__(self, params):
self.params = params
@madsmtm
madsmtm / h11_sync_client.py
Last active December 9, 2018 23:41
`h11` example client as a class
# This file is adapted from h11's example client
# See https://github.com/python-hyper/h11/blob/master/examples/basic-client.py
import socket
import ssl
import h11
class Client:
def __init__(self, host):
self.conn = h11.Connection(our_role=h11.CLIENT)
@madsmtm
madsmtm / graphene-pytest.py
Created December 9, 2018 13:41
Graphene pytest fixtures
from pytest import fixture
from graphene.test import Client
@fixture(scope="session")
def _schema():
raise NotImplementedError(
"You must implement this fixture yourself."
"It should return an instance of `graphene.Schema`"
)
@madsmtm
madsmtm / eve-sqlalchemy-enums.py
Last active October 25, 2018 14:43
Minimal example of Eve-SQLAlchemy enumerators not working when using enumerated classes
import enum
from eve import Eve
from sqlalchemy import Column, Integer, Enum
from sqlalchemy.ext.declarative import declarative_base
from eve_sqlalchemy import SQL
from eve_sqlalchemy.config import DomainConfig, ResourceConfig
from eve_sqlalchemy.validation import ValidatorSQL
@madsmtm
madsmtm / Vagrantfile
Last active June 27, 2018 08:28
Conditional Vagrant Provisioner
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
# When you type 'up' the first time, it's always updated
config.vm.provision 'update', type: 'shell', inline: "apt-get update;apt-get -yq upgrade"
# When you provision the machine, you're asked whether you want to update it
if ARGV.include?('up') or ARGV.include?('reload') or ARGV.include?('resume') and ARGV.include?('--provision') or ARGV.include?('provision')