Skip to content

Instantly share code, notes, and snippets.

Avatar

Ivan Kleshnin ivan-kleshnin

View GitHub Profile
@ivan-kleshnin
ivan-kleshnin / gist:601503b9062ba45863d5
Last active Aug 29, 2015
Brainstorming middleware ordering... Much more wider application though.
View gist:601503b9062ba45863d5
;; 0. Given that `app` is a RING handler
;; 1. We have 6 functions: 3 to modify requests, 3 to modify responses
;; 1.1. Request modifiers
(defn modify-request-first [handler]
(fn [request]
(println "modify-request-first") ; "println" is a placeholder for real code
(handler request)))
@ivan-kleshnin
ivan-kleshnin / gist:6b17b0f6ac863cb17217
Created Oct 9, 2014
MongoDB: order positions by timedelta between CLOSE_DATE and NOW
View gist:6b17b0f6ac863cb17217
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 Aug 29, 2015
How to reveal hackers and spammers in standard nginx logs (OS X instruction)
View .goaccessrc
# 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 Aug 29, 2015
Resize with lwip
View lwip.utils.js
// 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 {
View bash.npm_package_is_installed.sh
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
View newParse.js
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 Nov 13, 2015
Lwip basic wrapper demo
View lwip.wrapper.demo.js
/*
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.
*/
View mongoengine_pprint.py
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))
View test.htmlparser2.js
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({
View test.parse5.js
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({