Skip to content

Instantly share code, notes, and snippets.

View dawranliou's full-sized avatar

Daw-Ran Liou dawranliou

View GitHub Profile
@dawranliou
dawranliou / mailprank.py
Created September 16, 2016 23:40
Office pranks
import smtplib
import email.utils
from email.mime.text import MIMEText
from collections import namedtuple
Prankee = namedtuple('Prankee', 'name id email')
colleague = Prankee('Colleague', 'colleague_id', 'colleague@email.com')
subject = '[IT Information] %s we detect unauthorized actions from your account' % colleague.name
body = '''Dear user %s (%s),
@dawranliou
dawranliou / takemehome.py
Created September 17, 2016 01:52
The program to check the traffic for my way home
#!python
import webbrowser
import os
destination = os.environ.get('HOME_ADDR')
destination = '+'.join(destination.split())
origin = os.environ.get('OFFICE_ADDR')
origin = '+'.join(origin.split())
url = 'https://www.google.com/maps/dir/%s/%s' % (origin, destination)
@dawranliou
dawranliou / yelp-api.py
Created September 21, 2016 07:27
Example to use Yelp Fusion API
import os
import requests
app_id = os.environ['APP_ID']
app_secret = os.environ['APP_SECRET']
data = {'grant_type': 'client_credentials',
'client_id': app_id,
'client_secret': app_secret}
token = requests.post('https://api.yelp.com/oauth2/token', data=data)
access_token = token.json()['access_token']
@dawranliou
dawranliou / mortgage_calculator.py
Created October 13, 2016 02:44
Simple program to calculate monthly payment and total payment
import argparse
def total_payment(principal, monthly_rate, payment_months):
return principal * (1 + monthly_rate) ** payment_months
def monthly_payment(principal, monthly_rate, payment_months):
weighted_month = ((1 + monthly_rate) ** payment_months - 1) / monthly_rate
return total_payment(principal, monthly_rate, payment_months) / weighted_month
print('--------------------------------------------')
@dawranliou
dawranliou / binary-tree-traverse.py
Created December 7, 2016 01:25
Practice Python's yield from expression
class Node:
def __init__(self, v):
self.v = v
self.l = None
self.r = None
def __repr__(self):
return '<Node %d>' % self.v
def children(node):
@dawranliou
dawranliou / projecteuler.py
Created January 8, 2017 18:05
My utility functions to help me solve Project Euler problems
"Utilities to help solving problems."
import itertools
import functools
from collections import Counter
from operator import mul, pow
def prime_factors(num):
""" Yield the factors of a given number."""
i = 2
while i * i <= num:
@dawranliou
dawranliou / ping_port.py
Created January 23, 2017 23:30
Application pinger
import socket
def ping(host, port):
"""A generator to ping a particular application.
Return True if the destination port is occupied
Example:
>>> # ping application at port 99
>>> pinger = ping('127.0.0.1', 99)
>>> # When there's no application ruuning
from time import perf_counter
from array import array
from contextlib import contextmanager
@contextmanager
def timing(label: str):
t0 = perf_counter()
yield lambda: (label, t1 - t0)
t1 = perf_counter()
user=> (range 10)
(0 1 2 3 4 5 6 7 8 9)
>>> range(10)
range(0, 10)
# Use list() to evaluate the range object
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]