Skip to content

Instantly share code, notes, and snippets.

View ivan-kleshnin's full-sized avatar
🏠
Working from home

Ivan Kleshnin ivan-kleshnin

🏠
Working from home
View GitHub Profile
@ivan-kleshnin
ivan-kleshnin / gist:6b17b0f6ac863cb17217
Created October 9, 2014 09:40
MongoDB: order positions by timedelta between CLOSE_DATE and NOW
from pymongo import MongoClient
from bson import SON
from time import mktime
from datetime import datetime, timedelta
from time import mktime
from pprint import pprint
from random import randrange, randint, choice
def seed_position_times(db):
@ivan-kleshnin
ivan-kleshnin / .goaccessrc
Last active August 29, 2015 14:10
How to reveal hackers and spammers in standard nginx logs (OS X instruction)
# put this file in home folder
date_format %d/%b/%Y
log_format %h %^[%d:%^] "%r" %s %b "%R" "%u"
@ivan-kleshnin
ivan-kleshnin / lwip.utils.js
Last active August 29, 2015 14:12
Resize with lwip
// resolution is [width, height] structure
// Evaluate resolution by width (height doesn't matter, just keep it proportional)
function evalResolutionByWidth(requiredWidth, actualResolution) {
var actualWidth = actualResolution[0];
var actualHeight = actualResolution[1];
if (actualWidth > requiredWidth) {
var scale = actualWidth / requiredWidth;
return [requiredWidth, Math.round(actualHeight / scale)];
} else {
@ivan-kleshnin
ivan-kleshnin / bash.npm_package_is_installed.sh
Created January 31, 2015 12:25
Check npm package is installed
function npm_package_is_installed {
if [ $(npm list --depth 0 --parseable true "${2}" | grep "${1}$") ]; then
echo "1"
else
echo "0"
fi
}
# npm_package_is_installed gulp
# npm_package_is_installed gulp -g
import Path from "path";
const splitPathRe =
/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;
function assertPath(path) {
if (typeof path !== 'string') {
throw new TypeError('Path must be a string. Received ' +
util.inspect(path));
}
@ivan-kleshnin
ivan-kleshnin / lwip.wrapper.demo.js
Last active November 13, 2015 13:43
Lwip basic wrapper demo
/*
This wrapper
1. Reduces boilerplate. Lwip functions like .resize() are very low-level. See the code to get what I mean.
2. Adds sharpen to downsampling.
3. Has promise-based API
4. Operates on file buffers instead of image buffers to make operations immutable.
Use Lwip directly if you experience bottleneck and need to batch operations for performance.
Use this wrapper (it's forks) in other cases.
*/
class PPrintMixin:
def __str__(self):
return '<{}: id={!r}>'.format(type(self).__name__, self.id)
def __repr__(self):
attrs = []
for name in self._fields.keys():
value = getattr(self, name)
if isinstance(value, (Document, EmbeddedDocument)):
attrs.append('\n {} = {!s},'.format(name, value))
@ivan-kleshnin
ivan-kleshnin / test.htmlparser2.js
Created January 29, 2016 11:15
Test HtmlParser2
import Fs from "fs";
import {Parser} from "htmlparser2";
import MemWatch from "memwatch-next";
import humanFormat from "human-format";
process.on("unhandledRejection", function (reason, p) {
throw reason;
});
let timeScale = new humanFormat.Scale({
@ivan-kleshnin
ivan-kleshnin / test.parse5.js
Created January 29, 2016 11:15
Test Parse5
import Fs from "fs";
import Parse5 from "parse5";
import MemWatch from "memwatch-next";
import humanFormat from "human-format";
process.on("unhandledRejection", function (reason, p) {
throw reason;
});
let timeScale = new humanFormat.Scale({
let assert = require("assert");
function curryN(N, fn) {
let self = undefined;
let collectFn = Object.defineProperties(function (...args) {
if (this) {
self = this;
}
if (args.length >= N) {
return fn.apply(self, args);