Skip to content

Instantly share code, notes, and snippets.

View AndreLouisCaron's full-sized avatar

André Caron AndreLouisCaron

View GitHub Profile
@AndreLouisCaron
AndreLouisCaron / test_leaks.py
Created June 2, 2020 00:56
Demonstrate leaks in ujson upgrade
import ujson
import sys
def test_does_not_leak_dictionary_values():
import gc
gc.collect()
value = ["abc"]
data = {"1": value}
ref_count = sys.getrefcount(value)
ujson.dumps(data)
# -*- coding: utf-8 -*-
# When running in a local back-end, both ElasticSearch and ElastAlert run
# inside Docker containers that are launched side-by-side (e.g. via
# `docker-compose` or K8S). Unfortunately, ElastAlert does not account for
# this and requires that the `elastalert_status` index be created before
# ElastAlert starts. In addition, when running via `docker-compose`, there is
# no automatic restart when the `elastalert` command crashes on start.
#
@AndreLouisCaron
AndreLouisCaron / cpumon.py
Created June 19, 2018 18:00
CPU attribution for Python server applications on Windows
# -*- coding: utf-8 -*-
import logging
import psutil
import threading
import time
from contextlib import contextmanager
from functools import partial
@AndreLouisCaron
AndreLouisCaron / named-pipes.py
Created April 21, 2018 00:28
Demonstration of race condition in call to `WaitNamedPipe()` which triggers `ERROR_FILE_NOT_FOUND`.
# -*- coding: utf-8 -*-
import logging
import os
import time
from binascii import hexlify
from contextlib import (
contextmanager,
@AndreLouisCaron
AndreLouisCaron / redis-scan-first-n-matches.py
Created October 16, 2017 12:58
Pick up to N keys that match a pattern on a large Redis instance
import itertools
def scan(c, pattern, n):
"""Find up to N Redis keys that match a pattern.
NOTE: even if this Python code looks innocuous, it involves
A LOT of work on the Redis instance, especially when
the number of matches is low with respect to the total
number of keys. Use with care!
"""
@AndreLouisCaron
AndreLouisCaron / docker-reachable-containers.py
Created July 7, 2017 17:22
Find IDs of containers that we can network to.
# -*- coding: utf-8 -*-
import docker
import os
import re
def readfile(path):
"""Read a UTF-8 encoded text file from disk."""
@AndreLouisCaron
AndreLouisCaron / grafana-exchange.py
Created June 29, 2017 16:34
Grafana dashboard exchange
# -*- coding: utf-8 -*-
from __future__ import print_function
import errno
import glob
import json
import os
import requests
@AndreLouisCaron
AndreLouisCaron / dicttree.py
Created June 1, 2017 15:23
Python dicttree: cutest recursion I've ever seen
from collections import defaultdict
def dicttree():
return defaultdict(dicttree)
@AndreLouisCaron
AndreLouisCaron / asyncio-wait-coroutines.py
Last active February 13, 2016 20:51
asyncio await inconsistency
# This will print nothing because the set contains tasks, not f1 and f2.
import asyncio
async def foo():
return 'foo'
async def bar():
f1 = foo();
f2 = foo();
@AndreLouisCaron
AndreLouisCaron / .coveragerc
Last active August 29, 2015 14:22
Computing combined code coverage with Tox
[run]
source =
greet
[paths]
source =
.
.tox/py27/lib/python2.7/site-packages/
.tox/py34/lib/python3.4/site-packages/
.tox/pypy/site-packages/