Skip to content

Instantly share code, notes, and snippets.

@EdisonChendi
EdisonChendi / document_distance.py
Last active April 9, 2018 15:02
calculate how different of two docs - word2vec
#coding:UTF-8
s1 = "my name is Hulk"
s2 = "my name is Iron man"
s3 = "your job is save the word"
from collections import defaultdict
import re
import math
from string import whitespace, punctuation, ascii_uppercase
@EdisonChendi
EdisonChendi / 1d_peak_finding.py
Created April 7, 2018 11:45
mit6006_fall2011_lesson1
#coding:UTF-8
from math import inf, floor
from time import time
from random import randint
def linear_find_peak_1d(l, start, end):
minus_inf = -1 * inf
l = [minus_inf] + l[start:end] + [minus_inf]
for i in range(1, len(l)+1):
@EdisonChendi
EdisonChendi / dot_operator_search_order.py
Created February 3, 2017 16:01
python dot operator search order
# Python . operator search order
# 1. if magic methods are used implicitly, those methods on object's type are used
# 2. __getattributes__(self, attr)
# 3. object's type's __dict__ and it's a data descriptor, return it
# 4. object's __dict__ and it's a data descriptor, return it
# 5. object's __dict__ then object's type's __dict__
# 6. __getattr__
# 7. raise AttributeError
@EdisonChendi
EdisonChendi / use_traceback.py
Created January 10, 2017 15:15
use traceback module to get more stackinfo when Exception occus
# coding:UTF-8
import sys
import traceback
def a():
print("a")
b()
def b():
@EdisonChendi
EdisonChendi / event.js
Last active January 6, 2017 06:50
A good example of using ES6 WeakMap
// https://github.com/hugeen/burst/blob/master/core/event.js
// http://stackoverflow.com/questions/29413222/what-are-the-actual-uses-of-es6-weakmap
// the main strength of WeakMap is that it does not interfere with garbage collection given that they do not keep a reference
var listenableMap = new WeakMap();
export function getListenable (object) {
if (!listenableMap.has(object)) {
listenableMap.set(object, {});
}
# coding=UTF-8
import operator
engineers = [{"name": "John", "age": 28}, {"name": "Peter", "age": 20}, {"name": "Paul", "age": 33}]
engineers_sorted_by_age = sorted(engineers, key=operator.itemgetter("age"))
print(engineers_sorted_by_age)
print(engineers)
engineers.sort(key=operator.itemgetter("age"), reverse=True)
print(engineers)
@EdisonChendi
EdisonChendi / master_str.py
Created December 9, 2016 15:38
mastering string manipulation is extremely important
# coding=UTF-8
import types
# multi-line string
def multi_line_str():
s = ('select * '
'from atable '
'where afiled="value"')
print(s)
@EdisonChendi
EdisonChendi / print_vs_repr.py
Created November 28, 2016 14:04
repr is for developers, print is for software users
# coding=UTF-8
class Engineer(object):
def __init__(self, name, specialty):
self.name = name
self.specialty = specialty
def __repr__(self):
# __repr__ is for developers
@EdisonChendi
EdisonChendi / default_params.py
Created November 27, 2016 08:25
don't use mutable types as default parameters
# coding=UTF-8
def append(newitem, l = []):
l.append(newitem)
print(l)
print(append.func_defaults) # []
append(10)
print(append.func_defaults) # [10]
append(20)
# coding=UTF-8
class User(object):
def __init__(self, id):
self.id = id
def __nonzero__(self):
return self.id not in (None, 0)