Skip to content

Instantly share code, notes, and snippets.

View iximiuz's full-sized avatar
🪲

Ivan Velichko iximiuz

🪲
View GitHub Profile
@iximiuz
iximiuz / Socket.js
Last active January 19, 2017 09:55
AngularJS namespaced socket.io service concept
'use strict';
/**
* Пример использования socket.io namespaces в сервисе AngularJS.
*
* Цель: получить несколько отдельных каналов связи в рамках одного соединения.
*
* Проблема: так как сервисы в AngularJS являются синглтонами, не возможно
* получить несколько экземпляров сервиса Socket с разными значениями
* namespace. При этом хочется иметь отдельные сервисы, представляющие
@iximiuz
iximiuz / scheme::sum_of_squares.scm
Last active August 29, 2015 14:02
Sum of squares of two biggest numbers (SICP excercise)
; pen test on functional programming
(define (min-2 a b) (if (> a b) b a))
(define (min-3 a b c) (min-2 (min-2 a b) (min-2 b c)))
(define (sq x) (* x x))
(define (foo a b c) (cond ((= a (min-3 a b c)) (+ (sq b) (sq c)))
((= b (min-3 a b c)) (+ (sq a) (sq c)))
(else (+ (sq a) (sq b)))))
@iximiuz
iximiuz / flask_static_files_cache_invalidator.py
Last active October 28, 2019 18:56
Flask: add static file's cache invalidator param to URLs generated by url_for(). Blueprints aware.
""" Inspired by http://flask.pocoo.org/snippets/40/ """
app = Flask(__name__)
@app.url_defaults
def hashed_url_for_static_file(endpoint, values):
if 'static' == endpoint or endpoint.endswith('.static'):
filename = values.get('filename')
if filename:
if '.' in endpoint: # has higher priority
@iximiuz
iximiuz / python_parse_set_cookie_headers.py
Last active August 14, 2018 01:55
Parse Set-Cookie headers in Python
@iximiuz
iximiuz / delayserver.py
Created March 25, 2016 11:41
Simple async HTTP server to emulate long request processing
import sys
from asyncio import coroutine, sleep
from aiohttp import web
@coroutine
def handle(request):
seconds = float(request.match_info.get('seconds', 1))
yield from sleep(seconds)
text = "You've been waiting for {} seconds".format(seconds)
return web.Response(body=text.encode('utf-8'))
@iximiuz
iximiuz / mean_median_mode_stddev.py
Last active August 15, 2018 09:45
Mean, median, mode and stddev calculation in Python 3
sample = [5, 3, 1, 2, 4]
n = len(sample)
mean = sum(sample)/n
median = (sample[n//2 - (n + 1)%2] + sample[n//2])/2
stddev = (sum((e - mean)**2 for e in sample)/n)**0.5
cnt = {}
for e in sample:
cnt[e] = cnt.get(e, 0)
cnt[e] += 1
mode = sorted(cnt.keys(), key=lambda e: (cnt[e], -e))[-1]
@iximiuz
iximiuz / goaccess.md
Created May 16, 2016 11:57
Analyze Nginx access log with GoAccess

Run:

grep "07/May/2016" app_access.log | goaccess -a --log-format '%h %^[%d:%t %^] "%r" %s %b "%R" "%u"' --date-format '%d/%b/%Y' --time-format '%H:%M:%S' > goaccess_may07.html

More here

@iximiuz
iximiuz / diff.txt
Created May 19, 2016 11:27
Python 2 vs Python 3 division operator
Input | Py2 | Py3
----------+-----+-----
5/2 | 2 | 2.5
----------+-----+-----
5//2 | 2 | 2
----------+-----+-----
5.0/2 | 2.5 | 2.5
----------+-----+-----
5.0//2 | 2.0 | 2.0
----------+-----+-----
@iximiuz
iximiuz / binomial_coefficients.py
Created June 8, 2016 16:40
Calculate binomial coefficients via Pascal's triangle
def next_row(prev_row):
row = [prev_row[0]] * (len(prev_row) + 1)
for idx in range(len(prev_row) - 1):
row[idx + 1] = (prev_row[idx] + prev_row[idx + 1])
return row
def binomial(n, k, cache=None):
""" Calculates binomial coefficients using Pascal's triangle.
0 1
@iximiuz
iximiuz / rvo_bench.cpp
Last active August 19, 2016 13:14
C++ Return value optimization benchmark
#include <chrono>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#define SIZE_MEDIUM 4096
#define SIZE_LARGE 16*4096
#define ITER_COUNT 100000
using namespace std;
using namespace std::chrono;