Skip to content

Instantly share code, notes, and snippets.

View lucaswiman's full-sized avatar

[object Object] lucaswiman

View GitHub Profile
@lucaswiman
lucaswiman / Makefile
Created October 4, 2021 19:19
Simple makefile to build virtualenv and run some make targets in it
.venv: requirements.txt
test -d ".venv" || python -m virtualenv .venv
.venv/bin/pip install -r ./requirements.txt
touch .venv
.PHONY: serve
serve: .venv
.venv/bin/python ./server.py
.PHONY: worker
@lucaswiman
lucaswiman / query.sql
Created June 22, 2021 00:44
Postgres show long-running queries with compacted whitespace
SELECT pid, age(clock_timestamp(), query_start) as age, usename, trim(regexp_replace(query, '\s+', ' ', 'g')) as query
FROM pg_stat_activity
WHERE
query != '<IDLE>'
AND query NOT ILIKE '%pg_stat_activity%' AND query_start < NOW() - interval '1 seconds'
ORDER BY query_start desc;
@lucaswiman
lucaswiman / queue_example.py
Last active January 9, 2021 00:27
multiprocessing queue example
import multiprocessing, os, time
try:
multiprocessing.set_start_method('fork')
except Exception:
pass
queue = multiprocessing.Queue()
def run(queue):
while True:
task = queue.get()
@lucaswiman
lucaswiman / that horrible year.py
Created January 1, 2021 05:32
"I tried. I tried to warn them. But it all happened, just the way I remembered."
import os
import datetime
import time
import sys
def hide_cursor():
sys.stdout.write("\033[?25l")
sys.stdout.flush()
@lucaswiman
lucaswiman / networkx_draw_graphviz.ipynb
Last active September 5, 2020 22:52
How to draw networkx graphs using graphviz in jupyter notebooks
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@lucaswiman
lucaswiman / sudoku.py
Last active November 4, 2019 05:52
python-constraint sudoku solver
from constraint import *
ROWS = 'abcdefghi'
COLS = '123456789'
DIGITS = range(1, 10)
VARS = [row + col for row in ROWS for col in COLS]
ROWGROUPS = [[row + col for col in COLS] for row in ROWS]
COLGROUPS = [[row + col for row in ROWS] for col in COLS]
SQUAREGROUPS = [
[ROWS[3 * rowgroup + k] + COLS[3 * colgroup + j]
for j in range(3) for k in range(3)]
from typing import Callable, Iterable, T
from multiprocessing.pool import ThreadPool
def threaded_map(f: Callable[..., T], it: Iterable, num_threads: int) -> Iterable[T]:
pool = ThreadPool(num_threads)
try:
results = pool.map(f, it)
finally:
pool.close()
@lucaswiman
lucaswiman / foo.dot
Created May 31, 2016 06:21
networkx / graphviz example
digraph {
0 -> "*" [key=0,
label="[b]"];
1 -> "*" [key=0,
label="[d]"];
enter -> "*" [key=0,
label=ε];
"*" -> 0 [key=0,
label="[a]"];
"*" -> 1 [key=0,
@lucaswiman
lucaswiman / field-serialize.py
Created July 18, 2018 22:11
Serialize/deserialize from a django model field.
class _Stub(object):
pass
def serialize(model_class, field_name, value):
field = model_class._meta.get_field(field_name)
obj = _Stub()
setattr(obj, field.attname, value)
return field.value_to_string(obj)
@lucaswiman
lucaswiman / jsonpatch-trigger.sql
Created July 14, 2017 21:35
jsonpatch plpythonu
DROP TABLE IF EXISTS mytable;
DROP TABLE IF EXISTS mytable_patches;
CREATE TABLE mytable(
id SERIAL PRIMARY KEY,
data JSON
);
CREATE TABLE mytable_patches(
id SERIAL PRIMARY KEY,