Skip to content

Instantly share code, notes, and snippets.

View turing4ever's full-sized avatar

Yi Wang turing4ever

View GitHub Profile
@turing4ever
turing4ever / click_colors.py
Last active January 7, 2019 04:02
Use click to colorize cli output
import click
all_colors = 'black', 'red', 'green', 'yellow', 'blue', 'magenta', \
'cyan', 'white', 'bright_black', 'bright_red', \
'bright_green', 'bright_yellow', 'bright_blue', \
'bright_magenta', 'bright_cyan', 'bright_white'
@click.command()
@turing4ever
turing4ever / validate_url.sh
Created January 3, 2019 06:16
bash script that validates a list of URLs using curl
cat new.txt | xargs -I{} sh -c 'printf {}; curl -s -o /dev/null -w " %{http_code}\n" {};' | grep -v 200
@turing4ever
turing4ever / gen_list_of_duplicate_items.py
Created August 19, 2018 05:18
Duplicate a list in python
# if e is immutable
[e] * n
# [e] is mutable
list_of_list = [[e] for _ in range(n)]
# if you do [e] * n, you will get n references to the same [e]
foo = [[]] *4
foo[0].append('x')
[['x'], ['x'], ['x'], ['x']]
@turing4ever
turing4ever / autoreload.txt
Created July 10, 2018 16:14
Enable Auto reload in ipython
$ ipython profile create
$ vi ~/.ipython/profile_default/ipython_config.py
c.InteractiveShellApp.extensions = [] to c.InteractiveShellApp.extensions = ['autoreload']
c.InteractiveShellApp.exec_lines = [] to c.InteractiveShellApp.exec_lines = ['%load_ext autoreload', '%autoreload 2']
@turing4ever
turing4ever / set_up_mongodb.sh
Created May 4, 2018 16:19
How to setup mongodb on Mac
# Install mongodb
brew update
brew install mongodb
mkdir -p /data/db
sudo chown -R `id -un` /data/db
# Get a sample databaes
wget http://media.mongodb.org/zips.json
mongoimport -v --file=zips.json #loads into test.zips
# if you want to specify a database and collection
# Get rid of `UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: http://initd.org/psycopg/docs/install.html#binary-install-from-pypi.`
pip uninstall psycopg2 -y
pip install --no-binary :all: psycopg2
# On Mac, you will most likely run into issue with ' ld: library not found for -lssl’. Then you will need to run `brew install openssl` and `export LIBRARY_PATH=/usr/local/Cellar/openssl/1.0.2o_1/lib`. Finally, run `pip install --no-binary :all: psycopg2`
-- EU is identified by the country code list: https://dev.maxmind.com/geoip/legacy/codes/eu_country_list/
(
--"country code","country name"
'EU', --"Europe"
'AD', --"Andorra"
'AL', --"Albania"
'AT', --"Austria"
'BA', --"Bosnia and Herzegovina"
'BE', --"Belgium"
'BG', --"Bulgaria"
@turing4ever
turing4ever / is_ascii.py
Created March 15, 2018 21:53
check if a string contains only ascii
def is_ascii(s):
return all(ord(c) < 128 for c in s)
# Clone the repo, depth=1 means only last 1 commit
git clone --depth=1 git://urloftherepo
# Remove the .git directory recursively
( find . -type d -name ".git" && find . -name ".gitignore" && find . -name ".gitmodules" ) | xargs rm -rf
@turing4ever
turing4ever / shut_stdout.py
Created February 22, 2018 00:08
What if you have to call a noisy function that prints a lot but you can't modify it? Mute it with a context manager.
# A context manager to shut down stdout
# Within this context, sys.stdout will be written into devnull.
# so, effectively it's muted.
@contextlib.contextmanager
def shut_stdout():
sys.stdout = open(os.devnull, 'w')
yield
sys.stdout = sys.__stdout__