Skip to content

Instantly share code, notes, and snippets.

View uliang's full-sized avatar

Tang U-Liang uliang

View GitHub Profile
@uliang
uliang / scraper.py
Last active December 11, 2022 14:08
Sample Web Scraping function
import re
import requests
from bs4 import BeautifulSoup
def scrape(max_visits=10_000):
seed, articles, visited = ['www.malaymail.com'], [], []
for _ in range(max_visits):
if not seed:
break
link = seed.pop(0)
@uliang
uliang / states.py
Last active February 8, 2024 23:03
Implementation of State Design Pattern in python
"""
Simple implementation of a finite state machine.
Author: Tang U-Liang
Date: 2 Oct 2020
"""
import attr
from enum import Enum
import inspect
@uliang
uliang / descriptors.py
Last active September 29, 2020 07:16
Illustration of customizing class creation using Python's descriptor protocol and metaclasses
from types import MethodType
class FunctionWrapper:
def __init__(self, func):
self.func = func
def __get__(self, obj, objtype=None):
if obj is None:
return self.func
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@uliang
uliang / text_preprocessing.py
Created August 10, 2020 02:47
A simple text filtering pipeline component in the spaCy style.
from typing import List, Dict, Union
from spacy.tokens import Doc, Token
from spacy.matcher import Matcher
from srsly import read_json
class FilterTextPreprocessing:
def __init__(self, nlp,
patterns: List[Dict[str, Union[str, List[Dict]]]]) :
Doc.set_extension('bow', default=[])
@uliang
uliang / curry.py
Last active August 6, 2020 04:03
An implementation of the curry decorator
"""
Implementation of a simple currying decorator
"""
from inspect import signature
def curry(f) :
def wrapper(*args, **kwds) :
sig = signature(f)
ba = sig.bind_partial(*args, **kwds)
@uliang
uliang / fizzy_order.py
Last active August 4, 2020 03:15
FizzBuzz solution 2
def gcd(m,n) :
m,n = sorted(abs(x) for x in (m,n))
while m:
m, n = n % m, m
return n
def ord(x, p) :
return p // gcd(x,p)
for i in range(1, 101) :
@uliang
uliang / fizzy_simple.py
Last active August 3, 2020 15:11
FizzBuzz solution 1
for i in range(1, 100) :
if not any(i%5, i%3) :
print("FizzBuzz")
elif not bool(i%5) :
print("Buzz")
elif not bool(i%3) :
print("Fizz")
else:
print(i)
@uliang
uliang / machine.js
Last active October 16, 2019 02:40
Generated by XState Viz: https://xstate.js.org/viz
const Application = Machine({
id: 'app',
initial: 'ready',
states: {
ready: {
initial: 'ok',
on: {
NewGame: "characterCreation",
LoadGame: 'loadCharacter'
},
@uliang
uliang / machine.js
Last active October 15, 2019 08:41
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions