Skip to content

Instantly share code, notes, and snippets.

💭
workon on https://github.com/omegaml

miraculixx

💭
workon on https://github.com/omegaml
  • 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
miraculixx / asciidecodeerror.py
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.
View asciidecodeerror.py
# Python ascii codec can't decode and unicode mess
#
# check this out https://pythonhosted.org/kitchen/unicode-frustrations.html
# and this http://www.joelonsoftware.com/articles/Unicode.html
#
# 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
miraculixx / git-keep
Created Oct 4, 2014
create .keep files for empty directories currently ignored by git
View git-keep
#!/bin/bash
# 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 http://stackoverflow.com/questions/115983/how-do-i-add-an-empty-directory-to-a-git-repository/21422128#21422128
git clean -nd | awk '{ print $3 }' | xargs -L1 -I{} touch {}.keep
@miraculixx
miraculixx / cities_match.py
Last active Jun 19, 2018
map cities by rotated letters
View cities_match.py
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]])
matches
# output [['Tokyo', 'Kyoto'], ['London', 'Donlon'], 'Rome', 'Paris']
@miraculixx
miraculixx / pickablecoll.py
Created Apr 30, 2018
pickable mongodb connection object
View pickablecoll.py
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
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
fi
if [ "$1" == "uninstall" ]; then
echo y | pip $1 $2 >> .pipremoved
@miraculixx
miraculixx / example.py
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
View example.py
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
typecheck.active = False
@typecheck((int, int), dict(c=(int, type(None))), str)
@miraculixx
miraculixx / getlicense.py
Created Jun 29, 2017
getlicense -- collect license information in your project
View getlicense.py
def getlicenses(dir=None):
"""
simple licence collector
"""
import re
dir = dir or '.'
LICENSE_FILES = r'LICENSE.*'
SOURCE_FILES = r'(.*css$)|(.*js$)|(.*min$)|(.*json$)'
LICENSE_IDS = r'.*(MIT|BSD|GPL|GNU.GPL|LPGL|APACHE).*'
SOURCE_LINKS = r'.*(http.*://\W*\s)'
@miraculixx
miraculixx / fieldselect.py
Last active Jun 7, 2017
select tastypie fields returned by the api
View fieldselect.py
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
miraculixx / README.md
Created Sep 5, 2016
start ipython notebook including django
View README.md
import os
if os.environ.get('DJANGO_SETTINGS_MODULE'):
  print "Initializing Django..."
  if not os.path.exists('app/settings.py'):
      os.chdir('..')
  print "(in directory %s)" % os.getcwd()
  try:
    from django_extensions import management
  except BaseException as e:
@miraculixx
miraculixx / README.md
Last active Aug 8, 2016
A simple, extendible python file parser with high processing speed and low memory overhead
View README.md

in response to [this question on stackoverflow]

Installation:

$ chmod +x csvparse.py

Usage:

You can’t perform that action at this time.