Skip to content

Instantly share code, notes, and snippets.

Ivan Velichko iximiuz

Block or report user

Report or block iximiuz

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@iximiuz
iximiuz / serv_async.py
Last active Apr 14, 2019
Python Web Servers
View serv_async.py
# python3
import asyncio
import sys
counter = 0
async def run_server(host, port):
server = await asyncio.start_server(serve_client, host, port)
await server.serve_forever()
@iximiuz
iximiuz / walkdir.js
Created Mar 13, 2019
Tiny recursive walkdir (aka readdir) in JavaScript
View walkdir.js
const fs = require('fs');
const path = require('path');
async function *walkdir(dir) {
const stack = [dir];
while (stack.length) {
const filename = stack.pop();
const stat = await fs.promises.stat(filename);
if (stat.isDirectory()) {
const files = (await fs.promises.readdir(filename))
@iximiuz
iximiuz / keep-me-alive.js
Last active Aug 1, 2018
Node.js http server keep-alive behaviour test
View keep-me-alive.js
const http = require('http');
const log = console.log;
console.log = (...args) => {
log.apply(console, [new Date().toISOString()].concat(args));
};
const port = process.argv[2];
const server = http.createServer((req, res) => {
console.log('Incoming request');
View indices.md
i => j1 = 2*i           0 => 0     1 => 2    2 => 4
     j2 = 2*i+1         0 => 1     1 => 3    2 => 5

i = j//2                0 => 0     2 => 1    4 => 2
                        1 => 0     3 => 1    5 => 2


i => j1 = 2*i + 1       0 => 1    1 => 3    2 => 5
     j2 = 2*i + 2       0 => 2    1 => 4    2 => 6
@iximiuz
iximiuz / README.md
Last active Jan 29, 2018
execve() file descriptors sharing example
View README.md

Description

As everybody knows, execve() system call replaces a calling process image with a new process image. At the same time the file descriptors table remains the same.

The idea of this example is to show how launched via execve() processes can access file descriptors from their parent.

The parent process creates 4 pipes (unidirectional data flows) for stdin/stdout streams of its two children. Then the parent forks itself twice to spawn its children. Each child process binds corresponding pipes to its stdin & stdout and then runs child executable via execl(). To simplify the example, child processes also explicitly shares their stdin file descriptors between each other via command line argument. In the end parent is able to read from children stdouts and write to their stdins, while children are able to write to sibling's stdins (see the schema below).

@iximiuz
iximiuz / rvo_bench.cpp
Last active Aug 19, 2016
C++ Return value optimization benchmark
View rvo_bench.cpp
#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;
@iximiuz
iximiuz / binomial_coefficients.py
Created Jun 8, 2016
Calculate binomial coefficients via Pascal's triangle
View binomial_coefficients.py
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 / diff.txt
Created May 19, 2016
Python 2 vs Python 3 division operator
View diff.txt
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 / goaccess.md
Created May 16, 2016
Analyze Nginx access log with GoAccess
View goaccess.md

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 / mean_median_mode_stddev.py
Last active Aug 15, 2018
Mean, median, mode and stddev calculation in Python 3
View mean_median_mode_stddev.py
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]
You can’t perform that action at this time.