Skip to content

Instantly share code, notes, and snippets.

View turing4ever's full-sized avatar

Yi Wang turing4ever

View GitHub Profile
@turing4ever
turing4ever / gist.py
Last active February 5, 2018 16:47
Hello World
Testing gist.
@turing4ever
turing4ever / unicode_to_ascii.py
Created February 5, 2018 17:37
Convert unicode into ascii recursively in nested object
def unicode_to_ascii(data):
""""Convert all unicode string into ascii string"""
if isinstance(data, basestring):
return str(data)
elif isinstance(data, collections.Mapping):
return dict(map(unicode_to_ascii, data.iteritems()))
elif isinstance(data, collections.Iterable):
return type(data)(map(unicode_to_ascii, data))
else:
return data
@turing4ever
turing4ever / string_length.sh
Created February 5, 2018 23:24
How to figure out the length of a string in bash?
# How to figure out the length of a strign in bash?
#expr length
expr length "string" #This works on Ubuntu, but not on Mac
#wc -c
echo -n "string" | wc -c # -n not to output trailing newline in echo
@turing4ever
turing4ever / Tidy_up_json_in_vim
Created February 22, 2018 00:02
Run json.tool in vim to clean up json formating
:%!python -m json.tool
@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__
# 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 / 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)
-- 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"
# 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`
@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