Skip to content

Instantly share code, notes, and snippets.

@rrafal
rrafal / .bash_profile
Created June 8, 2023 14:16
Set tab title in Terminal
tab() {
printf '\e]1;%s\a' "$*"
}
@rrafal
rrafal / transaction.py
Last active September 21, 2023 13:15
Lock PostgreSQL table in Django app
from django.db.transaction import * # NOQA
from django.utils.decorators import ContextDecorator
LOCK_MODES = (
'ACCESS SHARE',
'ROW SHARE',
'ROW EXCLUSIVE',
'SHARE UPDATE EXCLUSIVE',
'SHARE',
@rrafal
rrafal / .profile
Created October 19, 2016 15:33
Git branch in prompt in OSX
source /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-completion.bash
source /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh
GIT_PS1_SHOWDIRTYSTATE=true
export PS1='\u@\h:\W$(__git_ps1)$ '
@rrafal
rrafal / .bashrc
Last active October 19, 2016 15:33
Git branch in prompt in Linux
GREEN='\e[32m'
DEFAULT='\e[39m'
PS1="\u@\h:\w$GREEN\$(git branch 2>/dev/null | grep '^*' | colrm 1 1)$DEFAULT\$ "
@rrafal
rrafal / pgx_json.go
Created March 31, 2016 00:03
Use JSON column with golang pgx driver.
package main
import (
"fmt"
"encoding/json"
"github.com/jackc/pgx"
"log"
)
var schema = `
@rrafal
rrafal / sqlx_json.go
Created March 31, 2016 00:02
Use JSON field with golang sqlx driver
package main
import (
"encoding/json"
"errors"
"database/sql/driver"
"fmt"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"log"
@rrafal
rrafal / decorators.py
Last active September 21, 2023 07:49
Util for making it easier to write decorators
import functools
def kwargs_decorator(decorator):
""" Allows to easily create docorators with optional keyword argument.
Decorator must not be called with positional arguments.
"""
@functools.wraps(decorator)
def wrapper(wrapped=None, **kwargs):
if wrapped is None:
@rrafal
rrafal / vstatic.py
Created November 12, 2014 16:03
Django template tag for creating URL with version number to static file.
import os
from django import template
from django.conf import settings
register = template.Library()
@register.simple_tag(name="vstatic")
def vstatic(path):
""" Return absolute URL to static file with versioning. """
@rrafal
rrafal / gist:a21312d3c7edb80eb36f
Created November 12, 2014 15:51
Filter request data read error in Django
class IgnoreRequestDataReadError(logging.Filter):
def filter(self, record):
if record.exc_info:
exc_value = record.exc_info[1]
if isinstance(exc_value, IOError) and unicode(exc_value) == 'request data read error':
return False
@rrafal
rrafal / gist:e176f7f0de255fc16fa4
Last active February 5, 2020 08:19
Django logging filter for Invalid HTTP_HOST header
class IgnoreHostError(logging.Filter):
def filter(self, record):
from django.core.exceptions import SuspiciousOperation
if record.name == 'django.security.DisallowedHost':
return False
if record.name == 'django.core.exceptions.DisallowedHost':
return False
if record.exc_info:
exc_value = record.exc_info[1]