Skip to content

Instantly share code, notes, and snippets.

View HugoPresents's full-sized avatar

Hugo Zhang HugoPresents

View GitHub Profile
@HugoPresents
HugoPresents / reboot_tplink.sh
Created March 24, 2016 17:56
curl reboot tp-link
@HugoPresents
HugoPresents / set_cookiejar.go
Created December 29, 2014 08:37
golang set cookieJar example
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
)
@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
@HugoPresents
HugoPresents / picture.go
Last active June 26, 2018 09:28
get & save picture size, width and height
package main
import (
"database/sql"
"flag"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/go-yaml/yaml"
"image"
_ "image/gif"
@HugoPresents
HugoPresents / com.chinadns.startup.plist
Created April 7, 2016 07:54
china DNS LaunchDaemon
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>KeepAlive</key>
<true/>
<key>Label</key>
<string>com.chinadns.startup</string>
<key>ProgramArguments</key>
<array>
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: