Skip to content

Instantly share code, notes, and snippets.

View bonfy's full-sized avatar
:octocat:
Nothing, but busy

Kai Chen bonfy

:octocat:
Nothing, but busy
View GitHub Profile
import time
import multiprocessing
def timerecord(func):
def wrapper(*args, **kwargs):
start = time.time()
func(*args, **kwargs)
end = time.time()
print 'COST: {}'.format(end - start)
return wrapper
@bonfy
bonfy / beautiful_idiomatic_python.md
Created December 7, 2016 14:06 — forked from JeffPaine/beautiful_idiomatic_python.md
Transforming Code into Beautiful, Idiomatic Python: notes from Raymond Hettinger's talk at pycon US 2013. The code examples and direct quotes are all from Raymond's talk. I've reproduced them here for my own edification and the hopes that others will find them as handy as I have!

Transforming Code into Beautiful, Idiomatic Python

Notes from Raymond Hettinger's talk at pycon US 2013 video, slides.

The code examples and direct quotes are all from Raymond's talk. I've reproduced them here for my own edification and the hopes that others will find them as handy as I have!

Looping over a range of numbers

for i in [0, 1, 2, 3, 4, 5]:
@bonfy
bonfy / uninstall_homebrew.sh
Created January 6, 2017 12:58 — forked from mxcl/uninstall_homebrew.sh
Uninstall Homebrew
#!/bin/sh
# Just copy and paste the lines below (all at once, it won't work line by line!)
# MAKE SURE YOU ARE HAPPY WITH WHAT IT DOES FIRST! THERE IS NO WARRANTY!
function abort {
echo "$1"
exit 1
}
set -e
@bonfy
bonfy / processify.py
Created September 13, 2017 01:41 — forked from schlamar/processify.py
processify
import os
import sys
import traceback
from functools import wraps
from multiprocessing import Process, Queue
def processify(func):
'''Decorator to run a function as a process.
Be sure that every argument and the return value
@bonfy
bonfy / multi_inheritance.py
Created October 19, 2017 05:41
Multi Inheritance in Python3
# coding: utf-8
# [<class '__main__.T_1'>, <class '__main__.B'>, <class '__main__.A'>, <class '__main__.C'>, <class 'object'>]
# do this by A
# [<class '__main__.T_2'>, <class '__main__.B'>, <class '__main__.D'>, <class '__main__.A'>, <class 'object'>]
# do this by D
# 先深度优先,如果有实现 method 就返回了, 如果是两个继承自同一个父类, 那这个父类之前会遍历它所有的子类 再去查找它
@bonfy
bonfy / example.go
Created December 6, 2017 01:35 — forked from linxlad/example.go
simple DRY controller. golang github.com/labstack/echo example
package main
import (
"fmt"
"net/http"
"reflect"
"strconv"
"github.com/jinzhu/gorm"
"github.com/labstack/echo"
@bonfy
bonfy / bobp-python.md
Created February 1, 2018 13:09 — forked from sloria/bobp-python.md
A "Best of the Best Practices" (BOBP) guide to developing in Python.

The Best of the Best Practices (BOBP) Guide for Python

A "Best of the Best Practices" (BOBP) guide to developing in Python.

In General

Values

  • "Build tools for others that you want to be built for you." - Kenneth Reitz
  • "Simplicity is alway better than functionality." - Pieter Hintjens
@bonfy
bonfy / timer.py
Created February 12, 2018 02:23
Timer
# timer.py
import time
# Decorator function that prints the time that a function takes to run.
def timedFunction(func):
# The wrapper function will take whatever arguments the original does.
def wrapper(*args, **kwargs):
# Call the function we're decorating, recording its start and end time.
start = time.time()
@bonfy
bonfy / async_example.py
Created April 26, 2018 02:44
async example modify a global dict
# coding: utf-8
import asyncio
g = {'num': 1}
async def add(g, num=1):
print(f'origin {num} from', g['num'])
await asyncio.sleep(10)
print(f'do add {num} from', g['num'])
@bonfy
bonfy / golang-tls.md
Created July 24, 2018 07:11 — forked from denji/golang-tls.md
Simple Golang HTTPS/TLS Examples

Moved to git repository: https://github.com/denji/golang-tls

Generate private key (.key)
# Key considerations for algorithm "RSA" ≥ 2048-bit
openssl genrsa -out server.key 2048

# Key considerations for algorithm "ECDSA" ≥ secp384r1
# List ECDSA the supported curves (openssl ecparam -list_curves)