Skip to content

Instantly share code, notes, and snippets.

View soldni's full-sized avatar
🏳️‍🌈
vibing!

Luca Soldaini soldni

🏳️‍🌈
vibing!
View GitHub Profile
import os
import sys
# BEGIN PYTHONPATH FIX #
# appends root of project if PYTHONPATH is not set;
# relies on the presence of .git in project root.
root_proj_path = os.getcwd()
root_flag = False
while not('.git' in os.listdir(root_proj_path)) and not(root_flag):
if root_proj_path == '/':
@soldni
soldni / pip-upgrade.py
Created June 20, 2014 21:18
update all pip packages at once (from http://stackoverflow.com/a/5839291)
import pip
from subprocess import call
for dist in pip.get_installed_distributions():
call("pip install --upgrade " + dist.project_name, shell=True)
@soldni
soldni / pip-upgrade.sh
Created June 20, 2014 21:19
upgrade all pip packages at once
pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs pip install -U
@soldni
soldni / gpu.applescript
Created October 25, 2014 01:51
Toggle between "discrete GPU only" and "automatic switching" on a 15" Retina MacBook Pro.
tell application "System Preferences"
reveal pane "com.apple.preference.energysaver"
end tell
tell application "System Events" to tell process "System Preferences" to tell window "Energy Saver"
set status to value of checkbox "Automatic graphics switching" of group 1
click checkbox "Automatic graphics switching" of group 1
end tell
quit application "System Preferences"
if status is 0 then
return "Automatic switch activated"
@soldni
soldni / UnicodeShelve
Last active August 29, 2015 14:14
Wrapper around shelve.DbfilenameShelf that ensure Unicode strings are properly encoded/decoded before being inserted/after being extracted from the database.
#!/usr/bin/python
import shelve
class UnicodeShelve(shelve.DbfilenameShelf, object):
"""Wrapper around shelve.DbfilenameShelf that ensure unicode strings
are properly encoded/decoded before being insered/after being extracted
from the database."""
@soldni
soldni / Left & Right curly braces in \tttext
Created January 30, 2015 23:25
Left & Right curly braces in \tttext
\newcommand{\lcb}{{\tt {\char '173}}}
\newcommand{\rcb}{{\tt {\char '175}}}
@soldni
soldni / migrate_index.py
Created March 13, 2015 02:56
migrate an index from one ElasticSearch cluster to another one.
# built-in modules
import json
from argparse import ArgumentParser
from time import time as now
# installed modules
from elasticsearch import Elasticsearch
from elasticsearch.helpers import scan
from elasticsearch.exceptions import ConflictError
@soldni
soldni / type-check-js-demo.html
Created April 14, 2015 00:14
Client-side check if data is a string
<!DOCTYPE html>
<html>
<head>
<title>Type check demo</title>
</head>
<body>
<form>
Insert only digit here<br>
<input id="digitsInput" type="text" name="digits" onblur="checkdigits()">
@soldni
soldni / error_wrapper_pool.py
Last active August 29, 2015 14:20
Make sure that your multiprocessing pool workers report their full traceback when crashing!
import sys
import traceback
from functools import wraps
def error_wrapper_pool(method):
""" Make sure that your multiprocessing pool workers report
their full traceback when crashing!
"""
@wraps(method)
import hashlib
import json
import sys
def hash_obj(obj):
try:
return hashlib.md5(json.dumps(obj)).hexdigest()
except TypeError:
pass