Skip to content

Instantly share code, notes, and snippets.

View hustshawn's full-sized avatar
🎯
Focusing

Shawn Zhang hustshawn

🎯
Focusing
  • AWS
  • Hong Kong
View GitHub Profile
@hustshawn
hustshawn / pep20_by_example.py
Created February 29, 2016 02:10 — forked from evandrix/pep20_by_example.py
PEP 20 (The Zen of Python) by example
#!/usr/bin/env python
"""
=====================================
PEP 20 (The Zen of Python) by example
=====================================
Usage: %prog
:Author: Hunter Blanks, hblanks@artifex.org / hblanks@monetate.com
@hustshawn
hustshawn / file-copy-stream.js
Last active December 5, 2016 07:45
Copy large file with node stream
const fs = require('fs');
var rs = fs.createReadStream(src);
var ws = fs.createWriteStream(dst);
rs.on('data', function (chunk) {
if (ws.write(chunk) === false) {
rs.pause();
}
});
/*
A directory tree like below.
A
/ \
B C
/ \ \
D E F
The access order of deep first and pre-order traversal algorithm should be :
@hustshawn
hustshawn / observer-pattern-demo.py
Last active December 6, 2016 07:46
The observer pattern implementation demo with Python
class Observable:
def __init__(self, *args, **kwargs):
self.__observers = []
def register_observer(self, observer):
self.__observers.append(observer)
def notify_observers(self, *args, **kwargs):
for observer in self.__observers:
observer.notify(self, *args, **kwargs)
@hustshawn
hustshawn / django_signal.py
Created December 7, 2016 02:27
A django signal working flow
from dispatch import Signal
# The Signal object (think of it as an exchange)
log_it = Signal(providing_args=['level', 'message'])
# These functions are receivers (subscribers). They will receive
# the signal (message) sent (published) by the sender (publisher).
def simple_receiver(**kwargs):
message, level = kwargs['message'], kwargs['level']
print 'Receiver # 1'
@hustshawn
hustshawn / observable.js
Created December 8, 2016 13:27
It shows how Observable in RxJS works.
let o = new Observable(observer => {
const token = doAsyncThing((err, value) => {
if (err) {
observer.error(err);
} else {
observer.next(value);
observer.complete();
}
});
return () => {
@hustshawn
hustshawn / rxjs_operators_by_example.md
Created December 12, 2016 02:40 — forked from btroncone/rxjs_operators_by_example.md
RxJS 5 Operators By Example
@hustshawn
hustshawn / email_sender.py
Created December 12, 2016 09:59
Sending email with plain python
import os
import smtplib
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def main():
#========================================================================
#
# Sample xpdfrc file
#
# The Xpdf tools look for a config file in two places:
# 1. ~/.xpdfrc
# 2. in a system-wide directory, typically /usr/local/etc/xpdfrc
#
# This sample config file demonstrates some of the more common
# configuration options. Everything here is commented out. You