Skip to content

Instantly share code, notes, and snippets.

Avatar

Alexander Schepanovski Suor

View GitHub Profile
@Suor
Suor / parse.py
Created Jun 23, 2018
Parse SWGOH zetas
View parse.py
import requests
import lxml.html
resp = requests.get('https://swgoh.gg/g/34508/darklightcrew/zetas/')
root = lxml.html.fromstring(resp.text)
rows = root.cssselect('.character-list table tbody tr')
for tr in rows:
nick = tr.cssselect('td:first-child')[0].attrib['data-sort-value']
@Suor
Suor / vote.py
Last active Feb 27, 2018 — forked from krakiun/vote.py
View 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)
View human_timedelta.py
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]
View fake_history_fail.log
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 Apr 27, 2017
Django local urls
View localurls.py
from localurls import Router
router = Router()
snapshot = router.url('/snapshot/', method='POST')
@snapshot.add(query={'action': 'make'})
def snapshot_make(request):
pass
View array_agg.sql
-- Возвращает уникальные элементы массива, порядок не сохраняет
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 Oct 19, 2017
Flake8 git pre-commit hook that works
View pre-commit
#!/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 Sep 25, 2015
Deep floats parse
View parseFloats.js
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 Aug 29, 2015
A decorator/context manager to route logging to redis
View redis_logger.py
# 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 Jul 3, 2015
Memoizing IO Stream
View memo_stream.py
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