Skip to content

Instantly share code, notes, and snippets.

View vsajip's full-sized avatar

Vinay Sajip vsajip

View GitHub Profile
vsajip /
Created August 11, 2023 16:21
A rotating handler based on time and size
# From
class EnhancedRotatingFileHandler(logging.handlers.TimedRotatingFileHandler):
def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None, delay=0, utc=0, maxBytes=0):
""" This is just a combination of TimedRotatingFileHandler and RotatingFileHandler (adds maxBytes to TimedRotatingFileHandler) """
logging.handlers.TimedRotatingFileHandler.__init__(self, filename, when, interval, backupCount, encoding, delay, utc)
def shouldRollover(self, record):
Determine if rollover should occur.
vsajip / example.html
Created June 30, 2023 10:52 — forked from croxton/example.html
Adds a `hx-history-preserve` attribute to preserve the initial dom state of an element's children for history (before it has been manipulated by JS).
View example.html
<div id="my-unique-id" hx-history-preserve>
<p>Markup here wil be returned to it's original state on history restore.</p>
vsajip / htmx-metadata.js
Created June 25, 2023 05:02 — forked from croxton/htmx-metadata.js
Htmx swapping metadata
View htmx-metadata.js
htmx.on('htmx:beforeSwap', (htmxEvent) => {
let incomingDOM = new DOMParser().parseFromString(htmxEvent.detail.xhr.response, "text/html");
// Transpose <meta> data, page-specific <link> tags and JSON-LD structured data
// Note that hx-boost automatically swaps the <title> tag
let selector = "head > meta:not([data-revision]), head *[rel=\"canonical\"], head *[rel=\"alternate\"], body script[type=\"application/ld+json\"]";
document.querySelectorAll(selector).forEach((e) => {
incomingDOM.querySelectorAll(selector).forEach((e) => {
if (e.tagName === 'SCRIPT') {
from __future__ import annotations
import asyncio
import datetime
import sys
import time
from import App, ComposeResult
from textual.containers import Container, Horizontal, Vertical
from textual.widgets import DataTable, Footer, Label
vsajip /
Created December 16, 2022 21:26 — forked from walkermatt/
A debounce function decorator in Python similar to the one in underscore.js, tested with 2.7
from threading import Timer
def debounce(wait):
""" Decorator that will postpone a functions
execution until after wait seconds
have elapsed since the last time it was invoked. """
def decorator(fn):
def debounced(*args, **kwargs):
def call_it():
vsajip / msg373145.rst
Created October 22, 2022 12:36 — forked from zzzeek/msg373145.rst
asyncio support for SQLAlchemy (and Flask, and any other blocking-IO library)
View msg373145.rst

This is a cross post of something I just posted on the Python bug tracker at

I seem to have two cents to offer so here it is. An obscure issue in the Python bug tracker is probably not the right place for this so consider this as an early draft of something that maybe I'll talk about more elsewhere.

> This basically divides code into two islands - async and non-async

vsajip /
Last active October 3, 2022 14:51
Test files for CPython gh-84532
import datetime
import logging
import logging.handlers
import time
d0 =
dt0 = datetime.datetime(d0.year, d0.month,
def message(fmt, *args, **kwargs):
t = time.time() - dt0
vsajip / activate.bat
Created September 20, 2022 09:49
Eryk Sun's activate/deactivate batch scripts
View activate.bat
@setlocal EnableExtensions EnableDelayedExpansion
@REM Set VIRTUAL_ENV and VIRTUAL_ENV_PROMPT to the UTF-8 encoded parameters.
@REM CMD decodes line by line, so switch to UTF-8 (65001) temporarily.
@for /f "tokens=2 delims=:." %%a in ('"%SystemRoot%\System32\"') do @(
set "_OLD_CODEPAGE=%%a"
@if defined _OLD_CODEPAGE (
"%SystemRoot%\System32\" 65001 > nul
vsajip /
Created August 26, 2022 22:28 — forked from anonymous/
Test script showing usage of a buffering SMTP handler.
#!/usr/bin/env python
# Copyright 2001-2002 by Vinay Sajip. All Rights Reserved.
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appear in all copies and that
# both that copyright notice and this permission notice appear in
# supporting documentation, and that the name of Vinay Sajip
# not be used in advertising or publicity pertaining to distribution
vsajip / Server.kt
Created June 9, 2022 15:56 — forked from Silverbaq/Server.kt
A simple socket-server written in Kotlin
View Server.kt
package dk.im2b
import java.nio.charset.Charset
import java.util.*
import kotlin.concurrent.thread