Skip to content

Instantly share code, notes, and snippets.

from django.core.paginator import ObjectPaginator
class QuickObjectPaginator(ObjectPaginator):
max_safe_pages = 0
def __init__(self, object_list, per_page, orphans=0, max_safe_pages=0):
self.max_safe_pages = max_safe_pages
super(QuickObjectPaginator, self).__init__(object_list, per_page, orphans)
def validate_page_number(self, page_number):
@jrivero
jrivero / HackerNewsRank.py
Last active February 21, 2024 11:39
Hacker News ranking algorithm
# From http://amix.dk/blog/post/19574
def calculate_score(votes, item_hour_age, gravity=1.8):
return (votes - 1) / pow((item_hour_age+2), gravity)
<?php
class GenericVariable
{
private $var;
function __construct($variable)
{
$this->var = $variable;
}
function __get($var)
@jrivero
jrivero / Autokill.php
Last active February 21, 2024 11:39
Kill process if is modified
<?php
class Autokill {
var $start_mtime_included_files = array();
function Autokill() { $this->__construct(); }
function __construct() {
clearstatcache();
$files = get_included_files();
@jrivero
jrivero / control_daemon_repeat.php
Last active February 21, 2024 11:39
Control repeat of daemon
<?php
function control_daemon_repeat() {
$ps = "/bin/ps aux | /bin/grep -i ".$_SERVER["argv"][0]." | /bin/grep -v grep | wc -l";
$exec = exec($ps, $output, $return);
if ($output[0] > 1) die("*** Ya esta en ejecucion\n");
}
// Example
control_daemon_repeat();
@jrivero
jrivero / jsCookies.js
Last active February 21, 2024 11:36
Pure Javascript Cookies Management
// found on http://snipplr.com/view/36790/jscookies--my-simple-easy-pure-js-javascript-cookies-function/
// create my jsCookies function
var jsCookies = {
// this gets a cookie and returns the cookies value, if no cookies it returns blank ""
get: function(c_name) {
if (document.cookie.length > 0) {
var c_start = document.cookie.indexOf(c_name + "=");
if (c_start != -1) {
@jrivero
jrivero / csv_splitter.py
Created July 15, 2011 20:33 — forked from palewire/csv_splitter.py
A Python CSV splitter
import os
def split(filehandler, delimiter=',', row_limit=10000,
output_name_template='output_%s.csv', output_path='.', keep_headers=True):
"""
Splits a CSV file into multiple pieces.
A quick bastardization of the Python CSV library.
Arguments:
@jrivero
jrivero / versionIE.js
Created July 17, 2011 01:38 — forked from padolsey/gist:527683
A short snippet for detecting versions of IE in JavaScript without resorting to user-agent sniffing
// ----------------------------------------------------------
// A short snippet for detecting versions of IE in JavaScript
// without resorting to user-agent sniffing
// ----------------------------------------------------------
// If you're not in IE (or IE version is less than 5) then:
// ie === undefined
// If you're in IE (>=5) then you can determine which version:
// ie === 7; // IE7
// Thus, to detect IE:
// if (ie) {}
@jrivero
jrivero / uuid_pad.php
Last active February 21, 2024 11:35
Create uuid with specific dimensions
<?php
function uuid_pad($pad = 6)
{
return substr(md5(uniqid(rand(), true)), $pad, $pad);
}
// example
echo uuid_pad();
@jrivero
jrivero / gist:1387602
Created November 23, 2011 00:54 — forked from jsl/gist:1387560
SELECT DISTINCT message_id
FROM SELECT *, id AS message_id FROM user_messages WHERE user_id = ?
AND read = false AND parent_id IS NULL AS initial_messages
UNION
SELECT *, parent_id AS message_id FROM user_messages WHERE user_id = ?
AND read = false AND parent_id IS NOT NULL AS replies;