Skip to content

Instantly share code, notes, and snippets.

View h2rd's full-sized avatar

Igor Skrynkovskyy h2rd

  • Route4Me
  • Lviv, Ukraine
View GitHub Profile
@h2rd
h2rd / gist:2365677
Created April 12, 2012 08:48
Алгоритм генерації стрічок 1 | 11 | 21 | 1211 | 111221 etc
<?php
class Generator {
private $start = '1';
function __construct($start = null) {
if ($start != null) {
$this->start = (string) $start;
}
}
@h2rd
h2rd / gist:4133295
Created November 22, 2012 23:20
inheritance on python
class A:
def __init__(self):
print(u'конструктор класса A')
class B(A):
def __init__(self):
print(u'конструктор класса B')
A.__init__(self)
@h2rd
h2rd / gist:4143502
Created November 25, 2012 13:32
make GET query string from object
exports.serialize = function (obj) {
var str = [];
for (var p in obj) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
};
@h2rd
h2rd / gist:4147665
Created November 26, 2012 11:03
Get current directory
from os import path
print path.dirname(path.realpath(__file__))
@h2rd
h2rd / gist:4148450
Created November 26, 2012 14:19
Encode string to utf8
import chardet
def encode(string):
if not string:
return ''
if not isinstance(string, unicode):
encoding = chardet.detect(string)
if not encoding['encoding']:
@h2rd
h2rd / gist:4148690
Created November 26, 2012 15:08
terminate application
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
import time
import signal
def termination_handler(signum, frame):
global running
@h2rd
h2rd / gist:4194177
Created December 3, 2012 10:52
mongo get avg cities from NY and CA
use test
db.zips.aggregate({
$group : {
_id : { state: '$state', city: '$city'}
, totalPop : { $sum: '$pop' }
}
},{
$match : {
totalPop: {$gte : 25000},
@h2rd
h2rd / gist:4194243
Created December 3, 2012 11:11
mongo get total population where the city starts with a digit
use test
db.zips.aggregate([
{ $project: {
first_char: {$substr : ["$city",0,1]},
pop: 1
}
}, { $match: {
first_char: /[0-9]/
}
@h2rd
h2rd / gist:4194282
Created December 3, 2012 11:20
Get top author from array in mongo
use blog
db.posts.aggregate([
{ $unwind: "$comments" }
, { $group: {
_id: "$comments.author"
, total: {$sum: 1}
}
}
, { $sort: { total: -1 } }
@h2rd
h2rd / gist:4194415
Created December 3, 2012 11:39
Mongo get grader class in the campus
use test
db.grades.aggregate([
{ $unwind: "$scores" }
, { $group: {
_id: {
type: "$scores.type"
, score: "$scores.score"
, class_id: "$class_id"
}