Skip to content

Instantly share code, notes, and snippets.

@Anexen
Anexen / order_by.py
Created July 24, 2024 07:08
SQL-like sorting for python
class Desc:
__slots__ = ("value",)
def __init__(self, value):
self.value = value
def __lt__(self, other):
return self.value > other.value
data = [
@Anexen
Anexen / bloom_filter.py
Created July 24, 2024 07:06
Bloom filter (numpy)
import numpy as np
from cityhash import CityHash64WithSeed
H = np.vectorize(CityHash64WithSeed, otypes=['uint64'])
class BloomFilter:
def __init__(self, n: int, fp_prob: float) -> None:
self.m = np.ceil(-(n * np.log(fp_prob)) / (np.log(2) ** 2)).astype(int)
@Anexen
Anexen / update_table_in_batches.sql
Last active October 15, 2023 20:07
PostgreSQL procedure to update large table in batches
-- usage:
-- CALL update_table_in_batches('table_1', array['a = 1'], batch_size := 10);
-- CALL update_table_in_batches('table_2', array['b = 2'], track_by := 'created', initial_value := '2021-01-01'::timestamp);
CREATE OR REPLACE PROCEDURE update_table_in_batches(
target_table text,
set_expressions text[],
track_by text default 'id',
initial_value anyelement default 0,
batch_size integer default 1000,
@Anexen
Anexen / Dockerfile
Last active July 18, 2025 18:46
production-ready dockerfile for python projects
# syntax = docker/dockerfile:experimental
ARG PYTHON_VERSION=3.7.6
ARG APP_ENV=prod
# ---------------------------------------------------------------
FROM python:${PYTHON_VERSION}-stretch AS base-builder
ENV VIRTUAL_ENV=/opt/venv
RUN python -m venv ${VIRTUAL_ENV}
@Anexen
Anexen / test.py
Created December 26, 2019 18:01
clickhouse driver insert columnar data benchmark
import sys
import time
import resource
from clickhouse_driver import Client
def memory_usage():
return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss