Skip to content

Instantly share code, notes, and snippets.

View kissgyorgy's full-sized avatar

György Kiss kissgyorgy

View GitHub Profile
@kissgyorgy
kissgyorgy / sqlalchemy_conftest.py
Last active May 6, 2024 08:06
Python: py.test fixture for SQLAlchemy test in a transaction, create tables only once!
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from myapp.models import BaseModel
import pytest
@pytest.fixture(scope="session")
def engine():
return create_engine("postgresql://localhost/test_database")
@kissgyorgy
kissgyorgy / asgi.py
Last active April 2, 2024 16:28
Django: Run ASGI application with Werkzeug's DebuggedApplication (traceback on Exception)
import os
from a2wsgi import WSGIMiddleware
from django.core.wsgi import get_wsgi_application
from django.views import debug as django_views_debug
from django_extensions.management.technical_response import (
null_technical_500_response,
)
from werkzeug.debug import DebuggedApplication
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings")
@kissgyorgy
kissgyorgy / listen.py
Created September 4, 2020 16:37
How to use PostgreSQL's LISTEN/NOTIFY as a simple message queue with psycopg2 and asyncio
import asyncio
import psycopg2
# dbname should be the same for the notifying process
conn = psycopg2.connect(host="localhost", dbname="example", user="example", password="example")
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
cursor = conn.cursor()
cursor.execute(f"LISTEN match_updates;")
@kissgyorgy
kissgyorgy / sleep-sort.sh
Last active March 9, 2024 14:24
Bash: sleep sort
#!/bin/bash
function f() {
sleep "$1"
echo "$1"
}
while [ -n "$1" ]
do
f $1 &
shift
done
@kissgyorgy
kissgyorgy / http_over_ssh_proxy.py
Last active December 24, 2023 16:35
Python: HTTP over SSH
import asyncio
import sys
from typing import Optional
import asyncssh
import httpx
class MySSHTCPSession(asyncssh.SSHTCPSession):
def __init__(self):
@kissgyorgy
kissgyorgy / show_nice_flask_routes.py
Created February 8, 2015 00:39
Python: Show flask routes nicely aligned with rules, endpoints and methods
@main.command()
def routes():
from mainweb import app
sorted_rules = sorted(app.url_map.iter_rules(), key=lambda x: x.rule)
max_rule = max(len(rule.rule) for rule in sorted_rules)
max_ep = max(len(rule.endpoint) for rule in sorted_rules)
max_meth = max(len(', '.join(rule.methods)) for rule in sorted_rules)
@kissgyorgy
kissgyorgy / mount-disk.service
Created July 24, 2023 13:21
WSL: Automatically mount disk on WSL startup
[Unit]
Description = Mount disk
Before = local-fs-pre.target
[Service]
User = root
Type = oneshot
RemainAfterExit = yes
ExecStart = /usr/local/sbin/mount-disk.sh
@kissgyorgy
kissgyorgy / bitarray.js
Created January 10, 2023 00:20
JavaScript: BitArray
import { inflateRaw, deflateRaw } from "pako";
// https://en.wikipedia.org/wiki/Run-length_encoding
const Z_RLE = 3;
class BitArray {
constructor(bitSize) {
const remainder = Math.min(1, bitSize % 8);
const byteSize = Math.floor(bitSize / 8) + remainder;
const buffer = new ArrayBuffer(byteSize);
@kissgyorgy
kissgyorgy / download_file.py
Last active November 15, 2022 10:38
Python: Download file, get file size and Content type.
import requests
r = requests.get("http://download.thinkbroadband.com/10MB.zip",
cookies={'cookie1': 'working'}
)
open('10MB.zip', 'wb').write(r.content)
@kissgyorgy
kissgyorgy / 55-bytes-of-css.md
Created October 1, 2022 22:31 — forked from JoeyBurzynski/55-bytes-of-css.md
58 bytes of css to look great nearly everywhere

58 bytes of CSS to look great nearly everywhere

When making this website, i wanted a simple, reasonable way to make it look good on most displays. Not counting any minimization techniques, the following 58 bytes worked well for me:

main {
  max-width: 38rem;
  padding: 2rem;
  margin: auto;
}