Skip to content

Instantly share code, notes, and snippets.

View cppio's full-sized avatar

Parth Shastri cppio

View GitHub Profile
class AsyncTkMixin:
def __init__(self, *args, **kwds):
super().__init__(*args, **kwds)
self._awaiting = []
self._await_loop()
def _await_loop(self):
for i in reversed(range(len(self._awaiting))):
coroutine, queue = self._awaiting[i]
from tkinter import ttk
class DebouncedEntry(ttk.Entry):
def __init__(self, master, delay, callback, validate=None):
super().__init__(master, validate="key")
self["validatecommand"] = self.register(self.validate), "%P"
self.delay = delay
self.callback = callback
self.validate = validate
@cppio
cppio / stack.c
Created September 15, 2019 18:35
Dead simple stack implemented as a linked list in C.
struct stack {
struct stack* next;
void* data;
};
void push(struct stack** stack, void* data) {
struct stack* new = malloc(sizeof *new);
new->next = *stack;
new->data = data;
*stack = new;
@cppio
cppio / SI.json
Created December 29, 2019 18:27
SI Units
{
"base": ["s", "m", "kg", "A", "K", "mol", "cd"],
"derived": {
"rad": {
"numerator": ["m"],
"denominator": ["m"]
},
"sr": {
"numerator": ["m", "m"],
"denominator": ["m", "m"]
@cppio
cppio / priority_queue.py
Created December 29, 2019 23:23
Python priority queue (with priority updating) built on top of heapq
from heapq import heappush, heappop
import itertools
__all__ = ["PriorityQueue"]
class PriorityQueue:
sentinel = object()
def __init__(self):
@cppio
cppio / skip_last.rs
Created April 14, 2020 17:45
Simple snippet showing how to skip the last item in an iterator.
fn main() {
let x = [0, 1, 2];
let y: Vec<_> = x
.iter()
.scan(None, |prev, i| Some(prev.replace(i)))
.flatten()
.collect();
println!("x = {:?}", x); // x = [0, 1, 2]
@cppio
cppio / git-get
Created April 25, 2020 17:43
A simple script to get specific files from a git repo without cloning the whole thing.
#!/bin/bash
shopt -s dotglob nullglob
dest="$(pwd)"
tempdir="$(mktemp -d)"
git clone --depth 1 --no-checkout --filter=blob:none "${1%%@*}" "$tempdir"
tag="$(tag=${1#*@}; repo="${1##*@*}"; repo="${repo:+HEAD}"; echo "${repo:-$tag}")"
shift
cd "$tempdir"
git checkout "$tag" -- "$@"
rm -rf .git
@cppio
cppio / unindent.js
Last active May 11, 2020 20:01
A simple JavaScript function/tag function that removes extra levels of indentation. Useful when embedding code in a template literal that is further indented.
const unindent = (template, ...substitutions) => {
const string = String.raw({ raw: template.raw || [template] }, ...substitutions).trimEnd().replace(/^\s*\n/, '');
const indent = (string.match(/^[ \t]*(?=\S)/gm) || []).map(i => i.length).reduce((a, b) => Math.min(a, b));
return string.replace(new RegExp(String.raw`^[ \t]{${indent}}`, 'gm'), '');
};
@cppio
cppio / cubic.tex
Last active May 12, 2020 02:10
Cardano's Formula for a Cubic Equation
\[
x=\sqrt[3]{-\frac{b^3}{27a^3}+\frac{bc}{6a^2}-\frac{d}{2a}+\sqrt{\left(-\frac{b^3}{27a^3}+\frac{bc}{6a^2}-\frac{d}{2a}\right)^2+\left(\frac{c}{3a}-\frac{b^2}{9a^2}\right)^3}}+\sqrt[3]{-\frac{b^3}{27a^3}+\frac{bc}{6a^2}-\frac{d}{2a}-\sqrt{\left(-\frac{b^3}{27a^3}+\frac{bc}{6a^2}-\frac{d}{2a}\right)^2+\left(\frac{c}{3a}-\frac{b^2}{9a^2}\right)^3}}-\frac{b}{3a}
\]
@cppio
cppio / loc
Created May 18, 2020 19:18
A wrapper around locate to exactly match names.
#!/usr/bin/env bash
exec locate -b "\\$1" "${@:2}"