Skip to content

Instantly share code, notes, and snippets.

View mahmoud's full-sized avatar
Back on the grid!

Mahmoud Hashemi mahmoud

Back on the grid!
View GitHub Profile
@kurtbrose
kurtbrose / no_overlap_ids.py
Last active January 11, 2024 22:34
Helper to reset the sequences on a postgres database before running CI to ensure that ids will be very different between tables.
from sqlalchemy import select, text
def reset_sequences(engine, schema="public", gap=10000):
"""
Reset all sequences in a given schema to 10000, 20000, etc. so that ids won't overlap easily.
Ensures we don't get "lucky" and crossing ids between tables works because they are both id=1,
passing a test that should fail.
"""
with engine.connect() as conn:
@justinvanwinkle
justinvanwinkle / broken.py
Last active March 22, 2024 22:52
Every python rate-limiting library (that I can find) is broken, at least a little.
# I was looking for a rate limiting library to call rate limited apis as closely
# as possible to their enforced limits. I looked at the first few python libraries
# that I found, and when I glanced at the source, they were all clearly broken.
# Curious how this could be, I took all the top google and pip search results for: python rate limiting
# and tried to get them to do the wrong thing and fail to rate limit in situations that could come up
# in normal use (though in some cases very specific use)
# https://github.com/tomasbasham/ratelimit
# Where broken:
@sebmarkbage
sebmarkbage / WhyReact.md
Created September 4, 2019 20:33
Why is React doing this?

I heard some points of criticism to how React deals with reactivity and it's focus on "purity". It's interesting because there are really two approaches evolving. There's a mutable + change tracking approach and there's an immutability + referential equality testing approach. It's difficult to mix and match them when you build new features on top. So that's why React has been pushing a bit harder on immutability lately to be able to build on top of it. Both have various tradeoffs but others are doing good research in other areas, so we've decided to focus on this direction and see where it leads us.

I did want to address a few points that I didn't see get enough consideration around the tradeoffs. So here's a small brain dump.

"Compiled output results in smaller apps" - E.g. Svelte apps start smaller but the compiler output is 3-4x larger per component than the equivalent VDOM approach. This is mostly due to the code that is usually shared in the VDOM "VM" needs to be inlined into each component. The tr

@zzzeek
zzzeek / sql.py
Last active August 15, 2023 10:00
The SQL is just as easy as an ORM challenge
""" "Writing SQL is just as fast as using an ORM" proof of concept
Below is a simple Python object model, where we represent a database that
stores the names of employees at a company, some of whom are "engineers",
and a list of the jobs they do and the programming languages they use.
We'd like to persist the state represented by this Python object model
in a relational database, using our Python objects as a start. Then we'd
like to write SQL queries for rows in this database, and we get back instances
of Python objects exactly as they were created.
@zzzeek
zzzeek / buildy_w_lambdas.py
Last active October 31, 2020 06:34
buildything with lambdas
# note: we are using SQLAlchemy that includes _cache_key
# currently at:
# https://gerrit.sqlalchemy.org/#/c/sqlalchemy/sqlalchemy/+/1204/5/
import typing
from sqlalchemy import bindparam
from sqlalchemy import Column
from sqlalchemy import func
from sqlalchemy import inspection
@ngtvspc
ngtvspc / EXIF_remover.py
Created January 9, 2018 23:00
Remove EXIF metadata from images
# Uses the Python Imaging Library
# `pip install Pillow` works too
from PIL import Image
image_filename = "picture_with_EXIF.jpg"
image_file = open('image_filename)
image = Image.open(image_file)
# next 3 lines strip exif
image_data = list(image.getdata())
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@foca
foca / release
Last active March 31, 2023 14:15
Small shell script to create GitHub releases from the command line
#!/usr/bin/env bash
set -e
[ -z "$DEBUG" ] || set -x;
usage() {
echo "$0 <repo> <tag> [<release name>] [-- <asset>...]" >&2;
}
if [ "$1" = "-h" -o "$1" = "--help" ]; then
@zzzeek
zzzeek / gist:a3bccad40610b9b69803531cc71a79b1
Last active August 12, 2020 19:24
how to do CIDR overlapping in SQL with SQLite / MySQL / SQLAlchemy
from sqlalchemy import event
from sqlalchemy import DDL
def _mysql_cidr_overlap(metadata):
@event.listens_for(metadata, "after_create")
def _create_mysql_proc(target, connection, **kw):
if connection.engine.name != 'mysql':
return
'''
Simple distributed, streaming algorithm for keeping counts on
an adaptive sample of data.
'''
import time
import hashlib
import json
import base64