Skip to content

Instantly share code, notes, and snippets.

View HugoPresents's full-sized avatar

Hugo Zhang HugoPresents

View GitHub Profile
@HugoPresents
HugoPresents / signal.py
Created November 13, 2013 13:28
limit execution time of a function call in Python
from __future__ import with_statement
import signal, time
from contextlib import contextmanager
def long_function_call():
while True:
if time.time() % 1 == 0:
print '*'
class TimeoutException(Exception): pass
jQuery.fn.filterByText = function(textbox, selectSingleMatch) {
return this.each(function() {
var select = this;
var options = [];
$(select).find('option').each(function() {
options.push({value: $(this).val(), text: $(this).text(), disabled:$(this).is(':disabled')});
});
$(select).data('options', options);
$(textbox).bind('change keyup', function() {
var options = $(select).empty().scrollTop(0).data('options');
@HugoPresents
HugoPresents / bcrypt.py
Created June 24, 2013 18:29
Python generate bcrypt & check
import bcrypt
def generate_password(str):
return bcrypt.hashpw(str, bcrypt.gensalt())
def check_password(str, stored_hash):
hash_check = bcrypt.hashpw(str, stored_hash)
return hash_check == stored_hash
@HugoPresents
HugoPresents / is_ajax.py
Created June 24, 2013 18:26
Django determine the request method is ajax?
def is_ajax(request):
try:
if request.META['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest':
return True
else:
return False
except KeyError:
return False
@HugoPresents
HugoPresents / upper2lower.py
Created June 24, 2013 18:23
Python upper to lower with underline
def upper2lower(string):
string1 = ''
start = 0
for m in re.finditer('[A-Z]', string):
if m.start() != 0:
string1 += string[start+1:m.start()] + '_' + m.group(0).lower()
else:
string1 += string[start:m.end()].lower()
start = m.start()
string1 += string[start+1:]
@HugoPresents
HugoPresents / time2duration.py
Last active December 18, 2015 22:09
Python time stamp to duration
import time
from math import ceil
def time2duration(stamp):
# 对当前时间向上取整,防止出现负数~
stamp = int(ceil(stamp))
cur_time = int(ceil(time.time()))
during = cur_time - stamp
if during < 30:
@HugoPresents
HugoPresents / print_vars.php
Last active December 18, 2015 22:09
PHP print vars with <pre>
<?php
function print_vars() {
$vars = func_get_args();
echo '<pre>';
foreach ($vars as $var) {
ob_start();
var_dump($var);
echo htmlspecialchars(ob_get_clean());
}
echo '</pre>';