Skip to content

Instantly share code, notes, and snippets.

View brouberol's full-sized avatar

Balthazar Rouberol brouberol

View GitHub Profile
@brouberol
brouberol / common_suffix.py
Created November 20, 2012 09:59
Remove common suffix of several strings
from os.path import commonprefix
def common_suffix(titles):
""" Return the common suffix of all the strings contained in the
argument list.
:param titles: a list of strings
Example:
>>> titles = [u'Homepage | wondercity', u'Page 1 | wondercity']
@brouberol
brouberol / remove_dots_in_json_keys.py
Created February 27, 2013 08:00
If you try to insert a JSON structure into MongoDB, and some of its keys contain a "." (dot), insertion will fail (see http://docs.mongodb.org/manual/reference/limits/#Restrictions%20on%20Database%20Names) You'll need to replace the dot by something else to make insertion possible.
# Regex finding all dots in all keys of a JSON structure
DOT_IN_DICT_KEY = re.compile(r"""[,{] # opening bracket for first keys, or previous comma
\s* # spaces until first key quote
['"]([^.'"]+\.[^'"]+)['"] # key name in bewteen quotes
\s*:""", # : after key name ,
flags=re.UNICODE | re.VERBOSE)
def dotrepl(matchobj):
"""Replace all . by _ in the match"""
@brouberol
brouberol / gist:6524605
Last active December 22, 2015 20:09
The birthday paradox - IPython notebook
{
"metadata": {
"name": "birthday-paradox"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
#!/bin/bash
# Check for uncommented debug patterns (pdb, ipdb, rdb) in python files.
# If such patterns are matched, prevent the commit
PATTERN="^[^#]*(i?pdb|rdb)"
debug_statements=`ack $PATTERN --py`
if [ -z "$debug_statements" ];then
exit 0
else
@brouberol
brouberol / gist:14414c7b0944b4ad5a39
Last active August 29, 2015 14:11
Git post-receive hook to build and deploy pelican blog
# The git user will run this script as a post-receive hook on the blog repository.
# It will build the latest version of the blog, and then copy it to the webserver directory
# This script is contained in the hooks/post-receive hook file of a git bare repository
# created with ``git init --bare``*
# We assume that pelican is installed globally, along with its dependencies.
BLOG_DIR=/var/www/blog
TMP_BLOG_CLONE=$HOME/blog-tmp
BLOG_GIT=/home/git/blog.git
#!/bin/bash
PAGE_DIR=/var/www/
TMP_PAGE_CLONE=$HOME/page-tmp
PAGE_GIT=/home/git/page.git
PAGE=$TMP_PAGE_CLONE/index.html
CURRENT_AGE=`python -c 'from datetime import date;print(date.today() - date(1990, 11, 4)).days / 365'`
CURRENT_YEAR=`date +%Y`
@brouberol
brouberol / gist:1cb2fbfe5bde11237c91
Last active August 29, 2015 14:19
Meet CoolDict: a python dict that always keeps its cool.
class CoolDict(dict):
"""A dict that returns an empty CoolDict if a key is not found.
It litterally goes: 'Can't find your thing, but that's cool, don't care.'
Example:
>>> d = CoolDict()
>>> d['value1']['value2'].get('value3', 0)
0
### Keybase proof
I hereby claim:
* I am brouberol on github.
* I am brouberol (https://keybase.io/brouberol) on keybase.
* I have a public key whose fingerprint is 2E29 FBDD 51F3 D3F6 74D1 9B3D 52BA BA41 74FC 6BD7
To claim this, I am signing this object:
@brouberol
brouberol / chainetask.py
Last active December 21, 2015 10:40
ChainedTask celery abstract task
from celery import Task
class ChainedTask(Task):
abstract = True
def apply_async(self, args, kwargs, **options):
"""Injects the dict return value of a a task as keyword args of the next one."""
if args:
# handle the case of a bound task (decorated with bind=True)
@brouberol
brouberol / website-proxy.py
Created March 9, 2016 22:55
This snippet creates a proxy from localhost:5000 to an entire website.
import requests
from flask import Flask, request, Response, stream_with_context
from functools import wraps
app = Flask('test', static_folder=None)
def check_auth(username, password):