Skip to content

Instantly share code, notes, and snippets.

@peterdemin
peterdemin / one_session_per_user.middleware.py
Last active May 8, 2023 17:10
django middleware that disallows concurent user login while allowing such for administrators
from one_session_per_user.models import User, Visitor
from django.contrib.sessions.models import Session
class OneSessionPerUserMiddleware(object):
"""http://stackoverflow.com/a/1814797"""
def process_request(self, request):
if isinstance(request.user, User):
current_key = request.session.session_key
@peterdemin
peterdemin / serializer.py
Created July 15, 2013 04:59
Abstract django model serializer. Instead of flat structure, allows using hierarchies. You must override Model in descendants. Constructor works in two ways: 1. Passing only model instance - serializes it to dict (self.data); 2. Passing both instance and data - in that order - deserializes data into passed instance. See attached usage.py for exa…
import os
from django.db import models
class SimpleSerializer(object):
Model = None
exclude = ()
ingored_fields = (
models.AutoField,
models.ForeignKey,
@peterdemin
peterdemin / User-Default (Linux).sublime-keymap
Last active December 21, 2015 05:18
My sublime text settings Command "prompt_insert_nums" is available through sublime package Insert Sequences
[
{ "keys": ["ctrl+alt+up"], "command": "select_lines", "args": {"forward": false} },
{ "keys": ["ctrl+alt+down"], "command": "select_lines", "args": {"forward": true} },
{ "keys": ["ctrl+q"], "command": "close" },
{ "keys": ["ctrl+w"], "command": "find_under_expand" },
{ "keys": ["ctrl+r"], "command": "wrap" },
{ "keys": ["ctrl+alt+n"], "command": "prompt_insert_nums" }
]
@peterdemin
peterdemin / twitter-unfollow.js
Created August 17, 2013 12:25
Web browser console command to unfollow everyone on https://twitter.com/following page by issuing onclick() on all "Unfollow" buttons.
$('button span.unfollow-text').parent().each(function(i, a) {a.click();})
@peterdemin
peterdemin / commit-msg.py
Created September 8, 2013 11:07
git commit message hook, that automatically adds ticket number to commit message
#!/usr/bin/python
#-*- coding: utf-8 -*-
import os
import re
import sys
import shutil
import subprocess
re_ticket = re.compile(ur'(?u)^([0-9]+)-.+')
@peterdemin
peterdemin / install-build-agent.sh
Last active December 22, 2015 15:59 — forked from ianbattersby/gist:4641450
Script meant to be used as vagrant shell provisioner. It doesn't use /vagrant shared folder though. That means it can be launched from any remote debian machine.
#!/bin/bash
# Replace this with actual server address
serverUrl="10.1.136.242:80"
if [ -e "/etc/init.d/teamcity" ]
then
echo "TeamCity allready installed"
exit 0
fi
@peterdemin
peterdemin / stress_heap.c
Created October 15, 2013 05:25
Slightly rewritten test from http://bugs.python.org/issue19246 that demonstrates memory fragmentation issue.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int create_huge_linked_list(void **last_item) {
int i;
void *prev_item = NULL;
for(i = sizeof(void *); i < 1000000; i++) {
@peterdemin
peterdemin / .gitconfig
Last active December 26, 2015 20:49
Git settings for using Meld under Windows. (C:\Users\user\.gitconfig) Note: Meld's executable path must not contain spaces or non-ascii characters!
[merge]
tool = meld
[mergetool "meld"]
prompt = false
keepBackup = false
keepTemporaries = false
path = C:/Meld/meld/meld.exe
cmd = "/c/Meld/meld/meld.exe $PWD/$LOCAL $PWD/$BASE $PWD/$REMOTE --output=$PWD/$MERGED"
#!/usr/bin/env python
# regression test for https://bitbucket.org/zzzeek/sqlalchemy/issue/2682/is_-and-isnot-in-conjunction-with-boolean
from sqlalchemy import create_engine, Column, Integer, Boolean
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
engine = create_engine('mysql://user:password@localhost/mre')
@peterdemin
peterdemin / description
Last active August 29, 2015 14:10
celery retry task in chain of groups
Steps to reproduce:
1. apply async chain of groups of tasks;
2. raise retry inside task(s) of first group;
3. watch, how tasks of second group are finished before those in first group.
Particularly in attached logs all 'add' tasks should finish before starting of 'mul' tasks.
But as retry occurs, order does not hold anymore.
The actual order was: