Skip to content

Instantly share code, notes, and snippets.

@Suor
Suor / vote.py
Last active February 27, 2018 11:41 — forked from krakiun/vote.py
@app.route('/api/service/<product_id>/vote/<any(up,down):direction>')
@xhr_required
@login_required
def api_service_vote(product_id, direction):
''' thi api need for vote up/down the services'''
product = Product.query.get_or_404(product_id)
query = Product_Vote.query.filter(Product_Vote.voter_id == g.user.id,
Product_Vote.product_id == product.id)
@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
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 / gist:876324
Created March 18, 2011 15:59
QuerySet which returns namedtuples
from itertools import imap
from collections import namedtuple
from django.db.models.query import QuerySet, ValuesQuerySet
class NamedTuplesQuerySet(ValuesQuerySet):
def iterator(self):
# Purge any extra columns that haven't been explicitly asked for
extra_names = self.query.extra_select.keys()
@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 / group_by_lead.py
Created November 19, 2013 05:08
Grouping items by lead node
# Original code
from funcy import ireductions
def itemcollector(last, x):
if "Node ID" in x:
return x
if "files" not in last:
last["files"] = []
last["files"].append(x)
@Suor
Suor / hip_types.py
Created August 19, 2013 07:27
Thats only for hip types, not for me
In [1]: class T(tuple): pass
In [2]: t = (1, 2)
In [3]: t.__class__ = T
TypeError: __class__ assignment: only for heap types
@Suor
Suor / apply_schema.py
Created June 2, 2013 05:02
apply_schema.py
from funcy import join
def apply_el(data, el):
if isinstance(el, str):
return {el: data.get(el)}
elif isinstance(el, dict):
return {
k: apply_schema(data.get(k), v)
for k, v in el.items()
}