This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding:UTF-8 | |
import sys | |
import traceback | |
def a(): | |
print("a") | |
b() | |
def b(): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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, {}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=UTF-8 | |
import types | |
# multi-line string | |
def multi_line_str(): | |
s = ('select * ' | |
'from atable ' | |
'where afiled="value"') | |
print(s) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=UTF-8 | |
class Engineer(object): | |
def __init__(self, name, specialty): | |
self.name = name | |
self.specialty = specialty | |
def __repr__(self): | |
# __repr__ is for developers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=UTF-8 | |
class User(object): | |
def __init__(self, id): | |
self.id = id | |
def __nonzero__(self): | |
return self.id not in (None, 0) |