Skip to content

Instantly share code, notes, and snippets.

View DMRobertson's full-sized avatar

David Robertson DMRobertson

View GitHub Profile
@DMRobertson
DMRobertson / test_dmr.py
Created September 30, 2021 11:09
Class and instance attributes in unittest.TestCase
from unittest import TestCase
class DMRTest(TestCase):
class_count = 0
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.instance_count = 0
@DMRobertson
DMRobertson / mypy-example.py
Last active September 7, 2021 17:34
TypedDicts and Dicts in mypy
#! /usr/bin/env python3
"""TypedDicts and Dicts in mypy"""
from typing import TypedDict, Dict, Any, Mapping
JsonDict = Dict[str, Any]
JsonMapping = Mapping[str, Any]

Scenario

Aiming to solve matrix-org/synapse#5677.

Follows on from matrix-org/synapse#10695.

We're running our own homeserver with users Bob and Eve. A remote user Alice is already in a public room on her own homeserver. Bob on our homeserver joins it.

Goal

We want only public profiles to be searchable and visible in the user directory search. By public profiles ("canonical" maybe?) we mean the thing returned by GET /profile/USER_ID.

Where does the leak come from?

We're getting names into the user directory (user_directory and user_directory_search) via calls to update_profile_in_user_dir. There are five calls:

  • main/user_directory.py (regenerate the user directory)
@DMRobertson
DMRobertson / cayley_f3.py
Created December 15, 2017 12:57
Blender Python API example
"""A script to build a model representing the Cayley graph of F_3 in Blender. I threw this together in a morning so it's quite hacky and quite dirty."""
import bpy
from itertools import product
from math import sqrt
def make_alphabet(letters):
"""Group/monoid elements are represented in memory as numbers, but we should be able to list them as human-readable strings. We use an asterisk to denote inverse, so that (e.g.) xx* = 1."""
symbols = {}
for i, char in enumerate(letters):