Skip to content

Instantly share code, notes, and snippets.

View hiphamster's full-sized avatar

Alex Yelluas hiphamster

View GitHub Profile
@hiphamster
hiphamster / gist:c352c9b264ecc920f0592aec5d412269
Created August 7, 2019 00:04 — forked from hest/gist:8798884
Fast SQLAlchemy counting (avoid query.count() subquery)
def get_count(q):
count_q = q.statement.with_only_columns([func.count()]).order_by(None)
count = q.session.execute(count_q).scalar()
return count
q = session.query(TestModel).filter(...).order_by(...)
# Slow: SELECT COUNT(*) FROM (SELECT ... FROM TestModel WHERE ...) ...
print q.count()
@hiphamster
hiphamster / flask_binary.py
Created August 30, 2019 05:41 — forked from Miserlou/flask_binary.py
Flask serving binary data example
import io
from flask import Flask, send_file
app = Flask(__name__)
@app.route('/logo.jpg')
def logo():
"""Serves the logo image."""
with open("logo.jpg", 'rb') as bites: