Skip to content

Instantly share code, notes, and snippets.

View cowlicks's full-sized avatar
💭
SEE TRANSLATION

cowlicks

💭
SEE TRANSLATION
View GitHub Profile
@cowlicks
cowlicks / runlast.py
Last active February 4, 2024 23:10
Vim Plugin to rerun last shell-command in last vim terminal buffer
'''
This plugin creates a vim command that re-runs the last shell command in the last vim terminal buffer.
This is useful for getting quick feedback while working on something. This workflow would look like:
* open a terminal in a vim window with `:terminal`
* run a shell command like `cargo check`
* Go back to editing and re-run the shell command from the previous step with :RerunLastThingInLastTerminal
Installation:
Save this file to "rplugin/python3" in a vim 'runtimepath' directory (~/.config/nvim/rplugin/python3/runlast.py for example).
@cowlicks
cowlicks / bash_location_agnostic.sh
Created June 22, 2023 17:54
Location agnostic bash script. Demonstrate using paths relative to script, and working directory. Return to working directory on script end.
#!/usr/bin/env bash
# exit on first failure
set -e
# uncomment to enable debug mode
#set -x
# The location this script is run from
OG_DIR=$(pwd)
# The directory that this script is in
@cowlicks
cowlicks / jwt_postgrahile_ad_hoc.sql
Created December 6, 2022 16:43
manually set email to run ad hoc pstgresql commands with user configured via postgraphile
BEGIN;
SET LOCAL "jwt.claims.whatever" to 'blake.foo@stuff.com';
SELECT the_name_of_your_function('yo-10');
COMMIt;
@cowlicks
cowlicks / Makefile
Created December 5, 2022 20:45
Simple python project dependency management
PY_FILES := $(shell find . -name '*.py')
venv: venv/bin/activate
venv/bin/activate: requirements.txt
test -d venv || python3 -m venv venv
venv/bin/python -m ensurepip
venv/bin/python -m pip install -r requirements.txt
touch venv/bin/activate
test: venv
@cowlicks
cowlicks / timeit.rs
Last active April 10, 2022 22:22
Rust. Simple macro for timing an expression
use log::info;
macro_rules! timeit {
($format_str:expr, $code:expr) => {
{
let start = Utc::now();
let out = $code;
info!(
$format_str,
(Utc::now() - start).num_milliseconds()
@cowlicks
cowlicks / qs.rs
Created January 24, 2022 00:43
Quicksort implemented in Rust
use core::fmt::Debug;
fn quick_sort<T: Ord + Clone + Debug>(mut list: Vec<T>) -> Vec<T> {
if list.len() < 2 {
return list;
}
let pivot = list.pop().unwrap();
let mut left: Vec<T> = vec![];
@cowlicks
cowlicks / analyze-memlog.py
Last active November 10, 2021 20:23
Log memory of a process
# Prints the time the log spans and how many bytes per second its memory has increased
import sys
logname = sys.argv[1]
with open(logname) as f:
first, *_, last = f
t0, m0 = map(int, first.split());
t1, m1 = map(int, last.split());
print(f'Time: [{(t1 - t0)/60:.2f}] at {((m1 - m0)/(t1 - t0)):.2f} B/s')
@cowlicks
cowlicks / deferred.js
Last active September 1, 2020 03:26
A concise implementation of a 'Deferred' object factory in javascript
function Deferred() {
const o = {},
p = new Promise((resolve, reject) => Object.assign(o, {resolve, reject}));
return Object.assign(p, o);
}
@cowlicks
cowlicks / asyncio.dot
Last active October 2, 2019 05:23
So you wanna run some asyncio code...
// Usage:
// dev with: dot -Tx11 this_file.dot
// output with: dot -Tpng this_file.dot > out_file.png
// change "png" above to desired output format
digraph asyncio {
"so you wanna run some asyncio code?" -> "do you want to run the code sequentially?";
"do you want to run the code sequentially?" -> "are you within an async block?"[ label="yes" ];
"are you within an async block?" -> "use await" [ label="yes" ];
"are you within an async block?" -> "is there already an event loop running in the current thread?" [ label="no" ];
"is there already an event loop running in the current thread?" -> "use asyncio.run" [ label="no" ];
@cowlicks
cowlicks / move_thing.py
Created July 25, 2019 23:33
python mover of things
def move_thing(src_mod, dest_mod, find_str):
import os
import rope.base.project
from rope.base import libutils
from rope.refactor import move
project = rope.base.project.Project(os.getcwd())
origin = libutils.path_to_resource(project, src_mod)
destination = libutils.path_to_resource(project, dest_mod)