Skip to content

Instantly share code, notes, and snippets.

View kitek's full-sized avatar
🏠
Working from home

Marcin Kitowicz kitek

🏠
Working from home
View GitHub Profile
@kitek
kitek / gist:1388195
Created November 23, 2011 08:34
jQuery.post i dodatkowe parametry
var doSomething = function(extraStuff) {
return function(data, textStatus) {
//do something with extraStuff
};
};
var clicked = function() {
var extraStuff = { myParam1: 'foo', myParam2: 'bar' };
$.post("someurl.php", someData, doSomething(extraStuff), "json");
};
@kitek
kitek / sort.php
Last active January 3, 2016 22:16
Sortowanie tablicy wielowymiarowej
<?
// Proste sortowanie tablicy wielowymiarowej w obiekcie
// (w tym przypadku sortujemy malejąco po wymiarze 'similar')
class klasa {
public sortuj() {
usort($tablicaDoSortowania, array('klasa','compare'));
}
public function compare($a, $b) {
if ($a['similar'] == $b['similar']) {
@kitek
kitek / gist:1388261
Created November 23, 2011 09:25
Prosty parser linków
<?
// Bardzo prosty parser linków
$html = preg_replace(
'#(([^ \'\"\<]+)?[^ \'\"\.\<]+\.(pl|com|net|org)(\/[^ \'\"\<]+)?)#',
'<a href="http://$1" target="_blank" rel="nofollow">$1</a>',
$html
);
@kitek
kitek / gist:1388267
Created November 23, 2011 09:26
Generator losowej godziny
<?
// Prosty generator losowej godziny
function getRandomHour() {
$godzina = str_pad(rand(6,23), 2, "0", STR_PAD_LEFT);
$minuta = str_pad(rand(0,59), 2, "0", STR_PAD_LEFT);
$sekunda = str_pad(rand(0,59), 2, "0", STR_PAD_LEFT);
return $godzina .':'. $minuta .':'. $sekunda;
}
@kitek
kitek / gist:1388299
Created November 23, 2011 09:49
Generator losowych adresów email
<?
$emails = array();
function getRandomEmail() {
global $emails;
if (empty($emails)) {
$f = file_get_contents('http://ktmatu.com/cgi-bin/rea.pl');
preg_match_all('/\=\"mailto\:(.*)\"/', $f, $matches);
$emails = $matches[1];
@kitek
kitek / gist:1392242
Created November 24, 2011 20:44
Zliczanie elementów w GAE
from botModels import Roster, PresenceStatus
import logging as console
# Zliczanie elementów w GAE
# SELECT count(1) FROM PresenceStatus WHERE online = True
p = PresenceStatus.all(keys_only=True)
p.filter("online =",True)
items = p.count()
console.info(items)
@kitek
kitek / gist:1392254
Created November 24, 2011 20:50
Szukanie po kluczu w GAE
# Szukanie po kluczu w GAE
p = PresenceStatus.get_by_key_name(jid)
console.info(p.online)
@kitek
kitek / gist:1392334
Created November 24, 2011 21:32
Short if in Python
# Short if in Python
usersOnline = p.count() - 1 if p.count() > 0 else 0
@kitek
kitek / gist:1397814
Created November 27, 2011 16:55
GAE NamedModel - własny klucz w modelu
class NamedModel(db.Model):
"""A Model subclass for entities which automatically generate their own key
names on creation. See documentation for _generate_key function for
requirements."""
def __init__(self, *args, **kwargs):
kwargs['key_name'] = _generate_key(self, kwargs)
super(NamedModel, self).__init__(*args, **kwargs)
def _generate_key(entity, kwargs):
"""Generates a key name for the given entity, which was constructed with
@kitek
kitek / gist:1400187
Created November 28, 2011 12:13
NODE.JS Wysyłanie HTTP POST
var http = require('http');
exports.sendPost = function(options,data,callback) {
var post_data = querystring.stringify(data);
var opts = {
host: options.host,
port: options.port,
maxSockets : 100,
path: options.path,
method: 'POST',