Skip to content

Instantly share code, notes, and snippets.

@socrateslee
socrateslee / functor.py
Created May 28, 2014 06:23
A simple wrapper make a class become a functor.
'''
A simple wrapper make a class become a functor,
the class need a __call__ function. The (*sub,
**kwargs) will be passed for the __init__ function,
and the name of class will be the name of function
object.
Example
-------
@functor(*sub, **kwargs)
@socrateslee
socrateslee / emacs_cmd_windows.el
Created March 13, 2013 10:10
Open the cmd in the current directory in Windows, place the gist in .emacs file.
;; cmd
;; ----------
;;; Open the cmd in the current directory.
(defun cmd ()
"Open the cmd in the current directory."
(interactive)
(w32-shell-execute
"open"
"cmd"))
@socrateslee
socrateslee / httplib2_remote_dns_proxy.py
Created March 26, 2013 05:31
Set remote dns for httplib2 proxy.
import httplib2
# set proxy_rdns=True to ensure use the dns on
# remote proxy, in case local DNS is pulluted.
proxy = httplib2.ProxyInfo(httplib2.socks.PROXY_TYPE_HTTP,
'localhost', 8000,
proxy_rdns=True)
http = httplib2.Http(proxy_info=proxy)
http.request('http://google.com')
@socrateslee
socrateslee / example.py
Created May 9, 2013 15:36
Inject a signal handler before the existed signal handler.
import signal
import time
from signal_inject import *
def dummy_handler(signum, frame):
print "In dummy handler"
inject_signal_handler(signal.SIGINT, dummy_handler)
while 1:
@socrateslee
socrateslee / btime.py
Created May 30, 2013 04:21
Get the Linux birth time of the system or a process using /proc/stat and /proc/pid/stat. For example, you may get local datetime when process 12345 started: import datetime import btime datetime.datetime.fromtimestamp(btime.process_time(12456)[1])
'''
Get the Linux birth time of the system or a process using /proc/stat
and /proc/pid/stat.
For example, you may get local datetime when process 12345 started:
import datetime
import btime
datetime.datetime.fromtimestamp(btime.process_time(12456)[1])
'''
import os
@socrateslee
socrateslee / compact_json_dumps.py
Last active September 15, 2017 14:32
pretty and compact json dumps for python
import re
import json
def compact_json_dumps(*sub, **kw):
'''
A wrapper of json.dumps support an optional compact_length parameter.
compact_length take effects when indent > 0, will return a more compact
indented result, i.e., pretty and compact json dumps.
'''
@socrateslee
socrateslee / script.js
Last active May 6, 2019 14:53
A Tampermonkey userscripts for better display of data.stats.gov.cn
// ==UserScript==
// @name better data.stats.gov.cn
// @namespace https://gist.github.com/socrateslee/69169c58be9cf2e0e3019b09b680461b
// @version 0.1
// @description Inject css for better display of data.stats.gov.cn
// @author https://github.com/socrateslee
// @include http://data.stats.gov.cn/easyquery.htm*
// @grant none
// ==/UserScript==
@socrateslee
socrateslee / python-dtw.py
Created August 5, 2012 16:25
Simple DTW(Dynamic time wrapping) in python
# -*- coding: utf-8 -*-
# An pure python implemetation of Dynamic Time Warpping
# http://en.wikipedia.org/wiki/Dynamic_time_warping
class Dtw(object):
def __init__(self, seq1, seq2, distance_func=None):
'''
seq1, seq2 are two lists,
distance_func is a function for calculating
@socrateslee
socrateslee / traced_obj.py
Created May 23, 2019 07:20
An object stores the change history of its attributes, the object can be use to store the hierarchies of different levels of configurations.
'''
An object stores the change history of its attributes,
the object can be use to store the hierarchies of different
levels of configurations.
Example
-------
# Set up config object
config = TracedObj()
@socrateslee
socrateslee / wider-yuque.js
Last active January 9, 2020 11:23
Wider Yuque
// ==UserScript==
// @name Wider Yuque
// @namespace https://gist.github.com/socrateslee/751c6670c59d5b9cdbd8428fab493375
// @version 0.1
// @description Let yuque a bit wider on browser screen.
// @author github.com/socrateslee
// @include https://*.yuque.com/*
// @grant none
// ==/UserScript==