Skip to content

Instantly share code, notes, and snippets.

@fjsj
fjsj / sorted_list_intersection.py
Created December 14, 2018 17:54
Python fast sorted list intersection
import bisect
def bisect_index(arr, start, end, x):
i = bisect.bisect_left(arr, x, lo=start, hi=end)
if i != end and arr[i] == x:
return i
return -1
def exponential_search(arr, start, x):
if x == arr[start]:
@fjsj
fjsj / speedtest.py
Created January 21, 2019 18:13
Python bloom filters performance test (based on pybloomfilter's own test)
#! /usr/bin/env python
import os
import tempfile
import time
import timeit
import pybloomfilter
tempfiles = []
@fjsj
fjsj / infinite_monkey.py
Created October 13, 2021 18:22
Infinite Monkey in Python
import random
import sys
keyboard = "0123456789abcdefghijklmnopqrstuvwxyz!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ "
if __name__ == "__main__":
expected = " ".join(sys.argv[1:]).lower()
already_produced_set = set()
produced = ""
@fjsj
fjsj / log_time.py
Created December 9, 2021 23:07
Flexible Python context manager to log elapsed time
# Based on this: https://stackoverflow.com/a/62956469/145349
import logging
import time
from contextlib import contextmanager
@contextmanager
def log_time(logger, log_message="Took %.4f secs", log_level=logging.INFO):
start = time.perf_counter()
yield
elapsed = time.perf_counter() - start
SELECT
sum(idx_blks_read) as idx_read,
sum(idx_blks_hit) as idx_hit,
(sum(idx_blks_hit) - sum(idx_blks_read)) / sum(idx_blks_hit) as ratio
FROM
pg_statio_user_indexes;
@fjsj
fjsj / celery_settings.py
Last active March 24, 2024 10:44
Recommended Celery Django settings for reliability. For more details, check the DjangoCon 2023 talk "Mixing reliability with Celery for delicious async tasks" by Flávio Juvenal: https://youtu.be/VuONiF99Oqc
# Recommended Celery Django settings for reliability:
# (use `app.config_from_object('django.conf:settings', namespace='CELERY')`
# in proj/celery.py module)
from decouple import config # use python-decouple: https://github.com/HBNetwork/python-decouple
# Prefer RabbitMQ over Redis for Broker,
# mainly because RabbitMQ doesn't need visibility timeout. See:
# https://blog.daftcode.pl/working-with-asynchronous-celery-tasks-lessons-learned-32bb7495586b
# https://engineering.instawork.com/celery-eta-tasks-demystified-424b836e4e94
@fjsj
fjsj / talk-types.md
Last active March 25, 2024 20:21
PyCon and DjangoCon commonly accepted talk types (with examples)

Tutorial-like

Best/worst practices