Skip to content

Instantly share code, notes, and snippets.

workon on


workon on
  • Switzerland
Block or report user

Report or block miraculixx

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
miraculixx /
Last active Jan 12, 2019
To enable CORS support in django-tastypie, use the following code snipet. Then create your tastypie resources as a subclass of BaseCorsResource.Basic code courtesy Daniel Conzalez of added documentation and the post_list method.
Add CORS headers for tastypie APIs
class MyModelResource(CORSModelResource):
class MyResource(CORSResource):
miraculixx /
Last active Nov 2, 2018
Tired of Python's UnicodeDeocodeError, ascii codec can't decode? Here's how to fix it, once and for all.
# Python ascii codec can't decode and unicode mess
# check this out
# and this
# The short of it is this
# 1. If you can, always set PYTHONIOENCODING=utf8 before you start your python programs.
# 2. If you can't or you can't ensure this, always use the following lambda _u to get unicode text
# whereever you convert to strings (str.format, str % etc.)
miraculixx / git-keep
Created Oct 4, 2014
create .keep files for empty directories currently ignored by git
View git-keep
# create .keep files for empty directories currently ignored by git
# run git clean -nd to see which directories are empty and therefore ignored by git currently
# run git keep to add these directories by adding a .keep file
# see this discussion
git clean -nd | awk '{ print $3 }' | xargs -L1 -I{} touch {}.keep
miraculixx /
Last active Jun 19, 2018
map cities by rotated letters
from itertools import combinations
# quick and dirty implementation, not optimized
cities = ['Tokyo', 'London', 'Rome', 'Donlon', 'Kyoto', 'Paris']
normalized = [(c, ''.join(sorted(c.lower()))) for c in cities]
cartesian = list(combinations(normalized, 2))
matches = [[l[0], r[0]] for l, r in cartesian if l[1] == r[1]]
matches.extend([c for c in cities if c not in [m[0] for m in matches] + [m[1] for m in matches]])
# output [['Tokyo', 'Kyoto'], ['London', 'Donlon'], 'Rome', 'Paris']
miraculixx /
Created Apr 30, 2018
pickable mongodb connection object
class PickableCollection(object):
def __init__(self, collection):
super(PickableCollection, self).__setattr__('collection', collection)
def __getattr__(self, k):
return getattr(self.collection, k)
def __setattr__(self, k, v):
return setattr(self.collection, k, v)
miraculixx / .bashrc
Created Jul 12, 2014
add and remove pip install/uninstall from requirements.txt automatically
View .bashrc
# add pip install/uninstall to requirements.txt automatically
pipr() {
if [ "$1" == "install" ]; then
pip $1 $2
pip freeze | grep -i $2 >> requirements.txt
echo ok, added $2 as:
tail -n1 requirements.txt
if [ "$1" == "uninstall" ]; then
echo y | pip $1 $2 >> .pipremoved
miraculixx /
Last active Dec 29, 2017
type checking for python functions. this is way better than PEP 484 type hints and it works for Python 2.x and Python 3.x
from typecheck import typecheck
@typecheck((int, int), dict(c=(int, type(None))), int)
def foo(a, b, c=None):
print("hello", a, b)
return 5 = False
@typecheck((int, int), dict(c=(int, type(None))), str)
miraculixx /
Created Jun 29, 2017
getlicense -- collect license information in your project
def getlicenses(dir=None):
simple licence collector
import re
dir = dir or '.'
SOURCE_FILES = r'(.*css$)|(.*js$)|(.*min$)|(.*json$)'
SOURCE_LINKS = r'.*(http.*://\W*\s)'
miraculixx /
Last active Jun 7, 2017
select tastypie fields returned by the api
class FieldSelectionMixin(object):
add ability to Resource to query specific fields. This works by
removing any fields not in the fields list provided by the fields
query parameter.
Programming Use:
class MyResource(FieldSelectionMixin, ModelResource):
(as with any normal resource)
miraculixx /
Created Sep 5, 2016
start ipython notebook including django
import os
if os.environ.get('DJANGO_SETTINGS_MODULE'):
  print "Initializing Django..."
  if not os.path.exists('app/'):
  print "(in directory %s)" % os.getcwd()
    from django_extensions import management
  except BaseException as e:
You can’t perform that action at this time.