Skip to content

Instantly share code, notes, and snippets.

View simon-liu's full-sized avatar
🏠
Working from home

Zenglu Liu simon-liu

🏠
Working from home
View GitHub Profile
@simon-liu
simon-liu / ensure-dir-exists.py
Last active September 3, 2021 08:31
python ensure dir exists
def ensure_dir(f):
d = os.path.dirname(f)
if not os.path.exists(d):
os.makedirs(d)
return os.path.exists(f)
@simon-liu
simon-liu / client-ip.py
Last active July 3, 2017 03:26
python django get client ip
def get_client_ip(request):
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
ip = x_forwarded_for.split(',')[0]
else:
ip = request.META.get('REMOTE_ADDR')
return ip
@simon-liu
simon-liu / chunks.py
Last active July 3, 2017 03:26
python chunks
def chunks(l, n):
"""Yield successive n-sized chunks from l."""
for i in range(0, len(l), n):
yield l[i:i + n]
@simon-liu
simon-liu / log.py
Last active July 3, 2017 03:26
python config logging
import logging
def config_logging(
filename,
format='[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s',
datefmt='%d/%b/%Y %H:%M:%S',
level=logging.INFO):
logging.basicConfig(
filename=filename,
@simon-liu
simon-liu / command.py
Last active July 3, 2017 03:25
python run shell command
import subprocess
def run(cmd):
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
if err:
raise RuntimeError()
return output.decode('utf8').strip()
@simon-liu
simon-liu / sed-search-replace.sh
Last active December 28, 2019 04:13
sed search & replace
sed -i -- 's/hello/moto/g' **/*(D.)
sed -i 's/hello/moto/g' file.txt
find /home/bruno/old-friends -type f -exec sed -i 's/ugly/beautiful/g' {} \;
@simon-liu
simon-liu / sqlite3-connect-db.py
Created June 9, 2017 08:14
sqlite3 connect db
def connect_db(path):
con = sqlite3.connect(path)
con.row_factory = sqlite3.Row
return con
@simon-liu
simon-liu / get-user-home-dir.py
Last active July 3, 2017 03:25
python get user home dir
from os.path import expanduser
home = expanduser("~")
@simon-liu
simon-liu / block-operator-ad.user.js
Last active June 9, 2017 08:29
firefox Greasemonkey Add-ons script
// ==UserScript==
// @name block-operator-ad
// @namespace liuzenglu
// @description block-operator-ad
// @version 1
// @grant none
// ==/UserScript==
if (window.location.href.startsWith("http://")) {
var black_list = new Array(