Skip to content

Instantly share code, notes, and snippets.

View drhanlau's full-sized avatar
💭
Working on AI projects

Dr. Lau Cher Han drhanlau

💭
Working on AI projects
View GitHub Profile
SELECT
regexp_extract(col_value, '^(?:([^,]*)\,?){1}', 1) player_id,
regexp_extract(col_value, '^(?:([^,]*)\,?){2}', 1) year,
regexp_extract(col_value, '^(?:([^,]*)\,?){9}', 1) run
from temp_batting;
class Borg(object):
_shared_state = {}
def __new__(cls, *args, **kwargs):
obj = super(Borg, cls).__new__(cls, *args, **kwargs)
obj.__dict__ = cls._shared_state
return obj
class Child(Borg):
pass
@drhanlau
drhanlau / duck_typing.py
Created March 2, 2016 08:26
Duck Typing
class Duck:
def quack(self):
print("Quaaaaaack!")
def feathers(self):
print("The duck has white and gray feathers.")
class Person:
def quack(self):
print("The person imitates a duck.")
def feathers(self):
@drhanlau
drhanlau / lambda_test.py
Last active March 3, 2016 06:17
Lambda testing
for x in range(0,101):
if (lambda i : not i % 2 and not i % 3 and not i % 4 and not i % 5 and not i % 6)(x):
print x
@drhanlau
drhanlau / book.py
Last active March 4, 2016 07:30
Main part of prototype_pattern.py
class Book:
def __init__(self, name, authors, price, **rest):
'''Examples of rest: publisher, length, tags, publication date'''
self.name = name
self.authors = authors
self.price = price
self.__dict__.update(rest)
def __str__(self):
mylist=[]
@drhanlau
drhanlau / img_download.py
Created March 3, 2016 07:15
ImageDownloader
import urllib
class ImageDownloader():
def download(self, filename):
urllib.urlretrieve(self.url, filename)
img = ImageDownloader()
img.url = "http://i.imgur.com/Ph4Xw.jpg"
img.download("duck.jpg")
@drhanlau
drhanlau / FileServer.py
Created March 3, 2016 14:09
File Server of Facade Pattern
class FileServer(Server):
def __init__(self):
'''actions required for initializing the file server'''
self.name = 'FileServer'
self.state = State.new
def boot(self):
print('booting the {}'.format(self))
'''actions required for booting the file server'''
self.state = State.running
@drhanlau
drhanlau / client_code.py
Last active April 7, 2021 18:27
Code for Factory Method
if __name__ == '__main__':
domain = 'ftp.freebsd.org'
path = '/pub/FreeBSD/'
protocol = input('Connecting to {}. Which Protocol to use? (0-http,1-ftp): '.format(domain))
if protocol == 0:
is_secure = bool(input('Use secure connection? (1-yes, 0-no): '))
connector = HTTPConnector(is_secure)
else:
is_secure = False
@drhanlau
drhanlau / age_validator.py
Created March 4, 2016 03:10
Code Snippets for Abstract Factory
def validate_age(name):
try:
age = raw_input('Welcome {}. How old are you? '.format(name))
age = int(age)
except ValueError as err:
print("Age {} is invalid, please try again...".format(age))
return (False, age)
return (True, age)
@drhanlau
drhanlau / main.py
Last active May 27, 2018 18:17
Observer
if __name__ == "__main__":
stock_watcher = Observable()
american_observer = AmericanStockMarket()
stock_watcher.register(american_observer)
stock_watcher.publish('KLSE Market Update', price='+0.5')