Skip to content

Instantly share code, notes, and snippets.

View michaelfeng's full-sized avatar
💭
Be Happy~

michaelfeng michaelfeng

💭
Be Happy~
View GitHub Profile
@michaelfeng
michaelfeng / reset_routing_table.sh
Created August 10, 2019 09:36 — forked from midwire/reset_routing_table.sh
Reset routing table on OSX
#!/usr/bin/env bash
# Reset routing table on OSX
# display current routing table
echo "********** BEFORE ****************************************"
netstat -r
echo "**********************************************************"
for i in {0..4}; do
sudo route -n flush # several times
@michaelfeng
michaelfeng / bfs_java
Last active October 7, 2018 09:03
BFS_template_java
// T 指代任何你希望存储的类型
Queue<T> queue = new LinkedList<>();
Set<T> set = new HashSet<>();
set.add(start);
queue.offer(start);
while (!queue.isEmpty()) {
T head = queue.poll();
for (T neighbor : head.neighbors) {
if (!set.contains(neighbor)) {
@michaelfeng
michaelfeng / python_pipeline.py
Created May 26, 2018 07:10
shell风格的python pipeline
class Pipe(object):
def __init__(self, func):
self.func = func
def __ror__(self, other):
def generator():
for obj in other:
if obj is not None:
yield self.func(obj)
return generator()
@michaelfeng
michaelfeng / df2json.py
Created April 19, 2018 16:39 — forked from adamgreenhall/df2json.py
Convert pandas.DataFrame to JSON (and optionally write the JSON blob to a file).
"""
tiny script to convert a pandas data frame into a JSON object
"""
import json as json
def df_to_json(df, filename=''):
x = df.reset_index().T.to_dict().values()
if filename:
with open(filename, 'w+') as f: f.write(json.dumps(x))
@michaelfeng
michaelfeng / backgroundAveraging.py
Created April 11, 2018 15:45 — forked from TheSalarKhan/backgroundAveraging.py
Background Averaging (Background Subtraction) in Python+OpenCV
import numpy as np
import cv2
class BackGroundSubtractor:
# When constructing background subtractor, we
# take in two arguments:
# 1) alpha: The background learning factor, its value should
# be between 0 and 1. The higher the value, the more quickly
# your program learns the changes in the background. Therefore,
@michaelfeng
michaelfeng / segmentation1.py
Created April 11, 2018 11:02 — forked from Munawwar/segmentation1.py
Background Removal with OpenCV - Attempt 1 (http://codepasta.com/site/vision/segmentation/)
import numpy as np
import cv2
def getSobel (channel):
sobelx = cv2.Sobel(channel, cv2.CV_16S, 1, 0, borderType=cv2.BORDER_REPLICATE)
sobely = cv2.Sobel(channel, cv2.CV_16S, 0, 1, borderType=cv2.BORDER_REPLICATE)
sobel = np.hypot(sobelx, sobely)
return sobel;
@michaelfeng
michaelfeng / karatsuba.py
Created March 26, 2018 01:24 — forked from anirudhjayaraman/karatsuba.py
Karatsuba Multiplication
def karatsuba(x,y):
"""Function to multiply 2 numbers in a more efficient manner than the grade school algorithm"""
if len(str(x)) == 1 or len(str(y)) == 1:
return x*y
else:
n = max(len(str(x)),len(str(y)))
nby2 = n / 2
a = x / 10**(nby2)
b = x % 10**(nby2)
@michaelfeng
michaelfeng / app.py
Created November 21, 2017 16:51 — forked from jmvrbanac/app.py
Using Jinja2 with Falcon
import os
import falcon
import jinja2
def load_template(name):
path = os.path.join('templates', name)
with open(os.path.abspath(path), 'r') as fp:
return jinja2.Template(fp.read())
@michaelfeng
michaelfeng / gist:5282469491a7d38f9cd635d7a7c0fbbd
Created November 21, 2017 10:27 — forked from tebeka/gist:5426211
Serving dynamic images with Pandas and matplotlib (using flask)
#!/usr/bin/env python2
'''Serving dynamic images with Pandas and matplotlib (using flask).'''
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from cStringIO import StringIO
@michaelfeng
michaelfeng / nginx.conf
Created July 14, 2017 07:38 — forked from plentz/nginx.conf
Best nginx configuration for improved security(and performance). Complete blog post here http://tautt.com/best-nginx-configuration-for-security/
# to generate your dhparam.pem file, run in the terminal
openssl dhparam -out /etc/nginx/ssl/dhparam.pem 2048