Skip to content

Instantly share code, notes, and snippets.

View demonkit's full-sized avatar
🎯
Focusing

demonkit demonkit

🎯
Focusing
View GitHub Profile
@demonkit
demonkit / timeout-with.py
Created September 17, 2018 10:14
Run codes under timeout control. Modified based on: https://stackoverflow.com/a/22348885/2236070
import signal
class TimeoutError(Exception):
pass
class TimeoutHandler(object):
def __init__(self, seconds=60, error_message='Timeout'):
self.seconds = seconds
@demonkit
demonkit / requests-dns.py
Last active August 15, 2018 11:52
Specify DNS server for requests.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from requests import Session
from requests.adapters import HTTPAdapter, DEFAULT_POOLSIZE, DEFAULT_RETRIES, DEFAULT_POOLBLOCK
class DNSResolverHTTPSAdapter(HTTPAdapter):
def __init__(self,
common_name,
# -*- coding: utf-8 -*-
"""
Get images from http://www.douban.com/photos/album/145486923/.
"""
__author__ = 'demonkit'
import re
import threading
@demonkit
demonkit / dijkstra.py
Created October 30, 2014 14:15
python implementation of dijkstra shortest path.
# -*- coding: utf-8 -*-
def dijkstra(graph, source=0):
nodes = dict.fromkeys(range(len(graph)), 0)
# all visited nodes, source is the first entry.
visited = {source: 0}
# previous node dict
@demonkit
demonkit / lcs_length.py
Created September 6, 2014 09:47
compute the lcs length of 2 sequences.
import numpy
def lcs_length(X, Y):
m, n = len(X), len(Y)
min_len = min(m, n)
max_len = max(m, n)
C = numpy.zeros(shape=(2, min_len+1))
if m == min_len:
min_list = X
max_list = Y
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import threading
class Node(object):
def __init__(self,
key, value,
pre=None, next=None):