Skip to content

Instantly share code, notes, and snippets.

from inspect import signature
class dynamicdict(dict):
def __init__(self, default_factory, **kwargs):
super().__init__(**kwargs)
self._default_factory = default_factory
def __missing__(self, key):
self[key] = self._default_factory(key)
@salt-die
salt-die / ipython_startup.py
Last active June 1, 2020 02:04
my ipython startup file!
#utf-8
######################
# Line Transformers: #
######################
# ------------------------------------------
# --- `λxy.` shortcut for `lambda x, y:` ---
# ------------------------------------------
@salt-die
salt-die / dust.py
Last active June 25, 2020 00:14
another kivy toy, image reconstructs itself
import numpy as np
from PIL import Image
from kivy.app import App
from kivy.animation import Animation
from kivy.clock import Clock
from kivy.uix.widget import Widget
from kivy.graphics import Color, Rectangle
@salt-die
salt-die / deque.py
Last active October 1, 2020 07:39
deque with rotatable slices
from itertools import islice
class block:
__slots__ = 'prev', 'next', 'val'
def __init__(self, val, prev=None, next=None):
self.prev = self if prev is None else prev
self.next = self if next is None else next
self.val = val
@salt-die
salt-die / graph.py
Last active November 9, 2020 19:36
How best to provide a consistent interface to similar objects across multiple modules?
from importlib import import_module
from inspect import signature
modules = 'igraph', 'networkx', 'graph_tool'
class interface:
"""A decorator for Graph methods which will set the correct interface method on the first call to that method.
"""
def __init__(self, func):
@salt-die
salt-die / main.pony
Last active November 12, 2020 08:05
Set with choice implementation in ponylang
"""
A small test of our setch.
"""
use "collections"
actor Main
new create(env: Env) =>
let s: Setch[USize] = Setch[USize]
for j in Range(0, 10) do
@salt-die
salt-die / shunting.py
Last active December 18, 2020 10:46
Implementation of shunting-yard algorithm: equal precedence of + and * because of AOC 2020 day 18 part 1
from collections import deque
class Calculator:
ops = {
"+":lambda x, y:x + y,
"*":lambda x, y:x * y,
}
precedence = (
("+", "*"),
@salt-die
salt-die / rickroll.py
Last active October 12, 2023 14:59
rick rolling in ascii!
import curses
from itertools import product
from time import time, sleep
import cv2
import numpy as np
path = "never_gonna.mp4"
@salt-die
salt-die / ast_graph.py
Last active May 31, 2021 12:22
Measuring AST similarity for plagiarism detection.
from ast import iter_child_nodes, parse, Load, Store, Delete
from igraph import Graph
class EnumDict(dict):
def __init__(self):
self.types = [ ]
def __missing__(self, key):
self[key] = len(self.types)
@salt-die
salt-die / make_table.py
Created July 4, 2021 18:18
salt's official solution to codejam 21
def _make_row(separators, entries):
"""A string representation of a single row in a table.
"""
left, middle, right = separators
return f'{left}{middle.join(entries)}{right}'
CENTERED = ' {:^{width}} '
LEFT_ALIGNED = ' {:<{width}} '
def make_table(rows, labels=None, centered=False):