Skip to content

Instantly share code, notes, and snippets.

def plural(n, word, suffix='s'):
show_suffix = n % 10 == 1 and n % 100 != 11
return '%d %s%s' % (n, word, '' if show_suffix else suffix)
# @register.filter
def human_timedelta(delta, precision=2):
units = ['year', 'month', 'day', 'hour', 'minute']
unit_delta = [getattr(delta, unit + 's'), unit for unit in units]
terms = [plural(n, unit) for n, unit in units if n > 0]
terms = terms[:precision]
@Suor
Suor / fake_history_fail.log
Created June 30, 2017 06:03
Fake history fail
rat samples: 100%|█████████████████████████████████████████████████████████████████████████████████████████| 94550/94550 [00:11<00:00, 7999.22it/s]
mouse samples: 100%|█████████████████████████████████████████████████████████████████████████████████████| 184785/184785 [00:20<00:00, 8892.27it/s]
human samples: 100%|█████████████████████████████████████████████████████████████████████████████████████| 894400/894400 [01:32<00:00, 9682.03it/s]
Traceback (most recent call last):
File "./manage.py", line 11, in <module>
execute_from_command_line(sys.argv)
File "/home/ubuntu/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
utility.execute()
File "/home/ubuntu/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 346, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
@Suor
Suor / localurls.py
Created April 27, 2017 06:05
Django local urls
from localurls import Router
router = Router()
snapshot = router.url('/snapshot/', method='POST')
@snapshot.add(query={'action': 'make'})
def snapshot_make(request):
pass
-- Возвращает уникальные элементы массива, порядок не сохраняет
create or replace function array_uniq(anyarray) returns anyarray as $$
select array(select distinct unnest($1))
$$ language sql immutable strict;
-- Аггрегирующая функция для конкатенации массивов
create aggregate array_concat (
sfunc = array_cat,
basetype = anyarray,
stype = anyarray,
@Suor
Suor / pre-commit
Last active October 19, 2017 21:47
Flake8 git pre-commit hook that works
#!/bin/sh
FILES=`git diff --cached --name-status | awk '$1 != "D" {print $2}' | grep -E '[.]py$' | grep -v migrations`
[ "$NOCHECK" != "" ] || [ "$FILES" = "" ] || flake8 $FILES || exit 1
@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 / 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 / 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 / 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)
[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