Skip to content

Instantly share code, notes, and snippets.

View Atala's full-sized avatar

Aloïs Guillopé Atala

View GitHub Profile
@Atala
Atala / Euler 11
Created December 17, 2013 09:00
This is my solution to the projecteuler 11th problem
from sys import stdin
def up(x, y, f):
try:
return reduce(lambda u,v: int(u)*int(v), f[x][y:y+4])
except IndexError:
return 0
def down(x, y, f):
try:
@Atala
Atala / postmkvirtualenv
Created September 25, 2014 13:38
Snippet for postmkvirtualenv hook
#!/bin/bash
# This hook is sourced after a new virtualenv is activated.
proj_name=$(basename $VIRTUAL_ENV)
mkdir $HOME/src/$proj_name
add2virtualenv $HOME/src/$proj_name
cd $HOME/src/$proj_name
@Atala
Atala / postactivate
Last active August 29, 2015 14:06
Snippet for postactivate hook
#!/bin/bash
# This hook is sourced after every virtualenv is activated.
proj_name=$(basename $VIRTUAL_ENV)
cd ~/src/$proj_name
# show virtualenv
PS1="$_OLD_VIRTUAL_PS1"
_OLD_RPROMPT="$RPROMPT"
RPROMPT="%{${fg_bold[white]}%}(env: %{${fg[green]}%}`basename \"$VIRTUAL_ENV\"`%{${fg_bold[white]}%})%{${reset_color}%} $RPROMPT"
@Atala
Atala / postdeactivate
Created September 30, 2014 07:55
Postdeactivate hook for virtulenvwrapper
#!/bin/zsh
# This hook is sourced after every virtualenv is deactivated.
# remove virtualenv
RPROMPT="$_OLD_RPROMPT"
@Atala
Atala / Unzip file
Created October 16, 2014 13:18
Unzip a file in python - note that extractall() is sensible to traversal attacks
import zipfile,os.path
def unzip(source_filename, dest_dir):
with zipfile.ZipFile(source_filename) as zf:
for member in zf.infolist():
# Path traversal defense copied from
# http://hg.python.org/cpython/file/tip/Lib/http/server.py#l789
words = member.filename.split('/')
path = dest_dir
for word in words[:-1]:
drive, word = os.path.splitdrive(word)
@Atala
Atala / gevent.unpatch
Last active August 29, 2015 14:08
Proposal for switching threading module
import sys
# keep original thread module for later use
original_thread_module = sys.modules.pop('threading', None)
import gevent.monkey
gevent.monkey.patch_all()
@Atala
Atala / test_mirror_issue.py
Created December 1, 2014 19:28
Django TEST_MIRROR issue
#! debug
ipdb> connections['default'].cursor().db.connection
<connection object at 0x10646d180; dsn: 'dbname=test_master', closed: 0>
ipdb> connections['prestashop'].cursor().db.connection
<connection object at 0x10670d3e0; dsn: 'dbname=test_master', closed: 0>
#! settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
@Atala
Atala / messagepack_streaming.md
Created January 19, 2015 14:56
Note on messagepack unpacking

As I understand, this won't work:

import msgpack
fd = sock.makefile("rb")
unpacker = msgpack.Unpacker(fd, encoding="utf-8")
packet = unpacker.next()

From the msgepack python implementation, the unpacker will emit this:

ssl_session_timeout 5m;
ssl_session_cache shared:SSL:5m;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS;
ssl_dhparam /etc/nginx/cert/dhparams.pem;
@Atala
Atala / mem_to_cloudwatch.py
Created August 10, 2015 10:20
Thanks to Shahar Evron for this script.
#!/usr/bin/env python
'''
Send memory usage metrics to Amazon CloudWatch
This is intended to run on an Amazon EC2 instance and requires an IAM
role allowing to write CloudWatch metrics. Alternatively, you can create
a boto credentials file and rely on it instead.
Original idea based on https://github.com/colinbjohnson/aws-missing-tools