Skip to content

Instantly share code, notes, and snippets.

View dmitryhd's full-sized avatar

Dmitry Khodakov dmitryhd

View GitHub Profile
@dmitryhd
dmitryhd / disc_cache.py
Last active July 18, 2017 08:20
Python disk cache
import time
import hashlib
import re
import os
import pickle
from functools import wraps
FILE_PREFIX = 'disk-cache'
@dmitryhd
dmitryhd / tornado_asyncio.py
Created July 16, 2017 20:13 — forked from drgarcia1986/tornado_asyncio.py
Tornado and Asyncio Mixed example
# -*- coding: utf-8 -*-
import asyncio
import re
import asyncio_redis
import tornado.concurrent
import tornado.httpclient
import tornado.web
import tornado.platform.asyncio
@dmitryhd
dmitryhd / launch_process.py
Created July 12, 2017 09:14
Isolate process
import multiprocessing as mp
def launch_process(func, args: list=None, consume_exceptions=False) -> int:
"""
Launch given function in separate process to save memory.
Monitors memory consumption.
:raises UserWarning: if process finished unsuccesfully and consume_exceptions set to False.
:return: error code (0 on success)
"""
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
export PYENV_VIRTUALENV_DISABLE_PROMPT=1
@dmitryhd
dmitryhd / pyzsh.zsh-theme
Last active June 27, 2019 19:39
pyzsh.zsh-theme
# oh-my-zsh Bureau Theme
# - git
# - python
# Theme color
THCOL="%{$fg_bold[green]%}"
THCOL2="%{$fg_bold[yellow]%}"
THBOLD="%{$fg_bold[white]%}"
NC="%{$reset_color%}"
@dmitryhd
dmitryhd / nginx.conf
Created May 22, 2017 19:57 — forked from foxxyz/nginx.conf
Serve current directory via nginx
# Extremely basic development setup to serve the current directory at http://localhost:9001
# Start nginx in this directory with `nginx -p . -c nginx.conf`
# Stop nginx with `nginx -p . -s stop`
events {}
http {
# Serve files with correct mimetypes on OSX
# location may have to be adjusted depending on your OS and nginx install
# include /usr/local/etc/nginx/mime.types;
@dmitryhd
dmitryhd / function_profiler.py
Last active December 8, 2016 07:57
Simple python function profiler
import logging
import time
profile_logger = logging.getLogger('rec.profile')
def profile_function(func):
func_name = func.__name__
def wrapper(*args, **kwargs):
tbegin = time.time()
@dmitryhd
dmitryhd / latency.txt
Created December 2, 2016 14:04 — forked from jboner/latency.txt
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers
--------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
@dmitryhd
dmitryhd / send_to_carbon.py
Created November 2, 2016 14:26
Send to carbon
import socket
import time
CARBON_SERVER = '0.0.0.0'
CARBON_PORT = 2003
def send_to_carbon(metric: str, value, server: str, port=2003):
message = '{} {} {}\n'.format(metric, value, int(time.time()))
import logging
import logging.handlers
import os.path as path
LOG_FORMAT = '[%(asctime)s] %(levelname)s %(message)s'
TIME_FORMAT = '%Y-%m-%d %H:%M:%S'
LOG_DEFAULT_LEVEL = logging.DEBUG