Skip to content

Instantly share code, notes, and snippets.

@acmerfight
acmerfight / encapsulation.py
Last active September 19, 2018 01:45
encapsulation_refactor.py
from datetime import date
from datetime import date
class Person(object):
def __init__(self, birth_day, sex, children):
@acmerfight
acmerfight / go
Last active November 9, 2018 02:54
golang cache decrator
package main
import (
"fmt"
"reflect"
"runtime"
)
func Decorator(decoPtr, fn interface{}) (err error) {
var decoratedFunc, targetFunc reflect.Value
@acmerfight
acmerfight / template_method.go
Created March 25, 2018 12:54
Golang template method
package main
import "fmt"
type Processor interface {
step1()
step2()
step3()
}
@acmerfight
acmerfight / MontyHallGame.py
Created September 20, 2017 10:43
MontyHallGame
import random
class MontyHallGame(object):
CAR = "car"
GOAT = "goat"
def __init__(self, attempts, changed):
self.attempts = attempts
self.doors = {}
@acmerfight
acmerfight / batch_lru.py
Created October 27, 2016 07:03
batch lru cache
class BatchLruCache(object):
def __init__(self, maxsize, timeout):
self.maxsize = maxsize
self.timeout = timeout
self.cache = OrderedDict()
self.Omitted_object = OmittedType()
self.is_full = False
def __call__(self, func):
@acmerfight
acmerfight / gist:f7c87404e427a031f6fa
Created December 2, 2014 05:43
过滤 html 标签
import re
str = "<img /><a>srcd</a>hello</br><br/>"
str = re.sub(r'</?\w+[^>]*>','',str)
print str
@acmerfight
acmerfight / 注重实效的偏执
Last active August 29, 2015 14:08
注重实效的偏执
1. You Can't Write Perfect Software.
2. Design with Constracts. (可以使用测试来做到这一点)
3. 尽快崩溃,更容易发现代码的问题。
4. 如果有一个 bug,哪怕是很小的一个类型没有检查,就说明糟糕的事情已经发生,是程序员本身思考的不够全面。
5. Crash Early.
6. 死程序带来的危害通常比带有疾患的程序要小得多。
7. If It Can't Happen, Use Assertions to Ensure That It Won't.
8. 不要用断言代替真正地错误处理(例如异常)。断言检查的是绝不应该发生的事情。
@acmerfight
acmerfight / 基本工具
Last active August 29, 2015 14:08
基本工具
1. Use the Power of Command Shells.
2. Use a Single Editor Well.
3. Always Use Source Code Control.
4. Fix the Problem, Not the Blame.
5. 设法找出问题的根源,而不是问题的特定表现。
6. 需要与报告 bug 的人面谈,以搜集更多的数据。
7. 充分地测试边界条件,又测试现实中最终用户的使用模式。
8. 出 bug 时,不要首先就怀疑是第三方库或者系统的问题。
#coding=utf-8
import jieba
import numpy as np
from sklearn.svm import *
from numpy import *
def load_data(file_name):
@acmerfight
acmerfight / 《程序员修炼之道》--注重实效的途径
Last active August 29, 2015 14:07
《程序员修炼之道》--注重实效的途径
1. 系统中的每一项知识都必须具有单一,无歧义,权威的表示。
2. 无耐性的重复是一种容易检测和处理的重复方式,但是需要接受训练,并愿意为以后的痛苦而预先花一些时间。
3. 避免开发者之间的重复一定要要不断地交流和确认。
4. Eliminate Effects Between Unrelated Things. (消除无关事物之间的影响,正交性,解耦)
5. 编写正交性系统的好处:提高生产率和降低风险。
6. 项目团队要有正交性,职责明确。检查下发生改动时需要涉及多少人,人数越多,团队正交性越差。
7. 编写代码时不要依赖你无法控制的事物的属性。
8. 让代码保持解耦,避免使用全局数据。