Skip to content

Instantly share code, notes, and snippets.

View lucaswiman's full-sized avatar

[object Object] lucaswiman

View GitHub Profile
@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.
import numpy as np
import cv2
def sp_noise(image, prob):
'''
Add salt and pepper noise to image
prob: Probability of the noise
'''
output = image.copy()
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 / 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,
@lucaswiman
lucaswiman / runtime_union.py
Last active June 25, 2017 17:11
It is hard to use Union in runtime type checks
>>> type(Union[int, str])
typing.Union
>>> type(Union[int, str]) == Union
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/lucaswiman/.pyenv/versions/3.6/lib/python3.6/typing.py", line 760, in __eq__
return self._subs_tree() == other
File "/Users/lucaswiman/.pyenv/versions/3.6/lib/python3.6/typing.py", line 760, in __eq__
return self._subs_tree() == other
File "/Users/lucaswiman/.pyenv/versions/3.6/lib/python3.6/typing.py", line 760, in __eq__
@lucaswiman
lucaswiman / .sh
Created June 10, 2017 00:18
grep co-occurrences
function git-co-occurrences() {
pattern1="$1"
pattern2="$2"
comm -12 <(git grep -E "$pattern1" | cut -d ':' -f1 | sort | uniq) <(git grep -E "$pattern2" | cut -d ':' -f1 | sort | uniq)
}