Skip to content

Instantly share code, notes, and snippets.

View MarkusH's full-sized avatar
🐍

Markus Holtermann MarkusH

🐍
View GitHub Profile
@MarkusH
MarkusH / tb-mbox-recover.py
Created January 17, 2015 17:26
Recover Thunderbird mbox mails
In [1]: import mailbox
In [2]: mbox = mailbox.mbox('/tmp/mbox/INBOX')
In [3]: maildir = mailbox.Maildir('/tmp/maildir/')
In [4]: for msg in mbox.itervalues():
...: maildir.add(msg)
@MarkusH
MarkusH / operations.py
Created January 22, 2015 20:00
LoadFixtures Django migration operation
from __future__ import unicode_literals
from os.path import splitext
from django.core import serializers
from django.db.migrations.operations.base import Operation
###############################################################################
# #
# In order to make the following code work, the model serializers and #
@MarkusH
MarkusH / gist:bc3cb7b526dc39470cdc
Created February 14, 2015 23:42
Django migration operations with custom schema editor
class MaterializedViewOperationMixin(object):
def get_schema_editor(self, app_label, schema_editor):
if app_label in {'app1', 'app2'}:
return CustomSchemaEditor()
return schema_editor
class MaterializedViewCreateModel(MaterializedViewOperationMixin, CreateModel):
def database_forwards(self, app_label, schema_editor, from_state, to_state):
schema_editor = self.get_schema_editor(app_label, schema_editor)
@MarkusH
MarkusH / gist:ac611c25d3e240d3277e
Created February 16, 2015 22:07
Prepends the current branch name to the commit message if the branch is named stable/\d.\d.x
#!/bin/sh
branch="$(git rev-parse --abbrev-ref HEAD)"
if [[ ${branch} == stable/* ]] ; then
branch=${branch#stable/}
head -n1 .git/COMMIT_EDITMSG | grep -qP "\[\d\.\d\.x\]"
has_marker=$?
if [[ $has_marker == 0 ]] ; then
echo "[${branch}] " | cat - "${1}" > /tmp/tmpmessage
mv /tmp/tmpmessage "${1}"
@MarkusH
MarkusH / gist:6038ec4e48450fe23051
Created February 24, 2015 03:06
SSH Login Key
$ ssh -i .ssh/id_rsa testsshuser@localhost
PTY allocation request failed
Login at
http://localhost:8000/sshlogin/vbDkI7K4cD7y6cgOuJptjY5gxiDi3QevWZpXE74P5Gqu0YdefDVTFxjBqRGaRSGffhYa321pcNwSw64HvTdFRbVWKrBVnOkvyq83wsoR1Dd43EscUUlwg2V8XNcuACCqzSOgYEoDcySZgYPn1k0RHeBYLCZw2KLcPNa9yL6FKeSCUcG4rzjqe5gU2dYfXtrQ1okLnpK3cFl8fvgc0YL5MZrTIlV3mXHIKyM3V7tHVxZeyOulah6zp3o8plKGpZ/
Shared connection to localhost closed.
@MarkusH
MarkusH / gist:f037f44f60fc78bef98a
Created February 24, 2015 19:10
Referencing custom environments with custom counters
\newcounter{mycounter}
\newenvironment{myenv}[2][]{
\begin{adjustwidth}{30pt}{30pt}\widowpenalties 1 10000
\refstepcounter{mycounter}
\begingroup
\GetTitleStringSetup{expand}%
\subsubsection*{Foo~\arabic{mycounter}: {#2}}%
\ifthenelse{\equal{#1}{}}{}{\label{foo:#1}}\vspace{-7.5pt}
\endgroup
@MarkusH
MarkusH / gist:a67fbf0151ad56199149
Last active February 23, 2017 09:55
sqlall for Django 1.8
for mig in $(python manage.py showmigrations --plan | awk '{split($0, a, " "); print a[2]}') ; do python manage.py sqlmigrate ${mig%.*} ${mig#*.} ; done
@MarkusH
MarkusH / gist:f55fca3a003723efb2a3
Created April 5, 2015 00:13
Require a user to be a member of a specific group to visit a view
def require_group(group_name):
def wrapper(user):
return user.groups.filter(name=group_name).exists()
return wrapper
@user_passes_test(require_group('Group 1'))
def view1(request):
# ...
@MarkusH
MarkusH / example1.py
Last active August 29, 2015 14:18
Reservoir Sampling
from random import randint
from node import init_list
def get_random(ll):
rand_pos = randint(0, len(ll))
i = 0
node = None
for n in ll:
@MarkusH
MarkusH / README.md
Last active September 5, 2021 12:58 — forked from quiver/README.md
Python PostgreSQL PubSub

Pub/Sub pattern with PostgreSQL's LISTEN/NOTIFY command

This is a simple chat-like program using pub-sub pattern, backed by PostgreSQL's LISTEN/NOTIFY command.

Publish

publish message to foo channel from user nickname.

$ python pub.py foo nickname
PUBLISH to channel #foo