Skip to content

Instantly share code, notes, and snippets.

View vsajip's full-sized avatar

Vinay Sajip vsajip

View GitHub Profile
import logging
class QueueHandler(logging.Handler):
"""
This handler sends events to a queue. Typically, it would be used together
with a multiprocessing Queue to centralise logging to file in one process
(in a multi-process application), so as to avoid file write contention
between processes.
This code is new in Python 3.2, but this class can be copy pasted into
@vsajip
vsajip / overlay_pdf.py
Created April 11, 2020 15:19 — forked from dwayneblew/overlay_pdf.py
Overlay text on a PDF template using fpdf and PyPDF2
import fpdf
from PyPDF2 import PdfFileWriter, PdfFileReader
overlay_pdf_file_name = 'overlay_PDF.pdf'
pdf_template_file_name = 'base_PDF_template.pdf'
result_pdf_file_name = 'final_PDF.pdf'
# This section creates a PDF containing the information you want to enter in the fields
# on your base PDF.
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
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)

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
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
@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.
#!/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 / private_stack.py
Created May 14, 2022 12:00
For Opalstack, configure Nginx and Apache proxy-port "private stack" applications to proxy to a static site with or without PHP.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2022 Red Dove Consultants Limited.
#
# License: Apache 2.0, see https://www.apache.org/licenses/LICENSE-2.0
#
# Gratefully based on work by Ryan Sanden and Sean Fulmer at
#
# https://github.com/rsanden/userspace-fpm-installer
@vsajip
vsajip / Server.kt
Created June 9, 2022 15:56 — forked from Silverbaq/Server.kt
A simple socket-server written in Kotlin
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