Skip to content

Instantly share code, notes, and snippets.

@Suor
Suor / test_works.py
Created March 12, 2015 09:31
Test against real database with pytest-django
import pytest
@pytest.fixture()
def real_db(_django_cursor_wrapper):
_django_cursor_wrapper.enable()
def test_index(client, real_db):
response = client.get('/')
@Suor
Suor / README.md
Last active August 29, 2015 14:18
Sublime Reform plugin showcase

Sublime Reform plug-in showcase

Here I will demonstrate a few things Reform plug-in is capable of.

Navigation

You can move up and down by function declarations, blocks of code and words.

[program:celery]
; Full path to use virtualenv, honcho to load .env
command=/home/ubuntu/venv/bin/honcho run celery -A stargeo worker -l info --no-color
directory=/home/ubuntu/app
environment=PATH="/home/ubuntu/venv/bin:%(ENV_PATH)s"
user=ubuntu
numprocs=1
stdout_logfile=/home/ubuntu/logs/celery.log
stderr_logfile=/home/ubuntu/logs/celery.err
@Suor
Suor / ungzip_stream.py
Last active August 29, 2015 14:23
Ungzip stream on the fly in Python 2
import gzip, zlib
def ungzip_stream(fd):
plain_fd = gzip.GzipFile(fileobj=fd, mode="r")
# NOTE: this is what GzipFile does on new file start,
# we do that directly as GzipFile tries to seek() before it
# and fd could be unseekable().
plain_fd._init_read()
plain_fd._read_gzip_header()
plain_fd.decompress = zlib.decompressobj(-zlib.MAX_WBITS)
@Suor
Suor / memo_stream.py
Created July 3, 2015 02:56
Memoizing IO Stream
class MemoizingStream(object):
"""
Write always to end, read from separate position.
"""
def __init__(self, fd):
print 'create memo'
self.memory = StringIO()
self.source_fd = fd
self.finished = False
@Suor
Suor / redis_logger.py
Last active August 29, 2015 14:27
A decorator/context manager to route logging to redis
# Usage
log_key = 'smth:%d:log' % some_id
with extra_logging_handler('name', RedisHandler(key=log_key)):
# Do your thing
# ...
# Implementation
@Suor
Suor / mutating_querysets.py
Created March 16, 2011 07:27
Django: mutating querysets
# THIS VERSION IS OUTDATED
# see .inplace(), .cloning(), ._clone() and .clone() methods of QuerySetMixin
# in https://github.com/Suor/django-cacheops/blob/master/cacheops/query.py
from django.conf import settings
from django.db.models import Model, Manager
MUTATING_QUERYSETS = getattr(settings, 'MUTATING_QUERYSETS', False)
class QuerySetMixin(object):
def __init__(self, *args, **kwargs):
@Suor
Suor / parseFloats.js
Created September 25, 2015 15:35
Deep floats parse
function parseStrings(obj) {
if (_.isString(obj)) {
var f = parseFloat(obj);
return Number.isName(f) ? obj : f;
} else if (_.isArray(obj)) {
obj.forEach(function (v, i) {
obj[i] = parseStrings(v);
})
} else if (_.isObject(obj)) {
_.forOwn(obj, function (v, k) {
@Suor
Suor / monkey.py
Created May 16, 2011 16:07
Efficient pickling of django models
from django.db.models import Model
from itertools import izip
def attnames(cls, _cache={}):
try:
return _cache[cls]
except KeyError:
_cache[cls] = [f.attname for f in cls._meta.fields]
return _cache[cls]
@Suor
Suor / update-sublime.pl
Created February 4, 2012 03:01
Sublime Text 2 updater for Linux
#!/usr/bin/perl
use v5.10;
use strict;
use warnings FATAL => 'all';
use Mojo::UserAgent;
my $latest_url = Mojo::UserAgent->new->get('http://www.sublimetext.com/2')