Skip to content

Instantly share code, notes, and snippets.

@piotch
piotch / keybase.md
Last active February 23, 2016 15:50

Keybase proof

I hereby claim:

  • I am piotch on github.
  • I am piotch (https://keybase.io/piotch) on keybase.
  • I have a public key whose fingerprint is 74D3 18AA 5E06 5306 5E63 E73D 3E33 D27A C49F 1CD2

To claim this, I am signing this object:

@piotch
piotch / londonismachine.sh
Created August 25, 2015 09:14
london is a $a machine, poor man twitter bot
a=`gsort -R /usr/share/dict/words | head -n1`
s="London is a $a machine."
echo
echo $s
echo
echo "Happy with this one ? [yn]"
read yn
case $yn in
y )
t update "$s" ;;
@piotch
piotch / DRFTokenAuth.py
Created December 16, 2014 12:12
Attaches Django Rest Framework token auth header to the given Request object.
from requests.auth import AuthBase
class DRFTokenAuth(AuthBase):
"""
Attaches Django Rest Framework token auth header to the given Request object.
http://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication
http://docs.python-requests.org/en/latest/user/authentication/#new-forms-of-authentication
"""
def __init__(self, api_key):
self.api_key = api_key
@piotch
piotch / str_load.py
Last active August 29, 2015 14:11
Load a class from its dotted path
from importlib import import_module
_module = lambda x: x[:x.rfind('.')]
_cls = lambda x: x[x.rfind('.')+1:]
s = 'some.module.class'
getattr(import_module(_module(s)), _cls(s))
@piotch
piotch / singleton.py
Last active August 29, 2015 14:08
Singleton using __metaclass__
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class Blah(object):
__metaclass__ = Singleton
@piotch
piotch / encodings.sql
Created August 12, 2014 15:30
Show all tables encodings
SELECT
COLUMN_NAME,
TABLE_NAME,
CHARACTER_SET_NAME,
COLUMN_TYPE,
COLLATION_NAME
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'your_database_name'
@piotch
piotch / convert_utf8.py
Last active August 11, 2017 09:12
South migration to convert tables to utf-8
from django.db.models import get_app, get_models
from django.conf import settings
from south.db import db
from south.v2 import SchemaMigration
class Migration(SchemaMigration):
"""
This is a generic migration to convert tables and column from latin-1 to utf-8
To use it create an empty migration:
@piotch
piotch / gist:afdae8b2e8687f1007a5
Created August 11, 2014 15:18
Commandes pour tester la migration en UTF-8
python manage.py schemamigration app_name convert_utf8 --empty
# ici éditer le contenu de la nouvelle migration pour avoir les méthodes backward et forward de la migration générique
python manage.py dumpdata app_name --indent=4 > before.json
python manage.py migrate XXXX_convert_utf8 # remplacer XXXX par le numéro de la migration
python manage.py dumpdata app_name --indent=4 > after.json
@piotch
piotch / gist:d66a86d97ff23204134d
Created July 29, 2014 15:10
Django: mark safe lazy
from django.utils import six # Python 3 compatibility
from django.utils.functional import lazy
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
mark_safe_lazy = lazy(mark_safe, six.text_type)
@piotch
piotch / nice_svn_log.sh
Created May 12, 2014 15:28
What happened to the SVN repo?
svn log | perl -l40pe 's/^-+/\n/' | head