Skip to content

Instantly share code, notes, and snippets.

@nedludd0
nedludd0 / gist:a9f3fe9cf138c423befa50ba89d40c55
Last active February 5, 2021 11:29 — forked from hest/gist:8798884
Fast SQLAlchemy counting (avoid query.count() subquery)
# https://gist.github.com/hest/8798884
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 ...) ...
@nedludd0
nedludd0 / nginx.conf
Last active January 26, 2021 14:35
nginx-reverse-proxy-flask-app
#### FLASK PROD wsgi python code
location /url_sub_path {
#forward application requests to the wsgi server
proxy_pass http://unix:/var/www/python/flask/flask.sock;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
@nedludd0
nedludd0 / borg_pyrogram.py
Last active January 13, 2021 14:40
borg_pyrogram.py
"""
SINGLETON CLASS ENCAPSULATING A PYROGRAM TELEGRAM CLIENT
Credit: https://gist.github.com/skeeved/79dfe5652b20182586bb55c3cbc94250
"""
""" Config """
from os import environ
BOT_TOKEN = environ.get('BOT_TOKEN')
API_ID = environ.get('API_ID')
API_HASH = environ.get('API_HASH')
@nedludd0
nedludd0 / borg_redis.py
Last active June 1, 2022 20:04
borg_redis.py
"""
SINGLETON CLASS ENCAPSULATING A REDIS CLIENT
Credit: https://gist.github.com/skeeved/79dfe5652b20182586bb55c3cbc94250
"""
""" Config """
from os import environ
REDIS_URL = environ.get('REDIS_URL')
""" Import """
@nedludd0
nedludd0 / py_time.py
Created October 21, 2020 07:15
small time utilities
from datetime import datetime
from datetime import timedelta
from pytz import timezone
import time
# My Default Timezone
def default_timezone():
_tz = timezone('Europe/Rome')
return(_tz)
@nedludd0
nedludd0 / borg_sqlalchemy.py
Last active May 19, 2025 15:27
Sql Raw on SqlAlchemy Engine with pool connections (Python Pattern Borg Singleton)
"""
SINGLETON CLASS ENCAPSULATING A SQLALCHEMY ENGINE and SESSION.
Credit: https://gist.github.com/skeeved/79dfe5652b20182586bb55c3cbc94250
"""
""" Config """
from os import environ
DB_TYPE = environ.get('DB_TYPE') # Example: mysql
DB_CONNECTOR = environ.get('DB_CONNECTOR') # Example: pymysql
DB_HOST = environ.get('DB_HOST')