Skip to content

Instantly share code, notes, and snippets.

@danielrichman
danielrichman / connection_factory.py
Last active July 8, 2024 12:46
nicer postgres connection class & flask postgres
class PostgreSQLConnection(psycopg2.extensions.connection):
"""
A custom `connection_factory` for :func:`psycopg2.connect`.
This
* puts the connection into unicode mode (for text)
* modifies the :meth:`cursor` method of a :class:`psycopg2.connection`,
facilitating easy acquiring of cursors made from
:cls:`psycopg2.extras.RealDictCursor`.
"""
@danielrichman
danielrichman / habitat_approve.py
Created July 4, 2013 13:12
miscellaneous habitat convenience shell scripts
#!/home/daniel/venv/bin/python
import sys
import os
import couchdbkit
password_filename = os.path.join(os.path.dirname(sys.argv[0]), 'couch')
with open(password_filename) as f:
couch_uri = f.readline().strip()
server = couchdbkit.Server(couch_uri)
@danielrichman
danielrichman / flask_request_logger.md
Last active December 19, 2015 03:48
logging formatter that appends details of a flask request to a log record (in much the same way that the default formatter appends a traceback)
@danielrichman
danielrichman / csrf.py
Last active February 4, 2024 04:14
Flask/Jinja2 CSRF utility functions
import random
from flask import request, session, abort, Markup
@app.template_global('csrf_token')
def csrf_token():
"""
Return the CSRF token for the current session
If not already generated, session["_csrf_token"] is set to a random
string. The token is used for the whole life of the session.
@danielrichman
danielrichman / filter.py
Created June 27, 2013 22:37
Flask/Jinja2 filter to insert zero width spaces into a long string (in this case a session ID) so that it word wraps as soon as the screen gets thin.
@app.template_filter('sidsplit')
def sidsplit(sid, chunk_size=10):
n = len(sid) / chunk_size
chunks = [sid[i * chunk_size : (i + 1) * chunk_size] for i in range(n)]
if len(sid) % chunk_size:
chunks.append(sid[n * chunk_size:])
return Markup('<wbr>').join(chunks)
@danielrichman
danielrichman / helpers.py
Last active December 29, 2015 10:34
flask & bootstrap pagination li width and phone/tablet/desktop page show sizes are suitable for up to 3 digit page numbers
@app.template_global('show_which_pages')
def show_which_pages(page, pages, show=5):
"""
Work out which page numbers to display
Pages are numbered 1 to pages, inclusive.
page: the current page
pages: the total number of pages.
show: maximum number of pages to show
@danielrichman
danielrichman / email_deliverability.py
Created April 28, 2013 16:55
Check that email is actually delivered
#!/usr/bin/python
import time
from subprocess import Popen, PIPE
from email.mime.text import MIMEText
from email import message_from_string
from poplib import POP3_SSL
emails = [
('[email protected]', 'pop.mail.yahoo.com'),
@danielrichman
danielrichman / email_failures.py
Last active December 15, 2015 21:09
Summarise exim4 email failures
#!/usr/bin/python
import glob
import gzip
import datetime
logfiles = glob.glob("/var/log/exim4/mainlog*")
time_format = "%Y-%m-%d %H:%M:%S"
def all_lines():
@danielrichman
danielrichman / tail_dmesg.c
Created April 2, 2013 11:53
sort of like tail -f dmesg, if you could do that
/* Copyright 2013 Daniel Richman; GNU GPL 3 */
/* gcc -Wall -Werror -pedantic -O2 -o tail_dmesg tail_dmesg.c */
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/klog.h>
#include <unistd.h>
<?php
/*
* Copyright (C) 2010 Daniel Richman
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of