Skip to content

Instantly share code, notes, and snippets.

View vsajip's full-sized avatar

Vinay Sajip vsajip

View GitHub Profile
@vsajip
vsajip / enh_rotating_handler.py
Created August 11, 2023 16:21
A rotating handler based on time and size
View enh_rotating_handler.py
# From https://stackoverflow.com/questions/6167587
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)
self.maxBytes=maxBytes
def shouldRollover(self, record):
"""
Determine if rollover should occur.
@vsajip
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>
</div>
@vsajip
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) => {
e.parentNode.removeChild(e);
});
incomingDOM.querySelectorAll(selector).forEach((e) => {
if (e.tagName === 'SCRIPT') {
View slow_op.py
from __future__ import annotations
import asyncio
import datetime
import sys
import time
from textual.app import App, ComposeResult
from textual.containers import Container, Horizontal, Vertical
from textual.widgets import DataTable, Footer, Label
@vsajip
vsajip / debounce.py
Created December 16, 2022 21:26 — forked from walkermatt/debounce.py
A debounce function decorator in Python similar to the one in underscore.js, tested with 2.7
View debounce.py
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
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 https://bugs.python.org/msg373145.

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
vsajip / t_client.py
Last active October 3, 2022 14:51
Test files for CPython gh-84532
View t_client.py
import datetime
import logging
import logging.handlers
import time
d0 = datetime.date.today()
dt0 = datetime.datetime(d0.year, d0.month, d0.day).timestamp()
def message(fmt, *args, **kwargs):
t = time.time() - dt0
@vsajip
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\chcp.com"') do @(
set "_OLD_CODEPAGE=%%a"
)
@if defined _OLD_CODEPAGE (
"%SystemRoot%\System32\chcp.com" 65001 > nul
@vsajip
vsajip / log_test11.py
Created August 26, 2022 22:28 — forked from anonymous/log_test11.py
Test script showing usage of a buffering SMTP handler.
View log_test11.py
#!/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
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.io.OutputStream
import java.net.ServerSocket
import java.net.Socket
import java.nio.charset.Charset
import java.util.*
import kotlin.concurrent.thread